public int childIndexOf (Element element) { int index = 0; for (Node n: getNodes ()) { if (n.equals (element)) return index; if (isElement (n)) index ++; } return -1; }
public Element clearChildren () { if (!hasNodes ()) return this; for (Iterator<Node> iter = getNodes ().iterator (); iter.hasNext (); ) { Node n = iter.next (); if (isElement (n)) iter.remove (); } return this; }
public boolean hasChildren () { if (!hasNodes ()) return false; for (Node node: getNodes ()) { if (isElement (node)) return true; } return false; }
public Element getChild (int index) { int count = 0; if (hasNodes ()) { for (Node n: getNodes ()) { if (isElement (n)) { if (index == count) return (Element)n; count ++; } } } throw new IndexOutOfBoundsException ("Cannot return child "+index +", node has only "+count+(count == 1 ? " child" : " children")); }
@Override public boolean matches (Node n) { if (!isElement (n)) return false; Element e = (Element)n; boolean match = true; if (match && name != null && !e.getName ().equals (name)) match = false; if (match && ns != null && !ns.equals (getNamespace ())) match = false; return match; } };
public Element clearText () { if (!hasNodes ()) return this; for (Iterator<Node> iter = getNodes ().iterator (); iter.hasNext ();) { Node n = iter.next (); if (isText (n)) { iter.remove (); } else if (isElement (n)) { ((Element)n).clearText (); } } return this; }
public String getText () { if (!hasNodes ()) return ""; StringBuilder buffer = new StringBuilder (); for (Node n: getNodes ()) { if (isText (n)) buffer.append (((Text)n).getText ()); else if (isElement (n)) buffer.append (((Element)n).getText ()); else if (n.getType () == Type.ENTITY) buffer.append (((Entity)n).getText ()); } return buffer.toString (); }
public Node next () { Node n = currentNode ().getNode (getCursor ()); incCursor (); if (XMLUtils.isElement (n)) { Element e = (Element)n; if (e.hasNodes ()) push (e); } return n; }
public void apply (Document doc) { for (TreeIterator iter = doc.iterator (); iter.hasNext (); ) { Node node = iter.next (); if (XMLUtils.isText (node)) { textHandler.handle (node); } else if (XMLUtils.isElement (node)) { String name = ((Element)node).getName (); INodeHandler handler = handlers.get (name); if (handler != null) handler.handle (node); } // TODO what about other nodes? } }
if (e.hasNodes () && !XMLUtils.isElement (e.getNode (0)))