1 package com.icl.saxon; 2 import com.icl.saxon.om.*; 3 import com.icl.saxon.tree.TreeBuilder; 4 5 import org.xml.sax.*; 6 import org.xml.sax.ext.LexicalHandler ; 7 import org.xml.sax.helpers.AttributesImpl ; 8 import org.xml.sax.helpers.NamespaceSupport ; 9 import org.xml.sax.helpers.DefaultHandler ; 10 11 import java.util.Enumeration ; 12 import java.util.Locale ; 13 import java.util.Hashtable ; 14 15 import org.w3c.dom.*; 16 17 27 28 public class DOMDriver implements Locator, XMLReader 29 { 30 31 protected ContentHandler contentHandler = new DefaultHandler (); 32 private LexicalHandler lexicalHandler = null; 33 private NamespaceSupport nsSupport = new NamespaceSupport (); 34 private AttributesImpl attlist = new AttributesImpl (); 35 private String [] parts = new String [3]; 36 private String [] elparts = new String [3]; 37 private Hashtable nsDeclarations = new Hashtable (); 38 protected Node root = null; 39 protected String systemId; 40 41 46 47 public void setContentHandler (ContentHandler handler) 48 { 49 this.contentHandler = handler; 50 if (handler instanceof LexicalHandler ) { 51 lexicalHandler = (LexicalHandler )handler; 52 } 53 } 54 55 public ContentHandler getContentHandler() { 56 return contentHandler; 57 } 58 59 64 65 public void setLocale (Locale locale) throws SAXException 66 {} 67 68 69 73 74 public EntityResolver getEntityResolver () 75 { 76 return null; 77 } 78 79 83 84 public void setEntityResolver (EntityResolver resolver) {} 85 86 87 91 92 public DTDHandler getDTDHandler () { 93 return null; 94 } 95 96 100 public void setDTDHandler (DTDHandler handler) {} 101 102 103 115 116 public void setDocumentHandler (DocumentHandler handler) {} 117 118 122 123 public void setErrorHandler (ErrorHandler handler) {} 124 125 129 130 public ErrorHandler getErrorHandler () { return null; } 131 132 133 136 137 140 public void setStartNode(Node start) { 141 root = start; 142 } 143 144 148 149 public void parse(InputSource source) throws SAXException { 150 parse(); 151 }; 152 153 157 158 public void parse(String source) throws SAXException { 159 parse(); 160 }; 161 162 167 168 public void parse() throws SAXException 169 { 170 if (root==null) { 171 throw new SAXException("DOMDriver: no start node defined"); 172 } 173 if (contentHandler==null) { 174 throw new SAXException("DOMDriver: no content handler defined"); 175 } 176 177 contentHandler.setDocumentLocator(this); 178 contentHandler.startDocument(); 179 if (root instanceof Element) { 180 sendElement((Element)root); 181 } else { 182 walkNode(root); } 184 contentHandler.endDocument(); 185 } 186 187 192 193 private void sendElement(Element startNode) throws SAXException { 194 Element node = startNode; 195 NamedNodeMap topAtts = gatherNamespaces(node, false); 196 while (true) { 197 gatherNamespaces(node, true); 198 Node parent = node.getParentNode(); 199 if (parent instanceof Element) { 200 node = (Element)parent; 201 } else { 202 break; 203 } 204 } 205 outputElement(startNode, topAtts); 206 } 207 208 214 215 private void walkNode (Node node) throws SAXException 216 { 217 if (node.hasChildNodes()) { 218 NodeList nit = node.getChildNodes(); 219 for (int i=0; i<nit.getLength(); i++) { 220 Node child = nit.item(i); 221 switch (child.getNodeType()) { 222 case Node.DOCUMENT_NODE: 223 break; case Node.ELEMENT_NODE: 225 Element element = (Element)child; 226 nsSupport.pushContext(); 227 attlist.clear(); 228 nsDeclarations.clear(); 229 230 235 String elname = element.getTagName(); 236 try { 239 String prefix = element.getPrefix(); 240 String uri = element.getNamespaceURI(); 241 if (nsDeclarations.get(prefix)==null) { 243 nsSupport.declarePrefix(prefix, uri); 244 contentHandler.startPrefixMapping(prefix, uri); 245 nsDeclarations.put(prefix, uri); 246 } 247 } catch (Throwable err) { 248 } 250 252 NamedNodeMap atts = element.getAttributes(); 253 for (int a1=0; a1<atts.getLength(); a1++) { 254 Attr att = (Attr)atts.item(a1); 255 String attname = att.getName(); 256 if (attname.equals("xmlns")) { 257 if (nsDeclarations.get("")==null) { 259 String uri = att.getValue(); 260 nsSupport.declarePrefix("", uri); 261 contentHandler.startPrefixMapping("", uri); 262 nsDeclarations.put("", uri); 263 } 264 } else if (attname.startsWith("xmlns:")) { 265 String prefix = attname.substring(6); 267 if (nsDeclarations.get(prefix)==null) { 268 String uri = att.getValue(); 269 nsSupport.declarePrefix(prefix, uri); 270 contentHandler.startPrefixMapping(prefix, uri); 271 nsDeclarations.put(prefix, uri); 272 } 273 } else if (attname.indexOf(':')>=0) { 274 try { 275 String prefix = att.getPrefix(); 276 String uri = att.getNamespaceURI(); 277 if (nsDeclarations.get(prefix)==null) { 279 nsSupport.declarePrefix(prefix, uri); 280 contentHandler.startPrefixMapping(prefix, uri); 281 nsDeclarations.put(prefix, uri); 282 } 283 } catch (Throwable err) { 284 } 286 } 287 } 288 for (int a2=0; a2<atts.getLength(); a2++) { 289 Attr att = (Attr)atts.item(a2); 290 String attname = att.getName(); 291 if (!attname.equals("xmlns") && !attname.startsWith("xmlns:")) { 292 String [] parts2 = nsSupport.processName(attname, parts, true); 294 if (parts2==null) { 295 throw new SAXException("Undeclared namespace in " + attname); 296 } 297 attlist.addAttribute( 298 parts2[0], parts2[1], parts2[2], "CDATA", att.getValue()); 299 } 300 } 301 String [] elparts2 = nsSupport.processName(element.getTagName(), elparts, false); 302 if (elparts2==null) { 303 throw new SAXException("Undeclared namespace in " + element.getTagName()); 304 } 305 String uri = elparts2[0]; 306 String local = elparts2[1]; 307 String raw = elparts2[2]; 308 309 contentHandler.startElement(uri, local, raw, attlist); 310 311 walkNode(element); 312 313 contentHandler.endElement(uri, local, raw); 314 Enumeration prefixes = nsSupport.getDeclaredPrefixes(); 315 while (prefixes.hasMoreElements()) { 316 String prefix = (String )prefixes.nextElement(); 317 contentHandler.endPrefixMapping(prefix); 318 } 319 nsSupport.popContext(); 320 break; 321 322 case Node.ATTRIBUTE_NODE: break; 324 case Node.PROCESSING_INSTRUCTION_NODE: 325 contentHandler.processingInstruction( 326 ((ProcessingInstruction)child).getTarget(), 327 ((ProcessingInstruction)child).getData()); 328 break; 329 case Node.COMMENT_NODE: 330 if (lexicalHandler!=null) { 331 String text = ((Comment)child).getData(); 332 if (text!=null) { 333 lexicalHandler.comment(text.toCharArray(), 0, text.length()); 334 } 335 } 336 break; 337 case Node.TEXT_NODE: 338 case Node.CDATA_SECTION_NODE: 339 String text = ((CharacterData)child).getData(); 340 if (text!=null) { 341 contentHandler.characters(text.toCharArray(), 0, text.length()); 342 } 343 break; 344 case Node.ENTITY_REFERENCE_NODE: 345 walkNode(child); 346 break; 347 default: 348 break; } 350 } 351 } 352 353 } 354 355 private void outputElement(Element element, NamedNodeMap atts) throws SAXException { 356 String [] elparts2 = nsSupport.processName(element.getTagName(), elparts, false); 357 if (elparts2==null) { 358 throw new SAXException("Undeclared namespace in " + element.getTagName()); 359 } 360 String uri = elparts2[0]; 361 String local = elparts2[1]; 362 String raw = elparts2[2]; 363 364 for (int a2=0; a2<atts.getLength(); a2++) { 365 Attr att = (Attr)atts.item(a2); 366 String attname = att.getName(); 367 if (!attname.equals("xmlns") && !attname.startsWith("xmlns:")) { 368 String [] parts2 = nsSupport.processName(attname, parts, true); 370 if (parts2==null) { 371 throw new SAXException("Undeclared namespace in " + attname); 372 } 373 attlist.addAttribute( 374 parts2[0], parts2[1], parts2[2], "CDATA", att.getValue()); 375 } 376 } 377 378 contentHandler.startElement(uri, local, raw, attlist); 379 380 walkNode(element); 381 382 contentHandler.endElement(uri, local, raw); 383 Enumeration prefixes = nsSupport.getDeclaredPrefixes(); 384 while (prefixes.hasMoreElements()) { 385 String prefix = (String )prefixes.nextElement(); 386 contentHandler.endPrefixMapping(prefix); 387 } 388 } 389 390 private NamedNodeMap gatherNamespaces(Element element, boolean cumulative) { 391 if (!cumulative) { 392 nsSupport.pushContext(); 393 attlist.clear(); 394 nsDeclarations.clear(); 395 } 396 397 402 try { 403 String prefix = element.getPrefix(); 404 String uri = element.getNamespaceURI(); 405 if (prefix==null) prefix=""; 406 if (uri==null) uri=""; 407 if (nsDeclarations.get(prefix)==null) { 409 nsSupport.declarePrefix(prefix, uri); 410 nsDeclarations.put(prefix, uri); 411 } 412 } catch (Throwable err) { 413 } 415 416 NamedNodeMap atts = element.getAttributes(); 417 for (int a1=0; a1<atts.getLength(); a1++) { 418 Attr att = (Attr)atts.item(a1); 419 String attname = att.getName(); 420 if (attname.equals("xmlns")) { 421 if (nsDeclarations.get("")==null) { 423 String uri = att.getValue(); 424 nsSupport.declarePrefix("", uri); 425 nsDeclarations.put("", uri); 426 } 427 } else if (attname.startsWith("xmlns:")) { 428 String prefix = attname.substring(6); 430 if (nsDeclarations.get(prefix)==null) { 431 String uri = att.getValue(); 432 nsSupport.declarePrefix(prefix, uri); 433 nsDeclarations.put(prefix, uri); 434 } 435 } else if (attname.indexOf(':')>=0) { 436 try { 437 String prefix = att.getPrefix(); 438 String uri = att.getNamespaceURI(); 439 if (nsDeclarations.get(prefix)==null) { 441 nsSupport.declarePrefix(prefix, uri); 442 nsDeclarations.put(prefix, uri); 444 } 445 } catch (Throwable err) { 446 } 448 } 449 } 450 return atts; 451 } 452 453 457 public void setSystemId(String systemId) { 458 this.systemId = systemId; 459 } 460 461 public String getPublicId () 462 { 463 return null; } 465 466 public String getSystemId () 467 { 468 return systemId; 469 } 470 471 public int getLineNumber () 472 { 473 return -1; 474 } 475 476 public int getColumnNumber () 477 { 478 return -1; 479 } 480 481 483 static final String FEATURE = "http://xml.org/sax/features/"; 484 static final String HANDLER = "http://xml.org/sax/properties/"; 485 486 492 493 public boolean getFeature (String featureId) throws SAXNotRecognizedException 494 { 495 if ((FEATURE + "validation").equals (featureId)) 496 return false; 497 498 if ((FEATURE + "external-general-entities").equals (featureId) 500 || (FEATURE + "external-parameter-entities").equals (featureId)) 501 return false; 502 503 if ((FEATURE + "namespace-prefixes").equals (featureId)) 505 return false; 506 507 if ((FEATURE + "namespaces").equals (featureId)) 509 return true; 510 511 if ((FEATURE + "string-interning").equals (featureId)) 513 return false; 514 515 throw new SAXNotRecognizedException (featureId); 516 } 517 518 524 525 public Object getProperty (String name) throws SAXNotRecognizedException { 526 if (name.equals("http://xml.org/sax/properties/lexical-handler")) { 527 return lexicalHandler; 528 } else { 529 throw new SAXNotRecognizedException(name); 530 } 531 } 532 533 538 public void setFeature (String featureId, boolean on) 539 throws SAXNotRecognizedException, SAXNotSupportedException 540 { 541 if ((FEATURE + "validation").equals (featureId)) 542 if (on) { 543 throw new SAXNotSupportedException(featureId + " feature cannot be switched on"); 544 } else { 545 return; 546 } 547 548 if ((FEATURE + "external-general-entities").equals (featureId) 550 || (FEATURE + "external-parameter-entities").equals (featureId) ) 551 if (on) { 552 throw new SAXNotSupportedException(featureId + " feature cannot be switched on"); 553 } else { 554 return; 555 } 556 557 if ((FEATURE + "namespace-prefixes").equals (featureId)) 559 if (on) { 560 throw new SAXNotSupportedException(featureId + " feature cannot be switched on"); 561 } else { 562 return; 563 } 564 565 if ((FEATURE + "namespaces").equals (featureId)) 567 if (!on) { 568 throw new SAXNotSupportedException(featureId + " feature cannot be switched off"); 569 } else { 570 return; 571 } 572 573 if ((FEATURE + "string-interning").equals (featureId)) 575 if (on) { 576 throw new SAXNotSupportedException(featureId + " feature cannot be switched on"); 577 } else { 578 return; 579 } 580 581 throw new SAXNotRecognizedException("Feature not recognized: " + featureId); 582 } 583 584 588 589 public void setProperty (String propertyId, Object property) 590 throws SAXNotRecognizedException, SAXNotSupportedException { 591 if (propertyId.equals("http://xml.org/sax/properties/lexical-handler")) { 592 if (property instanceof LexicalHandler ) { 593 lexicalHandler = (LexicalHandler )property; 594 } else { 595 throw new SAXNotSupportedException( 596 "Lexical Handler must be instance of org.xml.sax.ext.LexicalHandler"); 597 } 598 } else { 599 throw new SAXNotRecognizedException(propertyId); 600 } 601 } 602 603 } 604 | Popular Tags |