private String escape(String value) { return XMLUtils.escapeXMLText(value); }
public static boolean isAttribute (Node n) { return n == null ? false : isAttributeType (n.getType ()); }
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 boolean hasSingleTextChild (Element e) { if (e.nodeCount () != 1) return false; Node node = e.getNode (0); if (!XMLUtils.isText (node)) return false; Text t = (Text)node; return !t.isWhitespace (); }
/** Unescape '<', '>', '&', '"' and ''' */ public static String unescapeXMLAttributeValue (String text) { if (text == null) return null; // TODO Replace with StringBuilder return unescapeXMLText (text) .replaceAll (EntityResolver.QUOT_ESCAPE, "\"") .replaceAll (EntityResolver.APOS_ESCAPE, "'") ; }
public static boolean isElement (Node n) { return n == null ? false : isElementType (n.getType ()); }
public static boolean isText (Node n) { return n == null ? false : isTextType (n.getType ()); }
this.value = XMLUtils.unescapeXMLAttributeValue (rawValue);
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 (); }
if (!XMLUtils.isText (last) || !((Text)last).isWhitespace ()) throw new XMLParseException ("Space is required before an NDATA entity annotation", token);
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? } }
public Text (Type type, String text) { super (type, type == Type.CDATA ? "<![CDATA[" + text + "]]>" : XMLUtils.escapeXMLText (text)); this.text = text; }
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 Text setText (String text) { setValue (getType () == Type.CDATA ? "<![CDATA[" + text + "]]>" : XMLUtils.escapeXMLText (text)); this.text = text; 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 Node next () { Node n = currentNode ().getNode (getCursor ()); incCursor (); if (XMLUtils.isElement (n)) { Element e = (Element)n; if (e.hasNodes ()) push (e); } return n; }