java.lang.Object
com.aquima.interactions.foundation.xml.generation.XmlNewAbstractElement
com.aquima.interactions.foundation.xml.generation.XmlNewElement
All Implemented Interfaces:
IXmlElement, Serializable
Direct Known Subclasses:
XmlNewRawElement

public class XmlNewElement extends XmlNewAbstractElement implements IXmlElement
Simple Element to build new XML Documents using the DOM Generator.

This class is not synchronized/thread safe by itself

For more information, see the IXmlElement interface. please also have a look at the XML escaping method used for special characters, see XmlGenerator, escape.
Since:
5.0
Author:
IdJ
See Also:
  • Constructor Details

    • XmlNewElement

      public XmlNewElement(IXmlElement existingElt)
      Construct a deep copy from an existing INewDOMNode object, recursively. This is a SLOW operation!! This means that the whole XML tree is converted into another tree, consisting of INewDOMNode objects. Element nodes and comment nodes are converted/copied, all other nodes are skipped.
      Parameters:
      existingElt - The element definition that should be copied.
    • XmlNewElement

      protected XmlNewElement()
      protected no-value constructor, not for public use.
    • XmlNewElement

      public XmlNewElement(String name)
      construct empty element.
      Parameters:
      name - The tag name of the element.
      Throws:
      XmlGenerateException - when an exception occurs generating XML
    • XmlNewElement

      public XmlNewElement(String name, IXmlElement[] children)
      Construct new element whith array of child elements. This constructor allows you to create a whole XML subtree in a single statement, for instance:
       gen = new XMLGenerator(new XMLNewElement("root",
           new INewDOMNode[] { new XMLNewElement("child1", "value one"),
               new XMLNewComment("see some nesting in one statement"),
                  new XMLNewElement("sub", new INewDOMNode[] {
                      new XMLNewElement("subchild1", "sub one"),
                          new XMLNewElement("subchild2", "sub two") }) }));
       System.out.println(gen.toXML());
       
      which will result in the following XML document:
       <?xml version="1.0" encoding="UTF-8"?>
       <root>
           <child1>value one</child1>
           <!--see some nesting in one statement-->
           <sub>
               <subchild1>sub one</subchild1>
               <subchild2>sub two</subchild2>
           </sub>
       </root>
       
      Parameters:
      name - The tage name of the element.
      children - The child elements of the element.
      Throws:
      XmlGenerateException - when an exception occurs generating XML
    • XmlNewElement

      public XmlNewElement(String name, String stringValue)
      construct string value element.
      Parameters:
      name - The tag name of the element.
      stringValue - The value of the element.
      Throws:
      XmlGenerateException - when an exception occurs generating XML
    • XmlNewElement

      public XmlNewElement(String name, byte[] rawValue, String encoding)
      construct string value element but with raw byte array input, supply character encoding used for byte array.
      Parameters:
      name - The tag name of the element.
      rawValue - A byte array containing the content of the value.
      encoding - The encoding of the byte array.
      Throws:
      XmlGenerateException - when something went wrong with encoding.
    • XmlNewElement

      public XmlNewElement(String name, int integerValue)
      construct integer value element.
      Parameters:
      name - The tag name of the element.
      integerValue - The integer value of the element.
      Throws:
      XmlGenerateException - when an exception occurs generating XML
    • XmlNewElement

      public XmlNewElement(String name, long longValue)
      construct long value element.
      Parameters:
      name - The tag name of the element.
      longValue - The long value of the element.
      Throws:
      XmlGenerateException - when an exception occurs generating XML
    • XmlNewElement

      public XmlNewElement(String name, double doubleValue)
      construct double value element (default number of digits after decimal point) .
      Parameters:
      name - The tag name of the element.
      doubleValue - The double value of the element.
      Throws:
      XmlGenerateException - when an exception occurs generating XML
    • XmlNewElement

      public XmlNewElement(String name, double doubleValue, int digits)
      construct double value element (with specified number of digits after the decimal point).
      Parameters:
      name - The tag name of the element.
      doubleValue - The double value of the element.
      digits - The number of digits that should be included.
      Throws:
      XmlGenerateException - when an exception occurs generating XML
    • XmlNewElement

      public XmlNewElement(String name, Date dateValue)
      construct Date (XSD/ISO notation; YYYY-MM-DDTHH:mm:ss) value element.
      Parameters:
      name - The tag name of the element.
      dateValue - The data value of the element.
      Throws:
      XmlGenerateException - when an exception occurs generating XML
    • XmlNewElement

      public XmlNewElement(String name, Date dateValue, boolean withTime)
      construct Date (XSD/ISO notation; YYYY-MM-DDTHH:mm:ss) value element (with or without the time part).
      Parameters:
      name - The tag name of the element.
      dateValue - The data value of the element.
      withTime - Boolean indicating if the time should be included.
      Throws:
      XmlGenerateException - when an exception occurs generating XML
    • XmlNewElement

      public XmlNewElement(String name, boolean booleanValue)
      construct boolean (true/false) value element.
      Parameters:
      name - The tag name of the element.
      booleanValue - The boolean value of the element.
      Throws:
      XmlGenerateException - when an exception occurs generating XML
  • Method Details

    • hasAttributes

      public boolean hasAttributes()
      Description copied from interface: IXmlElement
      Shortcut method to quickly see if there are any attributes.
      Specified by:
      hasAttributes in interface IXmlElement
      Returns:
      Boolean indicating if this element has any attributes.
    • isCData

      public boolean isCData()
      Description copied from interface: IXmlElement
      This method returns a boolean indicating if the value will be wrapped in a CDATA block.
      Specified by:
      isCData in interface IXmlElement
      Returns:
      Boolean indicating if the value will be wrapped in a CDATA block
    • setCData

      public void setCData(boolean enable)
      Description copied from interface: IXmlElement
      Enable (or disable) CDATA representation. CDATA mode outputs unescaped data in <![CDATA[ ... ]]> delimiters. Note that this has no effect on parsing because they will process the CDATA block automatically. You only change the output representation of this element's value.
      Specified by:
      setCData in interface IXmlElement
      Parameters:
      enable - enable CDATA representation? true, false=disable.
    • setValue

      public void setValue(XmlGenerator gen)
      Description copied from interface: IXmlElement
      change the value to be another full XML document (not indented). You might want to consider setting CDATA for this element if you use this.
      Specified by:
      setValue in interface IXmlElement
      Parameters:
      gen - The XML element that should entirely be included as value.
    • setValue

      public void setValue(int integerValue)
      Description copied from interface: IXmlElement
      change the value to a new int.
      Specified by:
      setValue in interface IXmlElement
      Parameters:
      integerValue - The new integer value.
    • setValue

      public void setValue(long longValue)
      Description copied from interface: IXmlElement
      change the value to a new long.
      Specified by:
      setValue in interface IXmlElement
      Parameters:
      longValue - The new long value.
    • setValue

      public void setValue(double doubleValue)
      Description copied from interface: IXmlElement
      change the value to a new double (default number of digits after decimal point).
      Specified by:
      setValue in interface IXmlElement
      Parameters:
      doubleValue - The new double value.
    • setValue

      public void setValue(double doubleValue, int digits)
      Description copied from interface: IXmlElement
      change the value to a new double (with specified number of digits after the decimal point).
      Specified by:
      setValue in interface IXmlElement
      Parameters:
      doubleValue - The new double value.
      digits - The number of digits that should be included.
    • setValue

      public void setValue(boolean booleanValue)
      Description copied from interface: IXmlElement
      change the value to a new boolean (true/false).
      Specified by:
      setValue in interface IXmlElement
      Parameters:
      booleanValue - The new boolean value.
    • setValue

      public void setValue(Date dateValue)
      Description copied from interface: IXmlElement
      change the value to a new Date (XSD/ISO notation: YYYY-MM-DDTHH:mm:ss).
      Specified by:
      setValue in interface IXmlElement
      Parameters:
      dateValue - The new date value.
    • setValue

      public void setValue(Date dateValue, boolean withTime)
      Description copied from interface: IXmlElement
      change the value to a new Date (XSD/ISO notation: YYYY-MM-DDTHH:mm:ss) with or without time.
      Specified by:
      setValue in interface IXmlElement
      Parameters:
      dateValue - The new date value.
      withTime - boolean indicating if the time should be included in the value.
    • setAttribute

      public String setAttribute(String name, String stringValue)
      Description copied from interface: IXmlElement
      Set an attribute for this element, name=value. Only useful if the element has not yet been added as a child
      • Name can contain letters, numbers, and other characters
      • Name cannot contain spaces, <, >, ", ' or &
      • Value cannot be null
      Specified by:
      setAttribute in interface IXmlElement
      Parameters:
      name - The name of the attribute that should be set.
      stringValue - The value of the attribute.
      Returns:
      The previous value of the attribute, may be null.
    • getAttributeSafe

      public String getAttributeSafe(String name)
      Description copied from interface: IXmlElement
      Get the value of an attribute of this element.
      Specified by:
      getAttributeSafe in interface IXmlElement
      Parameters:
      name - the attribute name to get the value of.
      Returns:
      the value of the attribute (String), or null if the attribute doesn't exist.
      See Also:
    • getAttribute

      public String getAttribute(String name)
      Description copied from interface: IXmlElement
      Get the value of an attribute of this element.
      Specified by:
      getAttribute in interface IXmlElement
      Parameters:
      name - the attribute name to get the value of.
      Returns:
      the value of the attribute
      See Also:
    • removeAttribute

      public String removeAttribute(String name)
      Description copied from interface: IXmlElement
      This method removes the attribute with the specified name. When there was no attribute specified for the given name, this method has no effect. The attribute name is case sensitive.
      Specified by:
      removeAttribute in interface IXmlElement
      Parameters:
      name - The name of the attribute that should be removed.
      Returns:
      The previous value of the attribute that has been removed.
    • getAttributes

      public String[] getAttributes()
      Description copied from interface: IXmlElement
      Get all attribute names of this element. If there are no attributes, returns an empty array.
      Specified by:
      getAttributes in interface IXmlElement
      Returns:
      Array containing all the attribute names of this element.
      See Also:
    • getAttributesMap

      public Map<String,String> getAttributesMap()
      Description copied from interface: IXmlElement
      Get all attributes of this element, as an ATTRNAME-to-VALUE mapping. If there are no attributes, returns an empty Map.
      Specified by:
      getAttributesMap in interface IXmlElement
      Returns:
      Map containing all the attribute names and values.
      See Also:
    • replaceChild

      public void replaceChild(IXmlElement oldChild, IXmlElement newChild)
      Description copied from interface: IXmlElement
      replace a child with a new child.
      Specified by:
      replaceChild in interface IXmlElement
      Parameters:
      oldChild - child node to replace
      newChild - child node to replace with
    • addChild

      public IXmlElement addChild(IXmlElement child)
      Description copied from interface: IXmlElement
      Add a child element. Only useful if the element has not yet been added as a child.
      Specified by:
      addChild in interface IXmlElement
      Parameters:
      child - the new node to add as a child node (a reference will be added - any changes made to the child after it was added will be performed on the child in the XML structure too) Beware of Circular References!!!
      Returns:
      the newly added child element
    • addChildren

      public void addChildren(IXmlElement[] children)
      Description copied from interface: IXmlElement
      Adds the children array to the element, it's an easy method for adding all children seperately. If the array is empty no childs are added.
      Specified by:
      addChildren in interface IXmlElement
      Parameters:
      children - The children array.
    • addChildCopy

      public IXmlElement addChildCopy(IXmlElement child)
      Description copied from interface: IXmlElement
      Add a child element. Only useful if the element has not yet been added as a child. Because copies are made, this method is slower (but safer) than the other add method.
      Specified by:
      addChildCopy in interface IXmlElement
      Parameters:
      child - the new node to add as a child node (a replica will be added - any changes made to the child after it was added are useless, unless you add the same object again).
      Returns:
      the newly added copy of the child element
      See Also:
    • removeChild

      public void removeChild(IXmlElement childNode)
      Description copied from interface: IXmlElement
      remove a child node.
      Specified by:
      removeChild in interface IXmlElement
      Parameters:
      childNode - node to remove
    • removeAllChildren

      public IXmlElement[] removeAllChildren()
      Description copied from interface: IXmlElement
      remove all child nodes.
      Specified by:
      removeAllChildren in interface IXmlElement
      Returns:
      Array containing the children that were removed.
    • addSibling

      public void addSibling(IXmlElement child, IXmlElement newSibling, boolean before)
      Description copied from interface: IXmlElement
      Add a sibling child next to a given existing child.
      Specified by:
      addSibling in interface IXmlElement
      Parameters:
      child - existing child node to add a new node next to
      newSibling - new node to add
      before - set to true if you want to insert before existing child, otherwise the new sibling is inserted after it
    • getChildren

      public List<IXmlElement> getChildren()
      Description copied from interface: IXmlElement
      Return a list of all children of this element (only one level deep). This includes comment nodes, entity nodes, TEXT nodes etc. !!!!!
      Specified by:
      getChildren in interface IXmlElement
      Returns:
      the list of XmlNode children for this element (empty List when there are no children)
    • toXmlFragment

      public void toXmlFragment(boolean useIndentation, boolean sortAttributes, int level, StringBuffer str)
      Description copied from interface: IXmlElement
      Recursive conversion to an XML document. XML problem chars will be escaped (",<,>, etc). The XML code is appended to a stringbuffer. It uses indentation for pretty printing. Indentation starts at 0.
      Specified by:
      toXmlFragment in interface IXmlElement
      Specified by:
      toXmlFragment in class XmlNewAbstractElement
      Parameters:
      useIndentation - true to use whitespace indentation for pretty printing, false to avoid any whitespace between XML elements
      sortAttributes - true to use sort the attributes by name in alphabetic order.
      level - indentation level (tabs), start with 0 (unused if useIndentation is false)
      str - existing StringBuffer to append into
    • getValueXMLRepr

      protected void getValueXMLRepr(StringBuffer outputBuffer)
      This method appends the value of the tag to the string buffer.
      Parameters:
      outputBuffer - The string buffer the value should be appended to.
    • escape

      protected String escape(String str)
      Escapes special characters in the given string.
      Parameters:
      str - The string that should be escaped
      Returns:
      The escaped string.
    • escape

      protected void escape(String str, StringBuffer outputBuffer)
      escape method operating on a StringBuffer. This method delegates to XMLGenerator.escape(String s, StringBuffer out ), but can be specialized in subclasses.
      Parameters:
      str - The string that should be escaped
      outputBuffer - The string buffer the escaped string should be appended to.
      See Also:
    • containsNode

      public boolean containsNode(IXmlElement n)
      Description copied from interface: IXmlElement
      check if node exists in our tree - used for cycle detection.
      Specified by:
      containsNode in interface IXmlElement
      Parameters:
      n - The element that should be checked.
      Returns:
      boolean indicating if the element is present as a child element.
    • findTag

      public IXmlElement findTag(String tagname)
      Get the first sub-element with a certain XML tag name.
      Specified by:
      findTag in interface IXmlElement
      Parameters:
      tagname - the name of the tag
      Returns:
      the first sub-element with the given tagname
      Throws:
      XmlException - if no tag was found
    • findChildren

      public List<IXmlElement> findChildren(String tagname)
      Description copied from interface: IXmlElement
      Find the elements that occurs as child (only one level deep). Returns empty list if no children are found.
      Specified by:
      findChildren in interface IXmlElement
      Parameters:
      tagname - The tag name for which all children are requested.
      Returns:
      The list of children matching the spacified tag name.
    • findTags

      public List<IXmlElement> findTags(String tagname)
      Description copied from interface: IXmlElement
      Make a list of all XmlNewElement elements with a certain XML tag name, recursively. The list is constructed in preorder document traversal (depth-first).
      Specified by:
      findTags in interface IXmlElement
      Parameters:
      tagname - the name of the tags you seek
      Returns:
      a List containing all XmlNewElement with the tag name. It will be an empty list if no tags are found.
    • findFirstChild

      public IXmlElement findFirstChild(String tagname)
      Find the first direct child with the given tag name, throws exception if it's not found.
      Specified by:
      findFirstChild in interface IXmlElement
      Parameters:
      tagname - The tag name of the child element that should be returned.
      Returns:
      The child element for the specified tag name.
      Throws:
      XmlException - This exception is thrown when no element with the specified tag-name could be found.
    • getFirstElement

      public IXmlElement getFirstElement()
      Description copied from interface: IXmlElement
      Returns the first element child of this element (only one level deep). This will skip comment nodes, entity nodes, #TEXT nodes etc.
      Specified by:
      getFirstElement in interface IXmlElement
      Returns:
      the first element child of this element
    • getFirstChild

      public IXmlElement getFirstChild()
      Returns the first child of this element (only one level deep). This may be a comment node, entity node, #TEXT node etc. !!!!!
      Specified by:
      getFirstChild in interface IXmlElement
      Returns:
      the first child node of this element
      Throws:
      XmlException - if this element has no child nodes
    • hasChildren

      protected boolean hasChildren()
      Specified by:
      hasChildren in class XmlNewAbstractElement
    • getNextSibling

      public IXmlElement getNextSibling()
      Description copied from interface: IXmlElement
      Return the node immediately following this node. If there is no such node, this returns null. This includes comment nodes, entity nodes, TEXT nodes etc. !!!!!
      Specified by:
      getNextSibling in interface IXmlElement
      Returns:
      the node immediately following this node. If there is no such node, this returns null.