1 9 package javolution.xml.sax; 10 11 import j2me.lang.CharSequence; 12 import j2me.nio.ByteBuffer; 13 14 import java.io.IOException; 15 import java.io.InputStream; 16 import java.io.Reader; 17 18 import javolution.lang.Reusable; 19 import javolution.xml.pull.XmlPullParser; 20 import javolution.xml.pull.XmlPullParserException; 21 import javolution.xml.pull.XmlPullParserImpl; 22 23 import org.xml.sax.DTDHandler; 24 import org.xml.sax.EntityResolver; 25 import org.xml.sax.ErrorHandler; 26 import org.xml.sax.Locator; 27 import org.xml.sax.SAXException; 28 import org.xml.sax.SAXNotRecognizedException; 29 import org.xml.sax.SAXNotSupportedException; 30 import org.xml.sax.SAXParseException; 31 32 56 public class XmlSaxParserImpl implements Reusable { 57 58 61 private static DefaultHandler DEFAULT_HANDLER = new DefaultHandler(); 62 63 66 private ContentHandler _contentHandler; 67 68 71 private ErrorHandler _errorHandler; 72 73 76 private final XmlPullParserImpl _pullParser = new XmlPullParserImpl(); 77 78 81 private final LocatorImpl _locator = new LocatorImpl(); 82 83 86 public XmlSaxParserImpl() { 87 setContentHandler(DEFAULT_HANDLER); 89 setErrorHandler(DEFAULT_HANDLER); 90 } 91 92 107 public void setContentHandler(ContentHandler handler) { 108 if (handler != null) { 109 _contentHandler = handler; 110 } else { 111 throw new NullPointerException(); 112 } 113 } 114 115 122 public ContentHandler getContentHandler() { 123 return (_contentHandler == DEFAULT_HANDLER) ? null : _contentHandler; 124 } 125 126 143 public void setErrorHandler(ErrorHandler handler) { 144 if (handler != null) { 145 _errorHandler = handler; 146 } else { 147 throw new NullPointerException(); 148 } 149 } 150 151 158 public ErrorHandler getErrorHandler() { 159 return (_errorHandler == DEFAULT_HANDLER) ? null : _errorHandler; 160 } 161 162 173 public void parse(InputStream in) throws IOException, SAXException { 174 _pullParser.setInput(in); 175 parseAll(); 176 } 177 178 190 public void parse(ByteBuffer byteBuffer) throws IOException, SAXException { 191 _pullParser.setInput(byteBuffer); 192 parseAll(); 193 } 194 195 204 public void parse(Reader reader) throws IOException, SAXException { 205 _pullParser.setInput(reader); 206 parseAll(); 207 } 208 209 225 public boolean getFeature(String name) throws SAXNotRecognizedException, 226 SAXNotSupportedException { 227 if (name.equals("http://xml.org/sax/features/namespaces")) { 228 return true; 229 } else if (name 230 .equals("http://xml.org/sax/features/namespace-prefixes")) { 231 return true; 232 } else { 233 throw new SAXNotRecognizedException("Feature " + name 234 + " not recognized"); 235 } 236 } 237 238 253 public void setFeature(String name, boolean value) 254 throws SAXNotRecognizedException, SAXNotSupportedException { 255 if (name.equals("http://xml.org/sax/features/namespaces") 256 || name 257 .equals("http://xml.org/sax/features/namespace-prefixes")) { 258 return; } else { 260 throw new SAXNotRecognizedException("Feature " + name 261 + " not recognized"); 262 } 263 } 264 265 277 public Object getProperty(String name) throws SAXNotRecognizedException, 278 SAXNotSupportedException { 279 throw new SAXNotRecognizedException("Property " + name 280 + " not recognized"); 281 } 282 283 294 public void setProperty(String name, Object value) 295 throws SAXNotRecognizedException, SAXNotSupportedException { 296 throw new SAXNotRecognizedException("Property " + name 297 + " not recognized"); 298 } 299 300 306 public void setEntityResolver(EntityResolver resolver) { 307 _entityResolver = resolver; 308 } 309 310 private EntityResolver _entityResolver; 311 312 319 public EntityResolver getEntityResolver() { 320 return _entityResolver; 321 } 322 323 328 public void setDTDHandler(DTDHandler handler) { 329 _dtdHandler = handler; 330 } 331 332 private DTDHandler _dtdHandler; 333 334 341 public DTDHandler getDTDHandler() { 342 return _dtdHandler; 343 } 344 345 public void reset() { 347 setContentHandler(DEFAULT_HANDLER); 348 setErrorHandler(DEFAULT_HANDLER); 349 _pullParser.reset(); 350 } 351 352 360 private void parseAll() throws IOException, SAXException { 361 try { 362 int eventType = _pullParser.getEventType(); 363 if (eventType != XmlPullParser.START_DOCUMENT) 364 throw new SAXException("Currently parsing"); 365 _contentHandler.startDocument(); 366 int namespaceCount = 0; 367 368 while (true) { 369 eventType = _pullParser.nextToken(); 370 if (eventType == XmlPullParser.START_TAG) { 371 372 final int depth = _pullParser.getDepth(); 374 final int nsStart = _pullParser 375 .getNamespaceCount(depth - 1); 376 final int nsEnd = _pullParser.getNamespaceCount(depth); 377 for (int i = nsStart; i < nsEnd; i++) { 378 CharSequence prefix = _pullParser.getNamespacePrefix(i); 379 CharSequence uri = _pullParser.getNamespaceUri(i); 380 _contentHandler.startPrefixMapping(prefix, uri); 381 } 382 383 CharSequence localName = _pullParser.getName(); 385 CharSequence uri = _pullParser.getNamespace(); 386 CharSequence qName = _pullParser.getQName(); 387 Attributes atts = _pullParser.getSaxAttributes(); 388 _contentHandler.startElement(uri, localName, qName, atts); 389 390 } else if (eventType == XmlPullParser.END_TAG) { 391 392 CharSequence localName = _pullParser.getName(); 394 CharSequence uri = _pullParser.getNamespace(); 395 CharSequence qName = _pullParser.getQName(); 396 _contentHandler.endElement(uri, localName, qName); 397 398 final int depth = _pullParser.getDepth(); 400 final int nsStart = _pullParser.getNamespaceCount(depth); 401 final int nsEnd = _pullParser.getNamespaceCount(depth + 1); 402 for (int i = nsStart; i < nsEnd; i++) { 403 CharSequence prefix = _pullParser.getNamespacePrefix(i); 404 _contentHandler.endPrefixMapping(prefix); 405 } 406 407 } else if ((eventType == XmlPullParser.TEXT) 408 || (eventType == XmlPullParser.CDSECT)) { 409 char ch[] = _pullParser.getTextCharacters(_startLength); 410 _contentHandler.characters(ch, _startLength[0], 411 _startLength[1]); 412 413 } else if (eventType == XmlPullParser.END_DOCUMENT) { 414 break; 415 416 } else { 417 } 419 } 420 } catch (XmlPullParserException e) { 421 SAXParseException error = new SAXParseException(e.getMessage(), 422 _locator); 423 _errorHandler.fatalError(error); 424 425 } finally { _contentHandler.endDocument(); 427 reset(); 428 } 429 } 430 431 int[] _startLength = new int[2]; 432 433 436 private class LocatorImpl implements Locator { 437 public String getPublicId() { 438 return null; 439 } 440 441 public String getSystemId() { 442 return null; 443 } 444 445 public int getLineNumber() { 446 return _pullParser.getLineNumber(); 447 } 448 449 public int getColumnNumber() { 450 return _pullParser.getColumnNumber(); 451 } 452 } 453 } | Popular Tags |