Class Element


  • public final class Element
    extends Node
    An element that will render with a begin tag and attributes, a body, and an end tag. Also acts as a factory for enclosed Element, Text and Comment nodes. TODO: Support for CDATA nodes. Do we need Entity nodes?
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      Element ... classNames)
      Deprecated.
      Deprecated in 5.4, as this is now special behavior for the "class" attribute.
      Element  name,  value)
      Adds an attribute to the element, but only if the attribute name does not already exist.
      Element  namespace,  name,  value)
      Adds a namespaced attribute to the element, but only if the attribute name does not already exist.
      Element ... namesAndValues)
      Convenience for invoking attribute(String, String) multiple times.
      CData  content)
      Adds and returns a new CDATA node.
      Element  text)
      Adds the comment and returns this element for further construction.
      Element  namespace,  namespacePrefix)
      Defines a namespace for this element, mapping a URI to a prefix.
      Element  name, ... namesAndValues)
      Creates and returns a new Element node as a child of this node.
      Element elementAt​(int index,  name, ... namesAndValues)
      Creates a new element, as a child of the current index, at the indicated index.
      Element  name, ... namesAndValues)
      Inserts a new element before this element.
      Element  namespace,  name)
      Creates and returns a new Element within a namespace as a child of this node.
      Element  path)
      Searchs for a child element with a particular name below this element.
      Element ... namesAndValues)
      Forces changes to a number of attributes.
      Element  namespace, ... namesAndValues)
      Forces changes to a number of attributes in the global namespace.
       attributeName)  
      <Attribute> getAttributes()
      Returns the attributes for this Element as a (often empty) collection of Attributes.
      getChildMarkup()  
      <Node> getChildren()
      Returns an unmodifiable list of children for this element.
      Document getDocument()  
      Element getElement​(Predicate<Element> predicate)
      Tries to find an element under this element (including itself) accepted by the given predicate.
      Element  attributeName,  attributeValue)
      Tries to find an element under this element (including itself) whose given attribute has a given value.
      Element  id)
      Tries to find an element under this element (including itself) whose id is specified.
      getName()  
      getNamespace()
      Returns the namespace for this element (which is typically a URL).
      protected <,​> getNamespaceURIToPrefix()  
      boolean isEmpty()
      Returns true if the element has no children, or has only text children that contain only whitespace.
      void pop()
      Removes an element; the element's children take the place of the node within its container.
      Element  text)
      Adds the raw text and returns this element for further construction.
      Element removeChildren()
      Removes all children from this element.
      Text  text)
      Adds and returns a new text node (the text node is returned so that Text.write(String) or [@link Text.writef(String, Object[]) may be invoked .
      void visit​(Visitor visitor)
      Depth-first visitor traversal of this Element and its Element children.
      • Methods inherited from class java.lang.

        , , , , , , , , ,
    • Method Detail

      • attribute

        public  name,
                                  value)
        Adds an attribute to the element, but only if the attribute name does not already exist. The "class" attribute is treated specially: the new value is appended, after a space, to the existing value.
        Parameters:
        name - the name of the attribute to add
        value - the value for the attribute. A value of null is allowed, and no attribute will be added to the element.
      • attribute

        public  namespace,
                                  name,
                                  value)
        Adds a namespaced attribute to the element, but only if the attribute name does not already exist. The "class" attribute of the default namespace is treated specially: the new value is appended, after a space, to the existing value.
        Parameters:
        namespace - the namespace to contain the attribute, or null for the default namespace
        name - the name of the attribute to add
        value - the value for the attribute. A value of null is allowed, and no attribute will be added to the element.
      • attributes

        public ... namesAndValues)
        Convenience for invoking attribute(String, String) multiple times.
        Parameters:
        namesAndValues - alternating attribute names and attribute values
      • forceAttributes

        public ... namesAndValues)
        Forces changes to a number of attributes. The new attributes overwrite previous values. Overriding an attribute's value to null will remove the attribute entirely.
        Parameters:
        namesAndValues - alternating attribute names and attribute values
        Returns:
        this element
      • forceAttributesNS

        public  namespace,
                                         ... namesAndValues)
        Forces changes to a number of attributes in the global namespace. The new attributes overwrite previous values (event for the "class" attribute). Overriding attribute's value to null will remove the attribute entirely. TAP5-708: don't use element namespace for attributes
        Parameters:
        namespace - the namespace or null
        namesAndValues - alternating attribute name and value
        Returns:
        this element
      • element

        public  name,
                               ... namesAndValues)
        Creates and returns a new Element node as a child of this node.
        Parameters:
        name - the name of the element to create
        namesAndValues - alternating attribute names and attribute values
      • elementBefore

        public  name,
                                     ... namesAndValues)
        Inserts a new element before this element.
        Parameters:
        name - element name
        namesAndValues - attribute names and values
        Returns:
        the new element
        Since:
        5.3
      • elementNS

        public  namespace,
                                  name)
        Creates and returns a new Element within a namespace as a child of this node.
        Parameters:
        namespace - namespace to contain the element, or null
        name - element name to create within the namespace
        Returns:
        the newly created element
      • elementAt

        public Element elementAt​(int index,
                                  name,
                                 ... namesAndValues)
        Creates a new element, as a child of the current index, at the indicated index.
        Parameters:
        index - to insert at
        name - element name
        namesAndValues - attribute name / attribute value pairs
        Returns:
        the new element
      • comment

        public  text)
        Adds the comment and returns this element for further construction.
      • raw

        public  text)
        Adds the raw text and returns this element for further construction.
      • text

        public  text)
        Adds and returns a new text node (the text node is returned so that Text.write(String) or [@link Text.writef(String, Object[]) may be invoked .
        Parameters:
        text - initial text for the node
        Returns:
        the new Text node
      • cdata

        public  content)
        Adds and returns a new CDATA node.
        Parameters:
        content - the content to be rendered by the node
        Returns:
        the newly created node
      • getElementById

        public  id)
        Tries to find an element under this element (including itself) whose id is specified. Performs a width-first search of the document tree.
        Parameters:
        id - the value of the id attribute of the element being looked for
        Returns:
        the element if found. null if not found.
      • getElementByAttributeValue

        public  attributeName,
                                                   attributeValue)
        Tries to find an element under this element (including itself) whose given attribute has a given value.
        Parameters:
        attributeName - the name of the attribute of the element being looked for
        attributeValue - the value of the attribute of the element being looked for
        Returns:
        the element if found. null if not found.
        Since:
        5.2.3
      • getElement

        public Element getElement​(Predicate<Element> predicate)
        Tries to find an element under this element (including itself) accepted by the given predicate.
        Parameters:
        predicate - Predicate to accept the element
        Returns:
        the element if found. null if not found.
        Since:
        5.2.3
      • find

        public  path)
        Searchs for a child element with a particular name below this element. The path parameter is a slash separated series of element names.
      • getAttribute

        public   attributeName)
      • addClassName

        public ... classNames)
        Deprecated.
        Deprecated in 5.4, as this is now special behavior for the "class" attribute.
        Adds one or more CSS class names to the "class" attribute.
        Parameters:
        classNames - one or more CSS class names
        Returns:
        the element for further configuration
      • defineNamespace

        public  namespace,
                                        namespacePrefix)
        Defines a namespace for this element, mapping a URI to a prefix. This will affect how namespaced elements and attributes nested within the element are rendered, and will also cause xmlns: attributes (to define the namespace and prefix) to be rendered.
        Parameters:
        namespace - URI of the namespace
        namespacePrefix - prefix
        Returns:
        this element
      • getNamespace

        public  getNamespace()
        Returns the namespace for this element (which is typically a URL). The namespace may be null.
      • pop

        public void pop()
        Removes an element; the element's children take the place of the node within its container.
      • removeChildren

        public Element removeChildren()
        Removes all children from this element.
        Returns:
        the element, for method chaining
      • isEmpty

        public boolean isEmpty()
        Returns true if the element has no children, or has only text children that contain only whitespace.
        Since:
        5.1.0.0
      • visit

        public void visit​(Visitor visitor)
        Depth-first visitor traversal of this Element and its Element children. The traversal order is the same as render order.
        Parameters:
        visitor - callback
        Since:
        5.1.0.0
      • getChildren

        public <NodegetChildren()
        Returns an unmodifiable list of children for this element. Only Elements will have children. Also, note that unlike W3C DOM, attributes are not represented as Nodes.
        Returns:
        unmodifiable list of children nodes