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/18 13:20:54 UTC

svn commit: r1185581 - in /santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security: transforms/implementations/TransformXPath.java transforms/implementations/TransformXPath2Filter.java utils/DOMNamespaceContext.java utils/JDKXPathAPI.java

Author: coheigea
Date: Tue Oct 18 11:20:53 2011
New Revision: 1185581

URL: http://svn.apache.org/viewvc?rev=1185581&view=rev
Log:
Added a wrapper for the JDK XPath implementation.
 - XPath transforms now use the JDK XPath implementation by default unless the here() function is used

Added:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/DOMNamespaceContext.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/JDKXPathAPI.java
Modified:
    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/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=1185581&r1=1185580&r2=1185581&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 Tue Oct 18 11:20:53 2011
@@ -30,6 +30,7 @@ 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.utils.Constants;
+import org.apache.xml.security.utils.JDKXPathAPI;
 import org.apache.xml.security.utils.XMLUtils;
 import org.apache.xml.security.utils.XPathAPI;
 import org.apache.xml.security.utils.XalanXPathAPI;
@@ -51,7 +52,7 @@ public class TransformXPath extends Tran
 
     /** Field implementedTransformURI */
     public static final String implementedTransformURI = Transforms.TRANSFORM_XPATH;
-
+    
     /**
      * Method engineGetURI
      *
@@ -100,13 +101,18 @@ public class TransformXPath extends Tran
                     DOMException.HIERARCHY_REQUEST_ERR, "Text must be in ds:Xpath"
                 );
             }
+            
+            XPathAPI xpathAPIInstance = new JDKXPathAPI();
+            if (str.contains("here()")) {
+                xpathAPIInstance = new XalanXPathAPI();
+            }
 
-            input.addNodeFilter(new XPathNodeFilter(xpathElement, xpathnode, str));
+            input.addNodeFilter(new XPathNodeFilter(xpathElement, xpathnode, str, xpathAPIInstance));
             input.setNodeSet(true);
             return input;
         } catch (DOMException ex) {
             throw new TransformationException("empty", ex);
-        } 
+        }
     }
 
     /**
@@ -119,15 +125,16 @@ public class TransformXPath extends Tran
 
     static class XPathNodeFilter implements NodeFilter {
         
-        XPathAPI xPathAPI = new XalanXPathAPI();
+        XPathAPI xPathAPI;
         Node xpathnode; 
         Element xpathElement;
         String str;
         
-        XPathNodeFilter(Element xpathElement, Node xpathnode, String str) {
+        XPathNodeFilter(Element xpathElement, Node xpathnode, String str, XPathAPI xPathAPI) {
             this.xpathnode = xpathnode;
             this.str = str;
             this.xpathElement = xpathElement;
+            this.xPathAPI = xPathAPI;
         }
 
         /**

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=1185581&r1=1185580&r2=1185581&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 Tue Oct 18 11:20:53 2011
@@ -38,6 +38,7 @@ 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.JDKXPathAPI;
 import org.apache.xml.security.utils.XMLUtils;
 import org.apache.xml.security.utils.XPathAPI;
 import org.apache.xml.security.utils.XalanXPathAPI;
@@ -60,7 +61,7 @@ public class TransformXPath2Filter exten
     /** Field implementedTransformURI */
     public static final String implementedTransformURI =
         Transforms.TRANSFORM_XPATH2FILTER;
