dmitri 2004/06/29 15:58:18 Modified: jxpath/src/java/org/apache/commons/jxpath/ri/model/dom DOMNodePointer.java jxpath/src/test/org/apache/commons/jxpath/ri/model/jdom TestJDOMFactory.java jxpath/src/test/org/apache/commons/jxpath/ri/model/dom TestDOMFactory.java jxpath/src/test/org/apache/commons/jxpath/ri/model XMLModelTestCase.java jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom JDOMNodePointer.java Log: Fixed a bug having to do with creation of nodes with prefixes Revision Changes Path 1.24 +12 -3 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java Index: DOMNodePointer.java =================================================================== RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- DOMNodePointer.java 1 Apr 2004 02:55:32 -0000 1.23 +++ DOMNodePointer.java 29 Jun 2004 22:58:17 -0000 1.24 @@ -387,8 +387,17 @@ name.toString(), index); if (success) { - NodeIterator it = - childIterator(new NodeNameTest(name), false, null); + NodeTest nodeTest; + String prefix = name.getPrefix(); + if (prefix != null) { + String namespaceURI = context.getNamespaceURI(prefix); + nodeTest = new NodeNameTest(name, namespaceURI); + } + else { + nodeTest = new NodeNameTest(name); + } + + NodeIterator it = childIterator(nodeTest, false, null); if (it != null && it.setPosition(index + 1)) { return it.getNodePointer(); } 1.6 +18 -4 jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/jdom/TestJDOMFactory.java Index: TestJDOMFactory.java =================================================================== RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/jdom/TestJDOMFactory.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- TestJDOMFactory.java 29 Feb 2004 14:17:43 -0000 1.5 +++ TestJDOMFactory.java 29 Jun 2004 22:58:17 -0000 1.6 @@ -44,13 +44,19 @@ if (name.equals("location") || name.equals("address") || name.equals("street")) { - addJDOMElement((Element) parent, index, name); + addJDOMElement((Element) parent, index, name, null); return true; } + if (name.startsWith("price:")) { + String namespaceURI = context.getNamespaceURI("price"); + addJDOMElement((Element) parent, index, name, namespaceURI); + return true; + } + return false; } - private void addJDOMElement(Element parent, int index, String tag) { + private void addJDOMElement(Element parent, int index, String tag, String namespaceURI) { List children = parent.getContent(); int count = 0; for (int i = 0; i < children.size(); i++) { @@ -65,7 +71,15 @@ while (count <= index) { // In a real factory we would need to do the right thing with // the namespace prefix. - Element newElement = new Element(tag); + Element newElement; + if (namespaceURI != null) { + String prefix = tag.substring(0, tag.indexOf(':')); + tag = tag.substring(tag.indexOf(':') + 1); + newElement = new Element(tag, prefix, namespaceURI); + } + else { + newElement = new Element(tag); + } parent.addContent(newElement); count++; } 1.6 +18 -4 jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/dom/TestDOMFactory.java Index: TestDOMFactory.java =================================================================== RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/dom/TestDOMFactory.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- TestDOMFactory.java 29 Feb 2004 14:17:46 -0000 1.5 +++ TestDOMFactory.java 29 Jun 2004 22:58:17 -0000 1.6 @@ -18,6 +18,7 @@ import org.apache.commons.jxpath.AbstractFactory; import org.apache.commons.jxpath.JXPathContext; import org.apache.commons.jxpath.Pointer; +import org.w3c.dom.Document; import org.w3c.dom.Node; /** @@ -41,13 +42,18 @@ if (name.equals("location") || name.equals("address") || name.equals("street")) { - addDOMElement((Node) parent, index, name); + addDOMElement((Node) parent, index, name, null); + return true; + } + if (name.startsWith("price:")) { + String namespaceURI = context.getNamespaceURI("price"); + addDOMElement((Node) parent, index, name, namespaceURI); return true; } return false; } - private void addDOMElement(Node parent, int index, String tag) { + private void addDOMElement(Node parent, int index, String tag, String namespaceURI) { Node child = parent.getFirstChild(); int count = 0; while (child != null) { @@ -59,7 +65,15 @@ // Keep inserting new elements until we have index + 1 of them while (count <= index) { - Node newElement = parent.getOwnerDocument().createElement(tag); + Document doc = parent.getOwnerDocument(); + Node newElement; + if (namespaceURI == null) { + newElement = doc.createElement(tag); + } + else { + newElement = doc.createElementNS(namespaceURI, tag); + } + parent.appendChild(newElement); count++; } 1.22 +20 -41 jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/XMLModelTestCase.java Index: XMLModelTestCase.java =================================================================== RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/XMLModelTestCase.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- XMLModelTestCase.java 1 Apr 2004 02:55:32 -0000 1.21 +++ XMLModelTestCase.java 29 Jun 2004 22:58:18 -0000 1.22 @@ -190,7 +190,16 @@ context, "/vendor[1]/location[4]/@manager", "", - "/vendor[1]/location[4]/@manager"); + "/vendor[1]/location[4]/@manager"); + + context.registerNamespace("price", "priceNS"); + + // Create a DOM element + assertXPathCreatePath( + context, + "/vendor[1]/price:foo/price:bar", + "", + "/vendor[1]/price:foo[1]/price:bar[1]"); } /** @@ -235,6 +244,15 @@ "vendor/product/product:name/attribute::price:language", "English", "/vendor[1]/product[1]/product:name[1]/@price:language"); + + context.registerNamespace("price", "priceNS"); + + // Create a DOM element + assertXPathCreatePathAndSetValue( + context, + "/vendor[1]/price:foo/price:bar", + "123.20", + "/vendor[1]/price:foo[1]/price:bar[1]"); } /** @@ -773,44 +791,5 @@ "//product:name", "Box of oranges", "/vendor[1]/product[1]/goods:name[1]"); - -// if (isExternalNamespaceSupported()) { -// DocumentContainer container = new DocumentContainer( -// XMLModelTestCase.class.getResource("ExternalNamespaceTest.xml"), -// getModel()); -// JXPathContext context = JXPathContext.newContext(container); -// NamespaceManager nsm = context.getNamespaceManager(); -// nsm.registerNamespace("quality", "qualityNS"); -// nsm.registerNamespace("money", "priceNS"); -// -// assertXPathValueAndPointer( -// context, -// "//quality:color", -// "orange", -// "/vendor[1]/product[1]/quality:color[1]"); -// -// // It is supposed to figure out that the prefixes "money" and -// // "value" map to the same namespaceURI -// assertXPathValueAndPointer( -// context, -// "//value:price", -// "1000.00", -// "/vendor[1]/product[1]/money:price[1]"); -// -// assertXPathValue( -// context, -// "local-name(vendor/product/value:price)", -// "price"); -// -// assertXPathValue( -// context, -// "name(vendor/product/quality:color)", -// "qualityNS:color"); -// -// assertXPathValue( -// context, -// "namespace-uri(vendor/product/value:price)", -// "priceNS"); -// } } } 1.17 +12 -2 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java Index: JDOMNodePointer.java =================================================================== RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- JDOMNodePointer.java 1 Apr 2004 02:55:31 -0000 1.16 +++ JDOMNodePointer.java 29 Jun 2004 22:58:18 -0000 1.17 @@ -497,8 +497,18 @@ name.toString(), index); if (success) { + NodeTest nodeTest; + String prefix = name.getPrefix(); + if (prefix != null) { + String namespaceURI = context.getNamespaceURI(prefix); + nodeTest = new NodeNameTest(name, namespaceURI); + } + else { + nodeTest = new NodeNameTest(name); + } + NodeIterator it = - childIterator(new NodeNameTest(name), false, null); + childIterator(nodeTest, false, null); if (it != null && it.setPosition(index + 1)) { return it.getNodePointer(); }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]