1 9 package javolution.xml.sax; 10 11 import j2me.lang.CharSequence; 12 import java.io.IOException ; 13 14 import javolution.lang.Reusable; 15 import javolution.text.CharArray; 16 import javolution.text.Text; 17 18 import org.xml.sax.Attributes ; 19 import org.xml.sax.ContentHandler ; 20 import org.xml.sax.DTDHandler ; 21 import org.xml.sax.EntityResolver ; 22 import org.xml.sax.ErrorHandler ; 23 import org.xml.sax.InputSource ; 24 import org.xml.sax.Locator ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.SAXNotRecognizedException ; 27 import org.xml.sax.SAXNotSupportedException ; 28 import org.xml.sax.SAXParseException ; 29 import org.xml.sax.XMLReader ; 30 31 44 public final class SAX2ReaderImpl implements XMLReader, Reusable { 45 46 49 private static Sax2DefaultHandler DEFAULT_HANDLER 50 = new Sax2DefaultHandler(); 51 52 55 private final XMLReaderImpl _parser = new XMLReaderImpl(); 56 57 60 private final Proxy _proxy = new Proxy(); 61 62 65 public SAX2ReaderImpl() { 66 } 67 68 public boolean getFeature(String name) throws SAXNotRecognizedException , 70 SAXNotSupportedException { 71 return _parser.getFeature(name); 72 } 73 74 public void setFeature(String name, boolean value) 76 throws SAXNotRecognizedException , SAXNotSupportedException { 77 _parser.setFeature(name, value); 78 } 79 80 public Object getProperty(String name) throws SAXNotRecognizedException , 82 SAXNotSupportedException { 83 return _parser.getProperty(name); 84 } 85 86 public void setProperty(String name, Object value) 88 throws SAXNotRecognizedException , SAXNotSupportedException { 89 _parser.setProperty(name, value); 90 } 91 92 public void setEntityResolver(EntityResolver resolver) { 94 _parser.setEntityResolver(resolver); 95 } 96 97 public EntityResolver getEntityResolver() { 99 return _parser.getEntityResolver(); 100 } 101 102 public void setDTDHandler(DTDHandler handler) { 104 _parser.setDTDHandler(handler); 105 } 106 107 public DTDHandler getDTDHandler() { 109 return _parser.getDTDHandler(); 110 } 111 112 public void setContentHandler(ContentHandler handler) { 114 if (handler != null) { 115 _proxy._sax2Handler = handler; 116 _parser.setContentHandler(_proxy); 117 } else { 118 throw new NullPointerException (); 119 } 120 } 121 122 public ContentHandler getContentHandler() { 124 return (_proxy._sax2Handler == DEFAULT_HANDLER) ? null 125 : _proxy._sax2Handler; 126 } 127 128 public void setErrorHandler(ErrorHandler handler) { 130 _parser.setErrorHandler(handler); 131 } 132 133 public ErrorHandler getErrorHandler() { 135 return _parser.getErrorHandler(); 136 } 137 138 public void parse(InputSource input) throws IOException , SAXException { 140 try { 141 _parser.parse(input); 142 } finally { 143 _parser.reset(); 144 } 145 } 146 147 public void parse(String systemId) throws IOException , SAXException { 149 try { 150 _parser.parse(systemId); 151 } finally { 152 _parser.reset(); 153 } 154 } 155 156 public void reset() { 158 _parser.reset(); 159 } 160 163 private static final class Proxy implements 164 javolution.xml.sax.ContentHandler, Attributes { 165 166 169 private ContentHandler _sax2Handler = DEFAULT_HANDLER; 170 171 175 private javolution.xml.sax.Attributes _attributes; 176 177 180 public Proxy() { 181 } 182 183 public void setDocumentLocator(Locator locator) { 185 _sax2Handler.setDocumentLocator(locator); 186 } 187 188 public void startDocument() throws SAXException { 190 _sax2Handler.startDocument(); 191 } 192 193 public void endDocument() throws SAXException { 195 _sax2Handler.endDocument(); 196 _sax2Handler = DEFAULT_HANDLER; 197 } 198 199 public void startPrefixMapping(CharArray prefix, CharArray uri) 201 throws SAXException { 202 _sax2Handler.startPrefixMapping(prefix.toString(), uri.toString()); 203 } 204 205 public void endPrefixMapping(CharArray prefix) throws SAXException { 207 _sax2Handler.endPrefixMapping(prefix.toString()); 208 } 209 210 public void startElement(CharArray namespaceURI, 212 CharArray localName, CharArray qName, 213 javolution.xml.sax.Attributes atts) throws SAXException { 214 _attributes = atts; 215 _sax2Handler.startElement(namespaceURI.toString(), localName 216 .toString(), qName.toString(), this); 217 } 218 219 public void endElement(CharArray namespaceURI, 221 CharArray localName, CharArray qName) throws SAXException { 222 _sax2Handler.endElement(namespaceURI.toString(), localName 223 .toString(), qName.toString()); 224 } 225 226 public void characters(char ch[], int start, int length) 228 throws SAXException { 229 _sax2Handler.characters(ch, start, length); 230 } 231 232 public void ignorableWhitespace(char ch[], int start, int length) 234 throws SAXException { 235 _sax2Handler.ignorableWhitespace(ch, start, length); 236 } 237 238 public void processingInstruction(CharArray target, CharArray data) 240 throws SAXException { 241 _sax2Handler.processingInstruction(target.toString(), data 242 .toString()); 243 } 244 245 public void skippedEntity(CharArray name) throws SAXException { 247 _sax2Handler.skippedEntity(name.toString()); 248 } 249 250 public int getLength() { 252 return _attributes.getLength(); 253 } 254 255 public String getURI(int index) { 257 CharSequence chars = _attributes.getURI(index); 258 return (chars != null) ? chars.toString() : null; 259 } 260 261 public String getLocalName(int index) { 263 CharSequence chars = _attributes.getLocalName(index); 264 return (chars != null) ? chars.toString() : null; 265 } 266 267 public String getQName(int index) { 269 CharSequence chars = _attributes.getQName(index); 270 return (chars != null) ? chars.toString() : null; 271 } 272 273 public String getType(int index) { 275 return _attributes.getType(index).toString(); 276 } 277 278 public String getValue(int index) { 280 CharSequence chars = _attributes.getValue(index); 281 return (chars != null) ? chars.toString() : null; 282 } 283 284 public int getIndex(String uri, String localName) { 286 return _attributes.getIndex(toCharSequence(uri), toCharSequence(localName)); 287 } 288 289 public int getIndex(String qName) { 291 return _attributes.getIndex(toCharSequence(qName)); 292 } 293 294 public String getType(String uri, String localName) { 296 return _attributes.getType(toCharSequence(uri), toCharSequence(localName)).toString(); 297 } 298 299 public String getType(String qName) { 301 return _attributes.getType(toCharSequence(qName)).toString(); 302 } 303 304 public String getValue(String uri, String localName) { 306 return _attributes.getValue(toCharSequence(uri), toCharSequence(localName)).toString(); 307 } 308 309 public String getValue(String qName) { 311 return _attributes.getValue(toCharSequence(qName)).toString(); 312 } 313 } 314 315 private static final class Sax2DefaultHandler implements EntityResolver , 316 DTDHandler , ContentHandler, ErrorHandler { 317 318 public InputSource resolveEntity(String publicId, String systemId) 319 throws SAXException , IOException { 320 return null; 321 } 322 323 public void notationDecl(String name, String publicId, String systemId) 324 throws SAXException { 325 } 326 327 public void unparsedEntityDecl(String name, String publicId, 328 String systemId, String notationName) throws SAXException { 329 } 330 331 public void setDocumentLocator(Locator locator) { 332 } 333 334 public void startDocument() throws SAXException { 335 } 336 337 public void endDocument() throws SAXException { 338 } 339 340 public void startPrefixMapping(String prefix, String uri) 341 throws SAXException { 342 } 343 344 public void endPrefixMapping(String prefix) throws SAXException { 345 } 346 347 public void startElement(String uri, String localName, String qName, 348 Attributes atts) throws SAXException { 349 } 350 351 public void endElement(String uri, String localName, String qName) 352 throws SAXException { 353 } 354 355 public void characters(char[] ch, int start, int length) 356 throws SAXException { 357 } 358 359 public void ignorableWhitespace(char[] ch, int start, int length) 360 throws SAXException { 361 } 362 363 public void processingInstruction(String target, String data) 364 throws SAXException { 365 } 366 367 public void skippedEntity(String name) throws SAXException { 368 } 369 370 public void warning(SAXParseException exception) throws SAXException { 371 } 372 373 public void error(SAXParseException exception) throws SAXException { 374 } 375 376 public void fatalError(SAXParseException exception) throws SAXException { 377 throw exception; 378 } 379 } 380 381 private static CharSequence toCharSequence(Object obj) { 382 return obj instanceof CharSequence ? (CharSequence )obj : 383 Text.valueOf(obj); 384 } 385 386 } | Popular Tags |