1
2
3
4 package myx;
5
6 import java.io.File;
7 import java.io.StringReader;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12
13 import javax.xml.transform.TransformerException;
14 import javax.xml.transform.sax.SAXSource;
15 import javax.xml.transform.stream.StreamSource;
16
17 import net.sf.saxon.Configuration;
18 import net.sf.saxon.om.NodeInfo;
19 import net.sf.saxon.om.SequenceIterator;
20 import net.sf.saxon.query.DynamicQueryContext;
21
22 import net.sf.saxon.query.StaticQueryContext;
23 import net.sf.saxon.query.XQueryExpression;
24 import net.sf.saxon.xpath.XPathEvaluator;
25 import net.sf.saxon.trans.XPathException;
26 import net.sf.saxon.sxpath.XPathExpression;
27
28 import org.xml.sax.InputSource;
29 import org.xmldb.api.base.Collection;
30 import org.xmldb.api.base.ErrorCodes;
31 import org.xmldb.api.base.Resource;
32 import org.xmldb.api.base.ResourceSet;
33 import org.xmldb.api.base.XMLDBException;
34 import org.xmldb.api.modules.XPathQueryService;
35 import org.xmldb.api.reference.ResourceSetImpl;
36 import org.xmldb.api.reference.modules.XMLResourceImpl;
37
38 /***
39 * @author Mladen Adamovic (adamm@blic.net)
40 */
41 public class myxXQuerySaxonService implements XPathQueryService {
42 private static Logger logger = Logger.getLogger(myxXQuerySaxonService.class.getName());
43
44 Collection col;
45
46 /***
47 *
48 */
49 public myxXQuerySaxonService() {
50 super();
51 }
52
53 /***
54 * @param col
55 */
56 public myxXQuerySaxonService(Collection col) {
57 super();
58 this.col = col;
59 }
60
61 /***
62 * @see org.xmldb.api.modules.XPathQueryService#setNamespace(java.lang.String,
63 * java.lang.String)
64 */
65 public void setNamespace(String prefix, String uri) throws XMLDBException {
66
67
68 }
69
70 /***
71 * @see org.xmldb.api.modules.XPathQueryService#getNamespace(java.lang.String)
72 */
73 public String getNamespace(String prefix) throws XMLDBException {
74
75 return null;
76 }
77
78 /***
79 * @see org.xmldb.api.modules.XPathQueryService#removeNamespace(java.lang.String)
80 */
81 public void removeNamespace(String prefix) throws XMLDBException {
82
83
84 }
85
86 /***
87 * @see org.xmldb.api.modules.XPathQueryService#clearNamespaces()
88 */
89 public void clearNamespaces() throws XMLDBException {
90
91
92 }
93
94 /***
95 * @see org.xmldb.api.modules.XPathQueryService#query(java.lang.String)
96 *
97 * Use iterator over Collections.listChildCollections() and call
98 * queryResource
99 */
100 public ResourceSet query(String query) throws XMLDBException {
101 logger.fine("col :" + col);
102 String resNames[] = col.listResources();
103 int i;
104 long j;
105 logger.fine("resNames :" + resNames);
106 logger.fine("resNames.length :" + resNames.length);
107
108 ResourceSet result = new ResourceSetImpl(col), qrs;
109 for (i = 0; i < resNames.length; i++) {
110 logger.fine("querying " + resNames[i] + "...");
111 qrs = queryResource(resNames[i], query);
112
113 for (j = 0; j < qrs.getSize(); j++) {
114 Resource res = qrs.getResource(j);
115 result.addResource(res);
116 }
117 }
118 return result;
119 }
120
121 /***
122 * @see org.xmldb.api.modules.XPathQueryService#queryResource(java.lang.String,
123 * java.lang.String)
124 */
125 public ResourceSet queryResource(String id, String query)
126 throws XMLDBException {
127 String content = (String) col.getResource(id).getContent();
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 Configuration config = new Configuration();
150 StaticQueryContext staticContext = new StaticQueryContext(config);
151
152 StaticQueryContext xquery = new StaticQueryContext(config);
153 XQueryExpression exp;
154 ResourceSetImpl rs = null;
155 SequenceIterator iter;
156 try {
157 exp = xquery.compileQuery(query);
158
159 DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
160 try {
161 dynamicContext.setContextNode(xquery.buildDocument(new StreamSource(new StringReader(
162 content))));
163 iter = exp.iterator(dynamicContext);
164 } catch (TransformerException e) {
165 throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
166 "TransformerException when tring to invoke query : "
167 + query + "\n" + e.getMessage());
168
169 }
170
171
172 rs = new ResourceSetImpl(col);
173
174 while (true) {
175 NodeInfo ni = (NodeInfo) iter.next();
176 if (ni == null)
177 break;
178 String data = ni.getStringValue();
179 XMLResourceImpl xmlResource = new XMLResourceImpl(col,
180 "QueryResult", "QueryResult", ni.getStringValue());
181 rs.addResource(xmlResource);
182
183 }
184 } catch (XPathException e) {
185 throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
186 "XpathException when tring to invoke query : " + query
187 + "\n" + e.getMessage());
188 }
189
190 return rs;
191 }
192
193 /***
194 * @see org.xmldb.api.base.Service#getName()
195 */
196 public String getName() throws XMLDBException {
197
198 return null;
199 }
200
201 /***
202 * @see org.xmldb.api.base.Service#getVersion()
203 */
204 public String getVersion() throws XMLDBException {
205
206 return null;
207 }
208
209 /***
210 * @see org.xmldb.api.base.Service#setCollection(org.xmldb.api.base.Collection)
211 */
212 public void setCollection(Collection col) throws XMLDBException {
213 this.col = col;
214 }
215
216 /***
217 * @see org.xmldb.api.base.Configurable#getProperty(java.lang.String)
218 */
219 public String getProperty(String name) throws XMLDBException {
220
221 return null;
222 }
223
224 /***
225 * @see org.xmldb.api.base.Configurable#setProperty(java.lang.String,
226 * java.lang.String)
227 */
228 public void setProperty(String name, String value) throws XMLDBException {
229
230
231 }
232
233 }