commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmi...@apache.org
Subject svn commit: r329468 - in /jakarta/commons/proper/jxpath/trunk/src: java/org/apache/commons/jxpath/ java/org/apache/commons/jxpath/ri/ java/org/apache/commons/jxpath/ri/axes/ java/org/apache/commons/jxpath/ri/model/dom/ java/org/apache/commons/jxpath/ri...
Date Sat, 29 Oct 2005 19:02:54 GMT
Author: dmitri
Date: Sat Oct 29 12:02:38 2005
New Revision: 329468

URL: http://svn.apache.org/viewcvs?rev=329468&view=rev
Log:
Fixes for bug 32360. Please follow discussions at:
http://issues.apache.org/bugzilla/show_bug.cgi?id=32360

Modified:
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/JXPathContext.java
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/NamespaceResolver.java
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/axes/RootContext.java
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodeIterator.java
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodeIterator.java
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java
    jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLModelTestCase.java

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/JXPathContext.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/JXPathContext.java?rev=329468&r1=329467&r2=329468&view=diff
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/JXPathContext.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/JXPathContext.java
Sat Oct 29 12:02:38 2005
@@ -860,4 +860,34 @@
         throw new UnsupportedOperationException(
                 "Namespace registration is not implemented by " + getClass());
     }
+
+    /**
+     * If true, default namespace declarations in XML are ignored. Thus, if you
+     * have this XML: 
+     * <pre>
+     *   &lt;a xmlns="myns"&gt; 
+     *     &lt;b&gt;109&lt;/b&gt;
+     *   &lt;/a&gt; 
+     * </pos>
+     * you can use the path "/a/b" to retrieve the contents of the nested element.
+     * <p>
+     * If false (default), you have to do the default thing, which is this:
+     * <pre>
+     *   context.registerNamespace("ns", "myns");
+     *   context.getValue("/ns:a/ns:b");
+     * </pre>
+     */
+    public boolean isDefaultNamespaceIgnored() {
+        return false;        
+    }
+    
+    
+    /**
+     * Changing this flag to true makes jxpath forgive missing default namespace specification.
+     * @see #isDefaultNamespaceIgnored(boolean)
+     */
+    public void setDefaultNamespaceIgnored(boolean flag) {
+        throw new UnsupportedOperationException(
+                "Namespace registration is not implemented by " + getClass());        
+    }
 }

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java?rev=329468&r1=329467&r2=329468&view=diff
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
Sat Oct 29 12:02:38 2005
@@ -108,6 +108,7 @@
     private Pointer contextPointer;
     
     protected NamespaceResolver namespaceResolver;
+    private boolean defaultNamespaceIgnored;
 
     // The frequency of the cache cleanup
     private static final int CLEANUP_THRESHOLD = 500;
@@ -674,9 +675,25 @@
 
     public NamespaceResolver getNamespaceResolver() {
         namespaceResolver.seal();
+        namespaceResolver.setDefaultNamespaceIgnored(isDefaultNamespaceIgnored());
         return namespaceResolver;
     }
     
