Package tigase.xml

Class Element

java.lang.Object
tigase.xml.Element
All Implemented Interfaces:
Cloneable, XMLNodeIfc<Element>
Direct Known Subclasses:
DBElement

@TODO(note="Make it a bit lighter.") public class Element extends Object implements XMLNodeIfc<Element>
Element - basic document tree node implementation. Supports Java 5.0 generic feature to make it easier to extend this class and still preserve some useful functionality. Sufficient for simple cases but probably in the more advanced cases should be extended with additional features. Look in API documentation for more details and information about existing extensions.

Created: Mon Oct 4 17:55:16 2004

Author:
Artur Hefczyc
  • Field Details

  • Constructor Details

    • Element

      public Element(@NonNull Element src)
      Shallow cloning constructor. Will create a copy of the element (name, attributes and list of subnodes) but it will not create copy of children - the same instances will be added to the new copy).
    • Element

      public Element(@NonNull String name)
      Constructor creating element with a name
  • Method Details

    • addCData

      public @NonNull Element addCData(@NonNull String cdata)
      Add CData node with passed value
      Returns:
      this element
    • addChild

      public @NonNull Element addChild(@NonNull Element child)
      Add element as a child.
      Returns:
      this element
    • addChildren

      public @NonNull Element addChildren(@NonNull List<Element> children)
      Add children
      Returns:
      this element
    • nodesToString

      public void nodesToString(@NonNull StringBuilder result)
      Serialize subnodes to passed builder
    • nodesToStringPretty

      public void nodesToStringPretty(@NonNull StringBuilder result)
      Serialize subnodes as a formatted string
    • nodesToStringSecure

      public void nodesToStringSecure(@NonNull StringBuilder result)
      Serialize subnodes to passed builder as a secure string
    • clone

      public Element clone()
      Specified by:
      clone in interface XMLNodeIfc<Element>
      Overrides:
      clone in class Object
    • equals

      public boolean equals(Object obj)
      Methods checks equality of instances of `Element` excluding list of children
      Overrides:
      equals in class Object
    • findChild

      public @Nullable Element findChild(@NonNull Predicate<Element> predicate)
      Method returns first child which matches predicate
    • findChild

      public @Nullable Element findChild(@NonNull String name)
      Method returns first child which name matches
    • findChild

      public @Nullable Element findChild(@NonNull String name, @NonNull String xmlns)
      Method returns first child which matches name and xmlns
    • findChildAt

      public @Nullable Element findChildAt(@NonNull Path path)
      Method returns first element which matches path
    • findChildAt

      public @Nullable Element findChildAt(@NonNull Predicate<Element> predicate, @NonNull Predicate<Element>... path)
      Method returns first element which matches path
    • findChildren

      public @NonNull List<Element> findChildren(@NonNull Predicate<Element> predicate)
      Method returns list of children matching predicate
    • findChildrenAt

      public @NonNull List<Element> findChildrenAt(@NonNull Path path)
      Method returns list of children matching path
    • getAttribute

      public @Nullable String getAttribute(String name)
      Method returns value of the attribute
    • getAttributeAt

      public @Nullable String getAttributeAt(Path path, String name)
      Method returns value of the attribute of the first element matching path
    • getAttributes

      public @NonNull Map<String,String> getAttributes()
      Method returns copy of all attributes
    • setAttributes

      public @NonNull Element setAttributes(@NonNull Map<String,String> attributes)
      Method replaces all attributes with name-value pairs provided in the map
      Returns:
      this element
    • getCData

      public @Nullable String getCData()
      Method returns CData value of the element
    • getCDataAt

      public @Nullable String getCDataAt(@NonNull Path path)
      Method returns CData value of the first element matching path
    • setCData

      public @NonNull Element setCData(@NonNull String cdata)
      Method sets CData of the element. WARNING: This method replaces existing CData and removes children of the element!
    • getChildren

      public @NonNull List<Element> getChildren()
      Method returns list of all children
    • setChildren

      public @NonNull Element setChildren(@NonNull List<XMLNodeIfc> nodes)
      Method replaces all nodes (children and cdata) with passed list of children
      Returns:
      this element
    • getName

      public @NonNull String getName()
      Method returns name of the element
    • getXMLNS

      public @Nullable String getXMLNS()
      Method returns XMLNS of the element
    • setXMLNS

      public @NonNull Element setXMLNS(@Nullable String ns)
      Method sets XMLNS of the element
      Returns:
      this element
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • map

      public <R> @Nullable R map(@NonNull Function<Element,? extends R> mapper)
      Method executes function passing self as a parameter and returns its result
    • modify

      public @NonNull Element modify(@NonNull Consumer<Element> modifier)
      Method executes consumer passing self as a parameter Useful for conditional building of element in the single line
      Returns:
      this element
    • mapChild

      public <R> R mapChild(@NonNull Predicate<Element> predicate, @NonNull Function<Element,R> mapper)
      Method transforms first child element matching predicate
    • mapChildren

      public <R> @NonNull List<R> mapChildren(@NonNull Function<Element,? extends R> mapper)
      Method applies function against each child of the element and returns list results
    • mapChildren

      public <R> @NonNull List<R> mapChildren(@NonNull Predicate<Element> predicate, @NonNull Function<Element,? extends R> mapper)
      Method applies function against each child of the element and returns list results
    • compactMapChildren

      @NotNull public <R> @NotNull List<R> compactMapChildren(@NonNull Function<Element,? extends R> mapper)
      Method applies function against each child of the element and returns list of non-null return values
    • compactMapChildren

      @NotNull public <R> @NotNull List<R> compactMapChildren(@Nullable Predicate<Element> predicate, @NonNull Function<Element,? extends R> mapper)
      Method applies function against each child of the element and returns list of non-null return values
    • forEachChild

      public void forEachChild(Consumer<Element> consumer)
    • forEachChild

      public void forEachChild(@NonNull Predicate<Element> predicate, Consumer<Element> consumer)
    • matches

      public boolean matches(@NonNull Predicate<Element> predicate)
      Method test if this element matches predicate
    • removeAttribute

      public void removeAttribute(@NonNull String name)
      Method removes attribute
    • removeChild

      public boolean removeChild(@NonNull Element child)
      Method removes child instance
      Returns:
      true - if child was removed
    • removeChild

      public boolean removeChild(@NonNull String name, @NonNull String xmlns)
      Method removes first child with matching name and xmlns
      Returns:
      true - if child was removed
    • removeChild

      public boolean removeChild(@NonNull Predicate<Element> predicate)
      Method removes first child matching predicate
      Returns:
      true - if child was removed
    • hasAttribute

      public boolean hasAttribute(@NonNull String name)
      Method check if attribute is set
    • hasAttribute

      public boolean hasAttribute(@NonNull String name, @NonNull String value)
      Method check if attribute is set to the value
    • setAttribute

      public @NonNull Element setAttribute(@NonNull String name, @NonNull String value)
      Method sets attribute value
      Returns:
      this element
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public void toString(StringBuilder result)
      Method serializes element to passed build
    • toStringPretty

      public String toStringPretty()
      Method serializes element to formatted string
      Specified by:
      toStringPretty in interface XMLNodeIfc<Element>
    • toStringNoChildren

      public String toStringNoChildren()
      Method serializes element without children
    • toStringSecure

      public String toStringSecure()
      Method serializes element to a secure string
      Specified by:
      toStringSecure in interface XMLNodeIfc<Element>
    • toStringSecure

      public void toStringSecure(StringBuilder result)
      Method serializes element as a secure string to passed builder
    • cdataToString

      public String cdataToString()
      Method returns joined CData values