1 16 19 package com.sun.org.apache.xml.internal.serializer; 20 21 import java.io.File ; 22 23 import com.sun.org.apache.xml.internal.serializer.utils.AttList; 24 import com.sun.org.apache.xml.internal.serializer.utils.DOM2Helper; 25 import org.w3c.dom.Comment ; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.EntityReference ; 28 import org.w3c.dom.NamedNodeMap ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.ProcessingInstruction ; 31 import org.w3c.dom.Text ; 32 33 import org.xml.sax.ContentHandler ; 34 import org.xml.sax.Locator ; 35 import org.xml.sax.ext.LexicalHandler ; 36 import org.xml.sax.helpers.LocatorImpl ; 37 38 47 48 public final class TreeWalker 49 { 50 51 52 final private ContentHandler m_contentHandler; 53 57 final private SerializationHandler m_Serializer; 58 59 62 63 final protected DOM2Helper m_dh; 64 65 66 final private LocatorImpl m_locator = new LocatorImpl (); 67 68 73 public ContentHandler getContentHandler() 74 { 75 return m_contentHandler; 76 } 77 78 public TreeWalker(ContentHandler ch) { 79 this(ch,null); 80 } 81 86 public TreeWalker(ContentHandler contentHandler, String systemId) 87 { 88 m_contentHandler = contentHandler; 90 if (m_contentHandler instanceof SerializationHandler) { 91 m_Serializer = (SerializationHandler) m_contentHandler; 92 } 93 else 94 m_Serializer = null; 95 96 m_contentHandler.setDocumentLocator(m_locator); 98 if (systemId != null) 99 m_locator.setSystemId(systemId); 100 else { 101 try { 102 m_locator.setSystemId(System.getProperty("user.dir") + File.separator + "dummy.xsl"); 104 } 105 catch (SecurityException se) { } 107 } 108 109 if (m_contentHandler != null) 111 m_contentHandler.setDocumentLocator(m_locator); 112 try { 113 m_locator.setSystemId(System.getProperty("user.dir") + File.separator + "dummy.xsl"); 115 } 116 catch (SecurityException se){ 118 } 119 m_dh = new DOM2Helper(); 120 } 121 122 134 public void traverse(Node pos) throws org.xml.sax.SAXException 135 { 136 137 this.m_contentHandler.startDocument(); 138 139 Node top = pos; 140 141 while (null != pos) 142 { 143 startNode(pos); 144 145 Node nextNode = pos.getFirstChild(); 146 147 while (null == nextNode) 148 { 149 endNode(pos); 150 151 if (top.equals(pos)) 152 break; 153 154 nextNode = pos.getNextSibling(); 155 156 if (null == nextNode) 157 { 158 pos = pos.getParentNode(); 159 160 if ((null == pos) || (top.equals(pos))) 161 { 162 if (null != pos) 163 endNode(pos); 164 165 nextNode = null; 166 167 break; 168 } 169 } 170 } 171 172 pos = nextNode; 173 } 174 this.m_contentHandler.endDocument(); 175 } 176 177 190 public void traverse(Node pos, Node top) throws org.xml.sax.SAXException 191 { 192 193 this.m_contentHandler.startDocument(); 194 195 while (null != pos) 196 { 197 startNode(pos); 198 199 Node nextNode = pos.getFirstChild(); 200 201 while (null == nextNode) 202 { 203 endNode(pos); 204 205 if ((null != top) && top.equals(pos)) 206 break; 207 208 nextNode = pos.getNextSibling(); 209 210 if (null == nextNode) 211 { 212 pos = pos.getParentNode(); 213 214 if ((null == pos) || ((null != top) && top.equals(pos))) 215 { 216 nextNode = null; 217 218 break; 219 } 220 } 221 } 222 223 pos = nextNode; 224 } 225 this.m_contentHandler.endDocument(); 226 } 227 228 229 boolean nextIsRaw = false; 230 231 234 private final void dispatachChars(Node node) 235 throws org.xml.sax.SAXException 236 { 237 if(m_Serializer != null) 238 { 239 this.m_Serializer.characters(node); 240 } 241 else 242 { 243 String data = ((Text ) node).getData(); 244 this.m_contentHandler.characters(data.toCharArray(), 0, data.length()); 245 } 246 } 247 248 256 protected void startNode(Node node) throws org.xml.sax.SAXException 257 { 258 259 269 if (node instanceof Locator ) 270 { 271 Locator loc = (Locator )node; 272 m_locator.setColumnNumber(loc.getColumnNumber()); 273 m_locator.setLineNumber(loc.getLineNumber()); 274 m_locator.setPublicId(loc.getPublicId()); 275 m_locator.setSystemId(loc.getSystemId()); 276 } 277 else 278 { 279 m_locator.setColumnNumber(0); 280 m_locator.setLineNumber(0); 281 } 282 283 switch (node.getNodeType()) 284 { 285 case Node.COMMENT_NODE : 286 { 287 String data = ((Comment ) node).getData(); 288 289 if (m_contentHandler instanceof LexicalHandler ) 290 { 291 LexicalHandler lh = ((LexicalHandler ) this.m_contentHandler); 292 293 lh.comment(data.toCharArray(), 0, data.length()); 294 } 295 } 296 break; 297 case Node.DOCUMENT_FRAGMENT_NODE : 298 299 break; 301 case Node.DOCUMENT_NODE : 302 303 break; 304 case Node.ELEMENT_NODE : 305 Element elem_node = (Element ) node; 306 { 307 String uri = elem_node.getNamespaceURI(); 311 if (uri != null) { 312 String prefix = elem_node.getPrefix(); 313 if (prefix==null) 314 prefix=""; 315 this.m_contentHandler.startPrefixMapping(prefix,uri); 316 } 317 } 318 NamedNodeMap atts = elem_node.getAttributes(); 319 int nAttrs = atts.getLength(); 320 322 323 for (int i = 0; i < nAttrs; i++) 326 { 327 final Node attr = atts.item(i); 328 final String attrName = attr.getNodeName(); 329 final int colon = attrName.indexOf(':'); 330 final String prefix; 331 332 if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) 334 { 335 if (colon < 0) 339 prefix = ""; 340 else 341 prefix = attrName.substring(colon + 1); 342 343 this.m_contentHandler.startPrefixMapping(prefix, 344 attr.getNodeValue()); 345 } 346 else if (colon > 0) { 347 prefix = attrName.substring(0,colon); 348 String uri = attr.getNamespaceURI(); 349 if (uri != null) 350 this.m_contentHandler.startPrefixMapping(prefix,uri); 351 } 352 } 353 354 String ns = m_dh.getNamespaceOfNode(node); 355 if(null == ns) 356 ns = ""; 357 this.m_contentHandler.startElement(ns, 358 m_dh.getLocalNameOfNode(node), 359 node.getNodeName(), 360 new AttList(atts, m_dh)); 361 break; 362 case Node.PROCESSING_INSTRUCTION_NODE : 363 { 364 ProcessingInstruction pi = (ProcessingInstruction ) node; 365 String name = pi.getNodeName(); 366 367 if (name.equals("xslt-next-is-raw")) 369 { 370 nextIsRaw = true; 371 } 372 else 373 { 374 this.m_contentHandler.processingInstruction(pi.getNodeName(), 375 pi.getData()); 376 } 377 } 378 break; 379 case Node.CDATA_SECTION_NODE : 380 { 381 boolean isLexH = (m_contentHandler instanceof LexicalHandler ); 382 LexicalHandler lh = isLexH 383 ? ((LexicalHandler ) this.m_contentHandler) : null; 384 385 if (isLexH) 386 { 387 lh.startCDATA(); 388 } 389 390 dispatachChars(node); 391 392 { 393 if (isLexH) 394 { 395 lh.endCDATA(); 396 } 397 } 398 } 399 break; 400 case Node.TEXT_NODE : 401 { 402 404 if (nextIsRaw) 405 { 406 nextIsRaw = false; 407 408 m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, ""); 409 dispatachChars(node); 410 m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, ""); 411 } 412 else 413 { 414 dispatachChars(node); 415 } 416 } 417 break; 418 case Node.ENTITY_REFERENCE_NODE : 419 { 420 EntityReference eref = (EntityReference ) node; 421 422 if (m_contentHandler instanceof LexicalHandler ) 423 { 424 ((LexicalHandler ) this.m_contentHandler).startEntity( 425 eref.getNodeName()); 426 } 427 else 428 { 429 430 } 432 } 433 break; 434 default : 435 } 436 } 437 438 446 protected void endNode(Node node) throws org.xml.sax.SAXException 447 { 448 449 switch (node.getNodeType()) 450 { 451 case Node.DOCUMENT_NODE : 452 break; 453 454 case Node.ELEMENT_NODE : 455 String ns = m_dh.getNamespaceOfNode(node); 456 if(null == ns) 457 ns = ""; 458 this.m_contentHandler.endElement(ns, 459 m_dh.getLocalNameOfNode(node), 460 node.getNodeName()); 461 462 if (m_Serializer == null) { 463 Element elem_node = (Element ) node; 467 NamedNodeMap atts = elem_node.getAttributes(); 468 int nAttrs = atts.getLength(); 469 470 for (int i = (nAttrs-1); 0 <= i; i--) 473 { 474 final Node attr = atts.item(i); 475 final String attrName = attr.getNodeName(); 476 final int colon = attrName.indexOf(':'); 477 final String prefix; 478 479 if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) 480 { 481 if (colon < 0) 485 prefix = ""; 486 else 487 prefix = attrName.substring(colon + 1); 488 489 this.m_contentHandler.endPrefixMapping(prefix); 490 } 491 else if (colon > 0) { 492 prefix = attrName.substring(0, colon); 493 this.m_contentHandler.endPrefixMapping(prefix); 494 } 495 } 496 { 497 String uri = elem_node.getNamespaceURI(); 498 if (uri != null) { 499 String prefix = elem_node.getPrefix(); 500 if (prefix==null) 501 prefix=""; 502 this.m_contentHandler.endPrefixMapping(prefix); 503 } 504 } 505 } 506 break; 507 case Node.CDATA_SECTION_NODE : 508 break; 509 case Node.ENTITY_REFERENCE_NODE : 510 { 511 EntityReference eref = (EntityReference ) node; 512 513 if (m_contentHandler instanceof LexicalHandler ) 514 { 515 LexicalHandler lh = ((LexicalHandler ) this.m_contentHandler); 516 517 lh.endEntity(eref.getNodeName()); 518 } 519 } 520 break; 521 default : 522 } 523 } 524 } 526 | Popular Tags |