Author: mbenson Date: Sat Feb 27 18:34:39 2010 New Revision: 917007 URL: http://svn.apache.org/viewvc?rev=917007&view=rev Log: [JXPATH-125] JXPathContext.iteratePointers() does not work with multiple prefixes for a single namespace URI
Added: commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/IterateAliasedNS.xml (with props) commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/AliasedNamespaceIterationTest.java (with props) Modified: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java Modified: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java?rev=917007&r1=917006&r2=917007&view=diff ============================================================================== --- commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java (original) +++ commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java Sat Feb 27 18:34:39 2010 @@ -507,7 +507,7 @@ if (nsURI == null) { buffer.append(ln); buffer.append('['); - buffer.append(getRelativePositionByName()).append(']'); + buffer.append(getRelativePositionByQName()).append(']'); } else { String prefix = getNamespaceResolver().getPrefix(nsURI); @@ -516,7 +516,7 @@ buffer.append(':'); buffer.append(ln); buffer.append('['); - buffer.append(getRelativePositionByName()); + buffer.append(getRelativePositionByQName()); buffer.append(']'); } else { @@ -553,21 +553,26 @@ * Get relative position of this among like-named siblings. * @return 1..n */ - private int getRelativePositionByName() { + private int getRelativePositionByQName() { int count = 1; Node n = node.getPreviousSibling(); while (n != null) { - if (n.getNodeType() == Node.ELEMENT_NODE) { - String nm = n.getNodeName(); - if (nm.equals(node.getNodeName())) { - count++; - } + if (n.getNodeType() == Node.ELEMENT_NODE && matchesQName(n)) { + count++; } n = n.getPreviousSibling(); } return count; } + private boolean matchesQName(Node n) { + if (getNamespaceURI() != null) { + return equalStrings(getNamespaceURI(n), getNamespaceURI()) + && equalStrings(node.getLocalName(), n.getLocalName()); + } + return equalStrings(node.getNodeName(), n.getNodeName()); + } + /** * Get relative position of this among all siblings. * @return 1..n Modified: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java?rev=917007&r1=917006&r2=917007&view=diff ============================================================================== --- commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java (original) +++ commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java Sat Feb 27 18:34:39 2010 @@ -641,7 +641,7 @@ if (nsURI == null) { buffer.append(ln); buffer.append('['); - buffer.append(getRelativePositionByName()).append(']'); + buffer.append(getRelativePositionByQName()).append(']'); } else { String prefix = getNamespaceResolver().getPrefix(nsURI); @@ -650,7 +650,7 @@ buffer.append(':'); buffer.append(ln); buffer.append('['); - buffer.append(getRelativePositionByName()); + buffer.append(getRelativePositionByQName()); buffer.append(']'); } else { @@ -681,7 +681,7 @@ * Get relative position of this among like-named siblings. * @return 1..n */ - private int getRelativePositionByName() { + private int getRelativePositionByQName() { if (node instanceof Element) { Object parent = ((Element) node).getParent(); if (!(parent instanceof Element)) { @@ -693,8 +693,7 @@ String name = ((Element) node).getQualifiedName(); for (int i = 0; i < children.size(); i++) { Object child = children.get(i); - if ((child instanceof Element) - && ((Element) child).getQualifiedName().equals(name)) { + if (child instanceof Element && matchesQName(((Element) child))) { count++; } if (child == node) { @@ -706,6 +705,16 @@ return 1; } + private boolean matchesQName(Element element) { + if (getNamespaceURI() != null) { + String ns = getNamespaceURI(element); + if (ns == null || !ns.equals(getNamespaceURI())) { + return false; + } + } + return element.getName().equals(((Element) node).getName()); + } + /** * Get relative position of this among all siblings. * @return 1..n Added: commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/IterateAliasedNS.xml URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/IterateAliasedNS.xml?rev=917007&view=auto ============================================================================== --- commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/IterateAliasedNS.xml (added) +++ commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/IterateAliasedNS.xml Sat Feb 27 18:34:39 2010 @@ -0,0 +1,21 @@ +<?xml version="1.0" ?> +<!-- + 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. +--> +<a:doc xmlns:a="ns"> + <a:elem /> + <b:elem xmlns:b="ns" /> +</a:doc> \ No newline at end of file Propchange: commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/IterateAliasedNS.xml ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/AliasedNamespaceIterationTest.java URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/AliasedNamespaceIterationTest.java?rev=917007&view=auto ============================================================================== --- commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/AliasedNamespaceIterationTest.java (added) +++ commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/AliasedNamespaceIterationTest.java Sat Feb 27 18:34:39 2010 @@ -0,0 +1,60 @@ +/* + * 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.commons.jxpath.ri.model; + +import java.util.Collection; + +import org.apache.commons.jxpath.JXPathContext; +import org.apache.commons.jxpath.JXPathTestCase; +import org.apache.commons.jxpath.xml.DocumentContainer; + +/** + * Test aliased/doubled XML namespace iteration; JXPATH-125. + * + * @version $Revision$ $Date$ + */ +public class AliasedNamespaceIterationTest extends JXPathTestCase { + protected JXPathContext context; + + protected DocumentContainer createDocumentContainer(String model) { + DocumentContainer result = new DocumentContainer(JXPathTestCase.class + .getResource("IterateAliasedNS.xml"), model); + return result; + } + + protected JXPathContext createContext(String model) { + JXPathContext context = JXPathContext.newContext(createDocumentContainer(model)); + context.registerNamespace("a", "ns"); + return context; + } + + protected void doTestIterate(String xpath, String model, Collection expected) { + assertXPathPointerIterator(createContext(model), xpath, expected); + } + + protected void doTestIterate(String model) { + assertXPathPointerIterator(createContext(model), "/a:doc/a:elem", list("/a:doc[1]/a:elem[1]", "/a:doc[1]/a:elem[2]")); + } + + public void testIterateDOM() { + doTestIterate(DocumentContainer.MODEL_DOM); + } + + public void testIterateJDOM() { + doTestIterate(DocumentContainer.MODEL_JDOM); + } +} Propchange: commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/AliasedNamespaceIterationTest.java ------------------------------------------------------------------------------ svn:eol-style = native