-
+    
     /**
      * Method engineGetURI
      *
@@ -104,18 +105,24 @@ public class TransformXPath2Filter exten
                 inputDoc = XMLUtils.getOwnerDocument(input.getNodeSet());
             }
 
-            XPathAPI xPathAPI = new XalanXPathAPI();
             for (int i = 0; i < xpathElements.length; i++) {
                 Element xpathElement = xpathElements[i];
                 
                 XPath2FilterContainer xpathContainer =
                     XPath2FilterContainer.newInstance(xpathElement, input.getSourceURI());
 
+                String str = 
+                    XMLUtils.getStrFromNode(xpathContainer.getXPathFilterTextNode());
+                XPathAPI xpathAPIInstance = new JDKXPathAPI();
+                if (str.contains("here()")) {
+                    xpathAPIInstance = new XalanXPathAPI();
+                }
+                
                 NodeList subtreeRoots = 
-                    xPathAPI.selectNodeList(
+                    xpathAPIInstance.selectNodeList(
                         inputDoc,
                         xpathContainer.getXPathFilterTextNode(),
-                        XMLUtils.getStrFromNode(xpathContainer.getXPathFilterTextNode()),
+                        str,
                         xpathContainer.getElement());
                 if (xpathContainer.isIntersect()) {
                     intersectNodes.add(subtreeRoots);
@@ -151,7 +158,7 @@ public class TransformXPath2Filter exten
             throw new TransformationException("empty", ex);
         } catch (ParserConfigurationException ex) {
             throw new TransformationException("empty", ex);
-        } 
+        }
     }
     
     static Set<Node> convertNodeListToSet(List<NodeList> l) {

Added: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/DOMNamespaceContext.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/DOMNamespaceContext.java?rev=1185581&view=auto
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/DOMNamespaceContext.java (added)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/DOMNamespaceContext.java Tue Oct 18 11:20:53 2011
@@ -0,0 +1,75 @@
+/**
+ * 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 java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.xml.namespace.NamespaceContext;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+/**
+ */
+public class DOMNamespaceContext implements NamespaceContext {
+    
+    private Map<String, String> namespaceMap = new HashMap<String, String>();
+    
+    public DOMNamespaceContext(Node contextNode) {
+        addNamespaces(contextNode);
+    }
+    
+    public String getNamespaceURI(String arg0) {
+        return namespaceMap.get(arg0);
+    }
+
+    public String getPrefix(String arg0) {
+        for (String key : namespaceMap.keySet()) {
+            String value = namespaceMap.get(key);
+            if (value.equals(arg0)) {
+                return key;
+            }
+        }
+        return null;
+    }
+
+    public Iterator<String> getPrefixes(String arg0) {
+        return namespaceMap.keySet().iterator();
+    }
+    
+    private void addNamespaces(Node element) {
+        if (element.getParentNode() != null) {
+            addNamespaces(element.getParentNode());
+        }
+        if (element instanceof Element) {
+            Element el = (Element)element;
+            NamedNodeMap map = el.getAttributes();
+            for (int x = 0; x < map.getLength(); x++) {
+                Attr attr = (Attr)map.item(x);
+                if ("xmlns".equals(attr.getPrefix())) {
+                    namespaceMap.put(attr.getLocalName(), attr.getValue());
+                }
+            }
+        }
+    }
+}

Added: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/JDKXPathAPI.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/JDKXPathAPI.java?rev=1185581&view=auto
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/JDKXPathAPI.java (added)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/JDKXPathAPI.java Tue Oct 18 11:20:53 2011
@@ -0,0 +1,86 @@
+/**
+ * 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 javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * An implementation for XPath evaluation that uses the JDK API.
+ */
+public class JDKXPathAPI implements 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 {
+        XPathFactory xpf = XPathFactory.newInstance();
+        XPath xpath = xpf.newXPath();
+        xpath.setNamespaceContext(new DOMNamespaceContext(namespaceNode));
+        try {
+            return (NodeList) xpath.evaluate(str, contextNode, XPathConstants.NODESET);
+        } catch (XPathExpressionException ex) {
+            throw new TransformerException("empty", ex);
+        }
+    }
+    
+    /**
+     * 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 {
+        XPathFactory xpf = XPathFactory.newInstance();
+        XPath xpath = xpf.newXPath();
+        xpath.setNamespaceContext(new DOMNamespaceContext(namespaceNode));
+        try {
+            return ((Boolean) xpath.evaluate(str, contextNode, XPathConstants.BOOLEAN)).booleanValue();
+        } catch (XPathExpressionException ex) {
+            throw new TransformerException("empty", ex);
+        }
+    }
+    
+    /**
+     * Clear any context information from this object
+     */
+    public void clear() {
+        
+    }
+
+}