1 7 package org.xml.sax.helpers; 8 9 import java.io.IOException ; 10 import java.util.Locale ; 11 12 import org.xml.sax.Parser ; import org.xml.sax.Locator ; 14 import org.xml.sax.InputSource ; 15 import org.xml.sax.AttributeList ; import org.xml.sax.EntityResolver ; 17 import org.xml.sax.DTDHandler ; 18 import org.xml.sax.DocumentHandler ; import org.xml.sax.ErrorHandler ; 20 import org.xml.sax.SAXException ; 21 22 import org.xml.sax.XMLReader ; 23 import org.xml.sax.Attributes ; 24 import org.xml.sax.ContentHandler ; 25 import org.xml.sax.SAXNotSupportedException ; 26 27 28 52 public class XMLReaderAdapter implements Parser , ContentHandler 53 { 54 55 56 60 61 71 public XMLReaderAdapter () 72 throws SAXException 73 { 74 setup(XMLReaderFactory.createXMLReader()); 75 } 76 77 78 88 public XMLReaderAdapter (XMLReader xmlReader) 89 { 90 setup(xmlReader); 91 } 92 93 94 95 100 private void setup (XMLReader xmlReader) 101 { 102 if (xmlReader == null) { 103 throw new NullPointerException ("XMLReader must not be null"); 104 } 105 this.xmlReader = xmlReader; 106 qAtts = new AttributesAdapter(); 107 } 108 109 110 111 115 116 126 public void setLocale (Locale locale) 127 throws SAXException 128 { 129 throw new SAXNotSupportedException ("setLocale not supported"); 130 } 131 132 133 139 public void setEntityResolver (EntityResolver resolver) 140 { 141 xmlReader.setEntityResolver(resolver); 142 } 143 144 145 151 public void setDTDHandler (DTDHandler handler) 152 { 153 xmlReader.setDTDHandler(handler); 154 } 155 156 157 166 public void setDocumentHandler (DocumentHandler handler) 167 { 168 documentHandler = handler; 169 } 170 171 172 178 public void setErrorHandler (ErrorHandler handler) 179 { 180 xmlReader.setErrorHandler(handler); 181 } 182 183 184 199 public void parse (String systemId) 200 throws IOException , SAXException 201 { 202 parse(new InputSource (systemId)); 203 } 204 205 206 221 public void parse (InputSource input) 222 throws IOException , SAXException 223 { 224 setupXMLReader(); 225 xmlReader.parse(input); 226 } 227 228 229 232 private void setupXMLReader () 233 throws SAXException 234 { 235 xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); 236 try { 237 xmlReader.setFeature("http://xml.org/sax/features/namespaces", 238 false); 239 } catch (SAXException e) { 240 } 242 xmlReader.setContentHandler(this); 243 } 244 245 246 247 251 252 258 public void setDocumentLocator (Locator locator) 259 { 260 if (documentHandler != null) 261 documentHandler.setDocumentLocator(locator); 262 } 263 264 265 272 public void startDocument () 273 throws SAXException 274 { 275 if (documentHandler != null) 276 documentHandler.startDocument(); 277 } 278 279 280 287 public void endDocument () 288 throws SAXException 289 { 290 if (documentHandler != null) 291 documentHandler.endDocument(); 292 } 293 294 295 302 public void startPrefixMapping (String prefix, String uri) 303 { 304 } 305 306 307 313 public void endPrefixMapping (String prefix) 314 { 315 } 316 317 318 329 public void startElement (String uri, String localName, 330 String qName, Attributes atts) 331 throws SAXException 332 { 333 if (documentHandler != null) { 334 qAtts.setAttributes(atts); 335 documentHandler.startElement(qName, qAtts); 336 } 337 } 338 339 340 350 public void endElement (String uri, String localName, 351 String qName) 352 throws SAXException 353 { 354 if (documentHandler != null) 355 documentHandler.endElement(qName); 356 } 357 358 359 369 public void characters (char ch[], int start, int length) 370 throws SAXException 371 { 372 if (documentHandler != null) 373 documentHandler.characters(ch, start, length); 374 } 375 376 377 387 public void ignorableWhitespace (char ch[], int start, int length) 388 throws SAXException 389 { 390 if (documentHandler != null) 391 documentHandler.ignorableWhitespace(ch, start, length); 392 } 393 394 395 404 public void processingInstruction (String target, String data) 405 throws SAXException 406 { 407 if (documentHandler != null) 408 documentHandler.processingInstruction(target, data); 409 } 410 411 412 419 public void skippedEntity (String name) 420 throws SAXException 421 { 422 } 423 424 425 426 430 XMLReader xmlReader; 431 DocumentHandler documentHandler; 432 AttributesAdapter qAtts; 433 434 435 436 440 441 444 final class AttributesAdapter implements AttributeList 445 { 446 AttributesAdapter () 447 { 448 } 449 450 451 456 void setAttributes (Attributes attributes) 457 { 458 this.attributes = attributes; 459 } 460 461 462 468 public int getLength () 469 { 470 return attributes.getLength(); 471 } 472 473 474 480 public String getName (int i) 481 { 482 return attributes.getQName(i); 483 } 484 485 486 492 public String getType (int i) 493 { 494 return attributes.getType(i); 495 } 496 497 498 504 public String getValue (int i) 505 { 506 return attributes.getValue(i); 507 } 508 509 510 516 public String getType (String qName) 517 { 518 return attributes.getType(qName); 519 } 520 521 522 528 public String getValue (String qName) 529 { 530 return attributes.getValue(qName); 531 } 532 533 private Attributes attributes; 534 } 535 536 } 537 538 | Popular Tags |