You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2011/10/17 11:39:33 UTC

svn commit: r1185059 - in /santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security: transforms/implementations/ utils/

Author: coheigea
Date: Mon Oct 17 09:39:33 2011
New Revision: 1185059

URL: http://svn.apache.org/viewvc?rev=1185059&view=rev
Log:
Abstracted XPath stuff into an interface + implementation that uses Xalan

Added:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XPathAPI.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XalanXPathAPI.java
      - copied, changed from r1181705, santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/CachedXPathFuncHereAPI.java
Removed:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/CachedXPathFuncHereAPI.java
Modified:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/FuncHere.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXPath.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXPath2Filter.java

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/FuncHere.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/FuncHere.java?rev=1185059&r1=1185058&r2=1185059&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/FuncHere.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/FuncHere.java Mon Oct 17 09:39:33 2011
@@ -148,7 +148,6 @@ public class FuncHere extends Function {
      * @param globalsSize
      */
     public void fixupVariables(java.util.Vector vars, int globalsSize) {
-
         // do nothing
     }
 }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXPath.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXPath.java?rev=1185059&r1=1185058&r2=1185059&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXPath.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXPath.java Mon Oct 17 09:39:33 2011
@@ -29,9 +29,10 @@ import org.apache.xml.security.transform
 import org.apache.xml.security.transforms.TransformSpi;
 import org.apache.xml.security.transforms.TransformationException;
 import org.apache.xml.security.transforms.Transforms;
-import org.apache.xml.security.utils.CachedXPathFuncHereAPI;
 import org.apache.xml.security.utils.Constants;
 import org.apache.xml.security.utils.XMLUtils;
