1 16 19 package org.apache.xml.utils; 20 21 import java.io.File ; 22 23 import org.w3c.dom.Comment ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.EntityReference ; 26 import org.w3c.dom.NamedNodeMap ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.ProcessingInstruction ; 29 import org.w3c.dom.Text ; 30 31 import org.xml.sax.ContentHandler ; 32 import org.xml.sax.Locator ; 33 import org.xml.sax.ext.LexicalHandler ; 34 import org.xml.sax.helpers.LocatorImpl ; 35 36 41 42 public class TreeWalker 43 { 44 45 46 private ContentHandler m_contentHandler = null; 47 48 51 52 protected DOMHelper m_dh; 53 54 55 private LocatorImpl m_locator = new LocatorImpl (); 56 57 62 public ContentHandler getContentHandler() 63 { 64 return m_contentHandler; 65 } 66 67 72 public void setContentHandler(ContentHandler ch) 73 { 74 m_contentHandler = ch; 75 } 76 77 83 public TreeWalker(ContentHandler contentHandler, DOMHelper dh, String systemId) 84 { 85 this.m_contentHandler = contentHandler; 86 m_contentHandler.setDocumentLocator(m_locator); 87 if (systemId != null) 88 m_locator.setSystemId(systemId); 89 else { 90 try { 91 m_locator.setSystemId(System.getProperty("user.dir") + File.separator + "dummy.xsl"); 93 } 94 catch (SecurityException se) { } 96 } 97 m_dh = dh; 98 } 99 100 105 public TreeWalker(ContentHandler contentHandler, DOMHelper dh) 106 { 107 this.m_contentHandler = contentHandler; 108 m_contentHandler.setDocumentLocator(m_locator); 109 try { 110 m_locator.setSystemId(System.getProperty("user.dir") + File.separator + "dummy.xsl"); 112 } 113 catch (SecurityException se){ } 115 m_dh = dh; 116 } 117 118 123 public TreeWalker(ContentHandler contentHandler) 124 { 125 this.m_contentHandler = contentHandler; 126 if (m_contentHandler != null) 127 m_contentHandler.setDocumentLocator(m_locator); 128 try { 129 m_locator.setSystemId(System.getProperty("user.dir") + File.separator + "dummy.xsl"); 131 } 132 catch (SecurityException se){ 134 } 135 m_dh = new DOM2Helper(); 136 } 137 138 150 public void traverse(Node pos) throws org.xml.sax.SAXException 151 { 152 153 this.m_contentHandler.startDocument(); 154 155 Node top = pos; 156 157 while (null != pos) 158 { 159 startNode(pos); 160 161 Node nextNode = pos.getFirstChild(); 162 163 while (null == nextNode) 164 { 165 endNode(pos); 166 167 if (top.equals(pos)) 168 break; 169 170 nextNode = pos.getNextSibling(); 171 172 if (null == nextNode) 173 { 174 pos = pos.getParentNode(); 175 176 if ((null == pos) || (top.equals(pos))) 177 { 178 if (null != pos) 179 endNode(pos); 180 181 nextNode = null; 182 183 break; 184 } 185 } 186 } 187 188 pos = nextNode; 189 } 190 this.m_contentHandler.endDocument(); 191 } 192 193 206 public void traverse(Node pos, Node top) throws org.xml.sax.SAXException 207 { 208 209 this.m_contentHandler.startDocument(); 210 211 while (null != pos) 212 { 213 startNode(pos); 214 215 Node nextNode = pos.getFirstChild(); 216 217 while (null == nextNode) 218 { 219 endNode(pos); 220 221 if ((null != top) && top.equals(pos)) 222 break; 223 224 nextNode = pos.getNextSibling(); 225 226 if (null == nextNode) 227 { 228 pos = pos.getParentNode(); 229 230 if ((null == pos) || ((null != top) && top.equals(pos))) 231 { 232 nextNode = null; 233 234 break; 235 } 236 } 237 } 238 239 pos = nextNode; 240 } 241 this.m_contentHandler.endDocument(); 242 } 243 244 245 boolean nextIsRaw = false; 246 247 250 private final void dispatachChars(Node node) 251 throws org.xml.sax.SAXException 252 { 253 if(m_contentHandler instanceof org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.CharacterNodeHandler) 254 { 255 ((org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.CharacterNodeHandler)m_contentHandler).characters(node); 256 } 257 else 258 { 259 String data = ((Text ) node).getData(); 260 this.m_contentHandler.characters(data.toCharArray(), 0, data.length()); 261 } 262 } 263 264 272 protected void startNode(Node node) throws org.xml.sax.SAXException 273 { 274 275 if (m_contentHandler instanceof NodeConsumer) 276 { 277 ((NodeConsumer) m_contentHandler).setOriginatingNode(node); 278 } 279 280 if (node instanceof Locator ) 281 { 282 Locator loc = (Locator )node; 283 m_locator.setColumnNumber(loc.getColumnNumber()); 284 m_locator.setLineNumber(loc.getLineNumber()); 285 m_locator.setPublicId(loc.getPublicId()); 286 m_locator.setSystemId(loc.getSystemId()); 287 } 288 else 289 { 290 m_locator.setColumnNumber(0); 291 m_locator.setLineNumber(0); 292 } 293 294 switch (node.getNodeType()) 295 { 296 case Node.COMMENT_NODE : 297 { 298 String data = ((Comment ) node).getData(); 299 300 if (m_contentHandler instanceof LexicalHandler ) 301 { 302 LexicalHandler lh = ((LexicalHandler ) this.m_contentHandler); 303 304 lh.comment(data.toCharArray(), 0, data.length()); 305 } 306 } 307 break; 308 case Node.DOCUMENT_FRAGMENT_NODE : 309 310 break; 312 case Node.DOCUMENT_NODE : 313 314 break; 315 case Node.ELEMENT_NODE : 316 NamedNodeMap atts = ((Element ) node).getAttributes(); 317 int nAttrs = atts.getLength(); 318 320 for (int i = 0; i < nAttrs; i++) 321 { 322 Node attr = atts.item(i); 323 String attrName = attr.getNodeName(); 324 325 if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) 327 { 328 int index; 330 String prefix = (index = attrName.indexOf(":")) < 0 334 ? "" : attrName.substring(index + 1); 335 336 this.m_contentHandler.startPrefixMapping(prefix, 337 attr.getNodeValue()); 338 } 339 340 } 341 342 String ns = m_dh.getNamespaceOfNode(node); 345 if(null == ns) 346 ns = ""; 347 this.m_contentHandler.startElement(ns, 348 m_dh.getLocalNameOfNode(node), 349 node.getNodeName(), 350 new AttList(atts, m_dh)); 351 break; 352 case Node.PROCESSING_INSTRUCTION_NODE : 353 { 354 ProcessingInstruction pi = (ProcessingInstruction ) node; 355 String name = pi.getNodeName(); 356 357 if (name.equals("xslt-next-is-raw")) 359 { 360 nextIsRaw = true; 361 } 362 else 363 { 364 this.m_contentHandler.processingInstruction(pi.getNodeName(), 365 pi.getData()); 366 } 367 } 368 break; 369 case Node.CDATA_SECTION_NODE : 370 { 371 boolean isLexH = (m_contentHandler instanceof LexicalHandler ); 372 LexicalHandler lh = isLexH 373 ? ((LexicalHandler ) this.m_contentHandler) : null; 374 375 if (isLexH) 376 { 377 lh.startCDATA(); 378 } 379 380 dispatachChars(node); 381 382 { 383 if (isLexH) 384 { 385 lh.endCDATA(); 386 } 387 } 388 } 389 break; 390 case Node.TEXT_NODE : 391 { 392 394 if (nextIsRaw) 395 { 396 nextIsRaw = false; 397 398 m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, ""); 399 dispatachChars(node); 400 m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, ""); 401 } 402 else 403 { 404 dispatachChars(node); 405 } 406 } 407 break; 408 case Node.ENTITY_REFERENCE_NODE : 409 { 410 EntityReference eref = (EntityReference ) node; 411 412 if (m_contentHandler instanceof LexicalHandler ) 413 { 414 ((LexicalHandler ) this.m_contentHandler).startEntity( 415 eref.getNodeName()); 416 } 417 else 418 { 419 420 } 422 } 423 break; 424 default : 425 } 426 } 427 428 436 protected void endNode(Node node) throws org.xml.sax.SAXException 437 { 438 439 switch (node.getNodeType()) 440 { 441 case Node.DOCUMENT_NODE : 442 break; 443 444 case Node.ELEMENT_NODE : 445 String ns = m_dh.getNamespaceOfNode(node); 446 if(null == ns) 447 ns = ""; 448 this.m_contentHandler.endElement(ns, 449 m_dh.getLocalNameOfNode(node), 450 node.getNodeName()); 451 452 NamedNodeMap atts = ((Element ) node).getAttributes(); 453 int nAttrs = atts.getLength(); 454 455 for (int i = 0; i < nAttrs; i++) 456 { 457 Node attr = atts.item(i); 458 String attrName = attr.getNodeName(); 459 460 if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) 461 { 462 int index; 463 String prefix = (index = attrName.indexOf(":")) < 0 467 ? "" : attrName.substring(index + 1); 468 469 this.m_contentHandler.endPrefixMapping(prefix); 470 } 471 } 472 break; 473 case Node.CDATA_SECTION_NODE : 474 break; 475 case Node.ENTITY_REFERENCE_NODE : 476 { 477 EntityReference eref = (EntityReference ) node; 478 479 if (m_contentHandler instanceof LexicalHandler ) 480 { 481 LexicalHandler lh = ((LexicalHandler ) this.m_contentHandler); 482 483 lh.endEntity(eref.getNodeName()); 484 } 485 } 486 break; 487 default : 488 } 489 } 490 } 492 | Popular Tags |