1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.xml.dom; |
21 |
| |
22 |
| import org.apache.commons.logging.Log; |
23 |
| import org.apache.commons.logging.LogFactory; |
24 |
| import org.apache.xindice.core.data.Value; |
25 |
| import org.apache.xindice.util.ObjectStack; |
26 |
| import org.apache.xindice.util.XindiceException; |
27 |
| |
28 |
| import org.w3c.dom.Document; |
29 |
| import org.w3c.dom.Element; |
30 |
| import org.w3c.dom.Node; |
31 |
| import org.xml.sax.Attributes; |
32 |
| import org.xml.sax.EntityResolver; |
33 |
| import org.xml.sax.ErrorHandler; |
34 |
| import org.xml.sax.InputSource; |
35 |
| import org.xml.sax.Locator; |
36 |
| import org.xml.sax.SAXException; |
37 |
| import org.xml.sax.SAXNotRecognizedException; |
38 |
| import org.xml.sax.SAXNotSupportedException; |
39 |
| import org.xml.sax.SAXParseException; |
40 |
| import org.xml.sax.XMLReader; |
41 |
| import org.xml.sax.ext.DeclHandler; |
42 |
| import org.xml.sax.ext.LexicalHandler; |
43 |
| import org.xml.sax.helpers.DefaultHandler; |
44 |
| |
45 |
| import javax.xml.parsers.SAXParser; |
46 |
| import javax.xml.parsers.SAXParserFactory; |
47 |
| |
48 |
| import java.io.ByteArrayInputStream; |
49 |
| import java.io.IOException; |
50 |
| import java.io.InputStream; |
51 |
| import java.io.Reader; |
52 |
| import java.io.StringReader; |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| public final class DOMParser extends DefaultHandler |
60 |
| implements DeclHandler, LexicalHandler { |
61 |
| |
62 |
| private static final Log log = LogFactory.getLog(DOMParser.class); |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| private static final SAXParserFactory factory = SAXParserFactory.newInstance(); |
68 |
| |
69 |
| private SAXParser sp; |
70 |
| private ErrorHandler errors; |
71 |
| private EntityResolver entities; |
72 |
| |
73 |
| private Document doc; |
74 |
| private Node context; |
75 |
| private int state = -1; |
76 |
| private StringBuffer buf; |
77 |
| private final ObjectStack states = new ObjectStack(); |
78 |
| |
79 |
| |
80 |
23718
| public DOMParser() throws XindiceException {
|
81 |
23718
| try {
|
82 |
23718
| sp = getSAXParser();
|
83 |
| } catch (Exception e) { |
84 |
0
| log.warn("Failed to create SAXParser", e);
|
85 |
0
| throw new XindiceException("Error creating parser", e);
|
86 |
| } |
87 |
| } |
88 |
| |
89 |
0
| public void setErrorHandler(ErrorHandler errors) throws SAXException {
|
90 |
0
| this.errors = errors;
|
91 |
0
| if (sp != null) {
|
92 |
0
| XMLReader xr = sp.getXMLReader();
|
93 |
0
| xr.setErrorHandler(errors);
|
94 |
| } |
95 |
| } |
96 |
| |
97 |
0
| public void setEntityResolver(EntityResolver entities) throws SAXException {
|
98 |
0
| this.entities = entities;
|
99 |
0
| if (sp != null) {
|
100 |
0
| XMLReader xr = sp.getXMLReader();
|
101 |
0
| xr.setEntityResolver(entities);
|
102 |
| } |
103 |
| } |
104 |
| |
105 |
20564
| public void parse(String xml) throws SAXException, IOException {
|
106 |
20564
| parse(new StringReader(xml));
|
107 |
| } |
108 |
| |
109 |
20564
| public void parse(Reader input) throws SAXException, IOException {
|
110 |
20564
| parse(new InputSource(input));
|
111 |
| } |
112 |
| |
113 |
4
| public void parse(byte[] xml) throws SAXException, IOException {
|
114 |
4
| parse(new ByteArrayInputStream(xml));
|
115 |
| } |
116 |
| |
117 |
3143
| public void parse(Value value) throws SAXException, IOException {
|
118 |
3143
| parse(value.getInputStream());
|
119 |
| } |
120 |
| |
121 |
20564
| public void parse(InputSource source) throws SAXException, IOException {
|
122 |
20564
| sp.parse(source, this);
|
123 |
| } |
124 |
| |
125 |
3154
| public void parse(InputStream input) throws SAXException, IOException {
|
126 |
3154
| sp.parse(input, this);
|
127 |
| } |
128 |
| |
129 |
0
| public static Document toDocument(Reader input) throws XindiceException {
|
130 |
0
| try {
|
131 |
0
| DOMParser dp = new DOMParser();
|
132 |
0
| dp.parse(input);
|
133 |
0
| return dp.getDocument();
|
134 |
| } catch (Exception e) { |
135 |
0
| throw new XindiceException("Error parsing Document", e);
|
136 |
| } |
137 |
| } |
138 |
| |
139 |
4
| public static Document toDocument(byte[] xml) throws XindiceException {
|
140 |
4
| try {
|
141 |
4
| DOMParser dp = new DOMParser();
|
142 |
4
| dp.parse(xml);
|
143 |
2
| return dp.getDocument();
|
144 |
| } catch (Exception e) { |
145 |
2
| throw new XindiceException("Error parsing Document", e);
|
146 |
| } |
147 |
| } |
148 |
| |
149 |
3143
| public static Document toDocument(Value value) throws XindiceException {
|
150 |
3143
| try {
|
151 |
3143
| DOMParser dp = new DOMParser();
|
152 |
3143
| dp.parse(value);
|
153 |
3143
| return dp.getDocument();
|
154 |
| } catch (Exception e) { |
155 |
0
| throw new XindiceException("Error parsing Document", e);
|
156 |
| } |
157 |
| } |
158 |
| |
159 |
0
| public static Document toDocument(InputSource source) throws XindiceException {
|
160 |
0
| try {
|
161 |
0
| DOMParser dp = new DOMParser();
|
162 |
0
| dp.parse(source);
|
163 |
0
| return dp.getDocument();
|
164 |
| } catch (Exception e) { |
165 |
0
| throw new XindiceException("Error parsing Document", e);
|
166 |
| } |
167 |
| } |
168 |
| |
169 |
7
| public static Document toDocument(InputStream input) throws XindiceException {
|
170 |
7
| try {
|
171 |
7
| DOMParser dp = new DOMParser();
|
172 |
7
| dp.parse(input);
|
173 |
7
| return dp.getDocument();
|
174 |
| } catch (Exception e) { |
175 |
0
| throw new XindiceException("Error parsing Document", e);
|
176 |
| } |
177 |
| } |
178 |
| |
179 |
20564
| public static Document toDocument(String xml) throws XindiceException {
|
180 |
20564
| try {
|
181 |
20564
| DOMParser dp = new DOMParser();
|
182 |
20564
| dp.parse(xml);
|
183 |
20564
| return dp.getDocument();
|
184 |
| } catch (Exception e) { |
185 |
0
| throw new XindiceException("Error parsing Document", e);
|
186 |
| } |
187 |
| } |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
23716
| public Document getDocument() {
|
194 |
23716
| return doc;
|
195 |
| } |
196 |
| |
197 |
23718
| private SAXParser getSAXParser() throws Exception {
|
198 |
23718
| final SAXParser sp = factory.newSAXParser();
|
199 |
23718
| final XMLReader xr = sp.getXMLReader();
|
200 |
23718
| setFeature(xr, "http://xml.org/sax/features/namespaces", true);
|
201 |
23718
| setFeature(xr, "http://xml.org/sax/features/validation", false);
|
202 |
23718
| setFeature(xr, "http://xml.org/sax/features/external-general-entities", false);
|
203 |
23718
| setFeature(xr, "http://xml.org/sax/features/external-parameter-entities", false);
|
204 |
23718
| setFeature(xr, "http://xml.org/sax/features/namespace-prefixes", true);
|
205 |
23718
| xr.setProperty("http://xml.org/sax/properties/lexical-handler", this);
|
206 |
23718
| xr.setProperty("http://xml.org/sax/properties/declaration-handler", this);
|
207 |
23718
| if (errors != null) {
|
208 |
0
| xr.setErrorHandler(errors);
|
209 |
| } |
210 |
23718
| if (entities != null) {
|
211 |
0
| xr.setEntityResolver(entities);
|
212 |
| } |
213 |
| |
214 |
23718
| return sp;
|
215 |
| } |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
118590
| private void setFeature(XMLReader xr, String name, boolean value)
|
229 |
| throws SAXNotRecognizedException, SAXNotSupportedException { |
230 |
118590
| try {
|
231 |
118590
| xr.setFeature(name, value);
|
232 |
| } catch (SAXNotSupportedException e) { |
233 |
0
| if (value) {
|
234 |
0
| throw e;
|
235 |
0
| } else if (log.isWarnEnabled()) {
|
236 |
0
| log.warn("SAX feature " + name + " is not supported");
|
237 |
| } |
238 |
| } |
239 |
| } |
240 |
| |
241 |
137443
| private void pushState(int newState) {
|
242 |
137443
| Integer i = new Integer(state);
|
243 |
137443
| states.push(i);
|
244 |
137443
| state = newState;
|
245 |
| } |
246 |
| |
247 |
137441
| private void popState() {
|
248 |
137441
| Integer i = (Integer) states.pop();
|
249 |
137441
| state = i != null ? i.intValue() : -1;
|
250 |
| } |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
0
| public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
|
256 |
0
| return null;
|
257 |
| } |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
0
| public void notationDecl(String name, String publicId, String systemId) throws SAXException {
|
263 |
| } |
264 |
| |
265 |
0
| public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName) throws SAXException {
|
266 |
| } |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
23718
| public void setDocumentLocator(Locator locator) {
|
272 |
| } |
273 |
| |
274 |
23718
| public void startDocument() throws SAXException {
|
275 |
23718
| doc = new DocumentImpl();
|
276 |
23718
| context = doc;
|
277 |
23718
| pushState(Node.DOCUMENT_NODE);
|
278 |
| } |
279 |
| |
280 |
23716
| public void endDocument() throws SAXException {
|
281 |
23716
| context = null;
|
282 |
23716
| popState();
|
283 |
| } |
284 |
| |
285 |
491
| public void startPrefixMapping(String prefix, String uri) throws SAXException {
|
286 |
| } |
287 |
| |
288 |
113697
| public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
289 |
113697
| Element e;
|
290 |
113697
| if (uri != null && uri.length() > 0) {
|
291 |
623
| e = doc.createElementNS(uri, qName);
|
292 |
| } else { |
293 |
113074
| e = doc.createElement(localName);
|
294 |
| } |
295 |
| |
296 |
113697
| for (int i = 0; i < attributes.getLength(); i++) {
|
297 |
83086
| String attrURI = attributes.getURI(i);
|
298 |
83086
| String attrQName = attributes.getQName(i);
|
299 |
83086
| String attrValue = attributes.getValue(i);
|
300 |
| |
301 |
83086
| boolean isNsDef = attrQName != null && attrQName.startsWith("xmlns")
|
302 |
| && (attrQName.length() == 5 || attrQName.charAt(5) == ':'); |
303 |
| |
304 |
83086
| if (isNsDef || (attrURI != null && attrURI.length() > 0)) {
|
305 |
977
| e.setAttributeNS(attrURI, attrQName, attrValue);
|
306 |
| } else { |
307 |
82109
| e.setAttribute(attributes.getLocalName(i), attrValue);
|
308 |
| } |
309 |
| } |
310 |
| |
311 |
113697
| context.appendChild(e);
|
312 |
113697
| pushState(Node.ELEMENT_NODE);
|
313 |
113697
| context = e;
|
314 |
| } |
315 |
| |
316 |
113697
| public void endElement(String uri, String localName, String qName) throws SAXException {
|
317 |
113697
| context = context.getParentNode();
|
318 |
113697
| popState();
|
319 |
| } |
320 |
| |
321 |
118908
| public void characters(char ch[], int start, int length) throws SAXException {
|
322 |
118908
| switch (state) {
|
323 |
28
| case Node.CDATA_SECTION_NODE:
|
324 |
28
| buf.append(ch, start, length);
|
325 |
28
| break;
|
326 |
| |
327 |
118880
| default:
|
328 |
118880
| String s = new String(ch, start, length);
|
329 |
118880
| context.appendChild(doc.createTextNode(s));
|
330 |
| } |
331 |
| } |
332 |
| |
333 |
0
| public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
|
334 |
0
| switch (state) {
|
335 |
0
| case Node.CDATA_SECTION_NODE:
|
336 |
0
| buf.append(ch, start, length);
|
337 |
0
| break;
|
338 |
| |
339 |
0
| default:
|
340 |
0
| String s = new String(ch, start, length);
|
341 |
0
| context.appendChild(doc.createTextNode(s));
|
342 |
| } |
343 |
| } |
344 |
| |
345 |
0
| public void processingInstruction(String target, String data) throws SAXException {
|
346 |
0
| context.appendChild(doc.createProcessingInstruction(target, data));
|
347 |
| } |
348 |
| |
349 |
0
| public void skippedEntity(String name) throws SAXException {
|
350 |
| } |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
0
| public void warning(SAXParseException e) throws SAXException {
|
356 |
0
| log.info("Parsing warning (ignored)", e);
|
357 |
| } |
358 |
| |
359 |
0
| public void error(SAXParseException e) throws SAXException {
|
360 |
0
| log.info("Parsing error (ignored)", e);
|
361 |
| } |
362 |
| |
363 |
2
| public void fatalError(SAXParseException e) throws SAXException {
|
364 |
2
| throw e;
|
365 |
| } |
366 |
| |
367 |
| |
368 |
| |
369 |
| |
370 |
0
| public void elementDecl(String name, String model) throws SAXException {
|
371 |
| } |
372 |
| |
373 |
0
| public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) throws SAXException {
|
374 |
| } |
375 |
| |
376 |
0
| public void internalEntityDecl(String name, String value) throws SAXException {
|
377 |
| } |
378 |
| |
379 |
0
| public void externalEntityDecl(String name, String publicId, String systemId) throws SAXException {
|
380 |
| } |
381 |
| |
382 |
| |
383 |
| |
384 |
| |
385 |
0
| public void startDTD(String name, String publicId, String systemId) throws SAXException {
|
386 |
| } |
387 |
| |
388 |
0
| public void endDTD() throws SAXException {
|
389 |
| } |
390 |
| |
391 |
132
| public void startEntity(String name) throws SAXException {
|
392 |
| } |
393 |
| |
394 |
132
| public void endEntity(String name) throws SAXException {
|
395 |
| } |
396 |
| |
397 |
28
| public void startCDATA() throws SAXException {
|
398 |
28
| pushState(Node.CDATA_SECTION_NODE);
|
399 |
28
| buf = new StringBuffer();
|
400 |
| } |
401 |
| |
402 |
28
| public void endCDATA() throws SAXException {
|
403 |
28
| context.appendChild(doc.createCDATASection(buf.toString()));
|
404 |
28
| buf = null;
|
405 |
28
| popState();
|
406 |
| } |
407 |
| |
408 |
469
| public void comment(char ch[], int start, int length) throws SAXException {
|
409 |
469
| String s = new String(ch, start, length);
|
410 |
469
| context.appendChild(doc.createComment(s));
|
411 |
| } |
412 |
| } |