1 16 19 20 package org.apache.xalan.xsltc.trax; 21 22 import java.io.IOException ; 23 24 import org.w3c.dom.NamedNodeMap ; 25 import org.w3c.dom.Node ; 26 import org.apache.xml.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 org.apache.xml.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 String prefix; 141 final NamedNodeMap map = node.getAttributes(); 142 final int length = map.getLength(); 143 144 NamespaceMappings nm = new NamespaceMappings(); 146 for (int i = 0; i < length; i++) { 147 int colon; 148 final Node attr = map.item(i); 149 final String qnameAttr = attr.getNodeName(); 150 151 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 else { 159 final String uriAttr = attr.getNamespaceURI(); 160 if (uriAttr != null && !uriAttr.equals(EMPTYSTRING) ) { 162 colon = qnameAttr.lastIndexOf(':'); 163 164 String newPrefix = nm.lookupPrefix(uriAttr); 169 if (newPrefix == null) 170 newPrefix = nm.generateNextPrefix(); 171 prefix = (colon > 0) ? qnameAttr.substring(0, colon) 172 : newPrefix; 173 _handler.namespaceAfterStartElement(prefix, uriAttr); 174 _handler.addAttribute((prefix + ":" + qnameAttr) 175 , attr.getNodeValue()); 176 } else { 177 _handler.addAttribute(qnameAttr, attr.getNodeValue()); 178 } 179 } 180 } 181 182 final String uri = node.getNamespaceURI(); 184 final String localName = node.getLocalName(); 185 186 if (uri != null) { 188 final int colon = qname.lastIndexOf(':'); 189 prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING; 190 _handler.namespaceAfterStartElement(prefix, uri); 191 }else { 192 if (uri == null && localName != null) { 198 prefix = EMPTYSTRING; 199 _handler.namespaceAfterStartElement(prefix, EMPTYSTRING); 200 } 201 } 202 203 next = node.getFirstChild(); 205 while (next != null) { 206 parse(next); 207 next = next.getNextSibling(); 208 } 209 210 _handler.endElement(qname); 212 break; 213 214 case Node.PROCESSING_INSTRUCTION_NODE: 215 _handler.processingInstruction(node.getNodeName(), 216 node.getNodeValue()); 217 break; 218 219 case Node.TEXT_NODE: 220 _handler.characters(node.getNodeValue()); 221 break; 222 } 223 } 224 225 229 public DTDHandler getDTDHandler() { 230 return null; 231 } 232 233 237 public ErrorHandler getErrorHandler() { 238 return null; 239 } 240 241 245 public boolean getFeature(String name) throws SAXNotRecognizedException , 246 SAXNotSupportedException 247 { 248 return false; 249 } 250 251 255 public void setFeature(String name, boolean value) throws 256 SAXNotRecognizedException , SAXNotSupportedException 257 { 258 } 259 260 264 public void parse(String sysId) throws IOException , SAXException { 265 throw new IOException ("This method is not yet implemented."); 266 } 267 268 272 public void setDTDHandler(DTDHandler handler) throws NullPointerException { 273 } 274 275 279 public void setEntityResolver(EntityResolver resolver) throws 280 NullPointerException 281 { 282 } 283 284 288 public EntityResolver getEntityResolver() { 289 return null; 290 } 291 292 296 public void setErrorHandler(ErrorHandler handler) throws 297 NullPointerException 298 { 299 } 300 301 305 public void setProperty(String name, Object value) throws 306 SAXNotRecognizedException , SAXNotSupportedException { 307 } 308 309 313 public Object getProperty(String name) throws SAXNotRecognizedException , 314 SAXNotSupportedException 315 { 316 return null; 317 } 318 319 323 public int getColumnNumber() { 324 return 0; 325 } 326 327 331 public int getLineNumber() { 332 return 0; 333 } 334 335 339 public String getPublicId() { 340 return null; 341 } 342 343 347 public String getSystemId() { 348 return null; 349 } 350 351 private String getNodeTypeFromCode(short code) { 353 String retval = null; 354 switch (code) { 355 case Node.ATTRIBUTE_NODE : 356 retval = "ATTRIBUTE_NODE"; break; 357 case Node.CDATA_SECTION_NODE : 358 retval = "CDATA_SECTION_NODE"; break; 359 case Node.COMMENT_NODE : 360 retval = "COMMENT_NODE"; break; 361 case Node.DOCUMENT_FRAGMENT_NODE : 362 retval = "DOCUMENT_FRAGMENT_NODE"; break; 363 case Node.DOCUMENT_NODE : 364 retval = "DOCUMENT_NODE"; break; 365 case Node.DOCUMENT_TYPE_NODE : 366 retval = "DOCUMENT_TYPE_NODE"; break; 367 case Node.ELEMENT_NODE : 368 retval = "ELEMENT_NODE"; break; 369 case Node.ENTITY_NODE : 370 retval = "ENTITY_NODE"; break; 371 case Node.ENTITY_REFERENCE_NODE : 372 retval = "ENTITY_REFERENCE_NODE"; break; 373 case Node.NOTATION_NODE : 374 retval = "NOTATION_NODE"; break; 375 case Node.PROCESSING_INSTRUCTION_NODE : 376 retval = "PROCESSING_INSTRUCTION_NODE"; break; 377 case Node.TEXT_NODE: 378 retval = "TEXT_NODE"; break; 379 } 380 return retval; 381 } 382 } 383 | Popular Tags |