Class XmlNode

java.lang.Object
org.libxmq.imp.xml.XmlNode
All Implemented Interfaces:
XMQNode
Direct Known Subclasses:
XmlAttr, XmlComment, XmlDocument, XmlDtd, XmlDtdDecl, XmlElement, XmlEntityDecl, XmlEntityRef, XmlPI, XmlText, XPathNamespaceNode

public abstract class XmlNode extends Object implements XMQNode
Base class of all nodes in the xmq DOM. The DOM is modelled after libxml2 (Copyright (C) 1998-2012 Daniel Veillard, MIT license), since the C version of xmq uses libxml2 and the java version must behave identically. Like libxml2, the nodes are linked with parent/children/last/next/prev pointers, entity references are kept as nodes, and adding a text node next to another text node merges them.
  • Constructor Details

    • XmlNode

      protected XmlNode(String name)
      Create a node with a name.
      Parameters:
      name - The node name, null for nameless nodes.
  • Method Details

    • type

      public abstract XMQNodeType type()
      Returns the type of this node.
      Specified by:
      type in interface XMQNode
      Returns:
      The node type.
    • name

      public String name()
      Returns the name of the node. For an element or attribute this is the local name (without namespace prefix). For a pi it is the target, for an entity reference or declaration it is the entity name.
      Specified by:
      name in interface XMQNode
      Returns:
      The name or null.
    • setName

      public void setName(String name)
      Set the name of the node.
      Specified by:
      setName in interface XMQNode
      Parameters:
      name - The new name.
    • line

      public int line()
      Returns the source line of the node, like libxml2 xmlGetLineNo: elements get the line where their start tag ends, text the line where its first chunk was read, comments, processing instructions and cdata sections the line where they end.
      Specified by:
      line in interface XMQNode
      Returns:
      The line, 0 if unknown.
    • setLine

      public void setLine(int line)
      Set the source line of the node.
      Specified by:
      setLine in interface XMQNode
      Parameters:
      line - The line.
    • parent

      public XmlNode parent()
      Returns the parent node.
      Specified by:
      parent in interface XMQNode
      Returns:
      The parent or null.
    • firstChild

      public XmlNode firstChild()
      Returns the first child.
      Specified by:
      firstChild in interface XMQNode
      Returns:
      The first child or null.
    • lastChild

      public XmlNode lastChild()
      Returns the last child.
      Specified by:
      lastChild in interface XMQNode
      Returns:
      The last child or null.
    • next

      public XmlNode next()
      Returns the next sibling.
      Specified by:
      next in interface XMQNode
      Returns:
      The next sibling or null.
    • prev

      public XmlNode prev()
      Returns the previous sibling.
      Specified by:
      prev in interface XMQNode
      Returns:
      The previous sibling or null.
    • doc

      public XmlDocument doc()
      Returns the document owning this node.
      Specified by:
      doc in interface XMQNode
      Returns:
      The document or null.
    • hasChildren

      public boolean hasChildren()
      Returns true if this node has children.
      Returns:
      True if there is at least one child.
    • children

      public List<XMQNode> children()
      Returns a new list with the children of this node.
      Specified by:
      children in interface XMQNode
      Returns:
      The children, an empty list if none.
    • numChildren

      public int numChildren()
      Count the children.
      Specified by:
      numChildren in interface XMQNode
      Returns:
      The number of children.
    • content

      public String content()
      Returns the text content of text like nodes (text, cdata, comment, pi). Returns null for other nodes, use textContent() for the concatenated text of an element.
      Specified by:
      content in interface XMQNode
      Returns:
      The content or null.
    • setContent

      public void setContent(String content)
      Set the content of a text like node. For an element, all children are replaced with a single text node, like libxml2 xmlNodeSetContent.
      Specified by:
      setContent in interface XMQNode
      Parameters:
      content - The new content.
    • textContent

      public String textContent()
      Returns the string value of the node, like libxml2 xmlNodeGetContent. For an element this is the concatenation of all descendant text, where entity references contribute nothing (entities are never expanded implicitly) and character references kept as nodes their character.
      Specified by:
      textContent in interface XMQNode
      Returns:
      The text content, never null.
    • appendRaw

      public XmlNode appendRaw(XMQNode child)
      Append a child without merging adjacent text nodes.
      Specified by:
      appendRaw in interface XMQNode
      Parameters:
      child - The child to append, it is unlinked first.
      Returns:
      The child.
    • addChild

      public XmlNode addChild(XMQNode child)
      Add a child like libxml2 xmlAddChild. If the child is a text node and the current last child is also a text node, then the text is merged into the existing last child, and the existing node is returned.
      Specified by:
      addChild in interface XMQNode
      Parameters:
      child - The child to add.
      Returns:
      The node holding the child content.
    • addPrevSibling

      public XmlNode addPrevSibling(XMQNode n)
      Insert a sibling before this node, like libxml2 xmlAddPrevSibling (but without text merging).
      Specified by:
      addPrevSibling in interface XMQNode
      Parameters:
      n - The node to insert.
      Returns:
      The inserted node.
    • addNextSibling

      public XmlNode addNextSibling(XMQNode n)
      Insert a sibling after this node, like libxml2 xmlAddNextSibling (but without text merging).
      Specified by:
      addNextSibling in interface XMQNode
      Parameters:
      n - The node to insert.
      Returns:
      The inserted node.
    • unlink

      public void unlink()
      Remove this node from its parent and siblings, like libxml2 xmlUnlinkNode.
      Specified by:
      unlink in interface XMQNode
    • replaceWith

      public void replaceWith(XMQNode n)
      Replace this node with another node, like libxml2 xmlReplaceNode.
      Specified by:
      replaceWith in interface XMQNode
      Parameters:
      n - The replacement.
    • fullName

      public String fullName()
      Returns the full name, ie prefix:name if the node has a namespace with a prefix.
      Specified by:
      fullName in interface XMQNode
      Returns:
      The full name.
    • toString

      public String toString()
      Overrides:
      toString in class Object