+    public boolean isDefaultNamespaceIgnored() {
+        if (defaultNamespaceIgnored) {
+            return true;
+        }
+        if (parentContext != null) {
+            return parentContext.isDefaultNamespaceIgnored();
+        }
+        
+        return false;
+    }
+
+    public void setDefaultNamespaceIgnored(boolean flag) {
+        defaultNamespaceIgnored = flag;
+    }
+
     /**
      * Checks if existenceCheckClass exists on the class path. If so, allocates
      * an instance of the specified class, otherwise returns null.

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/NamespaceResolver.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/NamespaceResolver.java?rev=329468&r1=329467&r2=329468&view=diff
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/NamespaceResolver.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/NamespaceResolver.java
Sat Oct 29 12:02:38 2005
@@ -36,6 +36,7 @@
     protected HashMap reverseMap;
     protected NodePointer pointer;
     private boolean sealed;
+    private boolean defaultNamespaceIgnored;
         
     public NamespaceResolver(NamespaceResolver parent) {
         this.parent = parent;
@@ -95,8 +96,8 @@
             if (ni != null) {
                 for (int position = 1; ni.setPosition(position); position++) {
                     NodePointer nsPointer = ni.getNodePointer();
-                    QName qname = nsPointer.getName();
-                    reverseMap.put(qname.getPrefix(), qname.getName());
+                    reverseMap.put(nsPointer.getNamespaceURI(), 
+                            nsPointer.getName().getName());
                 }
             }
             Iterator it = namespaceMap.entrySet().iterator();
@@ -111,6 +112,14 @@
         }
         return prefix;
     }
+        
+    public boolean isDefaultNamespaceIgnored() {
+        return defaultNamespaceIgnored;
+    }
+
+    public void setDefaultNamespaceIgnored(boolean flag) {
+        this.defaultNamespaceIgnored = flag;
+    }
     
     public boolean isSealed() {
         return sealed;
@@ -133,4 +142,5 @@
             return null;
         }
     }
+
 }

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/axes/RootContext.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/axes/RootContext.java?rev=329468&r1=329467&r2=329468&view=diff
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/axes/RootContext.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/axes/RootContext.java
Sat Oct 29 12:02:38 2005
@@ -20,7 +20,6 @@
 import org.apache.commons.jxpath.NodeSet;
 import org.apache.commons.jxpath.ri.EvalContext;
 import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
-import org.apache.commons.jxpath.ri.NamespaceResolver;
 import org.apache.commons.jxpath.ri.QName;
 import org.apache.commons.jxpath.ri.model.NodePointer;
 
@@ -35,7 +34,6 @@
     private NodePointer pointer;
     private Object registers[];
     private int availableRegister = 0;
-    private NamespaceResolver namespaceResolver;
     public static final Object UNKNOWN_VALUE = new Object();
     private static final int MAX_REGISTER = 4;
 

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodeIterator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodeIterator.java?rev=329468&r1=329467&r2=329468&view=diff
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodeIterator.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodeIterator.java
Sat Oct 29 12:02:38 2005
@@ -140,6 +140,8 @@
     }
 
     private boolean testChild() {
-        return DOMNodePointer.testNode(child, nodeTest);
+        boolean ignoreNS = 
+            parent.getNamespaceResolver().isDefaultNamespaceIgnored();
+        return DOMNodePointer.testNode(child, nodeTest, ignoreNS);
     }
 }

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java?rev=329468&r1=329467&r2=329468&view=diff
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java
Sat Oct 29 12:02:38 2005
@@ -29,9 +29,9 @@
 import org.apache.commons.jxpath.ri.compiler.NodeTest;
 import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
 import org.apache.commons.jxpath.ri.compiler.ProcessingInstructionTest;
-import org.apache.commons.jxpath.ri.model.beans.NullPointer;
 import org.apache.commons.jxpath.ri.model.NodeIterator;
 import org.apache.commons.jxpath.ri.model.NodePointer;
+import org.apache.commons.jxpath.ri.model.beans.NullPointer;
 import org.apache.commons.jxpath.util.TypeUtils;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Comment;
@@ -49,6 +49,9 @@
  * @version $Revision$ $Date$
  */
 public class DOMNodePointer extends NodePointer {
+
+    private static final long serialVersionUID = -8751046933894857319L;
+    
     private Node node;
     private Map namespaces;
     private String defaultNamespace;
@@ -76,10 +79,17 @@
     }
     
     public boolean testNode(NodeTest test) {
-        return testNode(node, test);
+        return testNode(node, test, 
+                getNamespaceResolver().isDefaultNamespaceIgnored());
     }
 
     public static boolean testNode(Node node, NodeTest test) {
+        return testNode(node, test, false);
+    }
+
+    public static boolean testNode(Node node, NodeTest test,
+            boolean ignoreDefaultNamespace)
+    {
         if (test == null) {
             return true;
         }
@@ -100,6 +110,10 @@
             if (wildcard
                 || testName.getName()
                         .equals(DOMNodePointer.getLocalName(node))) {
+                if (ignoreDefaultNamespace && testPrefix == null
+                        && node.getPrefix() == null) {
+                    return true;
+                }
                 String nodeNS = DOMNodePointer.getNamespaceURI(node);
                 return equalStrings(namespaceURI, nodeNS);
             }
@@ -231,25 +245,6 @@
         }
         
         // TBD: We are supposed to resolve relative URIs to absolute ones.
