public void startElement(final String namespaceURI, final String localName, final String qName, final Attributes atts) throws SAXException { addCdata(); final Map<String, String> attributes = new NamespaceAwareHashMap(); final Map<String, String> attributeNamespaces = new HashMap<String, String>(); for (int i = atts.getLength() - 1; i != -1; i--) { if (atts.getURI(i).length() == 0) { attributes.put(atts.getQName(i), atts.getValue(i)); } else { String key = new QName(atts.getURI(i), atts.getLocalName(i)).toString(); attributes.put(key, atts.getValue(i)); attributeNamespaces.put(key, atts.getURI(i)); } } final Node newElement; if (namespaceURI.length() == 0) { newElement = new Node(currentNode, qName, attributes, attributeNamespaces, namespaceURI); } else { newElement = new Node(currentNode, localName, attributes, attributeNamespaces, namespaceURI); } if (currentNode != null) { currentNode.addChild(newElement); } stack.push(currentNode); currentNode = newElement; }
/** * Returns a map containing all attributes of the Node of this NodeChild. * @return a map containing all attributes */ public Map attributes() { return this.node.attributes(); }
public Iterator childNodes() { return this.node.childNodes(); }
/** * @param node a node * @param parent the GPathResult prior to the application of the expression creating this GPathResult * @param namespacePrefix the namespace prefix if any * @param namespaceTagHints the known tag to namespace mappings */ public NodeChild(final Node node, final GPathResult parent, final String namespacePrefix, final Map<String, String> namespaceTagHints) { super(parent, node.name(), namespacePrefix, namespaceTagHints); this.node = node; ((NamespaceAwareHashMap)this.node.attributes()).setNamespaceTagHints(namespaceTagHints); }
protected Object getNextNode(Iterator iter) { while (iter.hasNext()) { final Node node = (Node) iter.next(); if (name.equals(node.name())) { if ("*".equals(namespacePrefix) || ("".equals(namespacePrefix) && "".equals(node.namespaceURI())) || node.namespaceURI().equals(namespaceMap.get(namespacePrefix))) { return node; } } } return null; } };
private Iterator nextChildIter() { while (iter.hasNext()) { final Node node = (Node) iter.next(); if (name.equals(node.name()) || name.equals("*")) { final Iterator result = node.childNodes(); if (result.hasNext()) { if ("*".equals(namespacePrefix) || ("".equals(namespacePrefix) && "".equals(node.namespaceURI())) || node.namespaceURI().equals(namespaceMap.get(namespacePrefix))) { return result; } } } } return null; } };
private void walkXml(IndentPrinter printer, Node node) { printer.printIndent(); printer.print(node.name()); if (!node.attributes().isEmpty()) { printer.print("("); List<String> attrs = new ArrayList<>(); for (Object o : node.attributes().entrySet()) { Map.Entry<?, ?> entry = (Map.Entry<?, ?>) o; attrs.add(entry.getKey() + ": " + entry.getValue()); if (!node.children().isEmpty()) { printer.println(" {"); printer.incrementIndent(); for (Iterator<?> iter = node.childNodes(); iter.hasNext(); ) { Object child = iter.next(); if (child instanceof NodeChild) { printer.printIndent(); printer.println("}"); } else if (!node.attributes().isEmpty()) { printer.println(""); } else {
public void build(final GroovyObject builder) { this.node.build(builder, this.namespaceMap, this.namespaceTagHints); }
private void addCdata() { if (charBuffer.length() != 0) { // // This element is preceded by CDATA if keepIgnorableWhitespace is false (the default setting) and // it's not whitespace add it to the body // Note that, according to the XML spec, we should preserve the CDATA if it's all whitespace // but for the sort of work I'm doing ignoring the whitespace is preferable // final String cdata = charBuffer.toString(); charBuffer.setLength(0); if (keepIgnorableWhitespace || cdata.trim().length() != 0) { currentNode.addChild(cdata); } } } }
public void doCall(Object[] args) { final GroovyObject delegate = (GroovyObject)getDelegate(); final GPathResult thisObject = (GPathResult)getThisObject(); Node node = (Node)thisObject.getAt(0); List children = node.children(); for (Object child : children) { delegate.getProperty("mkp"); if (child instanceof Node) { delegate.invokeMethod("yield", new Object[]{new NodeChild((Node) child, thisObject, "*", null)}); } else { delegate.invokeMethod("yield", new Object[]{child}); } } } };
protected void appendNode(final Object newValue) { this.node.appendNode(newValue, this); } }
private static String getTagFor(final Object namespaceURI, final Map current, final Map pending, final Map local, final Map tagHints, final List newTags, final GroovyObject builder) { String tag = findNamespaceTag(pending, namespaceURI); // look in the namespaces whose declaration has already been emitted if (tag == null) { tag = findNamespaceTag(current, namespaceURI); // look in the namespaces who will be declared at the next element if (tag == null) { // we have to declare the namespace - choose a tag tag = findNamespaceTag(local, namespaceURI); // If the namespace has been declared in the GPath expression use that tag if (tag == null || tag.length() == 0) { tag = findNamespaceTag(tagHints, namespaceURI); // If the namespace has been used in the parse document use that tag } if (tag == null || tag.length() == 0) { // otherwise make up a new tag and check it has not been used before int suffix = 0; do { final String possibleTag = "tag" + suffix++; if (!pending.containsKey(possibleTag) && !current.containsKey(possibleTag) && !local.containsKey(possibleTag)) { tag = possibleTag; } } while (tag == null); } final Map newNamespace = new HashMap(); newNamespace.put(tag, namespaceURI); builder.getProperty("mkp"); builder.invokeMethod("declareNamespace", new Object[]{newNamespace}); newTags.add(tag); } } return tag; }
builder.getProperty(getTagFor(this.namespaceURI, current, pending, namespaceMap, namespaceTagHints, newTags, builder)); builder.invokeMethod(this.name, new Object[]{this.attributes, rest}); } else { final Object attributeNamespaceURI = this.attributeNamespaces.get(key); if (attributeNamespaceURI != null) { attributesWithNamespaces.put(getTagFor(attributeNamespaceURI, current, pending, namespaceMap, namespaceTagHints, newTags, builder) + "$" + key, attributesWithNamespaces.remove(key)); builder.getProperty(getTagFor(this.namespaceURI, current, pending, namespaceMap, namespaceTagHints, newTags, builder)); builder.invokeMethod(this.name, new Object[]{attributesWithNamespaces, rest});
/** * @param node a node * @param parent the GPathResult prior to the application of the expression creating this GPathResult * @param namespacePrefix the namespace prefix if any * @param namespaceTagHints the known tag to namespace mappings */ public NodeChild(final Node node, final GPathResult parent, final String namespacePrefix, final Map<String, String> namespaceTagHints) { super(parent, node.name(), namespacePrefix, namespaceTagHints); this.node = node; }
public static void dive(final int maxDives) { dive(root, 0, maxDives); } public static void dive(Node current, final int level, final int maxDives) { for(Node n : current.children) { n.runStrat(); n.printNode(); if(level < maxDives) { for (int lvl : n.currOtherPieces) { n.addChild(lvl, n.level + 1); } for(Node m : n.children) { dive(m, level + 1, maxDives); } } } }
private Iterator nextChildIter() { while (iter.hasNext()) { final Node node = (Node) iter.next(); if (name.equals(node.name()) || name.equals("*")) { final Iterator result = node.childNodes(); if (result.hasNext()) { if ("*".equals(namespacePrefix) || ("".equals(namespacePrefix) && "".equals(node.namespaceURI())) || node.namespaceURI().equals(namespaceMap.get(namespacePrefix))) { return result; } } } } return null; } };
protected Object getNextNode(Iterator iter) { while (iter.hasNext()) { final Node node = (Node) iter.next(); if (name.equals(node.name())) { if ("*".equals(namespacePrefix) || ("".equals(namespacePrefix) && "".equals(node.namespaceURI())) || node.namespaceURI().equals(namespaceMap.get(namespacePrefix))) { return node; } } } return null; } };
public void build(final GroovyObject builder) { final Iterator iter = nodeIterator(); while (iter.hasNext()) { final Object next = iter.next(); if (next instanceof Buildable) { ((Buildable) next).build(builder); } else { ((Node) next).build(builder, namespaceMap, namespaceTagHints); } } }
/** * @param node a node * @param parent the GPathResult prior to the application of the expression creating this GPathResult * @param namespacePrefix the namespace prefix if any * @param namespaceTagHints the known tag to namespace mappings */ public NodeChild(final Node node, final GPathResult parent, final String namespacePrefix, final Map<String, String> namespaceTagHints) { super(parent, node.name(), namespacePrefix, namespaceTagHints); this.node = node; ((NamespaceAwareHashMap)this.node.attributes()).setNamespaceTagHints(namespaceTagHints); }
public class MyNodeList implements NodeList { Node root = null; int length = 0; public MyNodeList() {} public void addNode(Node node) { if(root == null) root = node; else root.addChild(node); length++; } public Node item(int index) { if(index < 1) return root; Node node = root; while(index > 0) { node = node.getFirstChild(); if(node == null) return node; index--; } return node; } public int getLength() { return length; } }