📄 fullsyntaxbuilder.java
字号:
{ element.setAttribute("type", "token"); element.setAttribute("datatypeLibrary", ""); } } // 4.16. Constraints // TODO validate type } // 4.6. externalRef element else if ("externalRef".equals(elementName)) { Element externalRef = (Element) node; String href = externalRef.getAttribute("href"); // check for recursion if (urls.contains(href)) throw new GrammarException("recursive href"); urls.add(href); Element element = resolve(href); String eNs = element.getNamespaceURI(); String eName = element.getLocalName(); if (!(XMLConstants.RELAXNG_NS_URI.equals(eNs) || eNs == null) || !PATTERN_ELEMENTS.contains(eName)) throw new GrammarException("externally referenced element " + "is not a pattern"); transform(element); urls.remove(href); String ns = element.getAttribute("ns"); if (ns != null) element.setAttribute("ns", externalRef.getAttribute("ns")); element = (Element) externalRef.getOwnerDocument() .importNode(element, true); parent.replaceChild(element, externalRef); return; } // 4.7 include element else if ("include".equals(elementName)) { Element include = (Element) node; String href = include.getAttribute("href"); // check for recursion if (urls.contains(href)) throw new GrammarException("recursive href"); urls.add(href); Element element = resolve(href); String eNs = element.getNamespaceURI(); String eName = element.getLocalName(); if (!(XMLConstants.RELAXNG_NS_URI.equals(eNs) || eNs == null) || !"grammar".equals(eName)) throw new GrammarException("included element is not " + "a grammar"); transform(element); urls.remove(href); // handle components List includeComponents = getComponents(include); List grammarComponents = getComponents(element); for (Iterator i = includeComponents.iterator(); i.hasNext(); ) { Element comp = (Element) i.next(); String compName = comp.getLocalName(); if ("start".equals(compName)) { boolean found = false; for (Iterator j = grammarComponents.iterator(); j.hasNext(); ) { Element c2 = (Element) j.next(); if ("start".equals(c2.getLocalName())) { c2.getParentNode().removeChild(c2); found = true; } } if (!found) throw new GrammarException("no start component in " + "included grammar"); } else if ("define".equals(compName)) { String name = comp.getAttribute("name"); boolean found = false; for (Iterator j = grammarComponents.iterator(); j.hasNext(); ) { Element c2 = (Element) j.next(); if ("define".equals(c2.getLocalName()) && name.equals(c2.getAttribute("name"))) { c2.getParentNode().removeChild(c2); found = true; } } if (!found) throw new GrammarException("no define component " + "with name '" + name + "' in included grammar"); } } // transform to div element Document doc = include.getOwnerDocument(); Element includeDiv = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "div"); Element grammarDiv = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "div"); // XXX copy include non-href attributes (none defined?) element = (Element) doc.importNode(element, true); Node ctx = element.getFirstChild(); while (ctx != null) { Node next = ctx.getNextSibling(); grammarDiv.appendChild(ctx); ctx = next; } includeDiv.appendChild(grammarDiv); ctx = include.getFirstChild(); while (ctx != null) { Node next = ctx.getNextSibling(); includeDiv.appendChild(ctx); ctx = next; } parent.replaceChild(includeDiv, include); transform(includeDiv); return; } // 4.8. name attribute of element and attribute elements else if ("attribute".equals(elementName) || "element".equals(elementName)) { Element element = (Element) node; String name = element.getAttribute("name"); if (name != null) { Document doc = element.getOwnerDocument(); Element n = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "name"); n.appendChild(doc.createTextNode(name)); Node first = element.getFirstChild(); if (first != null) element.insertBefore(n, first); else element.appendChild(n); if ("attribute".equals(elementName)) { String ns = element.getAttribute("ns"); if (ns != null) { n.setAttribute("ns", ns); element.removeAttribute("ns"); } } element.removeAttribute("name"); } // 4.12. Number of child elements if ("attribute".equals(elementName)) { if (getComponents(node).size() == 1) { Document doc = node.getOwnerDocument(); Element text = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "text"); node.appendChild(text); } } else // element { if (node.getChildNodes().getLength() > 2) { // transform to 2 child elements Document doc = node.getOwnerDocument(); Element child = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "group"); Node ctx = getFirstChildElement(node); ctx = getNextSiblingElement(ctx); // skip 1 while (ctx != null) { Node next = getNextSiblingElement(ctx); child.appendChild(ctx); ctx = next; } node.appendChild(child); } } } // 4.11. div element else if ("div".equals(elementName)) { Node ctx = node.getFirstChild(); while (ctx != null) { Node next = ctx.getNextSibling(); parent.insertBefore(ctx, node); transform(ctx); ctx = next; } parent.removeChild(node); return; } else if ("mixed".equals(elementName)) { // 4.12. Number of child elements transformToOneChildElement(node, "group"); // 4.13. mixed element Document doc = node.getOwnerDocument(); Node interleave = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "interleave"); Node ctx = node.getFirstChild(); while (ctx != null) { Node next = ctx.getNextSibling(); interleave.appendChild(ctx); ctx = next; } Node text = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "text"); interleave.appendChild(text); parent.insertBefore(interleave, node); parent.removeChild(node); node = interleave; } else if ("optional".equals(elementName)) { // 4.12. Number of child elements transformToOneChildElement(node, "group"); // 4.14. optional element Document doc = node.getOwnerDocument(); Node choice = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "choice"); Node ctx = node.getFirstChild(); while (ctx != null) { Node next = ctx.getNextSibling(); choice.appendChild(ctx); ctx = next; } Node empty = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "empty"); choice.appendChild(empty); parent.insertBefore(choice, node); parent.removeChild(node); node = choice; } else if ("zeroOrMore".equals(elementName)) { // 4.12. Number of child elements transformToOneChildElement(node, "group"); // 4.15. zeroOrMore element Document doc = node.getOwnerDocument(); Node choice = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "choice"); Node oneOrMore = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "oneOrMore"); Node ctx = node.getFirstChild(); while (ctx != null) { Node next = ctx.getNextSibling(); oneOrMore.appendChild(ctx); ctx = next; } Node empty = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "empty"); choice.appendChild(oneOrMore); choice.appendChild(empty); parent.insertBefore(choice, node); parent.removeChild(node); node = choice; } else if ("list".equals(elementName) || "oneOrMore".equals(elementName) || "define".equals(elementName)) { // 4.12. Number of child elements transformToOneChildElement(node, "group"); } else if ("except".equals(elementName)) { // 4.12. Number of child elements transformToOneChildElement(node, "choice"); // 4.16. Constraints String parentName = parent.getLocalName(); if ("anyName".equals(parentName)) forbidDescendants(node, Collections.singleton("anyName")); else if ("nsName".equals(parentName)) { Set names = new HashSet(); names.add("nsName"); names.add("anyName"); forbidDescendants(node, names); } } else if ("choice".equals(elementName) || "group".equals(elementName) || "interleave".equals(elementName)) { // 4.12. Number of child elements Node ctx = getFirstChildElement(node); Node next = getNextSiblingElement(ctx); if (next == null) { // replace parent.insertBefore(ctx, node); parent.removeChild(node); transform(ctx); return; } else { // transform to 2 child elements Node next2 = getNextSiblingElement(next); if (next2 != null) { Document doc = node.getOwnerDocument(); Node child = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, elementName); child.appendChild(ctx); child.appendChild(next); node.insertBefore(next2, child); transform(node); // recurse } } } // 4.17. combine attribute else if ("grammar".equals(elementName)) { String combine = null; List nodes = new LinkedList(); Node ctx = node.getFirstChild(); while (ctx != null) { Node next = ctx.getNextSibling(); if ("start".equals(ctx.getLocalName())) { String c = ((Element) ctx).getAttribute("combine"); if (combine != null && !combine.equals(c)) throw new GrammarException("multiple start elements "+ "but no combine attribute"); combine = c; nodes.add(ctx); } ctx = next; } if (!nodes.isEmpty()) combineNodes(node, combine, "start", nodes); // defines Map defines = new HashMap(); Map defineCombines = new HashMap(); ctx = node.getFirstChild(); while (ctx != null) { Node next = ctx.getNextSibling(); if ("define".equals(ctx.getLocalName())) { String name = ((Element) ctx).getAttribute("name"); combine = (String) defineCombines.get(name); String c = ((Element) ctx).getAttribute("combine"); if (combine != null && !combine.equals(c)) throw new GrammarException("multiple define " + "elements with name '"+ name + "' but no " + "combine attribute"); defineCombines.put(name, c); nodes = (List) defines.get(name); if (nodes == null) { nodes = new LinkedList(); defines.put(name, nodes); } nodes.add(ctx); } ctx = next; } for (Iterator i = defines.keySet().iterator(); i.hasNext(); ) { String name = (String) i.next(); combine = (String) defineCombines.get(name); nodes = (List) defines.get(name); if (!nodes.isEmpty()) combineNodes(node, combine, "define", nodes); } } // 4.9. ns attribute if ("name".equals(elementName) || "nsName".equals(elementName) || "value".equals(elementName)) { Element element = (Element) node; String ns = element.getAttribute("ns"); if (ns == null) { Node ctx = parent; while (ns == null && ctx != null && ctx.getNodeType() == Node.ELEMENT_NODE) { ns = ((Element) ctx).getAttribute("ns"); ctx = ctx.getParentNode(); } element.setAttribute("ns", (ns == null) ? "" : ns); } if ("name".equals(elementName)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -