KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > xml > sax > SAX2ReaderImpl


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.xml.sax;
10
11 import j2me.lang.CharSequence;
12 import java.io.IOException JavaDoc;
13
14 import javolution.lang.Reusable;
15 import javolution.text.CharArray;
16 import javolution.text.Text;
17
18 import org.xml.sax.Attributes JavaDoc;
19 import org.xml.sax.ContentHandler JavaDoc;
20 import org.xml.sax.DTDHandler JavaDoc;
21 import org.xml.sax.EntityResolver JavaDoc;
22 import org.xml.sax.ErrorHandler JavaDoc;
23 import org.xml.sax.InputSource JavaDoc;
24 import org.xml.sax.Locator JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.SAXNotRecognizedException JavaDoc;
27 import org.xml.sax.SAXNotSupportedException JavaDoc;
28 import org.xml.sax.SAXParseException JavaDoc;
29 import org.xml.sax.XMLReader JavaDoc;
30
31 /**
32  * <p> This class provides a SAX2-compliant parser wrapping a
33  * {@link javolution.xml.sax.XMLReaderImpl}. This parser allocates
34  * <code>java.lang.String</code> instances while parsing in accordance
35  * with the SAX2 specification. For faster performance (2-5x), the use of
36  * the SAX2-like {@link javolution.xml.sax.XMLReaderImpl
37  * XMLSaxParserImpl} or better{@link javolution.xml.stream.XMLStreamReader
38  * XMLStreamReader} is recommended.</p>
39  *
40  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
41  * @version 4.0, June 16, 2005
42  * @see <a HREF="http://www.saxproject.org"> SAX -- Simple API for XML</a>
43  */

