1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.trax; 21 22 import java.io.IOException ; 23 24 import org.w3c.dom.NamedNodeMap ; 25 import org.w3c.dom.Node ; 26 import com.sun.org.apache.xml.internal.serializer.SerializationHandler; 27 import org.xml.sax.ContentHandler ; 28 import org.xml.sax.DTDHandler ; 29 import org.xml.sax.EntityResolver ; 30 import org.xml.sax.ErrorHandler ; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.Locator ; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.SAXNotRecognizedException ; 35 import org.xml.sax.SAXNotSupportedException ; 36 import org.xml.sax.XMLReader ; 37 import com.sun.org.apache.xml.internal.serializer.NamespaceMappings; 38 39 42 public class DOM2TO implements XMLReader , Locator { 43 44 private final static String EMPTYSTRING = ""; 45 private static final String XMLNS_PREFIX = "xmlns"; 46 47 50 private Node _dom; 51 52 55 private SerializationHandler _handler; 56 57 public DOM2TO(Node root, SerializationHandler handler) { 58 _dom = root; 59 _handler = handler; 60 } 61 62 public ContentHandler getContentHandler() { 63 return null; 64 } 65 66 public void setContentHandler(ContentHandler handler) { 67 } 69 70 public void parse(InputSource unused) throws IOException , SAXException { 71 parse(_dom); 72 } 73 74 public void parse() throws IOException , SAXException { 75 if (_dom != null) { 76 boolean isIncomplete = 77 (_dom.getNodeType() != org.w3c.dom.Node.DOCUMENT_NODE); 78 79 if (isIncomplete) { 80 _handler.startDocument(); 81 parse(_dom); 82 _handler.endDocument(); 83 } 84 else { 85 parse(_dom); 86 } 87 } 88 } 89 90 94 private void parse(Node node) 95 throws IOException , SAXException 96 { 97 if (node == null) return; 98 99 switch (node.getNodeType()) { 100 case Node.ATTRIBUTE_NODE: case Node.DOCUMENT_TYPE_NODE : 102 case Node.ENTITY_NODE : 103 case Node.ENTITY_REFERENCE_NODE: 104 case Node.NOTATION_NODE : 105 break; 107 case Node.CDATA_SECTION_NODE: 108 _handler.startCDATA(); 109 _handler.characters(node.getNodeValue()); 110 _handler.endCDATA(); 111 break; 112 113 case Node.COMMENT_NODE: _handler.comment(node.getNodeValue()); 115 break; 116 117 case Node.DOCUMENT_NODE: 118 _handler.startDocument(); 119 Node next = node.getFirstChild(); 120 while (next != null) { 121 parse(next); 122 next = next.getNextSibling(); 123 } 124 _handler.endDocument(); 125 break; 126 127 case Node.DOCUMENT_FRAGMENT_NODE: 128 next = node.getFirstChild(); 129 while (next != null) { 130 parse(next); 131 next = next.getNextSibling(); 132 } 133 break; 134 135 case Node.ELEMENT_NODE: 136 final String qname = node.getNodeName(); 138 _handler.startElement(null, null, qname); 139 140 int colon; 141 String prefix; 142 final NamedNodeMap map = node.getAttributes(); 143 final int length = map.getLength(); 144 145 for (int i = 0; i < length; i++) { 147 final Node attr = map.item(i); 148 final String qnameAttr = attr.getNodeName(); 149 150 if (qnameAttr.startsWith(XMLNS_PREFIX)) { 152 final String uriAttr = attr.getNodeValue(); 153 colon = qnameAttr.lastIndexOf(':'); 154 prefix = (colon > 0) ? qnameAttr.substring(colon + 1) 155 : EMPTYSTRING; 156 _handler.namespaceAfterStartElement(prefix, uriAttr); 157 } 158 } 159 160 NamespaceMappings nm = new NamespaceMappings(); 162 for (int i = 0; i < length; i++) { 163 final Node attr = map.item(i); 164 final String qnameAttr = attr.getNodeName(); 165 166 if (!qnameAttr.startsWith(XMLNS_PREFIX)) { 168 final String uriAttr = attr.getNamespaceURI(); 169 if (uriAttr != null && !uriAttr.equals(EMPTYSTRING) ) { 171 colon = qnameAttr.lastIndexOf(':'); 172 173 String newPrefix = nm.lookupPrefix(uriAttr); 178 if (newPrefix == null) 179 newPrefix = nm.generateNextPrefix(); 180 prefix = (colon > 0) ? qnameAttr.substring(0, colon) 181 : newPrefix; 182 _handler.namespaceAfterStartElement(prefix, uriAttr); 183 _handler.addAttribute((prefix + ":" + qnameAttr), 184 attr.getNodeValue()); 185 } else { 186 _handler.addAttribute(qnameAttr, attr.getNodeValue()); 187 } 188 } 189 } 190 191 final String uri = node.getNamespaceURI(); 193 final String localName = node.getLocalName(); 194 195 if (uri != null) { 197 colon = qname.lastIndexOf(':'); 198 prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING; 199 _handler.namespaceAfterStartElement(prefix, uri); 200 }else { 201 if (uri == null && localName != null) { 207 prefix = EMPTYSTRING; 208 _handler.namespaceAfterStartElement(prefix, EMPTYSTRING); 209 } 210 } 211 212 next = node.getFirstChild(); 214 while (next != null) { 215 parse(next); 216 next = next.getNextSibling(); 217 } 218 219 _handler.endElement(qname); 221 break; 222 223 case Node.PROCESSING_INSTRUCTION_NODE: 224 _handler.processingInstruction(node.getNodeName(), 225 node.getNodeValue()); 226 break; 227 228 case Node.TEXT_NODE: 229 _handler.characters(node.getNodeValue()); 230 break; 231 } 232 } 233 234 238 public DTDHandler getDTDHandler() { 239 return null; 240 } 241 242 246 public ErrorHandler getErrorHandler() { 247 return null; 248 } 249 250 254 public boolean getFeature(String name) throws SAXNotRecognizedException , 255 SAXNotSupportedException 256 { 257 return false; 258 } 259 260 264 public void setFeature(String name, boolean value) throws 265 SAXNotRecognizedException , SAXNotSupportedException 266 { 267 } 268 269 273 public void parse(String sysId) throws IOException , SAXException { 274 throw new IOException ("This method is not yet implemented."); 275 } 276 277 281 public void setDTDHandler(DTDHandler handler) throws NullPointerException { 282 } 283 284 288 public void setEntityResolver(EntityResolver resolver) throws 289 NullPointerException 290 { 291 } 292 293 297 public EntityResolver getEntityResolver() { 298 return null; 299 } 300 301 305 public void setErrorHandler(ErrorHandler handler) throws 306 NullPointerException 307 { 308 } 309 310 314 public void setProperty(String name, Object value) throws 315 SAXNotRecognizedException , SAXNotSupportedException { 316 } 317 318 322 public Object getProperty(String name) throws SAXNotRecognizedException , 323 SAXNotSupportedException 324 { 325 return null; 326 } 327 328 332 public int getColumnNumber() { 333 return 0; 334 } 335 336 340 public int getLineNumber() { 341 return 0; 342 } 343 344 348 public String getPublicId() { 349 return null; 350 } 351 352 356 public String getSystemId() { 357 return null; 358 } 359 360 private String getNodeTypeFromCode(short code) { 362 String retval = null; 363 switch (code) { 364 case Node.ATTRIBUTE_NODE : 365 retval = "ATTRIBUTE_NODE"; break; 366 case Node.CDATA_SECTION_NODE : 367 retval = "CDATA_SECTION_NODE"; break; 368 case Node.COMMENT_NODE : 369 retval = "COMMENT_NODE"; break; 370 case Node.DOCUMENT_FRAGMENT_NODE : 371 retval = "DOCUMENT_FRAGMENT_NODE"; break; 372 case Node.DOCUMENT_NODE : 373 retval = "DOCUMENT_NODE"; break; 374 case Node.DOCUMENT_TYPE_NODE : 375 retval = "DOCUMENT_TYPE_NODE"; break; 376 case Node.ELEMENT_NODE : 377 retval = "ELEMENT_NODE"; break; 378 case Node.ENTITY_NODE : 379 retval = "ENTITY_NODE"; break; 380 case Node.ENTITY_REFERENCE_NODE : 381 retval = "ENTITY_REFERENCE_NODE"; break; 382 case Node.NOTATION_NODE : 383 retval = "NOTATION_NODE"; break; 384 case Node.PROCESSING_INSTRUCTION_NODE : 385 retval = "PROCESSING_INSTRUCTION_NODE"; break; 386 case Node.TEXT_NODE: 387 retval = "TEXT_NODE"; break; 388 } 389 return retval; 390 } 391 } 392 | Popular Tags |