/** * Creates a Predicate that accepts Nodes that are element whose named belongs to a set. * * @param names The names. * @return A new Predicate that accepts Nodes that are elements whose name belongs to {@code names}. */ public static Predicate<Node> named(Set<String> names) { return node -> node.getType() == NodeType.ELEMENT && names.contains(((Element) node).getName()); }
/** * Creates a Predicate that accepts Nodes that are element with a given name. * * @param name The name. * @return A new Predicate that accepts Nodes that are elements named {@code name}. */ public static Predicate<Node> named(String name) { return node -> node.getType() == NodeType.ELEMENT && ((Element) node).getName().equals(name); }
public static Predicate<Node> matching(Set<SPath> paths) { return node -> { if (node.getType() == NodeType.ELEMENT) { final Element element = (Element) node; for (final SPath path : paths) { if (path.matchesElement(element)) { return true; } } } return false; }; }
/** * Creates a Predicate that accepts Nodes that are element with a given name, ignoring case. * * @param name The name. * @return A new Predicate that accepts Nodes that are elements named {@code name}, ignoring case. */ public static Predicate<Node> namedIgnoreCase(String name) { return node -> node.getType() == NodeType.ELEMENT && ((Element) node).getName().equalsIgnoreCase(name); } }
@Override public final Element getRootElement() { final Node root = getRootChild(); if (root.getType() == NodeType.DOCUMENT) { // Root is a document return ((Document) root).getRootElement(); } else if (root.getType() == NodeType.ELEMENT) { // Root is an element return (Element) root; } else { // Root is either a comment or a text. return null; } }
private static String toString(Node node) { final StringBuilder builder = new StringBuilder(); builder.append(node.getType()); switch (node.getType()) { case COMMENT: case TEXT: builder.append(" '" + StringUtil.extract(((AbstractContentNode) node).getContent(), 10) + "'"); break; case ELEMENT: builder.append(" " + ((Element) node).getName()); break; default: break; } return builder.toString(); } }
/** * Creates a Predicate that accepts Nodes that are elements that have an attribute with a given value. * * @param attributeName The attribute name. * @param attributeValue The attribute value. * @return A new Predicate that accepts Nodes that are elements that have * an attribute named {@code attributeName} and containing {@code attributeValue}. */ public static Predicate<Node> hasAttribute(String attributeName, String attributeValue) { return node -> { if (node.getType() == NodeType.ELEMENT) { final Element e = (Element) node; return Operators.equals(e.getAttributeValue(attributeName, null), attributeValue); } else { return false; } }; }
/** * Creates a Predicate that accepts Nodes that are elements with a given name and have an attribute with a given value. * * @param name The element name. * @param attributeName The attribute name. * @param attributeValue The attribute value. * @return A new Predicate that accepts Nodes that are elements named {@code name} * with an attribute named {@code attributeName} and containing {@code attributeValue}. */ public static Predicate<Node> namedWithAttribute(String name, String attributeName, String attributeValue) { return node -> { if (node.getType() == NodeType.ELEMENT) { final Element e = (Element) node; return e.getName().equals(name) && Operators.equals(e.getAttributeValue(attributeName, null), attributeValue); } else { return false; } }; }
private void count(Node node) { counts[node.getType().ordinal()]++; if (node instanceof Element) { atts += ((Element) node).getAttributesCount(); } if (node instanceof Parent) { for (final Node child : ((Parent) node).getChildren()) { count(child); } } } }
switch (node.getType()) { case COMMENT: final Comment comment = (Comment) node;