-        return namespace;
-    }
-
-    private String getNamespaceURI(String prefix, String namespace) {
-        String qname = "xmlns:" + prefix;
-        Node aNode = node;
-        if (aNode instanceof Document) {
-            aNode = ((Document)aNode).getDocumentElement();
-        }
-        while (aNode != null) {
-            if (aNode.getNodeType() == Node.ELEMENT_NODE) {
-                Attr attr = ((Element) aNode).getAttributeNode(qname);
-                if (attr != null) {
-                    namespace = attr.getValue();
-                    break;
-                }
-            }
-            aNode = aNode.getParentNode();
-        }
         return namespace;
     }
 

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodeIterator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodeIterator.java?rev=329468&r1=329467&r2=329468&view=diff
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodeIterator.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodeIterator.java
Sat Oct 29 12:02:38 2005
@@ -160,6 +160,8 @@
     }
 
     private boolean testChild() {
-        return JDOMNodePointer.testNode(parent, child, nodeTest);
+        boolean ignoreNS = 
+            parent.getNamespaceResolver().isDefaultNamespaceIgnored();
+        return JDOMNodePointer.testNode(parent, child, nodeTest, ignoreNS);
     }
 }

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java?rev=329468&r1=329467&r2=329468&view=diff
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java
Sat Oct 29 12:02:38 2005
@@ -17,7 +17,6 @@
 
 import java.util.List;
 import java.util.Locale;
-import java.util.Map;
 
 import org.apache.commons.jxpath.AbstractFactory;
 import org.apache.commons.jxpath.JXPathContext;
@@ -47,9 +46,9 @@
  * @version $Revision$ $Date$
  */
 public class JDOMNodePointer extends NodePointer {
+    private static final long serialVersionUID = -6346532297491082651L;
+    
     private Object node;
-    private Map namespaces;
-    private String defaultNamespace;
     private String id;
 
     public static final String XML_NAMESPACE_URI =
@@ -336,14 +335,21 @@
     }
     
     public boolean testNode(NodeTest test) {
-        return testNode(this, node, test);
+        return testNode(this, node, test, 
+                getNamespaceResolver().isDefaultNamespaceIgnored());
     }
-
+    
     public static boolean testNode(
         NodePointer pointer,
         Object node,
         NodeTest test) 
     {
+        return testNode(pointer, node, test, false);
+    }
+    
+    public static boolean testNode(NodePointer pointer, Object node,
+            NodeTest test, boolean ignoreDefaultNamespace)
+    {
         if (test == null) {
             return true;
         }
@@ -364,6 +370,10 @@
             if (wildcard
                 || testName.getName()
                         .equals(JDOMNodePointer.getLocalName(node))) {
+                if (ignoreDefaultNamespace && testPrefix == null
+                        && ((Element)node).getNamespacePrefix().equals("")) {
+                    return true;
+                }
                 String nodeNS = JDOMNodePointer.getNamespaceURI(node);
                 return equalStrings(namespaceURI, nodeNS);
             }

Modified: jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLModelTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLModelTestCase.java?rev=329468&r1=329467&r2=329468&view=diff
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLModelTestCase.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLModelTestCase.java
Sat Oct 29 12:02:38 2005
@@ -380,6 +380,12 @@
         context.registerNamespace("x", "temp");
         assertXPathValue(context, "vendor/x:pos//number", "109");
     }
+    
+    public void testDefaultNamespaceIgnored() {
+        context.setDefaultNamespaceIgnored(true);
+        assertXPathValue(context, "vendor/pos//number", "109");
+        context.setDefaultNamespaceIgnored(false);
+    }
 
     public void testAxisChildIndexPredicate() {
         assertXPathValue(



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message