Package cdc.util.data

Class Element

  • All Implemented Interfaces:
    Child, Node, Parent

    public final class Element
    extends AbstractChild
    implements Parent
    XML like Element node.

    It has a name, can have children (elements, comments or texts).
    It can also have attributes.

    Author:
    Damien Carbonne
    • Field Detail

      • NAME_COMPARATOR

        public static final Comparator<Element> NAME_COMPARATOR
        A comparator of elements using their names.
    • Constructor Detail

      • Element

        public Element​(String name)
        Creates an element.
        Parameters:
        name - The element name.
        Throws:
        IllegalArgumentException - When name is invalid.
      • Element

        public Element​(Element other)
        Creates an element by cloning another one.

        Cloning is shallow.
        For deep cloning, use clone(boolean).

        Parameters:
        other - The element to clone.
    • Method Detail

      • compareAttribute

        public static Comparator<Element> compareAttribute​(String attributeName)
        Returns a comparator that compares the values of a particular attribute.
        Parameters:
        attributeName - The name of the attribute to use.
        Returns:
        A comparator comparing the value of attributes named attributeName.
      • compareNameAndAttribute

        public static Comparator<Element> compareNameAndAttribute​(String attributeName)
        Returns a comparator that compares the names then the values of a particular attribute.
        Parameters:
        attributeName - The name of the attribute to use.
        Returns:
        A comparator that compares the names then the values of attributes named attributeName.
      • getType

        public NodeType getType()
        Specified by:
        getType in interface Node
        Returns:
        The node type.
      • clone

        public Element clone​(boolean recurse)
        Description copied from interface: Node
        Return a clone of this node.
        Specified by:
        clone in interface Child
        Specified by:
        clone in interface Node
        Specified by:
        clone in interface Parent
        Specified by:
        clone in class AbstractChild
        Parameters:
        recurse - If true, also clone children nodes.
        Returns:
        A clone of this node, and optionally its children.
      • deepEquals

        public boolean deepEquals​(Node node)
        Description copied from interface: Node
        Return true when this node and its descendants equals other node hierarchy.

        Parents are ignored.

        Specified by:
        deepEquals in interface Node
        Parameters:
        node - The other node to compare.
        Returns:
        True when this node and other node have same hierarchies.
      • getQName

        public String getQName()
        Specified by:
        getQName in interface Node
        Returns:
        A string representation of this node. May be used for debug.
      • canAddChild

        public boolean canAddChild​(Child child)
        Description copied from interface: Parent
        Returns true when a child can be added.
        Specified by:
        canAddChild in interface Parent
        Parameters:
        child - The child
        Returns:
        true when child can be added.
      • hasChildren

        public boolean hasChildren()
        Specified by:
        hasChildren in interface Parent
        Returns:
        true if this parent has children.
      • setName

        public void setName​(String name)
        Sets the name of this element.
        Parameters:
        name - The name.
        Throws:
        IllegalArgumentException - When name is invalid.
      • getName

        public String getName()
        Returns:
        the name of this element.
      • getContentType

        public ElementContentType getContentType()
        Returns:
        The content type of this element.
      • isEmpty

        public boolean isEmpty()
        Returns:
        true if this element has no children. It may have attributes.
      • isPure

        public boolean isPure()
        Returns:
        true if this element is empty and has no attributes.
      • hasOnlyText

        public boolean hasOnlyText()
        Returns:
        true if this element has only text children.
      • hasOnlyNonText

        public boolean hasOnlyNonText()
        Returns:
        true if this element has only non-text (comments and elements) children.
      • isMixed

        public boolean isMixed()
        Returns:
        true if this element has text and non-text children.
      • isRoot

        public boolean isRoot()
        Returns:
        true if this element is a root element.
      • getAttributes

        public List<Attribute> getAttributes()
        Returns:
        A list of attributes of this element.
      • getSortedAttributes

        public List<Attribute> getSortedAttributes()
        Returns:
        A list of attributes of this element, sorted by names.
      • getAttributesCount

        public int getAttributesCount()
        Returns:
        The number of attributes of this element.
      • hasAttributes

        public boolean hasAttributes()
        Returns:
        true if this element has attributes.
      • getAttributeAt

        public Attribute getAttributeAt​(int index)
        Returns the attribute at a given index.
        Parameters:
        index - The index.
        Returns:
        The attribute at index.
        Throws:
        IndexOutOfBoundsException - If the index is out of range (index < 0 || index >= getAttributesCount())
      • hasAttribute

        public boolean hasAttribute​(String name)
        Returns true when an attribute with a particular name exists.
        Parameters:
        name - The name.
        Returns:
        true when an attribute named name exists.
      • getAttribute

        public Attribute getAttribute​(String name)
        Returns the attribute that has a particular name, or null.
        Parameters:
        name - The name.
        Returns:
        The attribute named name or null.
      • getAttributeIndex

        public int getAttributeIndex​(String name)
        Returns the index of the attribute that has a particular name or -1.
        Parameters:
        name - The name.
        Returns:
        The index of attribute named name or -1.
      • getAttributeValue

        public String getAttributeValue​(String name,
                                        String def,
                                        FailureReaction missingReaction)
        Returns the value associated to an attribute.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists.
        missingReaction - The reaction to adopt when attribute is not found.
        Returns:
        The value of attribute named name or def.
        Throws:
        NotFoundException - when attribute is not found and missingReaction is FailureReaction.FAIL.
      • getAttributeValue

        public String getAttributeValue​(String name,
                                        String def)
        Returns the value associated to an attribute as a string or a default value.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists.
        Returns:
        The value of attribute named name or def.
      • getAttributeValue

        public String getAttributeValue​(String name)
        Returns the value associated to an attribute as a string or null.
        Parameters:
        name - The attribute name.
        Returns:
        The value of attribute named name.
        Throws:
        NotFoundException - when attribute is not found.
      • getAttributeAsBoolean

        public boolean getAttributeAsBoolean​(String name,
                                             boolean def)
        Returns the value associated to an attribute as a boolean or a default value.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a boolean.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a boolean, then def is returned and a warning is issued.
      • getAttributeAsOptionalBoolean

        public Boolean getAttributeAsOptionalBoolean​(String name,
                                                     Boolean def)
        Returns the value associated to an attribute as a Boolean or a default value.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a Boolean.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a Boolean, then def is returned and a warning is issued.
      • getAttributeAsLong

        public long getAttributeAsLong​(String name,
                                       long def)
        Returns the value associated to an attribute as a long.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a long.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a long, then def is returned and a warning is issued.
      • getAttributeAsOptionalLong

        public Long getAttributeAsOptionalLong​(String name,
                                               Long def)
        Returns the value associated to an attribute as a Long.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a Long.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a Long, then def is returned and a warning is issued.
      • getAttributeAsInt

        public int getAttributeAsInt​(String name,
                                     int def)
        Returns the value associated to an attribute as an int.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to an int.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to an int, then def is returned and a warning is issued.
      • getAttributeAsOptionalInt

        public Integer getAttributeAsOptionalInt​(String name,
                                                 Integer def)
        Returns the value associated to an attribute as an Integer.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to an Integer.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to an Integer, then def is returned and a warning is issued.
      • getAttributeAsShort

        public short getAttributeAsShort​(String name,
                                         short def)
        Returns the value associated to an attribute as a short.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a short.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a short, then def is returned and a warning is issued.
      • getAttributeAsOptionalShort

        public Short getAttributeAsOptionalShort​(String name,
                                                 Short def)
        Returns the value associated to an attribute as a Short.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a Short.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a Short, then def is returned and a warning is issued.
      • getAttributeAsByte

        public byte getAttributeAsByte​(String name,
                                       byte def)
        Returns the value associated to an attribute as a byte.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a byte.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a byte, then def is returned and a warning is issued.
      • getAttributeAsOptionalByte

        public Byte getAttributeAsOptionalByte​(String name,
                                               Byte def)
        Returns the value associated to an attribute as a Byte.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a Byte.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a Byte, then def is returned and a warning is issued.
      • getAttributeAsDouble

        public double getAttributeAsDouble​(String name,
                                           double def)
        Returns the value associated to an attribute as a double.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a double.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a double, then def is returned and a warning is issued.
      • getAttributeAsOptionalDouble

        public Double getAttributeAsOptionalDouble​(String name,
                                                   Double def)
        Returns the value associated to an attribute as a Double.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a Double.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a Double, then def is returned and a warning is issued.
      • getAttributeAsFloat

        public float getAttributeAsFloat​(String name,
                                         float def)
        Returns the value associated to an attribute as a float.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a float.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a float, then def is returned and a warning is issued.
      • getAttributeAsOptionalFloat

        public Float getAttributeAsOptionalFloat​(String name,
                                                 Float def)
        Returns the value associated to an attribute as a Float.
        Parameters:
        name - The attribute name.
        def - The value to return if no attribute named name exists or if its value can not be converted to a Float.
        Returns:
        The value of attribute named name or def.
        If the attribute exists but its value can not be converted to a Float, then def is returned and a warning is issued.
      • getAttributeAsRawEnum

        public Enum<?> getAttributeAsRawEnum​(String name,
                                             Class<? extends Enum<?>> enumClass,
                                             Enum<?> def)
      • getAttributeAsOptionalRawEnum

        public Enum<?> getAttributeAsOptionalRawEnum​(String name,
                                                     Class<? extends Enum<?>> enumClass,
                                                     Enum<?> def)
      • getAttributeAsEnum

        public <E extends Enum<E>> E getAttributeAsEnum​(String name,
                                                        Class<E> enumClass,
                                                        E def)
        Returns the value associated to an attribute as an enum.
        Type Parameters:
        E - The enum type.
        Parameters:
        name - The attribute name.
        enumClass - The enum class.
        def - The value to return if no attribute named name exists or if its value can not be converted to an enum.
        Returns:
        The value of attribute named name or def.
      • getAttributeAsOptionalEnum

        public <E extends Enum<E>> E getAttributeAsOptionalEnum​(String name,
                                                                Class<E> enumClass,
                                                                E def)
      • addAttribute

        public void addAttribute​(Attribute attribute)
        Adds an attribute.
        Parameters:
        attribute - The attribute.
        Throws:
        IllegalArgumentException - When attribute is invalid or another attribute with same name already exists.
      • addAttributes

        public void addAttributes​(Collection<Attribute> attributes)
        Adds all attributes of a collection to this element.
        Parameters:
        attributes - The attributes to add.
        Throws:
        IllegalArgumentException - When one of attributes is invalid or another attribute with same name already exists.
      • addAttributes

        public void addAttributes​(Attribute... attributes)
        Adds an array of attributes to this element.
        Parameters:
        attributes - The attributes to add.
        Throws:
        IllegalArgumentException - When one of attributes is invalid or another attribute with same name already exists.
      • addAttribute

        public void addAttribute​(String name,
                                 Object value)
        Adds an object attribute.

        The string value is constructed using value.toString().

        Parameters:
        name - The attribute name.
        value - The attribute value.
        Throws:
        IllegalArgumentException - When name is invalid or another attribute with same name already exists.
      • addAttribute

        public void addAttribute​(String name,
                                 String value)
        Adds a string attribute.
        Parameters:
        name - The attribute name.
        value - The attribute value.
        Throws:
        IllegalArgumentException - When name is invalid or another attribute with same name already exists.
      • addAttribute

        public void addAttribute​(String name,
                                 boolean value)
        Adds a boolean attribute.
        Parameters:
        name - The attribute name.
        value - The attribute value.
        Throws:
        IllegalArgumentException - When name is invalid or another attribute with same name already exists.
      • addAttribute

        public void addAttribute​(String name,
                                 long value)
        Adds a long attribute.
        Parameters:
        name - The attribute name.
        value - The attribute value.
        Throws:
        IllegalArgumentException - When name is invalid or another attribute with same name already exists.
      • addAttribute

        public void addAttribute​(String name,
                                 int value)
        Adds an int attribute.
        Parameters:
        name - The attribute name.
        value - The attribute value.
        Throws:
        IllegalArgumentException - When name is invalid or another attribute with same name already exists.
      • addAttribute

        public void addAttribute​(String name,
                                 short value)
        Adds a short attribute.
        Parameters:
        name - The attribute name.
        value - The attribute value.
        Throws:
        IllegalArgumentException - When name is invalid or another attribute with same name already exists.
      • addAttribute

        public void addAttribute​(String name,
                                 byte value)
        Adds a byte attribute.
        Parameters:
        name - The attribute name.
        value - The attribute value.
        Throws:
        IllegalArgumentException - When name is invalid or another attribute with same name already exists.
      • addAttribute

        public void addAttribute​(String name,
                                 double value)
        Adds a double attribute.
        Parameters:
        name - The attribute name.
        value - The attribute value.
        Throws:
        IllegalArgumentException - When name is invalid or another attribute with same name already exists.
      • addAttribute

        public void addAttribute​(String name,
                                 float value)
        Adds a float attribute.
        Parameters:
        name - The attribute name.
        value - The attribute value.
        Throws:
        IllegalArgumentException - When name is invalid or another attribute with same name already exists.
      • removeAttribute

        public Attribute removeAttribute​(String name)
        Removes an attribute identified by its name.
        Parameters:
        name - The name of the attribute to remove.
        Returns:
        The removed attribute or null.
      • removeAttributes

        public void removeAttributes​(Predicate<Attribute> predicate,
                                     boolean recurse)
      • removeAttributesNamed

        public void removeAttributesNamed​(String name)
      • removeAttributesNamed

        public void removeAttributesNamed​(String name,
                                          boolean recurse)
      • removeAttributesNamed

        public void removeAttributesNamed​(Set<String> names)
      • removeAttributesNamed

        public void removeAttributesNamed​(Set<String> names,
                                          boolean recurse)
      • removeAttributesNamed

        public void removeAttributesNamed​(String... names)
      • removeAttributes

        public void removeAttributes()
        Remove all attributes of this element.
      • changeAttributeValue

        public void changeAttributeValue​(String name,
                                         UnaryOperator<String> modifier)
        Changes the value of attributes that have a given name.
        Parameters:
        name - The attribute name.
        modifier - A function that take value and returns new value.
      • changeAttributeValue

        public void changeAttributeValue​(String name,
                                         UnaryOperator<String> modifier,
                                         boolean recurse)
        Changes the value of attributes that have a given name.
        Parameters:
        name - The attribute name.
        modifier - A function that take value and returns new value.
        recurse - If true, then this is applied recursively.
      • changeName

        public void changeName​(UnaryOperator<String> modifier)
        Changes the element name.
        Parameters:
        modifier - A function that take name and returns new name.
      • changeName

        public void changeName​(UnaryOperator<String> modifier,
                               boolean recurse)
        Changes element names recursively.
        Parameters:
        modifier - A function that take name and returns new name.
        recurse - If true, then this is applied recursively.
      • addText

        public Text addText​(String content,
                            boolean merge)
        Adds a text as last child.
        Parameters:
        content - The text content.
        merge - If true, then if last child exists and is a text, content is added to this last child.
        Otherwise, a text child is created and added to last position.
        Returns:
        The modified or created text.
      • addText

        public Text addText​(String content)
        Adds a text as last child.

        If last child exists and is a text, content is added to this last child.
        Otherwise, a text child is created and added to last position.

        Parameters:
        content - The text content.
        Returns:
        The modified or created text.
      • getText

        public String getText​(String def)
        Returns the merged content of all children text nodes, if this node has no element children.

        This will work when this node has only text and comment children.
        If it has element children, the default value is returned.

        Parameters:
        def - The default value.
        Returns:
        The merged content of children texts (ignoring comments) or def.
      • getText

        public String getText()
        Returns the merged content of all children text nodes, if this node has no element children.

        This will work when this node has only text and comment children.
        If it has element children, null is returned.

        Returns:
        The merged content of children texts (ignoring comments) or null.
      • getTextAsBoolean

        public boolean getTextAsBoolean​(boolean def)
      • getTextAsOptionalBoolean

        public Boolean getTextAsOptionalBoolean​(Boolean def)
      • getTextAsLong

        public long getTextAsLong​(long def)
      • getTextAsOptionalLong

        public Long getTextAsOptionalLong​(Long def)
      • getTextAsInt

        public int getTextAsInt​(int def)
      • getTextAsOptionalInt

        public Integer getTextAsOptionalInt​(Integer def)
      • getTextAsShort

        public short getTextAsShort​(short def)
      • getTextAsOptionalShort

        public Short getTextAsOptionalShort​(Short def)
      • getTextAsByte

        public byte getTextAsByte​(byte def)
      • getTextAsOptionalByte

        public Byte getTextAsOptionalByte​(Byte def)
      • getTextAsDouble

        public double getTextAsDouble​(double def)
      • getTextAsOptionalDouble

        public Double getTextAsOptionalDouble​(Double def)
      • getTextAsFloat

        public float getTextAsFloat​(float def)
      • getTextAsOptionalFloat

        public Float getTextAsOptionalFloat​(Float def)
      • getTextAsEnum

        public <E extends Enum<E>> E getTextAsEnum​(Class<E> enumClass,
                                                   E def)
      • getTextAsOptionalEnum

        public <E extends Enum<E>> E getTextAsOptionalEnum​(Class<E> enumClass,
                                                           E def)
      • getElementNamedText

        public String getElementNamedText​(String name,
                                          String def)
        Returns the text content of the first child element that has a given name.
        Parameters:
        name - The child name.
        def - The default value.
        Returns:
        The text of the first child named name or def.
      • getElementNamedText

        public String getElementNamedText​(String name)
        Returns the text content of the first child element that has a given name.
        Parameters:
        name - The child name.
        Returns:
        The text of the first child named name or null.
      • named

        public static Predicate<Node> named​(String name)
        Creates a Predicate that accepts Nodes that are element with a given name.
        Parameters:
        name - The name.
        Returns:
        A new Predicate that accepts Nodes that are elements named name.
      • named

        public static Predicate<Node> named​(Set<String> names)
        Creates a Predicate that accepts Nodes that are element whose named belongs to a set.
        Parameters:
        names - The names.
        Returns:
        A new Predicate that accepts Nodes that are elements whose name belongs to names.
      • named

        public static Predicate<Node> named​(String... names)
        Creates a Predicate that accepts Nodes that are element whose named belongs to an array.
        Parameters:
        names - The names.
        Returns:
        A new Predicate that accepts Nodes that are elements whose name belongs to names.
      • namedWithAttribute

        public static Predicate<Node> namedWithAttribute​(String name,
                                                         String attributeName,
                                                         String attributeValue)
        Creates a Predicate that accepts Nodes that are elements with a given name and have an attribute with a given value.
        Parameters:
        name - The element name.
        attributeName - The attribute name.
        attributeValue - The attribute value.
        Returns:
        A new Predicate that accepts Nodes that are elements named name with an attribute named attributeName and containing attributeValue.
      • hasAttribute

        public static Predicate<Node> hasAttribute​(String attributeName,
                                                   String attributeValue)
        Creates a Predicate that accepts Nodes that are elements that have an attribute with a given value.
        Parameters:
        attributeName - The attribute name.
        attributeValue - The attribute value.
        Returns:
        A new Predicate that accepts Nodes that are elements that have an attribute named attributeName and containing attributeValue.
      • namedIgnoreCase

        public static Predicate<Node> namedIgnoreCase​(String name)
        Creates a Predicate that accepts Nodes that are element with a given name, ignoring case.
        Parameters:
        name - The name.
        Returns:
        A new Predicate that accepts Nodes that are elements named name, ignoring case.