Package org.apache.rat.testhelpers
Class XmlUtils
java.lang.Object
org.apache.rat.testhelpers.XmlUtils
Utilities to help test XML documents.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidAssert that attributes are set on a node.static voidassertIsNotPresent(Object source, XPath xPath, String xpath) Assert that an XPath is not present in the document.static voidassertIsNotPresent(String identifier, Object source, XPath xPath, String xpath) Assert that a named XPath is not present in the document.static voidassertIsPresent(Object source, XPath xPath, String xpath) Assert that an XPath is present in the document.static voidassertIsPresent(String identifier, Object source, XPath xPath, String xpath) Assert that a named XPath is present in the document.static StringgetAttribute(Object source, XPath xPath, String xpath, String attribute) Get an attribute from an XPath statementstatic NodeGets a node identified by an XPath.static NodeListgetNodeList(Object source, XPath xPath, String xpath) Gets a Nodelist from an XPath string.Gets a List of Nodes from an XPath string.static booleanDetermines whether an XPath identified a node in the source.static booleanDetermines whether the document in the input stream is well-formed.static booleanisWellFormedXml(String string) Determines whether the document string is well-formed.static booleanisWellFormedXml(InputSource isource) Determines whether the document in the input source is well-formed.static XMLReaderConstruct a safe XML reader.static voidprintDocument(OutputStream out, Document document) Print the XML document to the output stream.static StringprintNodeList(NodeList nodeList) Prints the specified NodeList as a string representation of its contents.static DocumenttoDom(InputStream inputStream) Reads an input stream into a document.static voidwriteAttribute(XmlWriter writer, String name, boolean booleanValue) Writes a boolean attribute to an XML writer.
-
Method Details
-
newXMLReader
Construct a safe XML reader.- Returns:
- an XML reader.
- Throws:
SAXException- on SAX exception.ParserConfigurationException- on parser configuration exception.
-
isWellFormedXml
Determines whether the document string is well-formed.- Parameters:
string- the document string to check.- Returns:
trueif the document is well-formed,falseotherwise.
-
isWellFormedXml
Determines whether the document in the input stream is well-formed.- Parameters:
in- the input stream containing the document.- Returns:
trueif the document is well-formed,falseotherwise.
-
isWellFormedXml
Determines whether the document in the input source is well-formed.- Parameters:
isource- the input source containing the document.- Returns:
trueif the document is well-formed,falseotherwise.
-
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. SeeXPathExpression.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. SeeXPathExpression.evaluate(Object, QName).xPath- the XPath object to compile the statement with.xpath- the XPath statement to compile.- Returns:
trueif the document contains the node identified by the XPath statement,falseotherwise.- 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. SeeXPathExpression.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. SeeXPathExpression.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
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
Reads an input stream into a document.- Parameters:
inputStream- the input stream to read.- Returns:
- the Document.
- Throws:
SAXException- on sax ErrorIOException- 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
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. SeeXPathExpression.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
-
assertAttributes
public static void assertAttributes(Object source, XPath xPath, String xpath, Map<String, String> attributes) throws XPathExpressionExceptionAssert that attributes are set on a node.- Parameters:
source- the context for the XPath statement to be evaluated in. SeeXPathExpression.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. SeeXPathExpression.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. SeeXPathExpression.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. SeeXPathExpression.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. SeeXPathExpression.evaluate(Object, QName).xPath- The XPath object to compile the statement with.xpath- the XPath statement to compile.- Throws:
XPathExpressionException
-