1 11 12 package org.cyberneko.html.parsers; 13 14 import org.cyberneko.html.HTMLConfiguration; 15 16 import org.apache.xerces.impl.Constants; 17 import org.apache.xerces.util.ErrorHandlerWrapper; 18 19 import org.apache.xerces.xni.Augmentations; 20 import org.apache.xerces.xni.NamespaceContext; 21 import org.apache.xerces.xni.QName; 22 import org.apache.xerces.xni.XMLAttributes; 23 import org.apache.xerces.xni.XMLDocumentHandler; 24 import org.apache.xerces.xni.XMLLocator; 25 import org.apache.xerces.xni.XMLResourceIdentifier; 26 import org.apache.xerces.xni.XMLString; 27 import org.apache.xerces.xni.XNIException; 28 29 import org.apache.xerces.xni.parser.XMLConfigurationException; 30 import org.apache.xerces.xni.parser.XMLDocumentSource; 31 import org.apache.xerces.xni.parser.XMLErrorHandler; 32 import org.apache.xerces.xni.parser.XMLInputSource; 33 import org.apache.xerces.xni.parser.XMLParseException; 34 import org.apache.xerces.xni.parser.XMLParserConfiguration; 35 36 import java.io.InputStream ; 37 import java.io.IOException ; 38 import java.io.Reader ; 39 40 import org.w3c.dom.Attr ; 41 import org.w3c.dom.CDATASection ; 42 import org.w3c.dom.Comment ; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.DocumentFragment ; 45 import org.w3c.dom.Element ; 46 import org.w3c.dom.EntityReference ; 47 import org.w3c.dom.Node ; 48 import org.w3c.dom.ProcessingInstruction ; 49 import org.w3c.dom.Text ; 50 51 import org.xml.sax.ErrorHandler ; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXException ; 54 import org.xml.sax.SAXParseException ; 55 import org.xml.sax.SAXNotRecognizedException ; 56 import org.xml.sax.SAXNotSupportedException ; 57 58 65 public class DOMFragmentParser 66 implements XMLDocumentHandler { 67 68 72 74 75 protected static final String DOCUMENT_FRAGMENT = 76 "http://cyberneko.org/html/features/document-fragment"; 77 78 79 protected static final String [] RECOGNIZED_FEATURES = { 80 DOCUMENT_FRAGMENT, 81 }; 82 83 85 86 protected static final String ERROR_HANDLER = 87 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 88 89 90 protected static final String CURRENT_ELEMENT_NODE = 91 Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY; 92 93 94 protected static final String [] RECOGNIZED_PROPERTIES = { 95 ERROR_HANDLER, 96 CURRENT_ELEMENT_NODE, 97 }; 98 99 103 104 protected XMLParserConfiguration fParserConfiguration; 105 106 107 protected XMLDocumentSource fDocumentSource; 108 109 110 protected DocumentFragment fDocumentFragment; 111 112 113 protected Document fDocument; 114 115 116 protected Node fCurrentNode; 117 118 119 protected boolean fInCDATASection; 120 121 125 126 public DOMFragmentParser() { 127 fParserConfiguration = new HTMLConfiguration(); 128 fParserConfiguration.addRecognizedFeatures(RECOGNIZED_FEATURES); 129 fParserConfiguration.addRecognizedProperties(RECOGNIZED_PROPERTIES); 130 fParserConfiguration.setFeature(DOCUMENT_FRAGMENT, true); 131 fParserConfiguration.setDocumentHandler(this); 132 } 134 138 139 public void parse(String systemId, DocumentFragment fragment) 140 throws SAXException , IOException { 141 parse(new InputSource(systemId), fragment); 142 } 144 145 public void parse(InputSource source, DocumentFragment fragment) 146 throws SAXException , IOException { 147 148 fCurrentNode = fDocumentFragment = fragment; 149 fDocument = fDocumentFragment.getOwnerDocument(); 150 151 try { 152 String pubid = source.getPublicId(); 153 String sysid = source.getSystemId(); 154 String encoding = source.getEncoding(); 155 InputStream stream = source.getByteStream(); 156 Reader reader = source.getCharacterStream(); 157 158 XMLInputSource inputSource = 159 new XMLInputSource(pubid, sysid, sysid); 160 inputSource.setEncoding(encoding); 161 inputSource.setByteStream(stream); 162 inputSource.setCharacterStream(reader); 163 164 fParserConfiguration.parse(inputSource); 165 } 166 catch (XMLParseException e) { 167 Exception ex = e.getException(); 168 if (ex != null) { 169 throw new SAXParseException (e.getMessage(), null, ex); 170 } 171 throw new SAXParseException (e.getMessage(), null); 172 } 173 174 } 176 194 public void setErrorHandler(ErrorHandler errorHandler) { 195 fParserConfiguration.setErrorHandler(new ErrorHandlerWrapper(errorHandler)); 196 } 198 205 public ErrorHandler getErrorHandler() { 206 207 ErrorHandler errorHandler = null; 208 try { 209 XMLErrorHandler xmlErrorHandler = 210 (XMLErrorHandler)fParserConfiguration.getProperty(ERROR_HANDLER); 211 if (xmlErrorHandler != null && 212 xmlErrorHandler instanceof ErrorHandlerWrapper) { 213 errorHandler = ((ErrorHandlerWrapper)xmlErrorHandler).getErrorHandler(); 214 } 215 } 216 catch (XMLConfigurationException e) { 217 } 219 return errorHandler; 220 221 } 223 237 public void setFeature(String featureId, boolean state) 238 throws SAXNotRecognizedException , SAXNotSupportedException { 239 240 try { 241 fParserConfiguration.setFeature(featureId, state); 242 } 243 catch (XMLConfigurationException e) { 244 String message = e.getMessage(); 245 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 246 throw new SAXNotRecognizedException (message); 247 } 248 else { 249 throw new SAXNotSupportedException (message); 250 } 251 } 252 253 } 255 269 public boolean getFeature(String featureId) 270 throws SAXNotRecognizedException , SAXNotSupportedException { 271 272 try { 273 return fParserConfiguration.getFeature(featureId); 274 } 275 catch (XMLConfigurationException e) { 276 String message = e.getMessage(); 277 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 278 throw new SAXNotRecognizedException (message); 279 } 280 else { 281 throw new SAXNotSupportedException (message); 282 } 283 } 284 285 } 287 302 public void setProperty(String propertyId, Object value) 303 throws SAXNotRecognizedException , SAXNotSupportedException { 304 305 try { 306 fParserConfiguration.setProperty(propertyId, value); 307 } 308 catch (XMLConfigurationException e) { 309 String message = e.getMessage(); 310 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 311 throw new SAXNotRecognizedException (message); 312 } 313 else { 314 throw new SAXNotSupportedException (message); 315 } 316 } 317 318 } 320 334 public Object getProperty(String propertyId) 335 throws SAXNotRecognizedException , SAXNotSupportedException { 336 337 if (propertyId.equals(CURRENT_ELEMENT_NODE)) { 338 return (fCurrentNode!=null && 339 fCurrentNode.getNodeType() == Node.ELEMENT_NODE)? fCurrentNode:null; 340 } 341 342 try { 343 return fParserConfiguration.getProperty(propertyId); 344 } 345 catch (XMLConfigurationException e) { 346 String message = e.getMessage(); 347 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 348 throw new SAXNotRecognizedException (message); 349 } 350 else { 351 throw new SAXNotSupportedException (message); 352 } 353 } 354 355 } 357 361 362 public void setDocumentSource(XMLDocumentSource source) { 363 fDocumentSource = source; 364 } 366 367 public XMLDocumentSource getDocumentSource() { 368 return fDocumentSource; 369 } 371 372 public void startDocument(XMLLocator locator, String encoding, 373 Augmentations augs) throws XNIException { 374 startDocument(locator, encoding, null, augs); 375 } 377 379 380 public void startDocument(XMLLocator locator, String encoding, 381 NamespaceContext nscontext, 382 Augmentations augs) throws XNIException { 383 fInCDATASection = false; 384 } 386 387 public void xmlDecl(String version, String encoding, 388 String standalone, Augmentations augs) 389 throws XNIException { 390 } 392 393 public void doctypeDecl(String root, String pubid, String sysid, 394 Augmentations augs) throws XNIException { 395 } 397 398 public void processingInstruction(String target, XMLString data, 399 Augmentations augs) 400 throws XNIException { 401 ProcessingInstruction pi = 402 fDocument.createProcessingInstruction(target, data.toString()); 403 fCurrentNode.appendChild(pi); 404 } 406 407 public void comment(XMLString text, Augmentations augs) 408 throws XNIException { 409 Comment comment = fDocument.createComment(text.toString()); 410 fCurrentNode.appendChild(comment); 411 } 413 414 public void startPrefixMapping(String prefix, String uri, 415 Augmentations augs) throws XNIException { 416 } 418 419 public void endPrefixMapping(String prefix, Augmentations augs) 420 throws XNIException { 421 } 423 424 public void startElement(QName element, XMLAttributes attrs, 425 Augmentations augs) throws XNIException { 426 Element elementNode = fDocument.createElement(element.rawname); 427 int count = attrs != null ? attrs.getLength() : 0; 428 for (int i = 0; i < count; i++) { 429 String aname = attrs.getQName(i); 430 String avalue = attrs.getValue(i); 431 elementNode.setAttribute(aname, avalue); 432 } 433 fCurrentNode.appendChild(elementNode); 434 fCurrentNode = elementNode; 435 } 437 438 public void emptyElement(QName element, XMLAttributes attrs, 439 Augmentations augs) throws XNIException { 440 startElement(element, attrs, augs); 441 endElement(element, augs); 442 } 444 445 public void characters(XMLString text, Augmentations augs) 446 throws XNIException { 447 448 if (fInCDATASection) { 449 Node node = fCurrentNode.getLastChild(); 450 if (node != null && node.getNodeType() == Node.CDATA_SECTION_NODE) { 451 CDATASection cdata = (CDATASection )node; 452 cdata.appendData(text.toString()); 453 } 454 else { 455 CDATASection cdata = fDocument.createCDATASection(text.toString()); 456 fCurrentNode.appendChild(cdata); 457 } 458 } 459 else { 460 Node node = fCurrentNode.getLastChild(); 461 if (node != null && node.getNodeType() == Node.TEXT_NODE) { 462 Text textNode = (Text )node; 463 textNode.appendData(text.toString()); 464 } 465 else { 466 Text textNode = fDocument.createTextNode(text.toString()); 467 fCurrentNode.appendChild(textNode); 468 } 469 } 470 471 } 473 474 public void ignorableWhitespace(XMLString text, Augmentations augs) 475 throws XNIException { 476 characters(text, augs); 477 } 479 480 public void startGeneralEntity(String name, XMLResourceIdentifier id, 481 String encoding, Augmentations augs) 482 throws XNIException { 483 EntityReference entityRef = fDocument.createEntityReference(name); 484 fCurrentNode.appendChild(entityRef); 485 fCurrentNode = entityRef; 486 } 488 489 public void textDecl(String version, String encoding, 490 Augmentations augs) throws XNIException { 491 } 493 494 public void endGeneralEntity(String name, Augmentations augs) 495 throws XNIException { 496 fCurrentNode = fCurrentNode.getParentNode(); 497 } 499 500 public void startCDATA(Augmentations augs) throws XNIException { 501 fInCDATASection = true; 502 } 504 505 public void endCDATA(Augmentations augs) throws XNIException { 506 fInCDATASection = false; 507 } 509 510 public void endElement(QName element, Augmentations augs) 511 throws XNIException { 512 fCurrentNode = fCurrentNode.getParentNode(); 513 } 515 516 public void endDocument(Augmentations augs) throws XNIException { 517 } 519 523 576 577 } | Popular Tags |