blob: 0ef4d6fca16ecf832f10c4e2c076491dd5989d26 [file] [log] [blame]
Rahul Ravikumar05336002019-10-14 15:04:32 -07001// XMLReaderAdapter.java - adapt an SAX2 XMLReader to a SAX1 Parser
2// http://www.saxproject.org
3// Written by David Megginson
4// NO WARRANTY! This class is in the public domain.
5// $Id: XMLReaderAdapter.java,v 1.9 2004/04/26 17:34:35 dmegginson Exp $
6
7package org.xml.sax.helpers;
8
9import dalvik.annotation.compat.UnsupportedAppUsage;
10import java.io.IOException;
11import java.util.Locale;
12import org.xml.sax.AttributeList;
13import org.xml.sax.Attributes;
14import org.xml.sax.ContentHandler;
15import org.xml.sax.DTDHandler;
16import org.xml.sax.DocumentHandler;
17import org.xml.sax.EntityResolver;
18import org.xml.sax.ErrorHandler;
19import org.xml.sax.InputSource;
20import org.xml.sax.Locator;
21import org.xml.sax.Parser;
22import org.xml.sax.SAXException;
23import org.xml.sax.SAXNotSupportedException;
24import org.xml.sax.XMLReader;
25
26
27/**
28 * Adapt a SAX2 XMLReader as a SAX1 Parser.
29 *
30 * <blockquote>
31 * <em>This module, both source code and documentation, is in the
32 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
33 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
34 * for further information.
35 * </blockquote>
36 *
37 * <p>This class wraps a SAX2 {@link org.xml.sax.XMLReader XMLReader}
38 * and makes it act as a SAX1 {@link org.xml.sax.Parser Parser}. The XMLReader
39 * must support a true value for the
40 * http://xml.org/sax/features/namespace-prefixes property or parsing will fail
41 * with a {@link org.xml.sax.SAXException SAXException}; if the XMLReader
42 * supports a false value for the http://xml.org/sax/features/namespaces
43 * property, that will also be used to improve efficiency.</p>
44 *
45 * @since SAX 2.0
46 * @author David Megginson
47 * @version 2.0.1 (sax2r2)
48 * @see org.xml.sax.Parser
49 * @see org.xml.sax.XMLReader
50 */
51public class XMLReaderAdapter implements Parser, ContentHandler
52{
53
54
55 ////////////////////////////////////////////////////////////////////
56 // Constructor.
57 ////////////////////////////////////////////////////////////////////
58
59
60 /**
61 * Create a new adapter.
62 *
63 * <p>Use the "org.xml.sax.driver" property to locate the SAX2
64 * driver to embed.</p>
65 *
66 * @exception org.xml.sax.SAXException If the embedded driver
67 * cannot be instantiated or if the
68 * org.xml.sax.driver property is not specified.
69 */
70 public XMLReaderAdapter ()
71 throws SAXException
72 {
73 setup(XMLReaderFactory.createXMLReader());
74 }
75
76
77 /**
78 * Create a new adapter.
79 *
80 * <p>Create a new adapter, wrapped around a SAX2 XMLReader.
81 * The adapter will make the XMLReader act like a SAX1
82 * Parser.</p>
83 *
84 * @param xmlReader The SAX2 XMLReader to wrap.
85 * @exception java.lang.NullPointerException If the argument is null.
86 */
87 public XMLReaderAdapter (XMLReader xmlReader)
88 {
89 setup(xmlReader);
90 }
91
92
93
94 /**
95 * Internal setup.
96 *
97 * @param xmlReader The embedded XMLReader.
98 */
99 @UnsupportedAppUsage
100 private void setup (XMLReader xmlReader)
101 {
102 if (xmlReader == null) {
103 throw new NullPointerException("XMLReader must not be null");
104 }
105 this.xmlReader = xmlReader;
106 qAtts = new AttributesAdapter();
107 }
108
109
110
111 ////////////////////////////////////////////////////////////////////
112 // Implementation of org.xml.sax.Parser.
113 ////////////////////////////////////////////////////////////////////
114
115
116 /**
117 * Set the locale for error reporting.
118 *
119 * <p>This is not supported in SAX2, and will always throw
120 * an exception.</p>
121 *
122 * @param locale the locale for error reporting.
123 * @see org.xml.sax.Parser#setLocale
124 * @exception org.xml.sax.SAXException Thrown unless overridden.
125 */
126 public void setLocale (Locale locale)
127 throws SAXException
128 {
129 throw new SAXNotSupportedException("setLocale not supported");
130 }
131
132
133 /**
134 * Register the entity resolver.
135 *
136 * @param resolver The new resolver.
137 * @see org.xml.sax.Parser#setEntityResolver
138 */
139 public void setEntityResolver (EntityResolver resolver)
140 {
141 xmlReader.setEntityResolver(resolver);
142 }
143
144
145 /**
146 * Register the DTD event handler.
147 *
148 * @param handler The new DTD event handler.
149 * @see org.xml.sax.Parser#setDTDHandler
150 */
151 public void setDTDHandler (DTDHandler handler)
152 {
153 xmlReader.setDTDHandler(handler);
154 }
155
156
157 /**
158 * Register the SAX1 document event handler.
159 *
160 * <p>Note that the SAX1 document handler has no Namespace
161 * support.</p>
162 *
163 * @param handler The new SAX1 document event handler.
164 * @see org.xml.sax.Parser#setDocumentHandler
165 */
166 public void setDocumentHandler (DocumentHandler handler)
167 {
168 documentHandler = handler;
169 }
170
171
172 /**
173 * Register the error event handler.
174 *
175 * @param handler The new error event handler.
176 * @see org.xml.sax.Parser#setErrorHandler
177 */
178 public void setErrorHandler (ErrorHandler handler)
179 {
180 xmlReader.setErrorHandler(handler);
181 }
182
183
184 /**
185 * Parse the document.
186 *
187 * <p>This method will throw an exception if the embedded
188 * XMLReader does not support the
189 * http://xml.org/sax/features/namespace-prefixes property.</p>
190 *
191 * @param systemId The absolute URL of the document.
192 * @exception java.io.IOException If there is a problem reading
193 * the raw content of the document.
194 * @exception org.xml.sax.SAXException If there is a problem
195 * processing the document.
196 * @see #parse(org.xml.sax.InputSource)
197 * @see org.xml.sax.Parser#parse(java.lang.String)
198 */
199 public void parse (String systemId)
200 throws IOException, SAXException
201 {
202 parse(new InputSource(systemId));
203 }
204
205
206 /**
207 * Parse the document.
208 *
209 * <p>This method will throw an exception if the embedded
210 * XMLReader does not support the
211 * http://xml.org/sax/features/namespace-prefixes property.</p>
212 *
213 * @param input An input source for the document.
214 * @exception java.io.IOException If there is a problem reading
215 * the raw content of the document.
216 * @exception org.xml.sax.SAXException If there is a problem
217 * processing the document.
218 * @see #parse(java.lang.String)
219 * @see org.xml.sax.Parser#parse(org.xml.sax.InputSource)
220 */
221 public void parse (InputSource input)
222 throws IOException, SAXException
223 {
224 setupXMLReader();
225 xmlReader.parse(input);
226 }
227
228
229 /**
230 * Set up the XML reader.
231 */
232 @UnsupportedAppUsage
233 private void setupXMLReader ()
234 throws SAXException
235 {
236 xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
237 try {
238 xmlReader.setFeature("http://xml.org/sax/features/namespaces",
239 false);
240 } catch (SAXException e) {
241 // NO OP: it's just extra information, and we can ignore it
242 }
243 xmlReader.setContentHandler(this);
244 }
245
246
247
248 ////////////////////////////////////////////////////////////////////
249 // Implementation of org.xml.sax.ContentHandler.
250 ////////////////////////////////////////////////////////////////////
251
252
253 /**
254 * Set a document locator.
255 *
256 * @param locator The document locator.
257 * @see org.xml.sax.ContentHandler#setDocumentLocator
258 */
259 public void setDocumentLocator (Locator locator)
260 {
261 if (documentHandler != null)
262 documentHandler.setDocumentLocator(locator);
263 }
264
265
266 /**
267 * Start document event.
268 *
269 * @exception org.xml.sax.SAXException The client may raise a
270 * processing exception.
271 * @see org.xml.sax.ContentHandler#startDocument
272 */
273 public void startDocument ()
274 throws SAXException
275 {
276 if (documentHandler != null)
277 documentHandler.startDocument();
278 }
279
280
281 /**
282 * End document event.
283 *
284 * @exception org.xml.sax.SAXException The client may raise a
285 * processing exception.
286 * @see org.xml.sax.ContentHandler#endDocument
287 */
288 public void endDocument ()
289 throws SAXException
290 {
291 if (documentHandler != null)
292 documentHandler.endDocument();
293 }
294
295
296 /**
297 * Adapt a SAX2 start prefix mapping event.
298 *
299 * @param prefix The prefix being mapped.
300 * @param uri The Namespace URI being mapped to.
301 * @see org.xml.sax.ContentHandler#startPrefixMapping
302 */
303 public void startPrefixMapping (String prefix, String uri)
304 {
305 }
306
307
308 /**
309 * Adapt a SAX2 end prefix mapping event.
310 *
311 * @param prefix The prefix being mapped.
312 * @see org.xml.sax.ContentHandler#endPrefixMapping
313 */
314 public void endPrefixMapping (String prefix)
315 {
316 }
317
318
319 /**
320 * Adapt a SAX2 start element event.
321 *
322 * @param uri The Namespace URI.
323 * @param localName The Namespace local name.
324 * @param qName The qualified (prefixed) name.
325 * @param atts The SAX2 attributes.
326 * @exception org.xml.sax.SAXException The client may raise a
327 * processing exception.
328 * @see org.xml.sax.ContentHandler#endDocument
329 */
330 public void startElement (String uri, String localName,
331 String qName, Attributes atts)
332 throws SAXException
333 {
334 if (documentHandler != null) {
335 qAtts.setAttributes(atts);
336 documentHandler.startElement(qName, qAtts);
337 }
338 }
339
340
341 /**
342 * Adapt a SAX2 end element event.
343 *
344 * @param uri The Namespace URI.
345 * @param localName The Namespace local name.
346 * @param qName The qualified (prefixed) name.
347 * @exception org.xml.sax.SAXException The client may raise a
348 * processing exception.
349 * @see org.xml.sax.ContentHandler#endElement
350 */
351 public void endElement (String uri, String localName,
352 String qName)
353 throws SAXException
354 {
355 if (documentHandler != null)
356 documentHandler.endElement(qName);
357 }
358
359
360 /**
361 * Adapt a SAX2 characters event.
362 *
363 * @param ch An array of characters.
364 * @param start The starting position in the array.
365 * @param length The number of characters to use.
366 * @exception org.xml.sax.SAXException The client may raise a
367 * processing exception.
368 * @see org.xml.sax.ContentHandler#characters
369 */
370 public void characters (char ch[], int start, int length)
371 throws SAXException
372 {
373 if (documentHandler != null)
374 documentHandler.characters(ch, start, length);
375 }
376
377
378 /**
379 * Adapt a SAX2 ignorable whitespace event.
380 *
381 * @param ch An array of characters.
382 * @param start The starting position in the array.
383 * @param length The number of characters to use.
384 * @exception org.xml.sax.SAXException The client may raise a
385 * processing exception.
386 * @see org.xml.sax.ContentHandler#ignorableWhitespace
387 */
388 public void ignorableWhitespace (char ch[], int start, int length)
389 throws SAXException
390 {
391 if (documentHandler != null)
392 documentHandler.ignorableWhitespace(ch, start, length);
393 }
394
395
396 /**
397 * Adapt a SAX2 processing instruction event.
398 *
399 * @param target The processing instruction target.
400 * @param data The remainder of the processing instruction
401 * @exception org.xml.sax.SAXException The client may raise a
402 * processing exception.
403 * @see org.xml.sax.ContentHandler#processingInstruction
404 */
405 public void processingInstruction (String target, String data)
406 throws SAXException
407 {
408 if (documentHandler != null)
409 documentHandler.processingInstruction(target, data);
410 }
411
412
413 /**
414 * Adapt a SAX2 skipped entity event.
415 *
416 * @param name The name of the skipped entity.
417 * @see org.xml.sax.ContentHandler#skippedEntity
418 * @exception org.xml.sax.SAXException Throwable by subclasses.
419 */
420 public void skippedEntity (String name)
421 throws SAXException
422 {
423 }
424
425
426
427 ////////////////////////////////////////////////////////////////////
428 // Internal state.
429 ////////////////////////////////////////////////////////////////////
430
431 @UnsupportedAppUsage
432 XMLReader xmlReader;
433 @UnsupportedAppUsage
434 DocumentHandler documentHandler;
435 @UnsupportedAppUsage
436 AttributesAdapter qAtts;
437
438
439
440 ////////////////////////////////////////////////////////////////////
441 // Internal class.
442 ////////////////////////////////////////////////////////////////////
443
444
445 /**
446 * Internal class to wrap a SAX2 Attributes object for SAX1.
447 */
448 static final class AttributesAdapter implements AttributeList
449 {
450 AttributesAdapter ()
451 {
452 }
453
454
455 /**
456 * Set the embedded Attributes object.
457 *
458 * @param The embedded SAX2 Attributes.
459 */
460 void setAttributes (Attributes attributes)
461 {
462 this.attributes = attributes;
463 }
464
465
466 /**
467 * Return the number of attributes.
468 *
469 * @return The length of the attribute list.
470 * @see org.xml.sax.AttributeList#getLength
471 */
472 public int getLength ()
473 {
474 return attributes.getLength();
475 }
476
477
478 /**
479 * Return the qualified (prefixed) name of an attribute by position.
480 *
481 * @return The qualified name.
482 * @see org.xml.sax.AttributeList#getName
483 */
484 public String getName (int i)
485 {
486 return attributes.getQName(i);
487 }
488
489
490 /**
491 * Return the type of an attribute by position.
492 *
493 * @return The type.
494 * @see org.xml.sax.AttributeList#getType(int)
495 */
496 public String getType (int i)
497 {
498 return attributes.getType(i);
499 }
500
501
502 /**
503 * Return the value of an attribute by position.
504 *
505 * @return The value.
506 * @see org.xml.sax.AttributeList#getValue(int)
507 */
508 public String getValue (int i)
509 {
510 return attributes.getValue(i);
511 }
512
513
514 /**
515 * Return the type of an attribute by qualified (prefixed) name.
516 *
517 * @return The type.
518 * @see org.xml.sax.AttributeList#getType(java.lang.String)
519 */
520 public String getType (String qName)
521 {
522 return attributes.getType(qName);
523 }
524
525
526 /**
527 * Return the value of an attribute by qualified (prefixed) name.
528 *
529 * @return The value.
530 * @see org.xml.sax.AttributeList#getValue(java.lang.String)
531 */
532 public String getValue (String qName)
533 {
534 return attributes.getValue(qName);
535 }
536
537 private Attributes attributes;
538 }
539
540}
541
542// end of XMLReaderAdapter.java