Class XmlUtils

java.lang.Object
org.apache.rat.testhelpers.XmlUtils

public final class XmlUtils extends Object
Utilities to help test XML documents.
  • Method Details

    • newXMLReader

      public static XMLReader newXMLReader() throws SAXException, ParserConfigurationException
      Construct a safe XML reader.
      Returns:
      an XML reader.
      Throws:
      SAXException - on SAX exception.
      ParserConfigurationException - on parser configuration exception.
    • isWellFormedXml

      public static boolean isWellFormedXml(String string)
      Determines whether the document string is well-formed.
      Parameters:
      string - the document string to check.
      Returns:
      true if the document is well-formed, false otherwise.
    • isWellFormedXml

      public static boolean isWellFormedXml(InputStream in)
      Determines whether the document in the input stream is well-formed.
      Parameters:
      in - the input stream containing the document.
      Returns:
      true if the document is well-formed, false otherwise.
    • isWellFormedXml

      public static boolean isWellFormedXml(InputSource isource)
      Determines whether the document in the input source is well-formed.
      Parameters:
      isource - the input source containing the document.
      Returns:
      true if the document is well-formed, false otherwise.
    • getNodeList

      public static NodeList getNodeList(Object source, XPath xPath, String xpath) throws XPathExpressionException
      Gets a Nodelist from an XPath string.
      Parameters:
      source - the context for the XPath statement to be evaluated in. See XPathExpression.evaluate(Object, QName).
      xPath - the XPath object to compile the statement with.
      xpath - the XPath statement to compile.
      Returns:
      the NodeList of nodes that matches the XPath.
      Throws:
      XPathExpressionException - on error.
    • isPresent

      public static boolean isPresent(Object source, XPath xPath, String xpath) throws XPathExpressionException
      Determines whether an XPath identified a node in the source.
      Parameters:
      source - the context for the XPath statement to be evaluated in. See XPathExpression.evaluate(Object, QName).
      xPath - the XPath object to compile the statement with.
      xpath - the XPath statement to compile.
      Returns:
      true if the document contains the node identified by the XPath statement, false otherwise.
      Throws:
      XPathExpressionException - on error.
    • getNodes

      public static List<Node> getNodes(Object source, XPath xPath, String xpath) throws XPathExpressionException
      Gets a List of Nodes from an XPath string.
      Parameters:
      source - the context for the XPath statement to be evaluated in. See XPathExpression.evaluate(Object, QName).
      xPath - the XPath object to compile the statement with.
      xpath - the XPath statement to compile.
      Returns:
      the list of nodes that match the XPath.
      Throws:
      XPathExpressionException - on error.
    • getNode

      public static Node getNode(Object source, XPath xPath, String xpath) throws XPathExpressionException
      Gets a node identified by an XPath.
      Parameters:
      source - the context for the XPath statement to be evaluated in. See XPathExpression.evaluate(Object, QName).
      xPath - the XPath object to compile the statement with.
      xpath - the XPath statement to compile.
      Returns:
      the identified Node.
      Throws:
      XPathExpressionException - on error.
      org.opentest4j.AssertionFailedError - if more than one node is found.
    • printNodeList

      public static String printNodeList(NodeList nodeList)
      Prints the specified NodeList as a string representation of its contents.
      Parameters:
      nodeList - the Nodelist to print.
      Returns:
      the string that contains the textual representation of the NodeList nodes.
    • toDom

      public static Document toDom(InputStream inputStream) throws SAXException, IOException
      Reads an input stream into a document.
      Parameters:
      inputStream - the input stream to read.
      Returns:
      the Document.
      Throws:
      SAXException - on sax Error
      IOException - on IO Error
    • writeAttribute

      public static void writeAttribute(XmlWriter writer, String name, boolean booleanValue) throws IOException
      Writes a boolean attribute to an XML writer.
      Parameters:
      writer - the writer to write to.
      name - the name of the attribute.
      booleanValue - the boolean value.
      Throws:
      IOException - on write error.
    • printDocument

      public static void printDocument(OutputStream out, Document document)
      Print the XML document to the output stream.
      Parameters:
      out - the OutputStream to print the document to.
      document - The XML DOM document to print.
    • getAttribute

      public static String getAttribute(Object source, XPath xPath, String xpath, String attribute) throws XPathExpressionException
      Get an attribute from an XPath statement
      Parameters:
      source - the context for the XPath statement to be evaluated in. See XPathExpression.evaluate(Object, QName).
      xPath - the XPath object to compile the statement with.
      xpath - the XPath statement to compile.
      attribute - attribute to retrieve from the node specified by the XPath statement.
      Returns:
      the string value of the attribute.
      Throws:
      XPathExpressionException - on error
    • mapOf

      public static Map<String,String> mapOf(String... parts)
    • assertAttributes

      public static void assertAttributes(Object source, XPath xPath, String xpath, Map<String,String> attributes) throws XPathExpressionException
      Assert that attributes are set on a node.
      Parameters:
      source - the context for the XPath statement to be evaluated in. See XPathExpression.evaluate(Object, QName).
      xPath - the XPath object to compile the statement with.
      xpath - the XPath statement to compile.
      attributes - a map of attribute names to values.
      Throws:
      XPathExpressionException - on error.
    • assertIsPresent

      public static void assertIsPresent(Object source, XPath xPath, String xpath) throws XPathExpressionException
      Assert that an XPath is present in the document.
      Parameters:
      source - the context for the XPath statement to be evaluated in. See XPathExpression.evaluate(Object, QName).
      xPath - the XPath object to compile the statement with.
      xpath - the XPath statement to compile.
      Throws:
      XPathExpressionException
    • assertIsNotPresent

      public static void assertIsNotPresent(Object source, XPath xPath, String xpath) throws XPathExpressionException
      Assert that an XPath is not present in the document.
      Parameters:
      source - the context for the XPath statement to be evaluated in. See XPathExpression.evaluate(Object, QName).
      xPath - the XPath object to compile the statement with.
      xpath - the XPath statement to compile.
      Throws:
      XPathExpressionException
    • assertIsPresent

      public static void assertIsPresent(String identifier, Object source, XPath xPath, String xpath) throws XPathExpressionException
      Assert that a named XPath is present in the document.
      Parameters:
      identifier - the name of the object.
      source - the context for the XPath statement to be evaluated in. See XPathExpression.evaluate(Object, QName).
      xPath - The XPath object to compile the statement with.
      xpath - the XPath statement to compile.
      Throws:
      XPathExpressionException
    • assertIsNotPresent

      public static void assertIsNotPresent(String identifier, Object source, XPath xPath, String xpath) throws XPathExpressionException
      Assert that a named XPath is not present in the document.
      Parameters:
      identifier - the name of the object.
      source - the context for the XPath statement to be evaluated in. See XPathExpression.evaluate(Object, QName).
      xPath - The XPath object to compile the statement with.
      xpath - the XPath statement to compile.
      Throws:
      XPathExpressionException