Class XPathUtils


  • @Service(XPathUtils.class)
    public class XPathUtils
    extends java.lang.Object
    Utility class which allows retrieval of values from XML documents using XPath queries, as well as updation of values inside an XML document.
    • Constructor Summary

      Constructors 
      Constructor Description
      XPathUtils()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object query​(Document xmlDoc, java.lang.String xpathQuery, javax.xml.namespace.QName type)
      Utility query method for a single XPath query.
      java.util.Map<java.lang.String,​java.lang.Object> query​(Document xmlDoc, java.util.Map<java.lang.String,​javax.xml.namespace.QName> queries)
      Queries the given XML document using the supplied XPath queries, and returns the results.
      java.lang.Object query​(java.lang.String xmlJcrPath, ResourceResolver rr, java.lang.String xpathQuery, javax.xml.namespace.QName type)
      Utility query method for executing a single XPath query against an XML document stored in the repository.
      java.util.Map<java.lang.String,​java.lang.Object> query​(java.lang.String xmlJcrPath, ResourceResolver rr, java.util.Map<java.lang.String,​javax.xml.namespace.QName> queries)
      Utility query method for the scenario where the XML document is a node in the repository.
      Document update​(Document xmlDoc, java.lang.String xpathQuery, java.lang.String value)
      Utility update method for the use-case where only one value needs to be updated.
      Document update​(Document xmlDoc, java.util.Map<java.lang.String,​java.lang.String> values)
      Updates the nodes or attributes at the given paths inside the given XML document with the supplied values, and returns a new XML document containing the modified values.
      void update​(java.lang.String xmlJcrPath, ResourceResolver rr, java.lang.String xpathQuery, java.lang.String value, boolean save)
      Utility method for the use-case where a single value needs to be updated in an XML document stored in the repository.
      void update​(java.lang.String xmlJcrPath, ResourceResolver rr, java.util.Map<java.lang.String,​java.lang.String> values, boolean save)
      Utility update method for the use-case where the XML to be updated is stored as a node in the repository.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • XPathUtils

        public XPathUtils()
    • Method Detail

      • query

        public java.util.Map<java.lang.String,​java.lang.Object> query​(Document xmlDoc,
                                                                            java.util.Map<java.lang.String,​javax.xml.namespace.QName> queries)
                                                                     throws javax.xml.parsers.ParserConfigurationException,
                                                                            java.io.IOException,
                                                                            org.xml.sax.SAXException,
                                                                            javax.xml.xpath.XPathException
        Queries the given XML document using the supplied XPath queries, and returns the results.
        Parameters:
        xmlDoc - An XML document
        queries - A map of XPath queries to the expected return types for the queries. The return types should be drawn from the types defined in javax.xml.xpath.XPathConstants. The return type can also be null, in which case the string representation of the element(s) at the specified XPath expression will be returned.
        Returns:
        A map of XPath queries to the values retrieved by executing the queries against the given XML.
        Throws:
        javax.xml.parsers.ParserConfigurationException - If an XML parser configuration error is detected.
        java.io.IOException - If an error occurs while reading the given XML document.
        org.xml.sax.SAXException - If an error occurs while parsing the given XML document.
        javax.xml.xpath.XPathException - If an error occurs while executing the given XPath queries.
      • query

        public java.lang.Object query​(Document xmlDoc,
                                      java.lang.String xpathQuery,
                                      javax.xml.namespace.QName type)
                               throws javax.xml.parsers.ParserConfigurationException,
                                      java.io.IOException,
                                      org.xml.sax.SAXException,
                                      javax.xml.xpath.XPathException
        Utility query method for a single XPath query.
        Parameters:
        xmlDoc - An XML document.
        xpathQuery - An XPath query to be executed on the given XML.
        type - The expected return type for the value retrieved by executing the given query.
        Returns:
        The result of executing the given query on the given XML.
        Throws:
        javax.xml.parsers.ParserConfigurationException - If an XML parser configuration error is detected.
        java.io.IOException - If an error occurs while reading the given XML document.
        org.xml.sax.SAXException - If an error occurs while parsing the given XML document.
        javax.xml.xpath.XPathException - If an error occurs while executing the given XPath queries.
        See Also:
        query(com.adobe.aemfd.docmanager.Document, Map)
      • query

        public java.util.Map<java.lang.String,​java.lang.Object> query​(java.lang.String xmlJcrPath,
                                                                            ResourceResolver rr,
                                                                            java.util.Map<java.lang.String,​javax.xml.namespace.QName> queries)
                                                                     throws javax.xml.parsers.ParserConfigurationException,
                                                                            java.io.IOException,
                                                                            org.xml.sax.SAXException,
                                                                            javax.xml.xpath.XPathException
        Utility query method for the scenario where the XML document is a node in the repository.
        Parameters:
        xmlJcrPath - The repository path of an XML document.
        rr - The resource-resolver to be used for reading the given XML path. This resolver must have read access for the given path.
        queries - A map of XPath queries to expected return types.
        Returns:
        A map of XPath queries to the values retrieved by executing the queries against the given XML.
        Throws:
        javax.xml.parsers.ParserConfigurationException - If an XML parser configuration error is detected.
        java.io.IOException - If an error occurs while reading the given XML document.
        org.xml.sax.SAXException - If an error occurs while parsing the given XML document.
        javax.xml.xpath.XPathException - If an error occurs while executing the given XPath queries.
        See Also:
        query(com.adobe.aemfd.docmanager.Document, Map)
      • query

        public java.lang.Object query​(java.lang.String xmlJcrPath,
                                      ResourceResolver rr,
                                      java.lang.String xpathQuery,
                                      javax.xml.namespace.QName type)
                               throws javax.xml.parsers.ParserConfigurationException,
                                      java.io.IOException,
                                      org.xml.sax.SAXException,
                                      javax.xml.xpath.XPathException
        Utility query method for executing a single XPath query against an XML document stored in the repository.
        Parameters:
        xmlJcrPath - The repository path of an XML document.
        rr - The resource-resolver to be used for reading the given XML path.
        xpathQuery - An XPath query to be executed on the given XML.
        type - The expected return type for the value retrieved by executing the given query.
        Returns:
        The result of executing the given query on the given XML.
        Throws:
        javax.xml.parsers.ParserConfigurationException - If an XML parser configuration error is detected.
        java.io.IOException - If an error occurs while reading the given XML document.
        org.xml.sax.SAXException - If an error occurs while parsing the given XML document.
        javax.xml.xpath.XPathException - If an error occurs while executing the given XPath queries.
        See Also:
        query(String, org.apache.sling.api.resource.ResourceResolver, Map)
      • update

        public Document update​(Document xmlDoc,
                               java.util.Map<java.lang.String,​java.lang.String> values)
                        throws javax.xml.parsers.ParserConfigurationException,
                               java.io.IOException,
                               org.xml.sax.SAXException,
                               javax.xml.xpath.XPathException,
                               javax.xml.transform.TransformerException
        Updates the nodes or attributes at the given paths inside the given XML document with the supplied values, and returns a new XML document containing the modified values.
        Parameters:
        xmlDoc - An XML document.
        values - A map of XML node/attribute paths to the new values to be set at these paths. The paths are represented using XPath expressions, and each path must resolve to a single existing node inside the given XML (or a single attribute of such a node). If a node is being updated, then it is typically expected to be a leaf node with some text content and no child nodes.
        Returns:
        A new XML document representing the given XML updated with the given values.
        Throws:
        javax.xml.parsers.ParserConfigurationException - If an XML parser configuration error is detected.
        java.io.IOException - If an error occurs while reading the given XML document.
        org.xml.sax.SAXException - If an error occurs while parsing the given XML document.
        javax.xml.xpath.XPathException - If an error occurs while executing the given XPath expressions.
        javax.xml.transform.TransformerException - If an unexpected error occurs while constructing the updated XML document.
      • update

        public Document update​(Document xmlDoc,
                               java.lang.String xpathQuery,
                               java.lang.String value)
                        throws javax.xml.parsers.ParserConfigurationException,
                               java.io.IOException,
                               org.xml.sax.SAXException,
                               javax.xml.xpath.XPathException,
                               javax.xml.transform.TransformerException
        Utility update method for the use-case where only one value needs to be updated.
        Parameters:
        xmlDoc - An XML document.
        xpathQuery - The node/attribute path to be updated.
        value - The new value to be set at the given path.
        Returns:
        A new XML document representing the given XML updated with the given value.
        Throws:
        javax.xml.parsers.ParserConfigurationException - If an XML parser configuration error is detected.
        java.io.IOException - If an error occurs while reading the given XML document.
        org.xml.sax.SAXException - If an error occurs while parsing the given XML document.
        javax.xml.xpath.XPathException - If an error occurs while executing the given XPath expressions.
        javax.xml.transform.TransformerException - If an unexpected error occurs while constructing the updated XML document.
        See Also:
        update(com.adobe.aemfd.docmanager.Document, Map)
      • update

        public void update​(java.lang.String xmlJcrPath,
                           ResourceResolver rr,
                           java.util.Map<java.lang.String,​java.lang.String> values,
                           boolean save)
                    throws javax.xml.parsers.ParserConfigurationException,
                           java.io.IOException,
                           org.xml.sax.SAXException,
                           javax.xml.xpath.XPathException,
                           javax.xml.transform.TransformerException,
                           javax.jcr.RepositoryException
        Utility update method for the use-case where the XML to be updated is stored as a node in the repository. This method updates the XML in place.
        Parameters:
        xmlJcrPath - The repository path of an XML document to be updated.
        rr - The resource-resolver to be used for reading and updating the document. This resolver must have both read and write access to the given path.
        values - A map of paths to the new values to be set at these paths.
        save - Whether the updates to the XML should be committed immediately or not. This should be set to false for scenarios where the resource-resolver is a managed one (e.g. one adapted from the workflow session provided by the workflow engine).
        Throws:
        javax.xml.parsers.ParserConfigurationException - If an XML parser configuration error is detected.
        java.io.IOException - If an error occurs while reading the given XML document.
        org.xml.sax.SAXException - If an error occurs while parsing the given XML document.
        javax.xml.xpath.XPathException - If an error occurs while executing the given XPath expressions.
        javax.xml.transform.TransformerException - If an unexpected error occurs while constructing the updated XML document.
        javax.jcr.RepositoryException - If a repository error occurs while reading or updating the given XML.
        See Also:
        update(com.adobe.aemfd.docmanager.Document, Map)
      • update

        public void update​(java.lang.String xmlJcrPath,
                           ResourceResolver rr,
                           java.lang.String xpathQuery,
                           java.lang.String value,
                           boolean save)
                    throws javax.xml.parsers.ParserConfigurationException,
                           java.io.IOException,
                           org.xml.sax.SAXException,
                           javax.xml.xpath.XPathException,
                           javax.xml.transform.TransformerException,
                           javax.jcr.RepositoryException
        Utility method for the use-case where a single value needs to be updated in an XML document stored in the repository.
        Parameters:
        xmlJcrPath - The repository path of an XML document.
        rr - The resource resolver to be used for reading and updating the XML.
        xpathQuery - The node/attribute path to be updated.
        value - The new value to be set at the given path.
        save - Whether the updates should be committed immediately.
        Throws:
        javax.xml.parsers.ParserConfigurationException - If an XML parser configuration error is detected.
        java.io.IOException - If an error occurs while reading the given XML document.
        org.xml.sax.SAXException - If an error occurs while parsing the given XML document.
        javax.xml.xpath.XPathException - If an error occurs while executing the given XPath expression.
        javax.xml.transform.TransformerException - If an unexpected error occurs while constructing the updated XML document.
        javax.jcr.RepositoryException - If a repository error occurs while reading or updating the given XML.
        See Also:
        update(String, org.apache.sling.api.resource.ResourceResolver, Map, boolean)