+import org.apache.xml.security.utils.XPathAPI;
+import org.apache.xml.security.utils.XalanXPathAPI;
 import org.w3c.dom.DOMException;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -118,7 +119,7 @@ public class TransformXPath extends Tran
 
     static class XPathNodeFilter implements NodeFilter {
         
-        CachedXPathFuncHereAPI xPathFuncHereAPI = new CachedXPathFuncHereAPI();
+        XPathAPI xPathAPI = new XalanXPathAPI();
         Node xpathnode; 
         Element xpathElement;
         String str;
@@ -134,8 +135,7 @@ public class TransformXPath extends Tran
          */
         public int isNodeInclude(Node currentNode) {			
             try {
-                boolean include = 
-                    xPathFuncHereAPI.evaluate(currentNode, xpathnode, str, xpathElement);
+                boolean include = xPathAPI.evaluate(currentNode, xpathnode, str, xpathElement);
                 if (include) {
                     return 1;
                 }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXPath2Filter.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXPath2Filter.java?rev=1185059&r1=1185058&r2=1185059&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXPath2Filter.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformXPath2Filter.java Mon Oct 17 09:39:33 2011
@@ -38,8 +38,9 @@ import org.apache.xml.security.transform
 import org.apache.xml.security.transforms.TransformationException;
 import org.apache.xml.security.transforms.Transforms;
 import org.apache.xml.security.transforms.params.XPath2FilterContainer;
-import org.apache.xml.security.utils.CachedXPathFuncHereAPI;
 import org.apache.xml.security.utils.XMLUtils;
+import org.apache.xml.security.utils.XPathAPI;
+import org.apache.xml.security.utils.XalanXPathAPI;
 import org.w3c.dom.DOMException;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -103,7 +104,7 @@ public class TransformXPath2Filter exten
                 inputDoc = XMLUtils.getOwnerDocument(input.getNodeSet());
             }
 
-            CachedXPathFuncHereAPI xPathFuncHereAPI = new CachedXPathFuncHereAPI();
+            XPathAPI xPathAPI = new XalanXPathAPI();
             for (int i = 0; i < xpathElements.length; i++) {
                 Element xpathElement = xpathElements[i];
                 
@@ -111,7 +112,7 @@ public class TransformXPath2Filter exten
                     XPath2FilterContainer.newInstance(xpathElement, input.getSourceURI());
 
                 NodeList subtreeRoots = 
-                    xPathFuncHereAPI.selectNodeList(
+                    xPathAPI.selectNodeList(
                         inputDoc,
                         xpathContainer.getXPathFilterTextNode(),
                         XMLUtils.getStrFromNode(xpathContainer.getXPathFilterTextNode()),

Added: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XPathAPI.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XPathAPI.java?rev=1185059&view=auto
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XPathAPI.java (added)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XPathAPI.java Mon Oct 17 09:39:33 2011
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.xml.security.utils;
+
+import javax.xml.transform.TransformerException;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * An interface to abstract XPath evaluation
+ */
+public interface XPathAPI {
+
+    /**
+     *  Use an XPath string to select a nodelist.
+     *  XPath namespace prefixes are resolved from the namespaceNode.
+     *
+     *  @param contextNode The node to start searching from.
+     *  @param xpathnode
+     *  @param str
+     *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
+     *  @return A NodeIterator, should never be null.
+     *
+     * @throws TransformerException
+     */
+    public NodeList selectNodeList(
+        Node contextNode, Node xpathnode, String str, Node namespaceNode
+    ) throws TransformerException;
+    
+    /**
+     * Evaluate an XPath string and return true if the output is to be included or not.
+     *  @param contextNode The node to start searching from.
+     *  @param xpathnode The XPath node
+     *  @param str The XPath expression
+     *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
+     */
+    public boolean evaluate(Node contextNode, Node xpathnode, String str, Node namespaceNode)
+        throws TransformerException;
+    
+    /**
+     * Clear any context information from this object
+     */
+    public void clear();
+
+}

Copied: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XalanXPathAPI.java (from r1181705, santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/CachedXPathFuncHereAPI.java)
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XalanXPathAPI.java?p2=santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XalanXPathAPI.java&p1=santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/CachedXPathFuncHereAPI.java&r1=1181705&r2=1185059&rev=1185059&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/CachedXPathFuncHereAPI.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XalanXPathAPI.java Mon Oct 17 09:39:33 2011
@@ -26,7 +26,6 @@ import javax.xml.transform.ErrorListener
 import javax.xml.transform.SourceLocator;
 import javax.xml.transform.TransformerException;
 
-import org.apache.xml.dtm.DTMManager;
 import org.apache.xml.security.transforms.implementations.FuncHere;
 import org.apache.xml.utils.PrefixResolver;
 import org.apache.xml.utils.PrefixResolverDefault;
@@ -40,34 +39,25 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 /**
+ * An implementation of XPathAPI using Xalan. This supports the "here()" function defined in the digital
+ * signature spec.
  */
-public class CachedXPathFuncHereAPI {
+public class XalanXPathAPI implements XPathAPI {
 
     private static org.apache.commons.logging.Log log =
-        org.apache.commons.logging.LogFactory.getLog(CachedXPathFuncHereAPI.class);
+        org.apache.commons.logging.LogFactory.getLog(XalanXPathAPI.class);
 
-    DTMManager dtmManager = null;
+    private String xpathStr = null;
 
-    String xpathStr=null;
+    private XPath xpath = null;
 
-    XPath xpath=null;
-
-    static FunctionTable funcTable = null;
+    private static FunctionTable funcTable = null;
 
     static {
         fixupFunctionTable();
     }
 
     /**
-     * Constructor CachedXPathFuncHereAPI
-     */
-    public CachedXPathFuncHereAPI() {
-        XPathContext context = new XPathContext();
-        context.setSecureProcessing(true);
-        this.dtmManager = context.getDTMManager();
-    }
-
-    /**
      *  Use an XPath string to select a nodelist.
      *  XPath namespace prefixes are resolved from the namespaceNode.
      *
@@ -102,20 +92,15 @@ public class CachedXPathFuncHereAPI {
         XObject object = eval(contextNode, xpathnode, str, namespaceNode);
         return object.bool();
     }
-
+    
     /**
-     *  Evaluate an XPath string to an XObject. XPath namespace prefixes are resolved from the 
-     *  namespaceNode.
-     *
-     *  @param contextNode The node to start searching from.
-     *  @param xpathnode The XPath node
-     *  @param str The XPath expression
-     *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
-     *  @return An XObject, which can be used to obtain a string, number, nodelist, etc, 
-     *  should never be null.
-     *
-     * @throws TransformerException
+     * Clear any context information from this object
      */
+    public void clear() {
+        xpathStr = null;
+        xpath = null;
+    }
+
     private XObject eval(Node contextNode, Node xpathnode, String str, Node namespaceNode)
         throws TransformerException {
         XPathContext context = new XPathContext(xpathnode);
@@ -125,24 +110,17 @@ public class CachedXPathFuncHereAPI {
         // XPath namespaces are resolved from the input context node's document element
         // if it is a root node, or else the current context node (for lack of a better
         // resolution space, given the simplicity of this sample code).
-        PrefixResolverDefault prefixResolver =
-            new PrefixResolverDefault((namespaceNode.getNodeType()
-                == Node.DOCUMENT_NODE)
-                ? ((Document) namespaceNode)
-                    .getDocumentElement()
-                    : namespaceNode);
+        Node resolverNode = 
+            (namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
+                ? ((Document) namespaceNode).getDocumentElement() : namespaceNode;
+        PrefixResolverDefault prefixResolver = new PrefixResolverDefault(resolverNode);
 
         if (!str.equals(xpathStr)) {
-            if (str.indexOf("here()")>0) {
-                context.reset();
-                dtmManager=context.getDTMManager();
-            }
             xpath = createXPath(str, prefixResolver);
-            xpathStr=str;
+            xpathStr = str;
         }
 
         // Execute the XPath, and have it return the result
-        // return xpath.execute(xpathSupport, contextNode, prefixResolver);
         int ctxtNode = context.getDTMHandleFromNode(contextNode);
 
         return xpath.execute(context, ctxtNode, prefixResolver);
@@ -150,11 +128,12 @@ public class CachedXPathFuncHereAPI {
 
     private XPath createXPath(String str, PrefixResolver prefixResolver) throws TransformerException {
         XPath xpath = null;
-        Class[] classes = new Class[]{String.class, SourceLocator.class, PrefixResolver.class, int.class,
+        Class<?>[] classes = new Class[]{String.class, SourceLocator.class, PrefixResolver.class, int.class,
                                       ErrorListener.class, FunctionTable.class};
-        Object[] objects = new Object[]{str, null, prefixResolver, Integer.valueOf(XPath.SELECT), null, funcTable};
+        Object[] objects = 
+            new Object[]{str, null, prefixResolver, Integer.valueOf(XPath.SELECT), null, funcTable};
         try {
-            Constructor constructor = XPath.class.getConstructor(classes);
+            Constructor<?> constructor = XPath.class.getConstructor(classes);
             xpath = (XPath) constructor.newInstance(objects);
         } catch (Throwable t) {
             if (log.isDebugEnabled()) {
@@ -176,7 +155,7 @@ public class CachedXPathFuncHereAPI {
          * Try to register our here() implementation as internal function.
          */
         try {
-            Class []args = {String.class, Expression.class};
+            Class<?>[] args = {String.class, Expression.class};
             Method installFunction = FunctionTable.class.getMethod("installFunction", args);
             if ((installFunction.getModifiers() & Modifier.STATIC) != 0) {
                 Object []params = {"here", new FuncHere()};
@@ -186,10 +165,10 @@ public class CachedXPathFuncHereAPI {
         } catch (Throwable t) {
             log.debug("Error installing function using the static installFunction method", t);
         }
-        if(!installed) {
+        if (!installed) {
             try {
                 funcTable = new FunctionTable();
-                Class []args = {String.class, Class.class};
+                Class<?>[] args = {String.class, Class.class};
                 Method installFunction = FunctionTable.class.getMethod("installFunction", args);
                 Object []params = {"here", FuncHere.class};
                 installFunction.invoke(funcTable, params);