1
2
3
4 package myx;
5
6 import java.io.StringReader;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11
12 import javax.xml.transform.sax.SAXSource;
13
14 import net.sf.saxon.om.NodeInfo;
15
16 import net.sf.saxon.sxpath.XPathEvaluator;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.sxpath.XPathExpression;
19
20 import org.xml.sax.InputSource;
21 import org.xmldb.api.base.*;
22 import org.xmldb.api.modules.XPathQueryService;
23 import org.xmldb.api.reference.ResourceSetImpl;
24 import org.xmldb.api.reference.modules.XMLResourceImpl;
25
26
27 /***
28 * Use Saxon to implement
29 * @author Mladen Adamovic (adamm@blic.net)
30 */
31 public class myxSaxonQueryService implements XPathQueryService {
32 private static Logger logger = Logger.getLogger(myxSaxonQueryService.class.getName());
33 Collection col;
34
35 /***
36 *
37 */
38 public myxSaxonQueryService() {
39 super();
40 }
41
42 /***
43 * @param col
44 */
45 public myxSaxonQueryService(Collection col) {
46 super();
47 this.col=col;
48 }
49
50 /***
51 * @see org.xmldb.api.modules.XPathQueryService#setNamespace(java.lang.String, java.lang.String)
52 */
53 public void setNamespace(String prefix, String uri) throws XMLDBException {
54
55
56 }
57
58 /***
59 * @see org.xmldb.api.modules.XPathQueryService#getNamespace(java.lang.String)
60 */
61 public String getNamespace(String prefix) throws XMLDBException {
62
63 return null;
64 }
65
66 /***
67 * @see org.xmldb.api.modules.XPathQueryService#removeNamespace(java.lang.String)
68 */
69 public void removeNamespace(String prefix) throws XMLDBException {
70
71
72 }
73
74 /***
75 * @see org.xmldb.api.modules.XPathQueryService#clearNamespaces()
76 */
77 public void clearNamespaces() throws XMLDBException {
78
79
80 }
81
82 /***
83 * @see org.xmldb.api.modules.XPathQueryService#query(java.lang.String)
84 *
85 * Use iterator over Collections.listChildCollections() and call
86 * queryResource
87 */
88 public ResourceSet query(String query) throws XMLDBException {
89 logger.fine("col :"+col);
90 String resNames[]=col.listResources();
91 int i;
92 long j;
93 logger.fine("resNames :"+resNames);
94 logger.fine("resNames.length :"+resNames.length);
95
96 ResourceSet result=new ResourceSetImpl(col),qrs;
97 for(i=0;i<resNames.length;i++) {
98 logger.fine("querying "+resNames[i]+"...");
99 qrs=queryResource(resNames[i],query);
100
101 for(j=0;j<qrs.getSize();j++) {
102 Resource res=qrs.getResource(j);
103 result.addResource(res);
104 }
105 }
106 return result;
107 }
108
109 /***
110 * @see org.xmldb.api.modules.XPathQueryService#queryResource(java.lang.String, java.lang.String)
111 */
112 public ResourceSet queryResource(String id, String query)
113 throws XMLDBException {
114 String content = (String) col.getResource(id).getContent();
115 InputSource is=new InputSource(new StringReader(content));
116 List matched;
117 SAXSource ss = new SAXSource(is);
118 try {
119 XPathEvaluator xpe = new XPathEvaluator();
120
121
122 XPathExpression extract = xpe.createExpression(query);
123 matched = extract.evaluate(ss);
124 } catch(XPathException e) {
125 e.printStackTrace();
126 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "XPathException");
127 }
128
129 ResourceSetImpl rs=null;
130
131
132 rs = new ResourceSetImpl(col);
133
134 for (Iterator iter = matched.iterator(); iter.hasNext();) {
135
136 NodeInfo ni = (NodeInfo) iter.next();
137 XMLResourceImpl xmlResource = new XMLResourceImpl(col,
138 "QueryResult", "QueryResult", ni.getStringValue());
139 rs.addResource(xmlResource);
140
141 }
142 return rs;
143 }
144
145 /***
146 * @see org.xmldb.api.base.Service#getName()
147 */
148 public String getName() throws XMLDBException {
149
150 return null;
151 }
152
153 /***
154 * @see org.xmldb.api.base.Service#getVersion()
155 */
156 public String getVersion() throws XMLDBException {
157
158 return null;
159 }
160
161 /***
162 * @see org.xmldb.api.base.Service#setCollection(org.xmldb.api.base.Collection)
163 */
164 public void setCollection(Collection col) throws XMLDBException {
165 this.col=col;
166 }
167
168 /***
169 * @see org.xmldb.api.base.Configurable#getProperty(java.lang.String)
170 */
171 public String getProperty(String name) throws XMLDBException {
172
173 return null;
174 }
175
176 /***
177 * @see org.xmldb.api.base.Configurable#setProperty(java.lang.String, java.lang.String)
178 */
179 public void setProperty(String name, String value) throws XMLDBException {
180
181
182 }
183
184 }