1 // 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.1 2004/12/23 22:38:42 mark Exp $
7 package org
.xml
.sax
.helpers
;
9 import java
.io
.IOException
;
10 import java
.util
.Locale
;
12 import org
.xml
.sax
.Parser
; // deprecated
13 import org
.xml
.sax
.Locator
;
14 import org
.xml
.sax
.InputSource
;
15 import org
.xml
.sax
.AttributeList
; // deprecated
16 import org
.xml
.sax
.EntityResolver
;
17 import org
.xml
.sax
.DTDHandler
;
18 import org
.xml
.sax
.DocumentHandler
; // deprecated
19 import org
.xml
.sax
.ErrorHandler
;
20 import org
.xml
.sax
.SAXException
;
22 import org
.xml
.sax
.XMLReader
;
23 import org
.xml
.sax
.Attributes
;
24 import org
.xml
.sax
.ContentHandler
;
25 import org
.xml
.sax
.SAXNotSupportedException
;
29 * Adapt a SAX2 XMLReader as a SAX1 Parser.
32 * <em>This module, both source code and documentation, is in the
33 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
34 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
35 * for further information.
38 * <p>This class wraps a SAX2 {@link org.xml.sax.XMLReader XMLReader}
39 * and makes it act as a SAX1 {@link org.xml.sax.Parser Parser}. The XMLReader
40 * must support a true value for the
41 * http://xml.org/sax/features/namespace-prefixes property or parsing will fail
42 * with a {@link org.xml.sax.SAXException SAXException}; if the XMLReader
43 * supports a false value for the http://xml.org/sax/features/namespaces
44 * property, that will also be used to improve efficiency.</p>
47 * @author David Megginson
48 * @version 2.0.1 (sax2r2)
49 * @see org.xml.sax.Parser
50 * @see org.xml.sax.XMLReader
52 public class XMLReaderAdapter
implements Parser
, ContentHandler
56 ////////////////////////////////////////////////////////////////////
58 ////////////////////////////////////////////////////////////////////
62 * Create a new adapter.
64 * <p>Use the "org.xml.sax.driver" property to locate the SAX2
65 * driver to embed.</p>
67 * @exception org.xml.sax.SAXException If the embedded driver
68 * cannot be instantiated or if the
69 * org.xml.sax.driver property is not specified.
71 public XMLReaderAdapter ()
74 setup(XMLReaderFactory
.createXMLReader());
79 * Create a new adapter.
81 * <p>Create a new adapter, wrapped around a SAX2 XMLReader.
82 * The adapter will make the XMLReader act like a SAX1
85 * @param xmlReader The SAX2 XMLReader to wrap.
86 * @exception java.lang.NullPointerException If the argument is null.
88 public XMLReaderAdapter (XMLReader xmlReader
)
98 * @param xmlReader The embedded XMLReader.
100 private void setup (XMLReader xmlReader
)
102 if (xmlReader
== null) {
103 throw new NullPointerException("XMLReader must not be null");
105 this.xmlReader
= xmlReader
;
106 qAtts
= new AttributesAdapter();
111 ////////////////////////////////////////////////////////////////////
112 // Implementation of org.xml.sax.Parser.
113 ////////////////////////////////////////////////////////////////////
117 * Set the locale for error reporting.
119 * <p>This is not supported in SAX2, and will always throw
122 * @param locale the locale for error reporting.
123 * @see org.xml.sax.Parser#setLocale
124 * @exception org.xml.sax.SAXException Thrown unless overridden.
126 public void setLocale (Locale locale
)
129 throw new SAXNotSupportedException("setLocale not supported");
134 * Register the entity resolver.
136 * @param resolver The new resolver.
137 * @see org.xml.sax.Parser#setEntityResolver
139 public void setEntityResolver (EntityResolver resolver
)
141 xmlReader
.setEntityResolver(resolver
);
146 * Register the DTD event handler.
148 * @param handler The new DTD event handler.
149 * @see org.xml.sax.Parser#setDTDHandler
151 public void setDTDHandler (DTDHandler handler
)
153 xmlReader
.setDTDHandler(handler
);
158 * Register the SAX1 document event handler.
160 * <p>Note that the SAX1 document handler has no Namespace
163 * @param handler The new SAX1 document event handler.
164 * @see org.xml.sax.Parser#setDocumentHandler
166 public void setDocumentHandler (DocumentHandler handler
)
168 documentHandler
= handler
;
173 * Register the error event handler.
175 * @param handler The new error event handler.
176 * @see org.xml.sax.Parser#setErrorHandler
178 public void setErrorHandler (ErrorHandler handler
)
180 xmlReader
.setErrorHandler(handler
);
185 * Parse the document.
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>
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)
199 public void parse (String systemId
)
200 throws IOException
, SAXException
202 parse(new InputSource(systemId
));
207 * Parse the document.
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>
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)
221 public void parse (InputSource input
)
222 throws IOException
, SAXException
225 xmlReader
.parse(input
);
230 * Set up the XML reader.
232 private void setupXMLReader ()
235 xmlReader
.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
237 xmlReader
.setFeature("http://xml.org/sax/features/namespaces",
239 } catch (SAXException e
) {
240 // NO OP: it's just extra information, and we can ignore it
242 xmlReader
.setContentHandler(this);
247 ////////////////////////////////////////////////////////////////////
248 // Implementation of org.xml.sax.ContentHandler.
249 ////////////////////////////////////////////////////////////////////
253 * Set a document locator.
255 * @param locator The document locator.
256 * @see org.xml.sax.ContentHandler#setDocumentLocator
258 public void setDocumentLocator (Locator locator
)
260 if (documentHandler
!= null)
261 documentHandler
.setDocumentLocator(locator
);
266 * Start document event.
268 * @exception org.xml.sax.SAXException The client may raise a
269 * processing exception.
270 * @see org.xml.sax.ContentHandler#startDocument
272 public void startDocument ()
275 if (documentHandler
!= null)
276 documentHandler
.startDocument();
281 * End document event.
283 * @exception org.xml.sax.SAXException The client may raise a
284 * processing exception.
285 * @see org.xml.sax.ContentHandler#endDocument
287 public void endDocument ()
290 if (documentHandler
!= null)
291 documentHandler
.endDocument();
296 * Adapt a SAX2 start prefix mapping event.
298 * @param prefix The prefix being mapped.
299 * @param uri The Namespace URI being mapped to.
300 * @see org.xml.sax.ContentHandler#startPrefixMapping
302 public void startPrefixMapping (String prefix
, String uri
)
308 * Adapt a SAX2 end prefix mapping event.
310 * @param prefix The prefix being mapped.
311 * @see org.xml.sax.ContentHandler#endPrefixMapping
313 public void endPrefixMapping (String prefix
)
319 * Adapt a SAX2 start element event.
321 * @param uri The Namespace URI.
322 * @param localName The Namespace local name.
323 * @param qName The qualified (prefixed) name.
324 * @param atts The SAX2 attributes.
325 * @exception org.xml.sax.SAXException The client may raise a
326 * processing exception.
327 * @see org.xml.sax.ContentHandler#endDocument
329 public void startElement (String uri
, String localName
,
330 String qName
, Attributes atts
)
333 if (documentHandler
!= null) {
334 qAtts
.setAttributes(atts
);
335 documentHandler
.startElement(qName
, qAtts
);
341 * Adapt a SAX2 end element event.
343 * @param uri The Namespace URI.
344 * @param localName The Namespace local name.
345 * @param qName The qualified (prefixed) name.
346 * @exception org.xml.sax.SAXException The client may raise a
347 * processing exception.
348 * @see org.xml.sax.ContentHandler#endElement
350 public void endElement (String uri
, String localName
,
354 if (documentHandler
!= null)
355 documentHandler
.endElement(qName
);
360 * Adapt a SAX2 characters event.
362 * @param ch An array of characters.
363 * @param start The starting position in the array.
364 * @param length The number of characters to use.
365 * @exception org.xml.sax.SAXException The client may raise a
366 * processing exception.
367 * @see org.xml.sax.ContentHandler#characters
369 public void characters (char ch
[], int start
, int length
)
372 if (documentHandler
!= null)
373 documentHandler
.characters(ch
, start
, length
);
378 * Adapt a SAX2 ignorable whitespace event.
380 * @param ch An array of characters.
381 * @param start The starting position in the array.
382 * @param length The number of characters to use.
383 * @exception org.xml.sax.SAXException The client may raise a
384 * processing exception.
385 * @see org.xml.sax.ContentHandler#ignorableWhitespace
387 public void ignorableWhitespace (char ch
[], int start
, int length
)
390 if (documentHandler
!= null)
391 documentHandler
.ignorableWhitespace(ch
, start
, length
);
396 * Adapt a SAX2 processing instruction event.
398 * @param target The processing instruction target.
399 * @param data The remainder of the processing instruction
400 * @exception org.xml.sax.SAXException The client may raise a
401 * processing exception.
402 * @see org.xml.sax.ContentHandler#processingInstruction
404 public void processingInstruction (String target
, String data
)
407 if (documentHandler
!= null)
408 documentHandler
.processingInstruction(target
, data
);
413 * Adapt a SAX2 skipped entity event.
415 * @param name The name of the skipped entity.
416 * @see org.xml.sax.ContentHandler#skippedEntity
417 * @exception org.xml.sax.SAXException Throwable by subclasses.
419 public void skippedEntity (String name
)
426 ////////////////////////////////////////////////////////////////////
428 ////////////////////////////////////////////////////////////////////
431 DocumentHandler documentHandler
;
432 AttributesAdapter qAtts
;
436 ////////////////////////////////////////////////////////////////////
438 ////////////////////////////////////////////////////////////////////
442 * Internal class to wrap a SAX2 Attributes object for SAX1.
444 final class AttributesAdapter
implements AttributeList
452 * Set the embedded Attributes object.
454 * @param The embedded SAX2 Attributes.
456 void setAttributes (Attributes attributes
)
458 this.attributes
= attributes
;
463 * Return the number of attributes.
465 * @return The length of the attribute list.
466 * @see org.xml.sax.AttributeList#getLength
468 public int getLength ()
470 return attributes
.getLength();
475 * Return the qualified (prefixed) name of an attribute by position.
477 * @return The qualified name.
478 * @see org.xml.sax.AttributeList#getName
480 public String
getName (int i
)
482 return attributes
.getQName(i
);
487 * Return the type of an attribute by position.
490 * @see org.xml.sax.AttributeList#getType(int)
492 public String
getType (int i
)
494 return attributes
.getType(i
);
499 * Return the value of an attribute by position.
502 * @see org.xml.sax.AttributeList#getValue(int)
504 public String
getValue (int i
)
506 return attributes
.getValue(i
);
511 * Return the type of an attribute by qualified (prefixed) name.
514 * @see org.xml.sax.AttributeList#getType(java.lang.String)
516 public String
getType (String qName
)
518 return attributes
.getType(qName
);
523 * Return the value of an attribute by qualified (prefixed) name.
526 * @see org.xml.sax.AttributeList#getValue(java.lang.String)
528 public String
getValue (String qName
)
530 return attributes
.getValue(qName
);
533 private Attributes attributes
;
538 // end of XMLReaderAdapter.java