44 public final class SAX2ReaderImpl implements XMLReader, Reusable {
45
46     /**
47      * Holds the SAX2 default handler instance.
48      */

49     private static Sax2DefaultHandler DEFAULT_HANDLER
50         = new Sax2DefaultHandler();
51
52     /**
53      * Holds the real-time parser instance associated to this SAX2 parser.
54      */

55     private final XMLReaderImpl _parser = new XMLReaderImpl();
56
57     /**
58      * Holds the content handler proxy.
59      */

60     private final Proxy _proxy = new Proxy();
61
62     /**
63      * Default constructor.
64      */

65     public SAX2ReaderImpl() {
66     }
67
68     // Implements org.xml.sax.XMLReader interface.
69
public boolean getFeature(String JavaDoc name) throws SAXNotRecognizedException JavaDoc,
70             SAXNotSupportedException JavaDoc {
71         return _parser.getFeature(name);
72     }
73
74     // Implements org.xml.sax.XMLReader interface.
75
public void setFeature(String JavaDoc name, boolean value)
76             throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
77         _parser.setFeature(name, value);
78     }
79
80     // Implements org.xml.sax.XMLReader interface.
81
public Object JavaDoc getProperty(String JavaDoc name) throws SAXNotRecognizedException JavaDoc,
82             SAXNotSupportedException JavaDoc {
83         return _parser.getProperty(name);
84     }
85
86     // Implements org.xml.sax.XMLReader interface.
87
public void setProperty(String JavaDoc name, Object JavaDoc value)
88             throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
89         _parser.setProperty(name, value);
90     }
91
92     // Implements org.xml.sax.XMLReader interface.
93
public void setEntityResolver(EntityResolver JavaDoc resolver) {
94         _parser.setEntityResolver(resolver);
95     }
96
97     // Implements org.xml.sax.XMLReader interface.
98
public EntityResolver JavaDoc getEntityResolver() {
99         return _parser.getEntityResolver();
100     }
101
102     // Implements org.xml.sax.XMLReader interface.
103
public void setDTDHandler(DTDHandler JavaDoc handler) {
104         _parser.setDTDHandler(handler);
105     }
106
107     // Implements org.xml.sax.XMLReader interface.
108
public DTDHandler JavaDoc getDTDHandler() {
109         return _parser.getDTDHandler();
110     }
111
112     // Implements org.xml.sax.XMLReader interface.
113
public void setContentHandler(ContentHandler handler) {
114         if (handler != null) {
115             _proxy._sax2Handler = handler;
116             _parser.setContentHandler(_proxy);
117         } else {
118             throw new NullPointerException JavaDoc();
119         }
120     }
121
122     // Implements org.xml.sax.XMLReader interface.
123
public ContentHandler getContentHandler() {
124         return (_proxy._sax2Handler == DEFAULT_HANDLER) ? null
125                 : _proxy._sax2Handler;
126     }
127
128     // Implements org.xml.sax.XMLReader interface.
129
public void setErrorHandler(ErrorHandler JavaDoc handler) {
130         _parser.setErrorHandler(handler);
131     }
132
133     // Implements org.xml.sax.XMLReader interface.
134
public ErrorHandler JavaDoc getErrorHandler() {
135         return _parser.getErrorHandler();
136     }
137
138     // Implements org.xml.sax.XMLReader interface.
139
public void parse(InputSource JavaDoc input) throws IOException JavaDoc, SAXException JavaDoc {
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 JavaDoc systemId) throws IOException JavaDoc, SAXException JavaDoc {
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      * This class defines the proxy for content handler and attributes.
162      */

163     private static final class Proxy implements
164             javolution.xml.sax.ContentHandler, Attributes {
165
166         /**
167          * Holds the SAX2 content handler to which SAX2 events are forwarded.
168          */

169         private ContentHandler _sax2Handler = DEFAULT_HANDLER;
170
171         /**
172          * Holds the real-time attributes implementation from which attributes
173          * values are read.
174          */

175         private javolution.xml.sax.Attributes _attributes;
176
177         /**
178          * Default constructor.
179          */

180         public Proxy() {
181         }
182
183         // Implements ContentHandler
184
public void setDocumentLocator(Locator JavaDoc locator) {
185             _sax2Handler.setDocumentLocator(locator);
186         }
187
188         // Implements ContentHandler
189
public void startDocument() throws SAXException JavaDoc {
190             _sax2Handler.startDocument();
191         }
192
193         // Implements ContentHandler
194
public void endDocument() throws SAXException JavaDoc {
195             _sax2Handler.endDocument();
196             _sax2Handler = DEFAULT_HANDLER;
197         }
198
199         // Implements ContentHandler
200
public void startPrefixMapping(CharArray prefix, CharArray uri)
201                 throws SAXException JavaDoc {
202             _sax2Handler.startPrefixMapping(prefix.toString(), uri.toString());
203         }
204
205         // Implements ContentHandler
206
public void endPrefixMapping(CharArray prefix) throws SAXException JavaDoc {
207             _sax2Handler.endPrefixMapping(prefix.toString());
208         }
209
210         // Implements ContentHandler
211
public void startElement(CharArray namespaceURI,
212                 CharArray localName, CharArray qName,
213                 javolution.xml.sax.Attributes atts) throws SAXException JavaDoc {
214             _attributes = atts;
215             _sax2Handler.startElement(namespaceURI.toString(), localName
216                     .toString(), qName.toString(), this);
217         }
218
219         // Implements ContentHandler
220
public void endElement(CharArray namespaceURI,
221                 CharArray localName, CharArray qName) throws SAXException JavaDoc {
222             _sax2Handler.endElement(namespaceURI.toString(), localName
223                     .toString(), qName.toString());
224         }
225
226         // Implements ContentHandler
227
public void characters(char ch[], int start, int length)
228                 throws SAXException JavaDoc {
229             _sax2Handler.characters(ch, start, length);
230         }
231
232         // Implements ContentHandler
233
public void ignorableWhitespace(char ch[], int start, int length)
234                 throws SAXException JavaDoc {
235             _sax2Handler.ignorableWhitespace(ch, start, length);
236         }
237
238         // Implements ContentHandler
239
public void processingInstruction(CharArray target, CharArray data)
240                 throws SAXException JavaDoc {
241             _sax2Handler.processingInstruction(target.toString(), data
242                     .toString());
243         }
244
245         // Implements ContentHandler
246
public void skippedEntity(CharArray name) throws SAXException JavaDoc {
247             _sax2Handler.skippedEntity(name.toString());
248         }
249
250         // Implements Attributes
251
public int getLength() {
252             return _attributes.getLength();
253         }
254
255         // Implements Attributes
256
public String JavaDoc getURI(int index) {
257             CharSequence JavaDoc chars = _attributes.getURI(index);
258             return (chars != null) ? chars.toString() : null;
259         }
260
261         // Implements Attributes
262
public String JavaDoc getLocalName(int index) {
263             CharSequence JavaDoc chars = _attributes.getLocalName(index);
264             return (chars != null) ? chars.toString() : null;
265         }
266
267         // Implements Attributes
268
public String JavaDoc getQName(int index) {
269             CharSequence JavaDoc chars = _attributes.getQName(index);
270             return (chars != null) ? chars.toString() : null;
271         }
272
273         // Implements Attributes
274
public String JavaDoc getType(int index) {
275             return _attributes.getType(index).toString();
276         }
277
278         // Implements Attributes
279
public String JavaDoc getValue(int index) {
280             CharSequence JavaDoc chars = _attributes.getValue(index);
281             return (chars != null) ? chars.toString() : null;
282         }
283
284         // Implements Attributes
285
public int getIndex(String JavaDoc uri, String JavaDoc localName) {
286             return _attributes.getIndex(toCharSequence(uri), toCharSequence(localName));
287         }
288
289         // Implements Attributes
290
public int getIndex(String JavaDoc qName) {
291             return _attributes.getIndex(toCharSequence(qName));
292         }
293
294         // Implements Attributes
295
public String JavaDoc getType(String JavaDoc uri, String JavaDoc localName) {
296             return _attributes.getType(toCharSequence(uri), toCharSequence(localName)).toString();
297         }
298
299         // Implements Attributes
300
public String JavaDoc getType(String JavaDoc qName) {
301             return _attributes.getType(toCharSequence(qName)).toString();
302         }
303
304         // Implements Attributes
305
public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName) {
306             return _attributes.getValue(toCharSequence(uri), toCharSequence(localName)).toString();
307         }
308
309         // Implements Attributes
310
public String JavaDoc getValue(String JavaDoc qName) {
311             return _attributes.getValue(toCharSequence(qName)).toString();
312         }
313     }
314
315     private static final class Sax2DefaultHandler implements EntityResolver JavaDoc,
316             DTDHandler JavaDoc, ContentHandler, ErrorHandler JavaDoc {
317
318         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
319                 throws SAXException JavaDoc, IOException JavaDoc {
320             return null;
321         }
322
323         public void notationDecl(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
324                 throws SAXException JavaDoc {
325         }
326
327         public void unparsedEntityDecl(String JavaDoc name, String JavaDoc publicId,
328                 String JavaDoc systemId, String JavaDoc notationName) throws SAXException JavaDoc {
329         }
330
331         public void setDocumentLocator(Locator JavaDoc locator) {
332         }
333
334         public void startDocument() throws SAXException JavaDoc {
335         }
336
337         public void endDocument() throws SAXException JavaDoc {
338         }
339
340         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
341                 throws SAXException JavaDoc {
342         }
343
344         public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
345         }
346
347         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
348                 Attributes atts) throws SAXException JavaDoc {
349         }
350
351         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
352                 throws SAXException JavaDoc {
353         }
354
355         public void characters(char[] ch, int start, int length)
356                 throws SAXException JavaDoc {
357         }
358
359         public void ignorableWhitespace(char[] ch, int start, int length)
360                 throws SAXException JavaDoc {
361         }
362
363         public void processingInstruction(String JavaDoc target, String JavaDoc data)
364                 throws SAXException JavaDoc {
365         }
366
367         public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
368         }
369
370         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
371         }
372
373         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
374         }
375
376         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
377             throw exception;
378         }
379     }
380     
381     private static CharSequence JavaDoc toCharSequence(Object JavaDoc obj) {
382         return obj instanceof CharSequence JavaDoc ? (CharSequence JavaDoc)obj :
383             Text.valueOf(obj);
384     }
385
386 }
Popular Tags