1 51 package org.apache.fop.tools; 52 53 import java.io.IOException ; 54 55 import org.w3c.dom.*; 57 58 import org.xml.sax.*; 60 import org.xml.sax.helpers.AttributesImpl ; 61 62 69 70 public class DocumentReader implements XMLReader { 71 72 private boolean _namespaces = true; 76 private boolean _namespace_prefixes = true; 77 78 79 134 public boolean getFeature(String name) 135 throws SAXNotRecognizedException, SAXNotSupportedException { 136 if ("http://xml.org/sax/features/namespaces".equals(name)) { 137 return _namespaces; 138 } else if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) { 139 return _namespace_prefixes; 140 } else { 141 throw new SAXNotRecognizedException("Feature '" + name 142 + "' not recognized or supported by Document2SAXAdapter"); 143 } 144 145 } 146 147 148 149 176 public void setFeature(String name, boolean value) 177 throws SAXNotRecognizedException, SAXNotSupportedException { 178 if ("http://xml.org/sax/features/namespaces".equals(name)) { 179 _namespaces = value; 180 } else if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) { 181 _namespace_prefixes = value; 182 } else { 183 throw new SAXNotRecognizedException("Feature '" + name 184 + "' not recognized or supported by Document2SAXAdapter"); 185 } 186 187 } 188 189 190 191 218 public Object getProperty(String name) 219 throws SAXNotRecognizedException, SAXNotSupportedException { 220 throw new SAXNotRecognizedException("Property '" + name 221 + "' not recognized or supported by Document2SAXAdapter"); 222 } 223 224 225 226 253 public void setProperty(String name, Object value) 254 throws SAXNotRecognizedException, SAXNotSupportedException { 255 throw new SAXNotRecognizedException("Property '" + name 256 + "' not recognized or supported by Document2SAXAdapter"); 257 } 258 259 260 261 private EntityResolver _entityResolver = null; 265 private DTDHandler _dtdHandler = null; 266 private ContentHandler _contentHandler = null; 267 private ErrorHandler _errorHandler = null; 268 269 270 285 public void setEntityResolver(EntityResolver resolver) { 286 _entityResolver = resolver; 287 } 288 289 290 291 298 public EntityResolver getEntityResolver() { 299 return _entityResolver; 300 } 301 302 303 304 319 public void setDTDHandler(DTDHandler handler) { 320 _dtdHandler = handler; 321 } 322 323 324 325 332 public DTDHandler getDTDHandler() { 333 return _dtdHandler; 334 } 335 336 337 338 354 public void setContentHandler(ContentHandler handler) { 355 _contentHandler = handler; 356 } 357 358 359 360 367 public ContentHandler getContentHandler() { 368 return _contentHandler; 369 } 370 371 372 373 391 public void setErrorHandler(ErrorHandler handler) { 392 _errorHandler = handler; 393 } 394 395 402 public ErrorHandler getErrorHandler() { 403 return _errorHandler; 404 } 405 406 407 408 412 431 public void parse(InputSource input) throws IOException , SAXException { 432 if (input instanceof DocumentInputSource) { 433 Document document = ((DocumentInputSource)input).getDocument(); 434 if (_contentHandler == null) { 435 throw new SAXException("ContentHandler is null. Please use setContentHandler()"); 436 } 437 438 440 441 Node currentNode; 442 AttributesImpl currentAtts; 443 444 445 char[] array = null; 446 447 currentAtts = new AttributesImpl (); 448 449 450 currentNode = document; 451 while (currentNode != null) { 452 switch (currentNode.getNodeType()) { 453 case Node.DOCUMENT_NODE: 454 _contentHandler.startDocument(); 455 break; 456 case Node.CDATA_SECTION_NODE: 457 case Node.TEXT_NODE: 458 String data = currentNode.getNodeValue(); 459 int datalen = data.length(); 460 if (array == null || array.length < datalen) { 461 465 array = new char[datalen]; 466 } 467 data.getChars(0, datalen, array, 0); 468 _contentHandler.characters(array, 0, datalen); 469 break; 470 case Node.PROCESSING_INSTRUCTION_NODE: 471 _contentHandler.processingInstruction(currentNode.getNodeName(), 472 currentNode.getNodeValue()); 473 break; 474 case Node.ELEMENT_NODE: 475 NamedNodeMap map = currentNode.getAttributes(); 476 currentAtts.clear(); 477 for (int i = map.getLength() - 1; i >= 0; i--) { 478 Attr att = (Attr)map.item(i); 479 currentAtts.addAttribute(att.getNamespaceURI(), 480 att.getLocalName(), 481 att.getName(), "CDATA", 482 att.getValue()); 483 } 484 _contentHandler.startElement(currentNode.getNamespaceURI(), 485 currentNode.getLocalName(), 486 currentNode.getNodeName(), 487 currentAtts); 488 break; 489 } 490 491 Node nextNode = currentNode.getFirstChild(); 492 if (nextNode != null) { 493 currentNode = nextNode; 494 continue; 495 } 496 497 while (currentNode != null) { 498 switch (currentNode.getNodeType()) { 499 case Node.DOCUMENT_NODE: 500 _contentHandler.endDocument(); 501 break; 502 case Node.ELEMENT_NODE: 503 _contentHandler.endElement(currentNode.getNamespaceURI(), 504 currentNode.getLocalName(), 505 currentNode.getNodeName()); 506 break; 507 } 508 509 nextNode = currentNode.getNextSibling(); 510 if (nextNode != null) { 511 currentNode = nextNode; 512 break; 513 } 514 515 currentNode = currentNode.getParentNode(); 516 } 517 } 518 519 } else { 520 throw new SAXException("DocumentReader only supports parsing of a DocumentInputSource"); 521 } 522 523 } 524 525 526 527 540 public void parse(String systemId) throws IOException , SAXException { 541 throw new SAXException("DocumentReader only supports parsing of a DocumentInputSource"); 542 } 543 544 } 545 546 547 | Popular Tags |