Class SimpleXpathEngine

  • All Implemented Interfaces:
    XMLConstants, XpathEngine, XSLTConstants

    public class SimpleXpathEngine
    extends java.lang.Object
    implements XpathEngine, XSLTConstants
    Simple class for accessing the Nodes matched by an Xpath expression, or evaluating the String value of an Xpath expression. Uses a copy-of or value-of XSL template (as appropriate) to execute the Xpath. This is not an efficient method for accessing XPaths but it is portable across underlying transform implementations. (Yes I know Jaxen is too, but this approach seemed to be the simplest thing that could possibly work...)
    • Constructor Detail

      • SimpleXpathEngine

        public SimpleXpathEngine()
    • Method Detail

      • getXSLTBase

        private java.lang.StringBuilder getXSLTBase()
        What every XSL transform needs
      • getCopyTransformation

        private java.lang.String getCopyTransformation​(java.lang.String select)
        Parameters:
        select - an xpath syntax select expression
        Returns:
        the copy-of transformation
      • getValueTransformation

        private java.lang.String getValueTransformation​(java.lang.String select)
        Parameters:
        select - an xpath syntax select expression
        Returns:
        the value-of transformation
      • performTransform

        private void performTransform​(java.lang.String xslt,
                                      org.w3c.dom.Document document,
                                      javax.xml.transform.Result result)
                               throws javax.xml.transform.TransformerException,
                                      ConfigurationException,
                                      XpathException
        Perform the actual transformation work required
        Parameters:
        xslt -
        document -
        result -
        Throws:
        XpathException
        javax.xml.transform.TransformerException
        ConfigurationException
      • getXPathResultNode

        protected org.w3c.dom.Node getXPathResultNode​(java.lang.String select,
                                                      org.w3c.dom.Document document)
                                               throws ConfigurationException,
                                                      javax.xml.transform.TransformerException,
                                                      XpathException
        Testable method to execute the copy-of transform and return the root node of the resulting Document.
        Parameters:
        select - the XPath expression
        document - the XML source to apply the expression to
        Returns:
        the root node of the Document created by the copy-of transform.
        Throws:
        ConfigurationException - if the underlying implementation does
        javax.xml.transform.TransformerException - if the underlying implementation does
        XpathException - if the underlying implementation does
      • getXPathResultAsDocument

        protected org.w3c.dom.Document getXPathResultAsDocument​(java.lang.String select,
                                                                org.w3c.dom.Document document)
                                                         throws ConfigurationException,
                                                                javax.xml.transform.TransformerException,
                                                                XpathException
        Execute the copy-of transform and return the resulting Document. Used for XMLTestCase comparison
        Parameters:
        select - the XPath expression
        document - the XML source to apply the expression to
        Returns:
        the Document created by the copy-of transform.
        Throws:
        ConfigurationException - if the underlying implementation does
        javax.xml.transform.TransformerException - if the underlying implementation does
        XpathException - if the underlying implementation does
      • getMatchingNodes

        public org.w3c.dom.NodeList getMatchingNodes​(java.lang.String select,
                                                     org.w3c.dom.Document document)
                                              throws ConfigurationException,
                                                     XpathException
        Execute the specified xpath syntax select expression on the specified document and return the list of nodes (could have length zero) that match
        Specified by:
        getMatchingNodes in interface XpathEngine
        Parameters:
        select - the XPath expression
        document - the XML source to apply the expression to
        Returns:
        matching nodes
        Throws:
        XpathException - if the underlying implementation does
        ConfigurationException
      • evaluate

        public java.lang.String evaluate​(java.lang.String select,
                                         org.w3c.dom.Document document)
                                  throws ConfigurationException,
                                         XpathException
        Evaluate the result of executing the specified xpath syntax select expression on the specified document
        Specified by:
        evaluate in interface XpathEngine
        Parameters:
        select - the XPath expression
        document - the XML source to apply the expression to
        Returns:
        evaluated result
        Throws:
        XpathException - if the underlying implementation does
        ConfigurationException
      • getNamespaceDeclarations

        private java.lang.String getNamespaceDeclarations()
        returns namespace declarations for all namespaces known to the current context.