📄 fullsyntaxbuilder.java
字号:
ctx = next; } node.appendChild(child); } private static Element getFirstChildElement(Node node) { Node ctx = node.getFirstChild(); while (ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE) ctx = ctx.getNextSibling(); return (Element) ctx; } private static Element getNextSiblingElement(Node node) { Node ctx = node.getNextSibling(); while (ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE) ctx = ctx.getNextSibling(); return (Element) ctx; } private static void forbidDescendants(Node node, Set names) throws GrammarException { for (Node ctx = node.getFirstChild(); ctx != null; ctx = ctx.getNextSibling()) { String ns = ctx.getNamespaceURI(); if (!XMLConstants.RELAXNG_NS_URI.equals(ns)) continue; String name = ctx.getLocalName(); if (names.contains(name)) throw new GrammarException("name not allowed: " + name); forbidDescendants(ctx, names); } } private static boolean isDescendantOfFirstChildOfAttribute(Node node) { Node child = node; Node parent = node.getParentNode(); while (parent != null && !"attribute".equals(parent.getLocalName())) { child = parent; parent = child.getParentNode(); } if (parent == null) return false; Node firstChild = getFirstChildElement(parent); return firstChild == child; } private static void combineNodes(Node node, String combine, String name, List nodes) { Document doc = node.getOwnerDocument(); Node child = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, name); Node combineNode = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, combine); child.appendChild(combineNode); boolean inserted = false; for (Iterator i = nodes.iterator(); i.hasNext(); ) { Node startNode = (Node) i.next(); if (!inserted) { node.insertBefore(child, startNode); inserted = true; } Node ctx = startNode.getFirstChild(); while (ctx != null) { Node next = ctx.getNextSibling(); combineNode.appendChild(ctx); ctx = next; } node.removeChild(startNode); } } Grammar parseGrammar(Element node) throws GrammarException { checkName(node, "grammar"); Grammar grammar = new Grammar(); Element start = getFirstChildElement(node); grammar.start = parsePattern(getFirstChildElement(start)); for (Element define = getNextSiblingElement(start); define != null; define = getNextSiblingElement(define)) grammar.defines.add(parseDefine(define)); return grammar; } Define parseDefine(Element node) throws GrammarException { checkName(node, "define"); Define define = new Define(); define.name = node.getAttribute("name"); define.element = parseElement(getFirstChildElement(node)); return define; } Pattern parseTop(Element node) throws GrammarException { String name = node.getLocalName(); if ("notAllowed".equals(name)) return parseNotAllowed(node); return parsePattern(node); } Pattern parsePattern(Element node) throws GrammarException { String name = node.getLocalName(); if ("empty".equals(name)) return parseEmpty(node); return parseNonEmptyPattern(node); } Pattern parseNonEmptyPattern(Element node) throws GrammarException { String name = node.getLocalName(); if ("text".equals(name)) return parseText(node); else if ("data".equals(name)) return parseData(node); else if ("value".equals(name)) return parseValue(node); else if ("list".equals(name)) return parseList(node); else if ("attribute".equals(name)) return parseAttribute(node); else if ("ref".equals(name)) return parseRef(node); else if ("oneOrMore".equals(name)) return parseOneOrMore(node); else if ("choice".equals(name)) return parseChoice(node); else if ("group".equals(name)) return parseGroup(node); else if ("interleave".equals(name)) return parseInterleave(node); throw new GrammarException("invalid pattern: " + name); } ElementPattern parseElement(Element node) throws GrammarException { checkName(node, "element"); ElementPattern element = new ElementPattern(); Element nameClass = getFirstChildElement(node); element.nameClass = parseNameClass(nameClass); element.pattern = parseTop(getNextSiblingElement(nameClass)); return element; } NotAllowedPattern parseNotAllowed(Element node) throws GrammarException { checkName(node, "notAllowed"); return NotAllowedPattern.INSTANCE; } EmptyPattern parseEmpty(Element node) throws GrammarException { checkName(node, "empty"); return EmptyPattern.INSTANCE; } TextPattern parseText(Element node) throws GrammarException { checkName(node, "text"); return TextPattern.INSTANCE; } DataPattern parseData(Element node) throws GrammarException { checkName(node, "data"); DataPattern data = new DataPattern(); DatatypeLibrary dl = getDatatypeLibrary(node.getAttribute("datatypeLibrary")); String type = node.getAttribute("type"); try { data.type = dl.createDatatype(type); data.datatypeLibrary = dl; } catch (DatatypeException e) { GrammarException e2 = new GrammarException(type); e2.initCause(e); throw e2; } Element ctx = getFirstChildElement(node); while (ctx != null) { Element next = getNextSiblingElement(ctx); String name = ctx.getLocalName(); if ("param".equals(name)) data.params.add(parseParam(ctx)); else if ("except".equals(name) && next == null) data.exceptPattern = parsePattern(getFirstChildElement(ctx)); else throw new GrammarException("invalid element: " + name); ctx = next; } return data; } Param parseParam(Element node) throws GrammarException { checkName(node, "param"); Param param = new Param(); param.name = node.getAttribute("name"); param.value = node.getTextContent(); return param; } ValuePattern parseValue(Element node) throws GrammarException { checkName(node, "value"); ValuePattern value = new ValuePattern(); DatatypeLibrary dl = getDatatypeLibrary(node.getAttribute("datatypeLibrary")); String type = node.getAttribute("type"); try { value.type = dl.createDatatype(type); value.datatypeLibrary = dl; } catch (DatatypeException e) { GrammarException e2 = new GrammarException(type); e2.initCause(e); throw e2; } value.ns = node.getAttribute("ns"); value.value = node.getTextContent(); return value; } ListPattern parseList(Element node) throws GrammarException { checkName(node, "list"); ListPattern list = new ListPattern(); list.pattern = parsePattern(getFirstChildElement(node)); return list; } AttributePattern parseAttribute(Element node) throws GrammarException { checkName(node, "attribute"); AttributePattern attribute = new AttributePattern(); Element nameClass = getFirstChildElement(node); attribute.nameClass = parseNameClass(nameClass); attribute.pattern = parsePattern(getNextSiblingElement(nameClass)); return attribute; } RefPattern parseRef(Element node) throws GrammarException { checkName(node, "ref"); RefPattern ref = new RefPattern(); ref.name = node.getAttribute("name"); return ref; } OneOrMorePattern parseOneOrMore(Element node) throws GrammarException { checkName(node, "oneOrMore"); OneOrMorePattern oneOrMore = new OneOrMorePattern(); oneOrMore.pattern = parseNonEmptyPattern(getFirstChildElement(node)); return oneOrMore; } ChoicePattern parseChoice(Element node) throws GrammarException { checkName(node, "choice"); ChoicePattern choice = new ChoicePattern(); Element p1 = getFirstChildElement(node); Element p2 = getNextSiblingElement(p1); choice.pattern1 = parsePattern(p1); choice.pattern2 = parseNonEmptyPattern(p2); return choice; } GroupPattern parseGroup(Element node) throws GrammarException { checkName(node, "group"); GroupPattern group = new GroupPattern(); Element p1 = getFirstChildElement(node); Element p2 = getNextSiblingElement(p1); group.pattern1 = parseNonEmptyPattern(p1); group.pattern2 = parseNonEmptyPattern(p2); return group; } InterleavePattern parseInterleave(Element node) throws GrammarException { checkName(node, "interleave"); InterleavePattern interleave = new InterleavePattern(); Element p1 = getFirstChildElement(node); Element p2 = getNextSiblingElement(p1); interleave.pattern1 = parseNonEmptyPattern(p1); interleave.pattern2 = parseNonEmptyPattern(p2); return interleave; } NameClass parseNameClass(Element node) throws GrammarException { String name = node.getLocalName(); if ("anyName".equals(name)) return parseAnyName(node); else if ("name".equals(name)) return parseName(node); else if ("nsName".equals(name)) return parseNsName(node); else if ("choice".equals(name)) return parseChoiceNameClass(node); throw new GrammarException("invalid name class: " + name); } AnyNameNameClass parseAnyName(Element node) throws GrammarException { checkName(node, "anyName"); AnyNameNameClass anyName = new AnyNameNameClass(); Element except = getFirstChildElement(node); if (except != null) { checkName(except, "except"); anyName.exceptNameClass = parseNameClass(getFirstChildElement(except)); } return anyName; } NameNameClass parseName(Element node) throws GrammarException { checkName(node, "name"); NameNameClass name = new NameNameClass(); name.ns = node.getAttribute("ns"); name.name = node.getTextContent(); return name; } NSNameNameClass parseNsName(Element node) throws GrammarException { checkName(node, "nsName"); NSNameNameClass nsName = new NSNameNameClass(); nsName.ns = node.getAttribute("ns"); Element except = getFirstChildElement(node); if (except != null) { checkName(except, "except"); nsName.exceptNameClass = parseNameClass(getFirstChildElement(except)); } return nsName; } ChoiceNameClass parseChoiceNameClass(Element node) throws GrammarException { checkName(node, "choice"); ChoiceNameClass choice = new ChoiceNameClass(); Element c1 = getFirstChildElement(node); Element c2 = getNextSiblingElement(c1); choice.name1 = parseNameClass(c1); choice.name2 = parseNameClass(c2); return choice; } void checkName(Element node, String name) throws GrammarException { if (!name.equals(node.getLocalName())) throw new GrammarException("expecting " + name); } DatatypeLibrary getDatatypeLibrary(String uri) throws GrammarException { if (datatypeLibraries == null) datatypeLibraries = new HashMap(); DatatypeLibrary library = (DatatypeLibrary) datatypeLibraries.get(uri); if (library == null) { library = new DatatypeLibraryLoader().createDatatypeLibrary(uri); if (library == null) throw new GrammarException("Datatype library not supported: " + uri); datatypeLibraries.put(uri, library); } return library; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -