1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.tools.command; |
21 |
| |
22 |
| import org.apache.xindice.tools.XMLTools; |
23 |
| |
24 |
| import org.xmldb.api.DatabaseManager; |
25 |
| import org.xmldb.api.base.Collection; |
26 |
| import org.xmldb.api.base.ResourceIterator; |
27 |
| import org.xmldb.api.base.ResourceSet; |
28 |
| import org.xmldb.api.base.XMLDBException; |
29 |
| import org.xmldb.api.modules.XMLResource; |
30 |
| import org.xmldb.api.modules.XPathQueryService; |
31 |
| |
32 |
| import java.util.StringTokenizer; |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| public class XPathQuery extends Command { |
41 |
| |
42 |
0
| public boolean execute(XMLTools.Config table) throws Exception {
|
43 |
| |
44 |
0
| if (table.getString(XMLTools.COLLECTION) == null) {
|
45 |
0
| System.out.println("ERROR : Collection name and switch required");
|
46 |
0
| return false;
|
47 |
| } |
48 |
| |
49 |
0
| if ("".equals(table.getString(XMLTools.QUERY))) {
|
50 |
0
| System.out.println("ERROR : Query and switch required");
|
51 |
0
| return false;
|
52 |
| } |
53 |
| |
54 |
0
| Collection col = null;
|
55 |
0
| try {
|
56 |
0
| String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
|
57 |
| table.getBoolean(XMLTools.LOCAL)); |
58 |
0
| String querystring = table.getString(XMLTools.QUERY);
|
59 |
| |
60 |
0
| col = DatabaseManager.getCollection(colstring);
|
61 |
| |
62 |
0
| if (col == null) {
|
63 |
0
| System.out.println("ERROR : Collection not found!");
|
64 |
0
| return false;
|
65 |
| } |
66 |
| |
67 |
0
| XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0");
|
68 |
0
| addNamespaces(service, table.getString(XMLTools.NAMESPACES));
|
69 |
| |
70 |
0
| ResourceSet resultSet = service.query(querystring);
|
71 |
0
| ResourceIterator results = resultSet.getIterator();
|
72 |
| |
73 |
0
| while (results.hasMoreResources()) {
|
74 |
0
| XMLResource resource = (XMLResource) results.nextResource();
|
75 |
0
| String documentstr = (String) resource.getContent();
|
76 |
0
| System.out.println(documentstr);
|
77 |
| } |
78 |
| } finally { |
79 |
0
| if (col != null) {
|
80 |
0
| col.close();
|
81 |
| } |
82 |
| } |
83 |
| |
84 |
0
| return true;
|
85 |
| } |
86 |
| |
87 |
0
| private void addNamespaces(XPathQueryService service, String namespacesString) throws XMLDBException {
|
88 |
0
| if (namespacesString != null && namespacesString.length() > 0) {
|
89 |
0
| StringTokenizer st = new StringTokenizer(namespacesString, "=;");
|
90 |
0
| if (st.countTokens() % 2 != 0) {
|
91 |
0
| throw new XMLDBException(0, "mismatched namespace prefixes and uris in '" + namespacesString + "'");
|
92 |
| } |
93 |
0
| while (st.hasMoreTokens()) {
|
94 |
0
| service.setNamespace(st.nextToken(), st.nextToken());
|
95 |
| } |
96 |
| } |
97 |
| } |
98 |
| |
99 |
0
| public void usage() {
|
100 |
0
| System.out.println("Format: xindice xpath -c <context> [-l [-d <path>]] [-v] [parameters...]");
|
101 |
0
| System.out.println();
|
102 |
0
| System.out.println("Searches collection for documents matching given XPath query");
|
103 |
0
| System.out.println();
|
104 |
0
| System.out.println("Command-specific switches:");
|
105 |
0
| System.out.println(" -q|--query <query>");
|
106 |
0
| System.out.println(" XPath query");
|
107 |
0
| System.out.println(" -s|--namespaces <namespaces>");
|
108 |
0
| System.out.println(" Semicolon delimited list of namespaces for query");
|
109 |
0
| System.out.println(" in the form prefix=namespace-uri");
|
110 |
0
| System.out.println();
|
111 |
0
| System.out.println("Examples:");
|
112 |
0
| System.out.println(" xindice xpath -c /db/test/ocs -q test");
|
113 |
0
| System.out.println(" xindice xpath -c /db/test -s a=http://somedomain.com/schema.xsd -q /a:foo");
|
114 |
0
| System.out.println();
|
115 |
| } |
116 |
| } |