Skip to content
Permalink
Browse files
[JXPATH-125] JXPathContext.iteratePointers() does not work with multi…
…ple prefixes for a single namespace URI

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/jxpath/trunk@917007 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbenson committed Feb 27, 2010
1 parent b67345a commit 7b2847e882a9e4216dcf0f35d27101e363611532
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 13 deletions.
@@ -507,7 +507,7 @@ public String asPath() {
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 @@ public String asPath() {
buffer.append(':');
buffer.append(ln);
buffer.append('[');
buffer.append(getRelativePositionByName());
buffer.append(getRelativePositionByQName());
buffer.append(']');
}
else {
@@ -553,21 +553,26 @@ public String asPath() {
* 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
@@ -641,7 +641,7 @@ public String asPath() {
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 @@ public String asPath() {
buffer.append(':');
buffer.append(ln);
buffer.append('[');
buffer.append(getRelativePositionByName());
buffer.append(getRelativePositionByQName());
buffer.append(']');
}
else {
@@ -681,7 +681,7 @@ else if (node instanceof ProcessingInstruction) {
* 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 @@ private int getRelativePositionByName() {
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 @@ private int getRelativePositionByName() {
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
@@ -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>
@@ -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);
}
}

0 comments on commit 7b2847e

Please sign in to comment.