001: /*
002: * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003: * Copyright (C) 2006 - Javolution (http://javolution.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package javolution.xml.sax;
010:
011: import j2me.lang.CharSequence;
012: import java.io.IOException;
013:
014: import javolution.lang.Reusable;
015: import javolution.text.CharArray;
016: import javolution.text.Text;
017:
018: import org.xml.sax.Attributes;
019: import org.xml.sax.ContentHandler;
020: import org.xml.sax.DTDHandler;
021: import org.xml.sax.EntityResolver;
022: import org.xml.sax.ErrorHandler;
023: import org.xml.sax.InputSource;
024: import org.xml.sax.Locator;
025: import org.xml.sax.SAXException;
026: import org.xml.sax.SAXNotRecognizedException;
027: import org.xml.sax.SAXNotSupportedException;
028: import org.xml.sax.SAXParseException;
029: import org.xml.sax.XMLReader;
030:
031: /**
032: * <p> This class provides a SAX2-compliant parser wrapping a
033: * {@link javolution.xml.sax.XMLReaderImpl}. This parser allocates
034: * <code>java.lang.String</code> instances while parsing in accordance
035: * with the SAX2 specification. For faster performance (2-5x), the use of
036: * the SAX2-like {@link javolution.xml.sax.XMLReaderImpl
037: * XMLSaxParserImpl} or better{@link javolution.xml.stream.XMLStreamReader
038: * XMLStreamReader} is recommended.</p>
039: *
040: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
041: * @version 4.0, June 16, 2005
042: * @see <a href="http://www.saxproject.org"> SAX -- Simple API for XML</a>
043: */
044: public final class SAX2ReaderImpl implements XMLReader, Reusable {
045:
046: /**
047: * Holds the SAX2 default handler instance.
048: */
049: private static Sax2DefaultHandler DEFAULT_HANDLER = new Sax2DefaultHandler();
050:
051: /**
052: * Holds the real-time parser instance associated to this SAX2 parser.
053: */
054: private final XMLReaderImpl _parser = new XMLReaderImpl();
055:
056: /**
057: * Holds the content handler proxy.
058: */
059: private final Proxy _proxy = new Proxy();
060:
061: /**
062: * Default constructor.
063: */
064: public SAX2ReaderImpl() {
065: }
066:
067: // Implements org.xml.sax.XMLReader interface.
068: public boolean getFeature(String name)
069: throws SAXNotRecognizedException, SAXNotSupportedException {
070: return _parser.getFeature(name);
071: }
072:
073: // Implements org.xml.sax.XMLReader interface.
074: public void setFeature(String name, boolean value)
075: throws SAXNotRecognizedException, SAXNotSupportedException {
076: _parser.setFeature(name, value);
077: }
078:
079: // Implements org.xml.sax.XMLReader interface.
080: public Object getProperty(String name)
081: throws SAXNotRecognizedException, SAXNotSupportedException {
082: return _parser.getProperty(name);
083: }
084:
085: // Implements org.xml.sax.XMLReader interface.
086: public void setProperty(String name, Object value)
087: throws SAXNotRecognizedException, SAXNotSupportedException {
088: _parser.setProperty(name, value);
089: }
090:
091: // Implements org.xml.sax.XMLReader interface.
092: public void setEntityResolver(EntityResolver resolver) {
093: _parser.setEntityResolver(resolver);
094: }
095:
096: // Implements org.xml.sax.XMLReader interface.
097: public EntityResolver getEntityResolver() {
098: return _parser.getEntityResolver();
099: }
100:
101: // Implements org.xml.sax.XMLReader interface.
102: public void setDTDHandler(DTDHandler handler) {
103: _parser.setDTDHandler(handler);
104: }
105:
106: // Implements org.xml.sax.XMLReader interface.
107: public DTDHandler getDTDHandler() {
108: return _parser.getDTDHandler();
109: }
110:
111: // Implements org.xml.sax.XMLReader interface.
112: public void setContentHandler(ContentHandler handler) {
113: if (handler != null) {
114: _proxy._sax2Handler = handler;
115: _parser.setContentHandler(_proxy);
116: } else {
117: throw new NullPointerException();
118: }
119: }
120:
121: // Implements org.xml.sax.XMLReader interface.
122: public ContentHandler getContentHandler() {
123: return (_proxy._sax2Handler == DEFAULT_HANDLER) ? null
124: : _proxy._sax2Handler;
125: }
126:
127: // Implements org.xml.sax.XMLReader interface.
128: public void setErrorHandler(ErrorHandler handler) {
129: _parser.setErrorHandler(handler);
130: }
131:
132: // Implements org.xml.sax.XMLReader interface.
133: public ErrorHandler getErrorHandler() {
134: return _parser.getErrorHandler();
135: }
136:
137: // Implements org.xml.sax.XMLReader interface.
138: public void parse(InputSource input) throws IOException,
139: SAXException {
140: try {
141: _parser.parse(input);
142: } finally {
143: _parser.reset();
144: }
145: }
146:
147: // Implements org.xml.sax.XMLReader interface.
148: public void parse(String systemId) throws IOException, SAXException {
149: try {
150: _parser.parse(systemId);
151: } finally {
152: _parser.reset();
153: }
154: }
155:
156: // Implements Reusable interface.
157: public void reset() {
158: _parser.reset();
159: }
160:
161: /**
162: * This class defines the proxy for content handler and attributes.
163: */
164: private static final class Proxy implements
165: javolution.xml.sax.ContentHandler, Attributes {
166:
167: /**
168: * Holds the SAX2 content handler to which SAX2 events are forwarded.
169: */
170: private ContentHandler _sax2Handler = DEFAULT_HANDLER;
171:
172: /**
173: * Holds the real-time attributes implementation from which attributes
174: * values are read.
175: */
176: private javolution.xml.sax.Attributes _attributes;
177:
178: /**
179: * Default constructor.
180: */
181: public Proxy() {
182: }
183:
184: // Implements ContentHandler
185: public void setDocumentLocator(Locator locator) {
186: _sax2Handler.setDocumentLocator(locator);
187: }
188:
189: // Implements ContentHandler
190: public void startDocument() throws SAXException {
191: _sax2Handler.startDocument();
192: }
193:
194: // Implements ContentHandler
195: public void endDocument() throws SAXException {
196: _sax2Handler.endDocument();
197: _sax2Handler = DEFAULT_HANDLER;
198: }
199:
200: // Implements ContentHandler
201: public void startPrefixMapping(CharArray prefix, CharArray uri)
202: throws SAXException {
203: _sax2Handler.startPrefixMapping(prefix.toString(), uri
204: .toString());
205: }
206:
207: // Implements ContentHandler
208: public void endPrefixMapping(CharArray prefix)
209: throws SAXException {
210: _sax2Handler.endPrefixMapping(prefix.toString());
211: }
212:
213: // Implements ContentHandler
214: public void startElement(CharArray namespaceURI,
215: CharArray localName, CharArray qName,
216: javolution.xml.sax.Attributes atts) throws SAXException {
217: _attributes = atts;
218: _sax2Handler.startElement(namespaceURI.toString(),
219: localName.toString(), qName.toString(), this );
220: }
221:
222: // Implements ContentHandler
223: public void endElement(CharArray namespaceURI,
224: CharArray localName, CharArray qName)
225: throws SAXException {
226: _sax2Handler.endElement(namespaceURI.toString(), localName
227: .toString(), qName.toString());
228: }
229:
230: // Implements ContentHandler
231: public void characters(char ch[], int start, int length)
232: throws SAXException {
233: _sax2Handler.characters(ch, start, length);
234: }
235:
236: // Implements ContentHandler
237: public void ignorableWhitespace(char ch[], int start, int length)
238: throws SAXException {
239: _sax2Handler.ignorableWhitespace(ch, start, length);
240: }
241:
242: // Implements ContentHandler
243: public void processingInstruction(CharArray target,
244: CharArray data) throws SAXException {
245: _sax2Handler.processingInstruction(target.toString(), data
246: .toString());
247: }
248:
249: // Implements ContentHandler
250: public void skippedEntity(CharArray name) throws SAXException {
251: _sax2Handler.skippedEntity(name.toString());
252: }
253:
254: // Implements Attributes
255: public int getLength() {
256: return (_attributes != null ? _attributes.getLength() : 0);
257: }
258:
259: // Implements Attributes
260: public String getURI(int index) {
261: CharSequence chars = (_attributes != null ? _attributes
262: .getURI(index) : null);
263: return (chars != null ? chars.toString() : "");
264: }
265:
266: // Implements Attributes
267: public String getLocalName(int index) {
268: CharSequence chars = (_attributes != null ? _attributes
269: .getLocalName(index) : null);
270: return (chars != null ? chars.toString() : "");
271: }
272:
273: // Implements Attributes
274: public String getQName(int index) {
275: CharSequence chars = (_attributes != null ? _attributes
276: .getQName(index) : null);
277: return (chars != null ? chars.toString() : "");
278: }
279:
280: // Implements Attributes
281: public String getType(int index) {
282: return (_attributes != null ? _attributes.getType(index)
283: .toString() : null);
284: }
285:
286: // Implements Attributes
287: public String getValue(int index) {
288: CharSequence chars = (_attributes != null ? _attributes
289: .getValue(index) : null);
290: return (chars != null ? chars.toString() : null);
291: }
292:
293: // Implements Attributes
294: public int getIndex(String uri, String localName) {
295: return (uri != null && localName != null
296: && _attributes != null ? _attributes.getIndex(
297: toCharSequence(uri), toCharSequence(localName))
298: : -1);
299: }
300:
301: // Implements Attributes
302: public int getIndex(String qName) {
303: return (qName != null && _attributes != null ? _attributes
304: .getIndex(toCharSequence(qName)) : -1);
305: }
306:
307: // Implements Attributes
308: public String getType(String uri, String localName) {
309: return (uri != null && localName != null
310: && _attributes != null ? _attributes.getType(
311: toCharSequence(uri), toCharSequence(localName))
312: .toString() : null);
313: }
314:
315: // Implements Attributes
316: public String getType(String qName) {
317: return (qName != null && _attributes != null ? _attributes
318: .getType(toCharSequence(qName)).toString() : null);
319: }
320:
321: // Implements Attributes
322: public String getValue(String uri, String localName) {
323: return (uri != null
324: && localName != null
325: && _attributes != null
326: && _attributes.getValue(toCharSequence(uri),
327: toCharSequence(localName)) != null ? _attributes
328: .getValue(toCharSequence(uri),
329: toCharSequence(localName)).toString()
330: : null);
331: }
332:
333: // Implements Attributes
334: public String getValue(String qName) {
335: return (qName != null && _attributes != null ? _attributes
336: .getValue(toCharSequence(qName)).toString() : null);
337: }
338: }
339:
340: private static final class Sax2DefaultHandler implements
341: EntityResolver, DTDHandler, ContentHandler, ErrorHandler {
342:
343: public InputSource resolveEntity(String publicId,
344: String systemId) throws SAXException, IOException {
345: return null;
346: }
347:
348: public void notationDecl(String name, String publicId,
349: String systemId) throws SAXException {
350: }
351:
352: public void unparsedEntityDecl(String name, String publicId,
353: String systemId, String notationName)
354: throws SAXException {
355: }
356:
357: public void setDocumentLocator(Locator locator) {
358: }
359:
360: public void startDocument() throws SAXException {
361: }
362:
363: public void endDocument() throws SAXException {
364: }
365:
366: public void startPrefixMapping(String prefix, String uri)
367: throws SAXException {
368: }
369:
370: public void endPrefixMapping(String prefix) throws SAXException {
371: }
372:
373: public void startElement(String uri, String localName,
374: String qName, Attributes atts) throws SAXException {
375: }
376:
377: public void endElement(String uri, String localName,
378: String qName) throws SAXException {
379: }
380:
381: public void characters(char[] ch, int start, int length)
382: throws SAXException {
383: }
384:
385: public void ignorableWhitespace(char[] ch, int start, int length)
386: throws SAXException {
387: }
388:
389: public void processingInstruction(String target, String data)
390: throws SAXException {
391: }
392:
393: public void skippedEntity(String name) throws SAXException {
394: }
395:
396: public void warning(SAXParseException exception)
397: throws SAXException {
398: }
399:
400: public void error(SAXParseException exception)
401: throws SAXException {
402: }
403:
404: public void fatalError(SAXParseException exception)
405: throws SAXException {
406: throw exception;
407: }
408: }
409:
410: private static CharSequence toCharSequence(Object obj) {
411: return obj instanceof CharSequence ? (CharSequence) obj : Text
412: .valueOf(obj);
413: }
414:
415: }
|