|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.client.xmldb.embed; |
|
21 |
| |
|
22 |
| import org.apache.xindice.client.xmldb.ResourceSetImpl; |
|
23 |
| import org.apache.xindice.client.xmldb.XindiceCollection; |
|
24 |
| import org.apache.xindice.client.xmldb.resources.BinaryResourceImpl; |
|
25 |
| import org.apache.xindice.client.xmldb.resources.XMLResourceImpl; |
|
26 |
| import org.apache.xindice.core.Collection; |
|
27 |
| import org.apache.xindice.core.DBException; |
|
28 |
| import org.apache.xindice.core.Database; |
|
29 |
| import org.apache.xindice.core.FaultCodes; |
|
30 |
| import org.apache.xindice.core.data.Entry; |
|
31 |
| import org.apache.xindice.core.data.NodeSet; |
|
32 |
| import org.apache.xindice.core.meta.MetaData; |
|
33 |
| import org.apache.xindice.core.query.QueryUtil; |
|
34 |
| import org.apache.xindice.util.Configuration; |
|
35 |
| import org.apache.xindice.xml.TextWriter; |
|
36 |
| import org.apache.xindice.xml.dom.DocumentImpl; |
|
37 |
| import org.w3c.dom.Document; |
|
38 |
| import org.w3c.dom.Element; |
|
39 |
| import org.w3c.dom.Node; |
|
40 |
| import org.xmldb.api.base.ErrorCodes; |
|
41 |
| import org.xmldb.api.base.Resource; |
|
42 |
| import org.xmldb.api.base.ResourceSet; |
|
43 |
| import org.xmldb.api.base.XMLDBException; |
|
44 |
| import org.xmldb.api.modules.BinaryResource; |
|
45 |
| import org.xmldb.api.modules.XMLResource; |
|
46 |
| |
|
47 |
| import java.util.Hashtable; |
|
48 |
| import java.util.StringTokenizer; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| public class CollectionImpl extends XindiceCollection { |
|
59 |
| |
|
60 |
| private Database db; |
|
61 |
| private Collection col; |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
1127
| public CollectionImpl(Database db, String collPath) throws XMLDBException {
|
|
74 |
1127
| super(collPath);
|
|
75 |
1127
| this.db = db;
|
|
76 |
| |
|
77 |
| |
|
78 |
1127
| String collName = "/";
|
|
79 |
1127
| int colIndex = collPath.indexOf('/', 1);
|
|
80 |
1127
| if (colIndex != -1) {
|
|
81 |
1122
| collName = collPath.substring(colIndex);
|
|
82 |
1122
| if (collName.equals("")) {
|
|
83 |
0
| collName = "/";
|
|
84 |
| } |
|
85 |
| } |
|
86 |
| |
|
87 |
1127
| try {
|
|
88 |
1127
| this.col = db.getCollection(collName);
|
|
89 |
| } catch (Exception e) { |
|
90 |
0
| throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_COLLECTION,
|
|
91 |
| "Collection not available: " + collPath, e); |
|
92 |
| } |
|
93 |
| |
|
94 |
1127
| if (this.col == null) {
|
|
95 |
1
| throw new XMLDBException(ErrorCodes.NO_SUCH_COLLECTION,
|
|
96 |
| "Collection not found: " + collPath); |
|
97 |
| } |
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
175
| public Resource getResource(String id) throws XMLDBException {
|
|
114 |
| |
|
115 |
175
| checkOpen();
|
|
116 |
175
| try {
|
|
117 |
175
| Entry entry = col.getEntry(id);
|
|
118 |
175
| if (entry == null) {
|
|
119 |
7
| return null;
|
|
120 |
| } |
|
121 |
| |
|
122 |
168
| switch (entry.getEntryType()) {
|
|
123 |
166
| case Entry.DOCUMENT:
|
|
124 |
166
| DocumentImpl doc = (DocumentImpl) entry.getValue();
|
|
125 |
| |
|
126 |
| |
|
127 |
166
| if (doc.getDataBytes() == null) {
|
|
128 |
166
| return new XMLResourceImpl(id, id, this, TextWriter.toString(doc));
|
|
129 |
| } else { |
|
130 |
0
| return new XMLResourceImpl(id, id, this, doc.getSymbols(), doc.getDataBytes());
|
|
131 |
| } |
|
132 |
| |
|
133 |
2
| case Entry.BINARY:
|
|
134 |
2
| return new BinaryResourceImpl(id, this, (byte[]) entry.getValue());
|
|
135 |
| |
|
136 |
0
| default:
|
|
137 |
0
| throw new XMLDBException(ErrorCodes.UNKNOWN_RESOURCE_TYPE,
|
|
138 |
| "Internal error: Unexpected resource type " + entry.getEntryType()); |
|
139 |
| } |
|
140 |
| |
|
141 |
| } catch (Exception e) { |
|
142 |
0
| throw FaultCodes.createXMLDBException("Resource not available: " + id, e);
|
|
143 |
| } |
|
144 |
| } |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
21
| public int getResourceCount() throws XMLDBException {
|
|
158 |
| |
|
159 |
21
| checkOpen();
|
|
160 |
21
| try {
|
|
161 |
21
| return (int) col.getDocumentCount();
|
|
162 |
| } catch (Exception e) { |
|
163 |
0
| throw FaultCodes.createXMLDBException(e);
|
|
164 |
| } |
|
165 |
| } |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
406
| public void storeResource(Resource res) throws XMLDBException {
|
|
182 |
406
| if (res.getContent() == null) {
|
|
183 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "No resource data");
|
|
184 |
| } |
|
185 |
| |
|
186 |
406
| checkOpen();
|
|
187 |
| |
|
188 |
406
| try {
|
|
189 |
406
| if (res instanceof XMLResource) {
|
|
190 |
402
| Node content = getXMLContent(res);
|
|
191 |
402
| if (res.getId() != null) {
|
|
192 |
402
| col.setDocument(res.getId(), (Document) content);
|
|
193 |
| } else { |
|
194 |
0
| String name = col.insertDocument((Document) content).toString();
|
|
195 |
0
| ((XMLResourceImpl) res).setId(name);
|
|
196 |
| } |
|
197 |
| |
|
198 |
4
| } else if (res instanceof BinaryResource) {
|
|
199 |
4
| byte[] bytes = getBinaryContent(res);
|
|
200 |
4
| if (res.getId() != null) {
|
|
201 |
4
| col.setBinary(res.getId(), bytes);
|
|
202 |
| } else { |
|
203 |
0
| String name = col.insertBinary(bytes).toString();
|
|
204 |
0
| ((BinaryResourceImpl) res).setId(name);
|
|
205 |
| } |
|
206 |
| |
|
207 |
| } else { |
|
208 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
|
|
209 |
| "Only XMLResource and BinaryResource supported"); |
|
210 |
| } |
|
211 |
| } catch (DBException e) { |
|
212 |
0
| throw FaultCodes.createXMLDBException(e.faultCode, e.getMessage(), e);
|
|
213 |
| } catch (XMLDBException e) { |
|
214 |
0
| throw e;
|
|
215 |
| } catch (Exception e) { |
|
216 |
0
| throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_RESOURCE,
|
|
217 |
| "Invalid resource: " + res.getId(), e); |
|
218 |
| } |
|
219 |
| } |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
2
| public void insertResource(Resource res) throws XMLDBException {
|
|
234 |
2
| if (res.getContent() == null) {
|
|
235 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "No resource data");
|
|
236 |
| } |
|
237 |
| |
|
238 |
2
| checkOpen();
|
|
239 |
| |
|
240 |
2
| try {
|
|
241 |
2
| if (res instanceof BinaryResource) {
|
|
242 |
0
| byte[] bytes = getBinaryContent(res);
|
|
243 |
0
| if (res.getId() != null) {
|
|
244 |
0
| col.insertBinary(res.getId(), bytes);
|
|
245 |
| } else { |
|
246 |
0
| String name = col.insertBinary(bytes).toString();
|
|
247 |
0
| ((BinaryResourceImpl) res).setId(name);
|
|
248 |
| } |
|
249 |
2
| } else if (res instanceof XMLResource) {
|
|
250 |
2
| Node content = getXMLContent(res);
|
|
251 |
2
| if (res.getId() != null) {
|
|
252 |
2
| col.insertDocument(res.getId(), (Document) content);
|
|
253 |
| } else { |
|
254 |
0
| String name = col.insertDocument((Document) content).toString();
|
|
255 |
0
| ((XMLResourceImpl) res).setId(name);
|
|
256 |
| } |
|
257 |
| |
|
258 |
| } else { |
|
259 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
|
|
260 |
| "Only XMLResource and BinaryResource supported"); |
|
261 |
| } |
|
262 |
| } catch (DBException e) { |
|
263 |
1
| throw FaultCodes.createXMLDBException(e.faultCode, e.getMessage(), e);
|
|
264 |
| } catch (XMLDBException e) { |
|
265 |
0
| throw e;
|
|
266 |
| } catch (Exception e) { |
|
267 |
0
| throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_RESOURCE,
|
|
268 |
| "Invalid resource: " + res.getId(), e); |
|
269 |
| } |
|
270 |
| } |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
2
| public void updateResource(Resource res) throws XMLDBException {
|
|
285 |
2
| if (res.getContent() == null) {
|
|
286 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "No resource data");
|
|
287 |
| } |
|
288 |
| |
|
289 |
2
| if (res.getId() == null) {
|
|
290 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Resource must have valid ID.");
|
|
291 |
| } |
|
292 |
| |
|
293 |
2
| checkOpen();
|
|
294 |
| |
|
295 |
2
| try {
|
|
296 |
2
| if (res instanceof BinaryResource) {
|
|
297 |
0
| byte[] bytes = getBinaryContent(res);
|
|
298 |
0
| col.updateBinary(res.getId(), bytes);
|
|
299 |
| |
|
300 |
2
| } else if (res instanceof XMLResource) {
|
|
301 |
2
| Node content = getXMLContent(res);
|
|
302 |
2
| col.updateDocument(res.getId(), (Document) content);
|
|
303 |
| |
|
304 |
| } else { |
|
305 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
|
|
306 |
| "Only XMLResource and BinaryResource supported"); |
|
307 |
| } |
|
308 |
| } catch (DBException e) { |
|
309 |
1
| throw FaultCodes.createXMLDBException(e.faultCode, e.getMessage(), e);
|
|
310 |
| } catch (XMLDBException e) { |
|
311 |
0
| throw e;
|
|
312 |
| } catch (Exception e) { |
|
313 |
0
| throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_RESOURCE,
|
|
314 |
| "Invalid resource: " + res.getId(), e); |
|
315 |
| } |
|
316 |
| } |
|
317 |
| |
|
318 |
4
| private byte[] getBinaryContent(Resource res) throws XMLDBException {
|
|
319 |
4
| Object content = res.getContent();
|
|
320 |
4
| byte[] bytes;
|
|
321 |
4
| if (content instanceof byte[]) {
|
|
322 |
4
| bytes = (byte[]) content;
|
|
323 |
| } else { |
|
324 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
|
|
325 |
| "The contents of a binary resource must have type byte[]."); |
|
326 |
| } |
|
327 |
4
| return bytes;
|
|
328 |
| } |
|
329 |
| |
|
330 |
406
| private Node getXMLContent(Resource res) throws XMLDBException {
|
|
331 |
406
| Node content = ((XMLResourceImpl) res).getContentAsDOM();
|
|
332 |
406
| if (content == null || !(content instanceof Document)) {
|
|
333 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
|
|
334 |
| "A resource must be a document in order to be stored."); |
|
335 |
| } |
|
336 |
406
| return content;
|
|
337 |
| } |
|
338 |
| |
|
339 |
| |
|
340 |
1598
| public boolean isOpen() {
|
|
341 |
1598
| return (col != null);
|
|
342 |
| } |
|
343 |
| |
|
344 |
| |
|
345 |
0
| public String getURI() {
|
|
346 |
0
| return "xmldb:" + DatabaseImpl.DRIVER_NAME + "://" + getCanonicalName();
|
|
347 |
| } |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
127
| public org.xmldb.api.base.Collection getChildCollection(String name) throws XMLDBException {
|
|
362 |
| |
|
363 |
127
| if (name.indexOf('/') != -1) {
|
|
364 |
1
| throw new XMLDBException(ErrorCodes.INVALID_COLLECTION,
|
|
365 |
| "Invalid collection: " + name); |
|
366 |
| } |
|
367 |
| |
|
368 |
126
| try {
|
|
369 |
126
| return new CollectionImpl(db, getCanonicalName() + "/" + name);
|
|
370 |
| } catch (XMLDBException e) { |
|
371 |
0
| if (e.errorCode == ErrorCodes.NO_SUCH_COLLECTION) {
|
|
372 |
| |
|
373 |
0
| return null;
|
|
374 |
| } |
|
375 |
0
| throw e;
|
|
376 |
| } catch (Exception e) { |
|
377 |
0
| throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_COLLECTION,
|
|
378 |
| "Invalid collection: " + name, e); |
|
379 |
| } |
|
380 |
| } |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
4
| public String createId() throws XMLDBException {
|
|
393 |
| |
|
394 |
4
| checkOpen();
|
|
395 |
4
| return col.createNewOID().toString();
|
|
396 |
| } |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
1
| public void close() throws XMLDBException {
|
|
410 |
1
| col = null;
|
|
411 |
| |
|
412 |
1
| db.flushConfig();
|
|
413 |
| } |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
4
| public org.xmldb.api.base.Collection getParentCollection() throws XMLDBException {
|
|
427 |
| |
|
428 |
| |
|
429 |
4
| if (collPath.lastIndexOf("/") == 0) {
|
|
430 |
1
| return null;
|
|
431 |
| } |
|
432 |
| |
|
433 |
3
| try {
|
|
434 |
3
| return new CollectionImpl(db, collPath.substring(0, collPath.lastIndexOf('/')));
|
|
435 |
| } catch (XMLDBException e) { |
|
436 |
0
| if (e.errorCode == ErrorCodes.NO_SUCH_COLLECTION) {
|
|
437 |
| |
|
438 |
0
| return null;
|
|
439 |
| } |
|
440 |
0
| throw e;
|
|
441 |
| } |
|
442 |
| } |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
153
| public void removeResource(Resource res) throws XMLDBException {
|
|
459 |
| |