/** * 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()); }
public static TextContentConverter fromNameMap(Map<String, String> map) { Checks.isNotNull(map, "map"); return (Parent parent, String content) -> parent instanceof Element ? map.getOrDefault(((Element) parent).getName(), content) : content; }
/** * 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); }
/** * 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); } }
/** * Returns a predicate that returns {@code true} when the element name belongs to a collection. * <p> * It is advised to use an efficient collection, typically a Set. * * @param names The collection of accepted names. * @return A predicate that returns {@code true} when the element name belongs {@code names}. * @throws IllegalArgumentException When {@code names} is {@code null}. */ public static ElementPredicate fromNames(Collection<String> names) { Checks.isNotNull(names, "names"); return (Parent parent, Element element) -> names.contains(element.getName()); }
static String getNameAndAttributes(Element element) { final StringBuilder builder = new StringBuilder(); builder.append(element.getName()); for (final Attribute att : element.getSortedAttributes()) { builder.append(':'); builder.append(att.getName()); builder.append('='); builder.append(att.getValue()); } return builder.toString(); }
@Override public String toString() { final StringBuilder builder = new StringBuilder(); builder.append("[" + getType() + " " + getName()); for (final Attribute att : getAttributes()) { builder.append(" " + att.getName() + "='" + att.getValue() + "'"); } builder.append("]"); return builder.toString(); }
public static ElementPredicate fromPaths(Collection<SPath> paths) { Checks.isNotNull(paths, "paths"); return (Parent parent, Element element) -> { for (final SPath path : paths) { if (path.matchesElement(parent, element.getName())) { return true; } } return false; }; }
public boolean matchesElement(Parent parent) { if (parent instanceof Element) { final Element element = (Element) parent; return matchesElement(element.getParent(), element.getName()); } else { return false; } }
/** * Creates an element by cloning another one. * <p> * Cloning is shallow.<br> * For deep cloning, use {@link #clone(boolean)}. * * @param other The element to clone. */ public Element(Element other) { this(null, other.getName()); for (int index = 0; index < other.getAttributesCount(); index++) { addAttribute(new Attribute(other.getAttributeAt(index))); } }
@Test public void testFeaturesDefault1() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root/>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><root></root><!-- comment -->", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(0, doc.getRootElement().getChildrenCount()); }); }
@Test public void testFeaturesDefault2() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root> </root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><root> <!-- comment --> </root><!-- comment -->", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); }); }
@Test public void testFeaturesDefault7() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child/></root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <root> \n <child/> \n </root>", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); }); }
@Test public void testFeaturesDefault0() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root/>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><root/><!-- comment -->", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(0, doc.getRootElement().getChildrenCount()); }); }
@Test public void testFeaturesDefault3() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>aaa</root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><root><!-- comment -->aaa</root><!-- comment -->", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); }); }
@Test public void testFeaturesDefault6() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child/></root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child/></root>", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); }); }
@Test public void testFeaturesDefault4() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child> </child></root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <root> \n <child> </child> \n </root>", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); assertEquals(1, doc.getRootElement().getChildAt(Element.class, 0).getChildrenCount()); }); }
@Test public void testFeaturesDefault5() throws IOException { test("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child/></root>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <root> \n <child></child> \n </root>", (doc) -> { assertEquals(1, doc.getChildrenCount()); assertEquals("root", doc.getRootElement().getName()); assertEquals(1, doc.getRootElement().getChildrenCount()); assertEquals(0, doc.getRootElement().getChildAt(Element.class, 0).getChildrenCount()); }); }