Package org.cip4.lib.xjdf.xml.internal
Class XmlNavigator
java.lang.Object
org.cip4.lib.xjdf.xml.internal.XmlNavigator
- Direct Known Subclasses:
XJdfNavigator
Navigator class which simplify XPath handling using XML Documents.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final NamespaceManagerprotected final Documentprotected final XPath -
Constructor Summary
ConstructorsConstructorDescriptionXmlNavigator(byte[] xmlBytes) Custom constructor.XmlNavigator(byte[] xmlBytes, boolean namespaceAware) Custom constructor.XmlNavigator(InputStream xmlStream) Custom constructor.XmlNavigator(InputStream xmlStream, boolean namespaceAware) Custom constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddNamespace(String prefix, String uri) Add XML Namespace if navigator works namespace aware.final <T extends AbstractXJdfType>
TEvaluates an XPath expression on XML Document and returns the result as XJDF DataType.booleanevaluateBoolean(String xPath) Evaluate an XPath expression on XML Document and returns an Boolean value as result.doubleevaluateDouble(String xPath) Evaluates an XPath expression on XML Document and returns a Double object as result.evaluateInt(String xPath) Evaluates an XPath expression on XML Document and returns an Integer value as result.evaluateLong(String xPath) Evaluates an XPath expression on XML Document and returns an Long value as result.evaluateNode(String xPath) Evaluates an XPath expression on XML Document and returns a Node object as result.evaluateNodeList(String xPath) Evaluates an XPath expression on XML Document and returns a NodeList object as result.evaluateString(String xPath) Evaluates an XPath expression on XML Document and returns a String object as result.protected ObjectextractNode(String xPath, AbstractXmlParser parser) Extract an XML Node from XML Document and return parsed result.byte[]Return the XML Document as Byte Array.Getter for xmlDocument attribute.Return the XML Document as input stream.readAttribute(String xPath) Read attribute from XML Document using XPath.readAttribute(String xPath, Class xJdfType) Read attribute from XML Document using XPath and convert to JDF Datatype.voidremoveNode(String xPath) Remove a node from XML Document locaed by the XPath expression.protected voidreplaceNode(String xPath, Object replacement, AbstractXmlParser parser) Replace a node in XML Document located by the XPath expression.voidupdateAttribute(String xPath, String value) Update attribute in XML Document.voidupdateAttribute(String xPath, AbstractXJdfType value) Update attribute in XML Document.
-
Field Details
-
xPath
-
xmlDocument
-
nsManager
-
-
Constructor Details
-
XmlNavigator
Custom constructor. Accepting a XML Document as Byte Array for initializing.- Parameters:
xmlBytes- XML Document as Byte Array.- Throws:
ParserConfigurationException- if a DocumentBuilder cannot be created which satisfies the configuration requested.SAXException- - If any parse errors occur.IOException- - If any IO errors occur.
-
XmlNavigator
public XmlNavigator(byte[] xmlBytes, boolean namespaceAware) throws IOException, SAXException, ParserConfigurationException Custom constructor. Accepting a XML Document as Byte Array for initializing.- Parameters:
xmlBytes- XML Document as Byte Array.namespaceAware- True if navigator should be XML Namespace aware.- Throws:
ParserConfigurationException- if a DocumentBuilder cannot be created which satisfies the configuration requested.SAXException- - If any parse errors occur.IOException- - If any IO errors occur.
-
XmlNavigator
public XmlNavigator(InputStream xmlStream) throws IOException, SAXException, ParserConfigurationException Custom constructor. Accepting a XML Document as Stream for initializing.- Parameters:
xmlStream- XML Document as Input Stream.- Throws:
ParserConfigurationException- if a DocumentBuilder cannot be created which satisfies the configuration requested.SAXException- - If any parse errors occur.IOException- - If any IO errors occur.
-
XmlNavigator
public XmlNavigator(InputStream xmlStream, boolean namespaceAware) throws ParserConfigurationException, IOException, SAXException Custom constructor. Accepting a XML Document as Stream for initializing.- Parameters:
xmlStream- XML Document as Input Stream.namespaceAware- True if navigator should be XML Namespace aware.- Throws:
ParserConfigurationException- if a DocumentBuilder cannot be created which satisfies the configuration requested.SAXException- - If any parse errors occur.IOException- - If any IO errors occur.
-
-
Method Details
-
getXmlDocument
Getter for xmlDocument attribute.- Returns:
- the xmlDocument
-
addNamespace
Add XML Namespace if navigator works namespace aware.- Parameters:
prefix- Prefix of Namespace.uri- Namespace URI.
-
readAttribute
Read attribute from XML Document using XPath.- Parameters:
xPath- XPath expression of attribute to read.- Returns:
- Value of attribute as String.
- Throws:
XPathExpressionException- Is being thrown in case an exception occurs.
-
readAttribute
public Object readAttribute(String xPath, Class xJdfType) throws XPathExpressionException, ReflectiveOperationException Read attribute from XML Document using XPath and convert to JDF Datatype.- Parameters:
xPath- XPath expression to evaluate.xJdfType- Target JDF Data Type of attribute.- Returns:
- Value as JDF Data Type.
- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.ReflectiveOperationException- Is thrown in case reflection fails.
-
evaluateDouble
Evaluates an XPath expression on XML Document and returns a Double object as result.- Parameters:
xPath- XPath expression to execute to.- Returns:
- Expression result as double.
- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.
-
evaluateInt
Evaluates an XPath expression on XML Document and returns an Integer value as result.- Parameters:
xPath- XPath expression to execute to.- Returns:
- Expression result as integer.
- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.
-
evaluateLong
Evaluates an XPath expression on XML Document and returns an Long value as result.- Parameters:
xPath- XPath expression to execute to.- Returns:
- Expression result as long.
- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.
-
evaluateString
Evaluates an XPath expression on XML Document and returns a String object as result.- Parameters:
xPath- XPath expression to execute to.- Returns:
- Expression result as string.
- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.
-
evaluateNode
Evaluates an XPath expression on XML Document and returns a Node object as result.- Parameters:
xPath- XPath expression to execute to.- Returns:
- Expression result as node.
- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.
-
evaluateNodeList
Evaluates an XPath expression on XML Document and returns a NodeList object as result.- Parameters:
xPath- XPath expression to execute to.- Returns:
- Expression result as node list.
- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.
-
evaluate
public final <T extends AbstractXJdfType> T evaluate(String xPath, Class<T> xJdfType) throws XPathExpressionException, ReflectiveOperationException Evaluates an XPath expression on XML Document and returns the result as XJDF DataType.- Type Parameters:
T- Type of the desired value object.- Parameters:
xPath- XPath expression to execute to.xJdfType- The desired result data type.- Returns:
- Result as desired data type.
- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.ReflectiveOperationException- Is thrown in case reflection fails.
-
extractNode
protected Object extractNode(String xPath, AbstractXmlParser parser) throws XPathExpressionException, javax.xml.bind.JAXBException Extract an XML Node from XML Document and return parsed result.
!! WORKS ONLY IF NAVIGATOR IS NAMESPACE AWARE !!- Parameters:
xPath- XPath expression in order to locate the node.- Returns:
- Expression result as parsed XML Node object.
- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.javax.xml.bind.JAXBException
-
removeNode
Remove a node from XML Document locaed by the XPath expression.- Parameters:
xPath- Location of the node to be removed.- Throws:
XPathExpressionException
-
replaceNode
protected void replaceNode(String xPath, Object replacement, AbstractXmlParser parser) throws XPathExpressionException, javax.xml.bind.JAXBException, ParserConfigurationException Replace a node in XML Document located by the XPath expression.
!! WORKS ONLY IF NAVIGATOR IS NAMESPACE AWARE !!- Parameters:
xPath- Location of the node to be replaced.replacement- The new node.parser- XML Parser.- Throws:
XPathExpressionExceptionjavax.xml.bind.JAXBExceptionParserConfigurationException
-
evaluateBoolean
Evaluate an XPath expression on XML Document and returns an Boolean value as result.- Parameters:
xPath- XPath expression to execute to.- Returns:
- Expression result as boolean value.
- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.
-
updateAttribute
Update attribute in XML Document.- Parameters:
xPath- XPath expression of attribute to read.value- New value of attribute defined by xPath.- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.
-
updateAttribute
Update attribute in XML Document.- Parameters:
xPath- XPath expression of attribute to read.value- New value of attribute defined by xPath.- Throws:
XPathExpressionException- Is thrown in case an XPath Exception occurs.
-
getXmlStream
Return the XML Document as input stream.- Returns:
- XML document as input stream.
- Throws:
TransformerException- If this document could not be transformed.IOException- If any IO errors occur.
-
getXmlBytes
Return the XML Document as Byte Array.- Returns:
- XML Document as Byte Array.
- Throws:
TransformerException- If this document could not be transformed.IOException- - If any IO errors occur.
-