1 16 19 20 21 package com.sun.org.apache.xalan.internal.xsltc.trax; 22 23 import java.io.IOException ; 24 import java.util.Hashtable ; 25 import java.util.Stack ; 26 import java.util.Vector ; 27 28 import org.w3c.dom.NamedNodeMap ; 29 import org.w3c.dom.Node ; 30 31 import org.xml.sax.ContentHandler ; 32 import org.xml.sax.DTDHandler ; 33 import org.xml.sax.EntityResolver ; 34 import org.xml.sax.ErrorHandler ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.Locator ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.SAXNotRecognizedException ; 39 import org.xml.sax.SAXNotSupportedException ; 40 import org.xml.sax.XMLReader ; 41 import org.xml.sax.ext.LexicalHandler ; 42 import org.xml.sax.helpers.AttributesImpl ; 43 import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl; 44 45 48 public class DOM2SAX implements XMLReader , Locator { 49 50 private final static String EMPTYSTRING = ""; 51 private static final String XMLNS_PREFIX = "xmlns"; 52 53 private Node _dom = null; 54 private ContentHandler _sax = null; 55 private LexicalHandler _lex = null; 56 private SAXImpl _saxImpl = null; 57 private Hashtable _nsPrefixes = new Hashtable (); 58 59 public DOM2SAX(Node root) { 60 _dom = root; 61 } 62 63 public ContentHandler getContentHandler() { 64 return _sax; 65 } 66 67 public void setContentHandler(ContentHandler handler) throws 68 NullPointerException 69 { 70 _sax = handler; 71 if (handler instanceof LexicalHandler ) { 72 _lex = (LexicalHandler ) handler; 73 } 74 75 if (handler instanceof SAXImpl) { 76 _saxImpl = (SAXImpl)handler; 77 } 78 } 79 80 85 private boolean startPrefixMapping(String prefix, String uri) 86 throws SAXException 87 { 88 boolean pushed = true; 89 Stack uriStack = (Stack ) _nsPrefixes.get(prefix); 90 91 if (uriStack != null) { 92 if (uriStack.isEmpty()) { 93 _sax.startPrefixMapping(prefix, uri); 94 uriStack.push(uri); 95 } 96 else { 97 final String lastUri = (String ) uriStack.peek(); 98 if (!lastUri.equals(uri)) { 99 _sax.startPrefixMapping(prefix, uri); 100 uriStack.push(uri); 101 } 102 else { 103 pushed = false; 104 } 105 } 106 } 107 else { 108 _sax.startPrefixMapping(prefix, uri); 109 _nsPrefixes.put(prefix, uriStack = new Stack ()); 110 uriStack.push(uri); 111 } 112 return pushed; 113 } 114 115 119 private void endPrefixMapping(String prefix) 120 throws SAXException 121 { 122 final Stack uriStack = (Stack ) _nsPrefixes.get(prefix); 123 124 if (uriStack != null) { 125 _sax.endPrefixMapping(prefix); 126 uriStack.pop(); 127 } 128 } 129 130 135 private static String getLocalName(Node node) { 136 final String localName = node.getLocalName(); 137 138 if (localName == null) { 139 final String qname = node.getNodeName(); 140 final int col = qname.lastIndexOf(':'); 141 return (col > 0) ? qname.substring(col + 1) : qname; 142 } 143 return localName; 144 } 145 146 public void parse(InputSource unused) throws IOException , SAXException { 147 parse(_dom); 148 } 149 150 public void parse() throws IOException , SAXException { 151 if (_dom != null) { 152 boolean isIncomplete = 153 (_dom.getNodeType() != org.w3c.dom.Node.DOCUMENT_NODE); 154 155 if (isIncomplete) { 156 _sax.startDocument(); 157 parse(_dom); 158 _sax.endDocument(); 159 } 160 else { 161 parse(_dom); 162 } 163 } 164 } 165 166 171 private void parse(Node node) throws IOException , SAXException { 172 Node first = null; 173 if (node == null) return; 174 175 switch (node.getNodeType()) { 176 case Node.ATTRIBUTE_NODE: case Node.DOCUMENT_FRAGMENT_NODE: 178 case Node.DOCUMENT_TYPE_NODE : 179 case Node.ENTITY_NODE : 180 case Node.ENTITY_REFERENCE_NODE: 181 case Node.NOTATION_NODE : 182 break; 184 case Node.CDATA_SECTION_NODE: 185 final String cdata = node.getNodeValue(); 186 if (_lex != null) { 187 _lex.startCDATA(); 188 _sax.characters(cdata.toCharArray(), 0, cdata.length()); 189 _lex.endCDATA(); 190 } 191 else { 192 _sax.characters(cdata.toCharArray(), 0, cdata.length()); 195 } 196 break; 197 198 case Node.COMMENT_NODE: if (_lex != null) { 200 final String value = node.getNodeValue(); 201 _lex.comment(value.toCharArray(), 0, value.length()); 202 } 203 break; 204 case Node.DOCUMENT_NODE: 205 _sax.setDocumentLocator(this); 206 207 _sax.startDocument(); 208 Node next = node.getFirstChild(); 209 while (next != null) { 210 parse(next); 211 next = next.getNextSibling(); 212 } 213 _sax.endDocument(); 214 break; 215 216 case Node.ELEMENT_NODE: 217 String prefix; 218 Vector pushedPrefixes = new Vector (); 219 final AttributesImpl attrs = new AttributesImpl (); 220 final NamedNodeMap map = node.getAttributes(); 221 final int length = map.getLength(); 222 223 for (int i = 0; i < length; i++) { 225 final Node attr = map.item(i); 226 final String qnameAttr = attr.getNodeName(); 227 228 if (qnameAttr.startsWith(XMLNS_PREFIX)) { 230 final String uriAttr = attr.getNodeValue(); 231 final int colon = qnameAttr.lastIndexOf(':'); 232 prefix = (colon > 0) ? qnameAttr.substring(colon + 1) : EMPTYSTRING; 233 if (startPrefixMapping(prefix, uriAttr)) { 234 pushedPrefixes.addElement(prefix); 235 } 236 } 237 } 238 239 for (int i = 0; i < length; i++) { 241 final Node attr = map.item(i); 242 final String qnameAttr = attr.getNodeName(); 243 244 if (!qnameAttr.startsWith(XMLNS_PREFIX)) { 246 final String uriAttr = attr.getNamespaceURI(); 247 final String localNameAttr = getLocalName(attr); 248 249 if (uriAttr != null) { 251 final int colon = qnameAttr.lastIndexOf(':'); 252 prefix = (colon > 0) ? qnameAttr.substring(0, colon) : EMPTYSTRING; 253 if (startPrefixMapping(prefix, uriAttr)) { 254 pushedPrefixes.addElement(prefix); 255 } 256 } 257 258 attrs.addAttribute(attr.getNamespaceURI(), getLocalName(attr), 260 qnameAttr, "CDATA", attr.getNodeValue()); 261 } 262 } 263 264 final String qname = node.getNodeName(); 266 final String uri = node.getNamespaceURI(); 267 final String localName = getLocalName(node); 268 269 if (uri != null) { 271 final int colon = qname.lastIndexOf(':'); 272 prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING; 273 if (startPrefixMapping(prefix, uri)) { 274 pushedPrefixes.addElement(prefix); 275 } 276 } 277 278 if (_saxImpl != null) { 280 _saxImpl.startElement(uri, localName, qname, attrs, node); 281 } 282 else { 283 _sax.startElement(uri, localName, qname, attrs); 284 } 285 286 next = node.getFirstChild(); 288 while (next != null) { 289 parse(next); 290 next = next.getNextSibling(); 291 } 292 293 _sax.endElement(uri, localName, qname); 295 296 final int nPushedPrefixes = pushedPrefixes.size(); 298 for (int i = 0; i < nPushedPrefixes; i++) { 299 endPrefixMapping((String ) pushedPrefixes.elementAt(i)); 300 } 301 break; 302 303 case Node.PROCESSING_INSTRUCTION_NODE: 304 _sax.processingInstruction(node.getNodeName(), 305 node.getNodeValue()); 306 break; 307 308 case Node.TEXT_NODE: 309 final String data = node.getNodeValue(); 310 _sax.characters(data.toCharArray(), 0, data.length()); 311 break; 312 } 313 } 314 315 319 public DTDHandler getDTDHandler() { 320 return null; 321 } 322 323 327 public ErrorHandler getErrorHandler() { 328 return null; 329 } 330 331 335 public boolean getFeature(String name) throws SAXNotRecognizedException , 336 SAXNotSupportedException 337 { 338 return false; 339 } 340 341 345 public void setFeature(String name, boolean value) throws 346 SAXNotRecognizedException , SAXNotSupportedException 347 { 348 } 349 350 354 public void parse(String sysId) throws IOException , SAXException { 355 throw new IOException ("This method is not yet implemented."); 356 } 357 358 362 public void setDTDHandler(DTDHandler handler) throws NullPointerException { 363 } 364 365 369 public void setEntityResolver(EntityResolver resolver) throws 370 NullPointerException 371 { 372 } 373 374 378 public EntityResolver getEntityResolver() { 379 return null; 380 } 381 382 386 public void setErrorHandler(ErrorHandler handler) throws 387 NullPointerException 388 { 389 } 390 391 395 public void setProperty(String name, Object value) throws 396 SAXNotRecognizedException , SAXNotSupportedException { 397 } 398 399 403 public Object getProperty(String name) throws SAXNotRecognizedException , 404 SAXNotSupportedException 405 { 406 return null; 407 } 408 409 413 public int getColumnNumber() { 414 return 0; 415 } 416 417 421 public int getLineNumber() { 422 return 0; 423 } 424 425 429 public String getPublicId() { 430 return null; 431 } 432 433 437 public String getSystemId() { 438 return null; 439 } 440 441 private String getNodeTypeFromCode(short code) { 443 String retval = null; 444 switch (code) { 445 case Node.ATTRIBUTE_NODE : 446 retval = "ATTRIBUTE_NODE"; break; 447 case Node.CDATA_SECTION_NODE : 448 retval = "CDATA_SECTION_NODE"; break; 449 case Node.COMMENT_NODE : 450 retval = "COMMENT_NODE"; break; 451 case Node.DOCUMENT_FRAGMENT_NODE : 452 retval = "DOCUMENT_FRAGMENT_NODE"; break; 453 case Node.DOCUMENT_NODE : 454 retval = "DOCUMENT_NODE"; break; 455 case Node.DOCUMENT_TYPE_NODE : 456 retval = "DOCUMENT_TYPE_NODE"; break; 457 case Node.ELEMENT_NODE : 458 retval = "ELEMENT_NODE"; break; 459 case Node.ENTITY_NODE : 460 retval = "ENTITY_NODE"; break; 461 case Node.ENTITY_REFERENCE_NODE : 462 retval = "ENTITY_REFERENCE_NODE"; break; 463 case Node.NOTATION_NODE : 464 retval = "NOTATION_NODE"; break; 465 case Node.PROCESSING_INSTRUCTION_NODE : 466 retval = "PROCESSING_INSTRUCTION_NODE"; break; 467 case Node.TEXT_NODE: 468 retval = "TEXT_NODE"; break; 469 } 470 return retval; 471 } 472 } 473 | Popular Tags |