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.resources; |
21 |
| |
22 |
| import org.apache.xindice.core.FaultCodes; |
23 |
| import org.apache.xindice.xml.SymbolTable; |
24 |
| import org.apache.xindice.xml.TextWriter; |
25 |
| import org.apache.xindice.xml.dom.DOMParser; |
26 |
| import org.apache.xindice.xml.dom.DocumentImpl; |
27 |
| import org.apache.xindice.xml.sax.SAXEventGenerator; |
28 |
| import org.apache.xindice.xml.sax.SetContentHandler; |
29 |
| |
30 |
| import org.w3c.dom.Node; |
31 |
| import org.xml.sax.ContentHandler; |
32 |
| import org.xml.sax.ErrorHandler; |
33 |
| import org.xml.sax.InputSource; |
34 |
| import org.xml.sax.XMLReader; |
35 |
| import org.xmldb.api.base.ErrorCodes; |
36 |
| import org.xmldb.api.base.XMLDBException; |
37 |
| |
38 |
| import javax.xml.parsers.SAXParser; |
39 |
| import javax.xml.parsers.SAXParserFactory; |
40 |
| import java.io.StringReader; |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| public class XMLResourceImpl implements org.xmldb.api.modules.XMLResource { |
49 |
| |
50 |
| private static final SAXParserFactory saxFactory; |
51 |
| |
52 |
| static { |
53 |
3
| saxFactory = SAXParserFactory.newInstance();
|
54 |
3
| saxFactory.setNamespaceAware(true);
|
55 |
| } |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| public static final String SAX_NAMESPACES_FEATURE |
64 |
| = SAXEventGenerator.SAX_NAMESPACES_FEATURE; |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| public static final String SAX_NAMESPACE_PREFIXES_FEATURE |
73 |
| = SAXEventGenerator.SAX_NAMESPACE_PREFIXES_FEATURE; |
74 |
| |
75 |
| |
76 |
| protected String id; |
77 |
| protected String documentId; |
78 |
| protected org.xmldb.api.base.Collection collection; |
79 |
| protected String content; |
80 |
| private SymbolTable symbols; |
81 |
| private byte[] bytes; |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| private boolean hasSaxNamespaces = true; |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| private boolean hasSaxNamespacesPrefixes; |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
1233
| public XMLResourceImpl(String id, org.xmldb.api.base.Collection collection) {
|
107 |
1233
| this(id, id, collection, null);
|
108 |
| } |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
0
| public XMLResourceImpl(String id, String documentId,
|
118 |
| org.xmldb.api.base.Collection collection) { |
119 |
0
| this(id, documentId, collection, null);
|
120 |
| } |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
0
| public XMLResourceImpl(String id, org.xmldb.api.base.Collection collection,
|
130 |
| String content) { |
131 |
0
| this(id, id, collection, content);
|
132 |
| } |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
1933
| public XMLResourceImpl(String id, String documentId,
|
143 |
| org.xmldb.api.base.Collection collection, String content) { |
144 |
1933
| this.collection = collection;
|
145 |
1933
| this.id = id;
|
146 |
1933
| this.documentId = documentId;
|
147 |
1933
| this.content = content;
|
148 |
| } |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
338
| public XMLResourceImpl(String id, String documentId,
|
161 |
| org.xmldb.api.base.Collection collection, SymbolTable syms, byte[] bytes) { |
162 |
338
| this.collection = collection;
|
163 |
338
| this.id = id;
|
164 |
338
| this.documentId = documentId;
|
165 |
338
| this.symbols = syms;
|
166 |
338
| this.bytes = bytes;
|
167 |
| } |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
0
| public XMLResourceImpl(String id, org.xmldb.api.base.Collection collection,
|
179 |
| SymbolTable syms, byte[] bytes) { |
180 |
0
| this(id, id, collection, syms, bytes);
|
181 |
| } |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
0
| public void setSAXFeature(String feature, boolean value) {
|
193 |
| |
194 |
0
| if (SAX_NAMESPACES_FEATURE.equals(feature)) {
|
195 |
| |
196 |
0
| hasSaxNamespaces = value;
|
197 |
0
| } else if (SAX_NAMESPACE_PREFIXES_FEATURE.equals(feature)) {
|
198 |
| |
199 |
0
| hasSaxNamespacesPrefixes = value;
|
200 |
| } |
201 |
| } |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
0
| public boolean getSAXFeature(String feature) {
|
214 |
| |
215 |
0
| if (SAX_NAMESPACES_FEATURE.equals(feature)) {
|
216 |
| |
217 |
0
| return hasSaxNamespaces;
|
218 |
0
| } else if (SAX_NAMESPACE_PREFIXES_FEATURE.equals(feature)) {
|
219 |
| |
220 |
0
| return hasSaxNamespacesPrefixes;
|
221 |
| } else { |
222 |
| |
223 |
0
| return false;
|
224 |
| } |
225 |
| } |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
60
| public String getDocumentId() throws XMLDBException {
|
236 |
60
| return documentId;
|
237 |
| } |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
1233
| public void setContent(Object value) throws XMLDBException {
|
247 |
1233
| if (value == null) {
|
248 |
3
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE);
|
249 |
1230
| } else if (value instanceof String) {
|
250 |
1230
| this.content = (String) value;
|
251 |
1230
| this.bytes = null;
|
252 |
| } else { |
253 |
0
| throw new XMLDBException(ErrorCodes.WRONG_CONTENT_TYPE);
|
254 |
| } |
255 |
| } |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
3
| public void setContentAsDOM(Node content) throws XMLDBException {
|
264 |
3
| if (content != null) {
|
265 |
3
| this.content = TextWriter.toString(content);
|
266 |
3
| this.bytes = null;
|
267 |
| } else { |
268 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE);
|
269 |
| } |
270 |
| } |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
| |
276 |
| |
277 |
| |
278 |
| |
279 |
0
| public ContentHandler setContentAsSAX() throws XMLDBException {
|
280 |
0
| return new SetContentHandler(this);
|
281 |
| } |
282 |
| |
283 |
| |
284 |
| |
285 |
| |
286 |
| |
287 |
| |
288 |
| |
289 |
0
| public org.xmldb.api.base.Collection getParentCollection() throws XMLDBException {
|
290 |
0
| return collection;
|
291 |
| } |
292 |
| |
293 |
| |
294 |
| |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
0
| public String getResourceType() throws XMLDBException {
|
304 |
0
| return RESOURCE_TYPE;
|
305 |
| } |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
| |
312 |
| |
313 |
3405
| public String getId() throws XMLDBException {
|
314 |
3405
| return id;
|
315 |
| } |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
1674
| public Object getContent() throws XMLDBException {
|
324 |
1674
| if (bytes != null) {
|
325 |
20
| DocumentImpl doc = new DocumentImpl(bytes, symbols, null);
|
326 |
20
| return TextWriter.toString(doc);
|
327 |
| } else { |
328 |
1654
| return content;
|
329 |
| } |
330 |
| } |
331 |
| |
332 |
| |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
1059
| public Node getContentAsDOM() throws XMLDBException {
|
339 |
1059
| try {
|
340 |
1059
| if (bytes != null) {
|
341 |
0
| return new DocumentImpl(bytes, symbols, null);
|
342 |
| } else { |
343 |
1059
| return DOMParser.toDocument(content);
|
344 |
| } |
345 |
| } catch (Exception e) { |
346 |
0
| throw FaultCodes.createXMLDBException(e);
|
347 |
| } |
348 |
| } |
349 |
| |
350 |
| |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
| |
356 |
| |
357 |
12
| public void getContentAsSAX(ContentHandler handler) throws XMLDBException {
|
358 |
| |
359 |
12
| try {
|
360 |
12
| if (bytes != null) {
|
361 |
8
| SAXEventGenerator events = new SAXEventGenerator(symbols, bytes);
|
362 |
| |
363 |
8
| events.setFeature(SAX_NAMESPACES_FEATURE, hasSaxNamespaces);
|
364 |
8
| events.setFeature(SAX_NAMESPACE_PREFIXES_FEATURE, hasSaxNamespacesPrefixes);
|
365 |
8
| events.setContentHandler(handler);
|
366 |
8
| if (handler instanceof ErrorHandler) {
|
367 |
0
| events.setErrorHandler((ErrorHandler) handler);
|
368 |
| } |
369 |
| |
370 |
8
| events.start();
|
371 |
4
| } else if (content != null) {
|
372 |
4
| SAXParser sp = saxFactory.newSAXParser();
|
373 |
4
| XMLReader xr = sp.getXMLReader();
|
374 |
| |
375 |
4
| xr.setFeature(SAX_NAMESPACES_FEATURE, hasSaxNamespaces);
|
376 |
4
| xr.setFeature(SAX_NAMESPACE_PREFIXES_FEATURE, hasSaxNamespacesPrefixes);
|
377 |
4
| xr.setContentHandler(handler);
|
378 |
4
| if (handler instanceof ErrorHandler) {
|
379 |
0
| xr.setErrorHandler((ErrorHandler) handler);
|
380 |
| } |
381 |
| |
382 |
4
| xr.parse(new InputSource(new StringReader(content)));
|
383 |
| } |
384 |
| } catch (Exception e) { |
385 |
0
| throw FaultCodes.createXMLDBException(e);
|
386 |
| } |
387 |
| } |
388 |
| |
389 |
409
| public void setId(String name) {
|
390 |
409
| this.id = name;
|
391 |
| } |
392 |
| } |