vgritsenko 2004/02/24 19:32:52
Modified: java/src/org/apache/xindice/core/query QueryUtil.java
java/tests/src/org/apache/xindice/integration/client/services
XPathQueryTest.java
Log:
Add XPath query support for comment nodes, multiple text nodes.
Revision Changes Path
1.4 +24 -3
xml-xindice/java/src/org/apache/xindice/core/query/QueryUtil.java
Index: QueryUtil.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/query/QueryUtil.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- QueryUtil.java 24 Feb 2004 16:52:55 -0000 1.3
+++ QueryUtil.java 25 Feb 2004 03:32:52 -0000 1.4
@@ -26,11 +26,16 @@
import org.apache.xindice.xml.dom.DBNode;
import org.apache.xindice.xml.dom.DocumentImpl;
import org.apache.xindice.xml.dom.NodeImpl;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import org.w3c.dom.Comment;
+import org.w3c.dom.ProcessingInstruction;
import java.util.Hashtable;
@@ -41,6 +46,8 @@
*/
public class QueryUtil {
+ private static final Log log = LogFactory.getLog(QueryUtil.class);
+
/**
* Maps a Hashtable containing namespace definitions into a Xindice
* NamespaceMap.
@@ -71,9 +78,23 @@
Element holder =
doc.createElementNS(XindiceCollection.QUERY_NS, "xq:result");
holder.setAttribute(NodeImpl.XMLNS_PREFIX + ":xq",
XindiceCollection.QUERY_NS);
- holder.setAttributeNode((Attr)doc.importNode(n, true));
+ holder.setAttributeNode((Attr) doc.importNode(n, true));
+
+ // TODO: expandSource
+ root.appendChild(holder);
+ } else if (element instanceof Text || element instanceof
Comment) {
+ Node n = (Node) element;
+
+ Element holder =
doc.createElementNS(XindiceCollection.QUERY_NS, "xq:result");
+ holder.setAttribute(NodeImpl.XMLNS_PREFIX + ":xq",
XindiceCollection.QUERY_NS);
+ holder.appendChild(doc.importNode(n, true));
+ // TODO: expandSource
root.appendChild(holder);
+ } else if (element instanceof ProcessingInstruction) {
+ if (log.isWarnEnabled()) {
+ log.warn("XPath query with ProcessingInstruction result
is not supported");
+ }
} else if (element instanceof Node) {
Node n = (Node) element;
1.14 +43 -12
xml-xindice/java/tests/src/org/apache/xindice/integration/client/services/XPathQueryTest.java
Index: XPathQueryTest.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/tests/src/org/apache/xindice/integration/client/services/XPathQueryTest.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- XPathQueryTest.java 24 Feb 2004 16:52:55 -0000 1.13
+++ XPathQueryTest.java 25 Feb 2004 03:32:52 -0000 1.14
@@ -50,6 +50,7 @@
String document1
= "<?xml version=\"1.0\"?>"
+ "<person>"
+ + "<!-- John Smith -->"
+ "<first>John</first>"
+ "<last>Smith</last>"
+ "<age>30</age>"
@@ -58,6 +59,7 @@
String document2
= "<?xml version=\"1.0\"?>"
+ "<person>"
+ + "<!-- Sally Smith -->"
+ "<first>Sally</first>"
+ "<last>Smith</last>"
+ "<age>20</age>"
@@ -246,7 +248,9 @@
ResourceSet resultSet = xpathservice.query(query);
assertEquals(1L, resultSet.getSize());
- assertEquals("John", resultSet.getResource(0).getContent());
+ Node result = ((XMLResource)
resultSet.getResource(0)).getContentAsDOM();
+ assertEquals(Node.TEXT_NODE,
result.getChildNodes().item(0).getChildNodes().item(0).getNodeType());
+ assertEquals("John",
result.getChildNodes().item(0).getChildNodes().item(0).getNodeValue());
}
public void testFetchMultipleTextNodes() throws Exception {
@@ -255,8 +259,12 @@
ResourceSet resultSet = xpathservice.query(query);
assertEquals(2, resultSet.getSize());
- assertEquals("John", resultSet.getResource(0).getContent());
- assertEquals("Sally", resultSet.getResource(1).getContent());
+ Node result0 = ((XMLResource)
resultSet.getResource(0)).getContentAsDOM();
+ Node result1 = ((XMLResource)
resultSet.getResource(1)).getContentAsDOM();
+ assertEquals(Node.TEXT_NODE,
result0.getChildNodes().item(0).getChildNodes().item(0).getNodeType());
+ assertEquals(Node.TEXT_NODE,
result1.getChildNodes().item(0).getChildNodes().item(0).getNodeType());
+ assertEquals("John",
result0.getChildNodes().item(0).getChildNodes().item(0).getNodeValue());
+ assertEquals("Sally",
result1.getChildNodes().item(0).getChildNodes().item(0).getNodeValue());
}
public void testFetchAttributeNode() throws Exception {
@@ -276,7 +284,7 @@
}
}
- public void testFetchMultipleAttributeNode() throws Exception {
+ public void testFetchMultipleAttributeNodes() throws Exception {
String document3 = "<?xml version=\"1.0\"?>" + "<foo bar='foo'>" +
"<bar foo='bar' bar='bar'/>" + "</foo>";
client.insertDocument(TEST_COLLECTION_PATH, "doc3", document3);
@@ -295,6 +303,28 @@
}
}
+ public void testFetchCommentNode() throws Exception {
+ String query = "//person/comment()[contains(., 'John')]";
+
+ ResourceSet resultSet = xpathservice.query(query);
+ assertEquals(1, resultSet.getSize());
+
+ Node result = ((XMLResource)
resultSet.getResource(0)).getContentAsDOM();
+ assertEquals(" John Smith ",
result.getChildNodes().item(0).getChildNodes().item(0).getNodeValue());
+ }
+
+ public void testFetchMultipleCommentNodes() throws Exception {
+ String query = "//person/comment()";
+
+ ResourceSet resultSet = xpathservice.query(query);
+ assertEquals(2, resultSet.getSize());
+
+ Node result0 = ((XMLResource)
resultSet.getResource(0)).getContentAsDOM();
+ Node result1 = ((XMLResource)
resultSet.getResource(1)).getContentAsDOM();
+ assertEquals(" John Smith ",
result0.getChildNodes().item(0).getChildNodes().item(0).getNodeValue());
+ assertEquals(" Sally Smith ",
result1.getChildNodes().item(0).getChildNodes().item(0).getNodeValue());
+ }
+
public void testFetchBoolean() throws Exception {
String query = "boolean(//person)";
@@ -339,13 +369,14 @@
XMLResource resource = (XMLResource) resultSet.getResource(0);
- String john =
- "<?xml version=\"1.0\"?>"
+ String john
+ = "<?xml version=\"1.0\"?>"
+ "<person xmlns:src='http://xml.apache.org/xindice/Query'
src:col='/db/testing/current' src:key='doc1'>"
- + "<first>John</first>"
- + "<last>Smith</last>"
- + "<age>30</age>"
- + "<phone type=\"work\">555-345-6789</phone>"
+ + "<!-- John Smith -->"
+ + "<first>John</first>"
+ + "<last>Smith</last>"
+ + "<age>30</age>"
+ + "<phone type=\"work\">555-345-6789</phone>"
+ "</person>";
// ensure that the resource has the correct doc id.