Class XMLUtil

java.lang.Object
net.okapia.osid.torrefacto.util.XMLUtil

public class XMLUtil extends Object
Some handy XML tools built on dom4j and jaxen XPath. An instance parses a single XML file when constructed and then answers XPath queries against it; register namespace prefixes with addNamespace(String, String) before querying with prefixed paths. The parser is hardened against XML external entity (XXE) attacks: it rejects DOCTYPE declarations and does not resolve external entities. Compiled XPath expressions are cached per instance, so a repeated query reuses the parsed expression. Instances are not thread-safe.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new XMLUtil and opens the specified file.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addNamespace(String prefix, String uri)
    Adds a namespace prefix binding used to resolve prefixed XPath expressions.
    boolean
    getBooleanAttribute(org.dom4j.Node node, String attr)
    Gets a boolean attribute of the given node.
    Gets the text content under the given xpath.
    getContent(org.dom4j.Node node, String xpath)
    Gets the text content for the given xpath under the given node.
    org.dom4j.Document
    Gets the Document element.
    org.dom4j.Element
    getElement(String xpath, boolean required)
    Gets the single element matching the xpath, evaluated from the document root.
    org.dom4j.Element
    getElement(org.dom4j.Node node, String xpath, boolean required)
    Gets the single element matching the xpath, evaluated from the given node.
    Collection<org.dom4j.Node>
    Gets the nodes matching the xpath, evaluated from the document root.
    Collection<org.dom4j.Node>
    getNodes(org.dom4j.Node node, String xpath)
    Gets the nodes matching the xpath, evaluated from the given node.
    Removes extra whitespace, collapsing runs of whitespace to a single space and trimming leading and trailing whitespace.
    Trims the asXML() content and removes the outer tags.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • addNamespace

      public void addNamespace(String prefix, String uri)
      Adds a namespace prefix binding used to resolve prefixed XPath expressions.
      Parameters:
      prefix - the namespace prefix
      uri - the namespace URI
    • getDocument

      public org.dom4j.Document getDocument()
      Gets the Document element.
      Returns:
      the document
    • getElement

      public org.dom4j.Element getElement(String xpath, boolean required) throws OperationFailedException
      Gets the single element matching the xpath, evaluated from the document root.
      Parameters:
      xpath - the xpath expression
      required - true to throw if no element matches, false to return null
      Returns:
      the matching element, or null if none matches and required is false
      Throws:
      NullArgumentException - xpath is null
      OperationFailedException - the xpath is invalid, or no element matches and required is true
    • getElement

      public org.dom4j.Element getElement(org.dom4j.Node node, String xpath, boolean required) throws OperationFailedException
      Gets the single element matching the xpath, evaluated from the given node.
      Parameters:
      node - the node to evaluate from
      xpath - the xpath expression
      required - true to throw if no element matches, false to return null
      Returns:
      the matching element, or null if none matches and required is false
      Throws:
      NullArgumentException - node or xpath is null
      OperationFailedException - the xpath is invalid, or no element matches and required is true
    • getNodes

      public Collection<org.dom4j.Node> getNodes(String xpath) throws OperationFailedException
      Gets the nodes matching the xpath, evaluated from the document root.
      Parameters:
      xpath - the xpath expression
      Returns:
      the matching nodes, empty if none match
      Throws:
      NullArgumentException - xpath is null
      OperationFailedException - the xpath is invalid
    • getNodes

      public Collection<org.dom4j.Node> getNodes(org.dom4j.Node node, String xpath) throws OperationFailedException
      Gets the nodes matching the xpath, evaluated from the given node.
      Parameters:
      node - the node to evaluate from
      xpath - the xpath expression
      Returns:
      the matching nodes, empty if none match
      Throws:
      NullArgumentException - node or xpath is null
      OperationFailedException - the xpath is invalid
    • getContent

      public String getContent(String xpath) throws OperationFailedException
      Gets the text content under the given xpath.
      Parameters:
      xpath - the xpath expression
      Returns:
      the text content
      Throws:
      NullArgumentException - xpath is null
      OperationFailedException - cannot access node
    • getContent

      public String getContent(org.dom4j.Node node, String xpath) throws OperationFailedException
      Gets the text content for the given xpath under the given node.
      Parameters:
      node - the node to evaluate from
      xpath - the xpath expression
      Returns:
      the text content
      Throws:
      NullArgumentException - node or xpath is null
      OperationFailedException - cannot access node
    • getBooleanAttribute

      public boolean getBooleanAttribute(org.dom4j.Node node, String attr)
      Gets a boolean attribute of the given node. A missing or unparseable attribute reads as false.
      Parameters:
      node - the node
      attr - the attribute name
      Returns:
      the boolean value of the attribute
    • trim

      public String trim(String s)
      Trims the asXML() content and removes the outer tags.
      Parameters:
      s - the string to trim
      Returns:
      the trimmed string
      Throws:
      NullArgumentException - s is null
    • removeExtraWS

      public String removeExtraWS(String s)
      Removes extra whitespace, collapsing runs of whitespace to a single space and trimming leading and trailing whitespace.
      Parameters:
      s - the string to clean
      Returns:
      the string without extra whitespace
      Throws:
      NullArgumentException - s is null