Class XmlNavigator

  • Direct Known Subclasses:
    XJdfNavigator

    public class XmlNavigator
    extends java.lang.Object
    Navigator class which simplify XPath handling using XML Documents.
    • Constructor Summary

      Constructors 
      Constructor Description
      XmlNavigator​(byte[] xmlBytes)
      Custom constructor.
      XmlNavigator​(byte[] xmlBytes, boolean namespaceAware)
      Custom constructor.
      XmlNavigator​(java.io.InputStream xmlStream)
      Custom constructor.
      XmlNavigator​(java.io.InputStream xmlStream, boolean namespaceAware)
      Custom constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addNamespace​(java.lang.String prefix, java.lang.String uri)
      Add XML Namespace if navigator works namespace aware.
      <T extends AbstractXJdfType>
      T
      evaluate​(java.lang.String xPath, java.lang.Class<T> xJdfType)
      Evaluates an XPath expression on XML Document and returns the result as XJDF DataType.
      boolean evaluateBoolean​(java.lang.String xPath)
      Evaluate an XPath expression on XML Document and returns an Boolean value as result.
      double evaluateDouble​(java.lang.String xPath)
      Evaluates an XPath expression on XML Document and returns a Double object as result.
      java.lang.Integer evaluateInt​(java.lang.String xPath)
      Evaluates an XPath expression on XML Document and returns an Integer value as result.
      java.lang.Long evaluateLong​(java.lang.String xPath)
      Evaluates an XPath expression on XML Document and returns an Long value as result.
      org.w3c.dom.Node evaluateNode​(java.lang.String xPath)
      Evaluates an XPath expression on XML Document and returns a Node object as result.
      org.w3c.dom.NodeList evaluateNodeList​(java.lang.String xPath)
      Evaluates an XPath expression on XML Document and returns a NodeList object as result.
      java.lang.String evaluateString​(java.lang.String xPath)
      Evaluates an XPath expression on XML Document and returns a String object as result.
      protected java.lang.Object extractNode​(java.lang.String xPath, AbstractXmlParser parser)
      Extract an XML Node from XML Document and return parsed result.
      byte[] getXmlBytes()
      Return the XML Document as Byte Array.
      org.w3c.dom.Document getXmlDocument()
      Getter for xmlDocument attribute.
      java.io.InputStream getXmlStream()
      Return the XML Document as input stream.
      java.lang.String readAttribute​(java.lang.String xPath)
      Read attribute from XML Document using XPath.
      java.lang.Object readAttribute​(java.lang.String xPath, java.lang.Class xJdfType)
      Read attribute from XML Document using XPath and convert to JDF Datatype.
      void removeNode​(java.lang.String xPath)
      Remove a node from XML Document locaed by the XPath expression.
      protected void replaceNode​(java.lang.String xPath, java.lang.Object replacement, AbstractXmlParser parser)
      Replace a node in XML Document located by the XPath expression.
      void updateAttribute​(java.lang.String xPath, java.lang.String value)
      Update attribute in XML Document.
      void updateAttribute​(java.lang.String xPath, AbstractXJdfType value)
      Update attribute in XML Document.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • xPath

        protected final javax.xml.xpath.XPath xPath
      • xmlDocument

        protected final org.w3c.dom.Document xmlDocument
    • Constructor Detail

      • XmlNavigator

        public XmlNavigator​(byte[] xmlBytes)
                     throws javax.xml.parsers.ParserConfigurationException,
                            org.xml.sax.SAXException,
                            java.io.IOException
        Custom constructor. Accepting a XML Document as Byte Array for initializing.
        Parameters:
        xmlBytes - XML Document as Byte Array.
        Throws:
        javax.xml.parsers.ParserConfigurationException - if a DocumentBuilder cannot be created which satisfies the configuration requested.
        org.xml.sax.SAXException - - If any parse errors occur.
        java.io.IOException - - If any IO errors occur.
      • XmlNavigator

        public XmlNavigator​(byte[] xmlBytes,
                            boolean namespaceAware)
                     throws java.io.IOException,
                            org.xml.sax.SAXException,
                            javax.xml.parsers.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:
        javax.xml.parsers.ParserConfigurationException - if a DocumentBuilder cannot be created which satisfies the configuration requested.
        org.xml.sax.SAXException - - If any parse errors occur.
        java.io.IOException - - If any IO errors occur.
      • XmlNavigator

        public XmlNavigator​(java.io.InputStream xmlStream)
                     throws java.io.IOException,
                            org.xml.sax.SAXException,
                            javax.xml.parsers.ParserConfigurationException
        Custom constructor. Accepting a XML Document as Stream for initializing.
        Parameters:
        xmlStream - XML Document as Input Stream.
        Throws:
        javax.xml.parsers.ParserConfigurationException - if a DocumentBuilder cannot be created which satisfies the configuration requested.
        org.xml.sax.SAXException - - If any parse errors occur.
        java.io.IOException - - If any IO errors occur.
      • XmlNavigator

        public XmlNavigator​(java.io.InputStream xmlStream,
                            boolean namespaceAware)
                     throws javax.xml.parsers.ParserConfigurationException,
                            java.io.IOException,
                            org.xml.sax.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:
        javax.xml.parsers.ParserConfigurationException - if a DocumentBuilder cannot be created which satisfies the configuration requested.
        org.xml.sax.SAXException - - If any parse errors occur.
        java.io.IOException - - If any IO errors occur.
    • Method Detail

      • getXmlDocument

        public org.w3c.dom.Document getXmlDocument()
        Getter for xmlDocument attribute.
        Returns:
        the xmlDocument
      • addNamespace

        public void addNamespace​(java.lang.String prefix,
                                 java.lang.String uri)
        Add XML Namespace if navigator works namespace aware.
        Parameters:
        prefix - Prefix of Namespace.
        uri - Namespace URI.
      • readAttribute

        public java.lang.String readAttribute​(java.lang.String xPath)
                                       throws javax.xml.xpath.XPathExpressionException
        Read attribute from XML Document using XPath.
        Parameters:
        xPath - XPath expression of attribute to read.
        Returns:
        Value of attribute as String.
        Throws:
        javax.xml.xpath.XPathExpressionException - Is being thrown in case an exception occurs.
      • readAttribute

        public java.lang.Object readAttribute​(java.lang.String xPath,
                                              java.lang.Class xJdfType)
                                       throws javax.xml.xpath.XPathExpressionException,
                                              java.lang.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:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
        java.lang.ReflectiveOperationException - Is thrown in case reflection fails.
      • evaluateDouble

        public double evaluateDouble​(java.lang.String xPath)
                              throws javax.xml.xpath.XPathExpressionException
        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:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
      • evaluateInt

        public java.lang.Integer evaluateInt​(java.lang.String xPath)
                                      throws javax.xml.xpath.XPathExpressionException
        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:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
      • evaluateLong

        public java.lang.Long evaluateLong​(java.lang.String xPath)
                                    throws javax.xml.xpath.XPathExpressionException
        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:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
      • evaluateString

        public java.lang.String evaluateString​(java.lang.String xPath)
                                        throws javax.xml.xpath.XPathExpressionException
        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:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
      • evaluateNode

        public org.w3c.dom.Node evaluateNode​(java.lang.String xPath)
                                      throws javax.xml.xpath.XPathExpressionException
        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:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
      • evaluateNodeList

        public org.w3c.dom.NodeList evaluateNodeList​(java.lang.String xPath)
                                              throws javax.xml.xpath.XPathExpressionException
        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:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
      • evaluate

        public final <T extends AbstractXJdfType> T evaluate​(java.lang.String xPath,
                                                             java.lang.Class<T> xJdfType)
                                                      throws javax.xml.xpath.XPathExpressionException,
                                                             java.lang.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:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
        java.lang.ReflectiveOperationException - Is thrown in case reflection fails.
      • extractNode

        protected java.lang.Object extractNode​(java.lang.String xPath,
                                               AbstractXmlParser parser)
                                        throws javax.xml.xpath.XPathExpressionException,
                                               jakarta.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:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
        jakarta.xml.bind.JAXBException
      • removeNode

        public void removeNode​(java.lang.String xPath)
                        throws javax.xml.xpath.XPathExpressionException
        Remove a node from XML Document locaed by the XPath expression.
        Parameters:
        xPath - Location of the node to be removed.
        Throws:
        javax.xml.xpath.XPathExpressionException
      • replaceNode

        protected void replaceNode​(java.lang.String xPath,
                                   java.lang.Object replacement,
                                   AbstractXmlParser parser)
                            throws javax.xml.xpath.XPathExpressionException,
                                   jakarta.xml.bind.JAXBException,
                                   javax.xml.parsers.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:
        javax.xml.xpath.XPathExpressionException
        jakarta.xml.bind.JAXBException
        javax.xml.parsers.ParserConfigurationException
      • evaluateBoolean

        public boolean evaluateBoolean​(java.lang.String xPath)
                                throws javax.xml.xpath.XPathExpressionException
        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:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
      • updateAttribute

        public void updateAttribute​(java.lang.String xPath,
                                    AbstractXJdfType value)
                             throws javax.xml.xpath.XPathExpressionException
        Update attribute in XML Document.
        Parameters:
        xPath - XPath expression of attribute to read.
        value - New value of attribute defined by xPath.
        Throws:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
      • updateAttribute

        public void updateAttribute​(java.lang.String xPath,
                                    java.lang.String value)
                             throws javax.xml.xpath.XPathExpressionException
        Update attribute in XML Document.
        Parameters:
        xPath - XPath expression of attribute to read.
        value - New value of attribute defined by xPath.
        Throws:
        javax.xml.xpath.XPathExpressionException - Is thrown in case an XPath Exception occurs.
      • getXmlStream

        public java.io.InputStream getXmlStream()
                                         throws javax.xml.transform.TransformerException,
                                                java.io.IOException
        Return the XML Document as input stream.
        Returns:
        XML document as input stream.
        Throws:
        javax.xml.transform.TransformerException - If this document could not be transformed.
        java.io.IOException - If any IO errors occur.
      • getXmlBytes

        public byte[] getXmlBytes()
                           throws javax.xml.transform.TransformerException,
                                  java.io.IOException
        Return the XML Document as Byte Array.
        Returns:
        XML Document as Byte Array.
        Throws:
        javax.xml.transform.TransformerException - If this document could not be transformed.
        java.io.IOException - - If any IO errors occur.