/** * Adds a string attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, String value) { addAttribute(new Attribute(name, value)); }
@Override public final Document getDocument() { final Element root = getRootElement(); if (root != null) { return (Document) root.getParent(); } else { return (Document) getParent(); } }
/** * Creates and adds a child element as last child, if possible. * * @param name The child name. * @return the created element. * @throws IllegalStateException If {@code child}'s parent can not be set. */ public default Element addElement(String name) { final Element child = new Element(name); return addChild(child); }
@Override public Element clone(boolean recurse) { final Element result = new Element(this); if (recurse) { for (final Child child : getChildren()) { result.addChild(child.clone(true)); } } return result; }
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); } } } }
@Override public Document clone(boolean recurse) { final Document result = new Document(); if (recurse) { for (final Child child : getChildren()) { result.addChild(child.clone(true)); } } return result; }
/** * Merges all consecutive comments recursively. * * @param recurse If {@code true}, then this is applied recursively. */ public default void mergeComments(boolean recurse) { mergeComments(); if (recurse) { for (final Element child : getElements()) { child.mergeComments(true); } } }
/** * Removes all children elements that have a given name, recursively. * * @param name The name of elements to remove. * @param recurse If {@code true}, then this is applied recursively. */ public default void removeElementsNamed(String name, boolean recurse) { removeChildren(Element.named(name), true, recurse); }
@Override public boolean canAddChild(Child child) { if (child == null) { return false; } else if (child.getType() == NodeType.ELEMENT) { return getRootElement() == null; } else { return true; } }
public void removeAttributesNamed(String name, boolean recurse) { removeAttributes(Attribute.named(name), recurse); }
/** * Returns an Iterable of children elements that have a given name, ignoring case. * * @param name The name. * @return An Iterable containing elements named {@code name} ignoring case. */ public default Iterable<Element> getElementsNamedIgnoreCase(String name) { return getChildren(Element.class, Element.namedIgnoreCase(name)); }
public default Element getElementNamedWithAttribute(String name, String attributeName, String attributeValue) { return getChild(Element.class, Element.namedWithAttribute(name, attributeName, attributeValue)); }
/** * Adds an int attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, int value) { addAttribute(new Attribute(name, value)); }
public void removeAttributesNamed(Set<String> names, boolean recurse) { removeAttributes(Attribute.named(names), recurse); }
/** * Adds a float attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, float value) { addAttribute(new Attribute(name, value)); }
/** * Adds a boolean attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, boolean value) { addAttribute(new Attribute(name, value)); }
/** * Adds a long attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, long value) { addAttribute(new Attribute(name, value)); }