Class XMLUtil
java.lang.Object
net.okapia.osid.torrefacto.util.XMLUtil
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 -
Method Summary
Modifier and TypeMethodDescriptionvoidaddNamespace(String prefix, String uri) Adds a namespace prefix binding used to resolve prefixed XPath expressions.booleangetBooleanAttribute(org.dom4j.Node node, String attr) Gets a boolean attribute of the given node.getContent(String xpath) 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.DocumentGets the Document element.org.dom4j.ElementgetElement(String xpath, boolean required) Gets the single element matching the xpath, evaluated from the document root.org.dom4j.ElementgetElement(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> 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 theasXML()content and removes the outer tags.
-
Constructor Details
-
XMLUtil
Constructs a newXMLUtiland opens the specified file.- Parameters:
path- the path to the xml file- Throws:
NotFoundException- file not foundNullArgumentException-pathisnullOperationFailedException- could not read file
-
-
Method Details
-
addNamespace
-
getDocument
public org.dom4j.Document getDocument()Gets the Document element.- Returns:
- the document
-
getElement
Gets the single element matching the xpath, evaluated from the document root.- Parameters:
xpath- the xpath expressionrequired-trueto throw if no element matches,falseto returnnull- Returns:
- the matching element, or
nullif none matches andrequiredisfalse - Throws:
NullArgumentException-xpathisnullOperationFailedException- the xpath is invalid, or no element matches andrequiredistrue
-
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 fromxpath- the xpath expressionrequired-trueto throw if no element matches,falseto returnnull- Returns:
- the matching element, or
nullif none matches andrequiredisfalse - Throws:
NullArgumentException-nodeorxpathisnullOperationFailedException- the xpath is invalid, or no element matches andrequiredistrue
-
getNodes
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-xpathisnullOperationFailedException- 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 fromxpath- the xpath expression- Returns:
- the matching nodes, empty if none match
- Throws:
NullArgumentException-nodeorxpathisnullOperationFailedException- the xpath is invalid
-
getContent
Gets the text content under the given xpath.- Parameters:
xpath- the xpath expression- Returns:
- the text content
- Throws:
NullArgumentException-xpathisnullOperationFailedException- cannot access node
-
getContent
Gets the text content for the given xpath under the given node.- Parameters:
node- the node to evaluate fromxpath- the xpath expression- Returns:
- the text content
- Throws:
NullArgumentException-nodeorxpathisnullOperationFailedException- cannot access node
-
getBooleanAttribute
Gets a boolean attribute of the given node. A missing or unparseable attribute reads asfalse.- Parameters:
node- the nodeattr- the attribute name- Returns:
- the boolean value of the attribute
-
trim
Trims theasXML()content and removes the outer tags.- Parameters:
s- the string to trim- Returns:
- the trimmed string
- Throws:
NullArgumentException-sisnull
-
removeExtraWS
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-sisnull
-