1 16 19 package org.apache.xml.dtm.ref; 20 21 import org.apache.xml.dtm.DTM; 22 import org.apache.xml.utils.NodeConsumer; 23 import org.apache.xml.utils.XMLString; 24 25 import org.xml.sax.ContentHandler ; 26 import org.xml.sax.ext.LexicalHandler ; 27 28 37 public class DTMTreeWalker 38 { 39 40 41 private ContentHandler m_contentHandler = null; 42 43 44 protected DTM m_dtm; 45 46 51 public void setDTM(DTM dtm) 52 { 53 m_dtm = dtm; 54 } 55 56 61 public ContentHandler getcontentHandler() 62 { 63 return m_contentHandler; 64 } 65 66 71 public void setcontentHandler(ContentHandler ch) 72 { 73 m_contentHandler = ch; 74 } 75 76 77 82 public DTMTreeWalker() 83 { 84 } 85 86 91 public DTMTreeWalker(ContentHandler contentHandler, DTM dtm) 92 { 93 this.m_contentHandler = contentHandler; 94 m_dtm = dtm; 95 } 96 97 106 public void traverse(int pos) throws org.xml.sax.SAXException 107 { 108 110 int top = pos; 112 while (DTM.NULL != pos) 113 { 114 startNode(pos); 115 int nextNode = m_dtm.getFirstChild(pos); 116 while (DTM.NULL == nextNode) 117 { 118 endNode(pos); 119 120 if (top == pos) 121 break; 122 123 nextNode = m_dtm.getNextSibling(pos); 124 125 if (DTM.NULL == nextNode) 126 { 127 pos = m_dtm.getParent(pos); 128 129 if ((DTM.NULL == pos) || (top == pos)) 130 { 131 if (DTM.NULL != pos) 134 endNode(pos); 135 136 nextNode = DTM.NULL; 137 138 break; 139 } 140 } 141 } 142 143 pos = nextNode; 144 } 145 } 146 147 158 public void traverse(int pos, int top) throws org.xml.sax.SAXException 159 { 160 165 while (DTM.NULL != pos) 166 { 167 startNode(pos); 168 int nextNode = m_dtm.getFirstChild(pos); 169 while (DTM.NULL == nextNode) 170 { 171 endNode(pos); 172 173 if ((DTM.NULL != top) && top == pos) 174 break; 175 176 nextNode = m_dtm.getNextSibling(pos); 177 178 if (DTM.NULL == nextNode) 179 { 180 pos = m_dtm.getParent(pos); 181 182 if ((DTM.NULL == pos) || ((DTM.NULL != top) && (top == pos))) 183 { 184 nextNode = DTM.NULL; 185 186 break; 187 } 188 } 189 } 190 191 pos = nextNode; 192 } 193 } 194 195 196 boolean nextIsRaw = false; 197 198 201 private final void dispatachChars(int node) 202 throws org.xml.sax.SAXException 203 { 204 m_dtm.dispatchCharactersEvents(node, m_contentHandler, false); 205 } 206 207 215 protected void startNode(int node) throws org.xml.sax.SAXException 216 { 217 218 if (m_contentHandler instanceof NodeConsumer) 219 { 220 } 223 224 switch (m_dtm.getNodeType(node)) 225 { 226 case DTM.COMMENT_NODE : 227 { 228 XMLString data = m_dtm.getStringValue(node); 229 230 if (m_contentHandler instanceof LexicalHandler ) 231 { 232 LexicalHandler lh = ((LexicalHandler ) this.m_contentHandler); 233 data.dispatchAsComment(lh); 234 } 235 } 236 break; 237 case DTM.DOCUMENT_FRAGMENT_NODE : 238 239 break; 241 case DTM.DOCUMENT_NODE : 242 this.m_contentHandler.startDocument(); 243 break; 244 case DTM.ELEMENT_NODE : 245 DTM dtm = m_dtm; 246 247 for (int nsn = dtm.getFirstNamespaceNode(node, true); DTM.NULL != nsn; 248 nsn = dtm.getNextNamespaceNode(node, nsn, true)) 249 { 250 String prefix = dtm.getNodeNameX(nsn); 252 253 this.m_contentHandler.startPrefixMapping(prefix, dtm.getNodeValue(nsn)); 254 255 } 256 257 String ns = dtm.getNamespaceURI(node); 260 if(null == ns) 261 ns = ""; 262 263 org.xml.sax.helpers.AttributesImpl attrs = 265 new org.xml.sax.helpers.AttributesImpl (); 266 267 for (int i = dtm.getFirstAttribute(node); 268 i != DTM.NULL; 269 i = dtm.getNextAttribute(i)) 270 { 271 attrs.addAttribute(dtm.getNamespaceURI(i), 272 dtm.getLocalName(i), 273 dtm.getNodeName(i), 274 "CDATA", 275 dtm.getNodeValue(i)); 276 } 277 278 279 this.m_contentHandler.startElement(ns, 280 m_dtm.getLocalName(node), 281 m_dtm.getNodeName(node), 282 attrs); 283 break; 284 case DTM.PROCESSING_INSTRUCTION_NODE : 285 { 286 String name = m_dtm.getNodeName(node); 287 288 if (name.equals("xslt-next-is-raw")) 290 { 291 nextIsRaw = true; 292 } 293 else 294 { 295 this.m_contentHandler.processingInstruction(name, 296 m_dtm.getNodeValue(node)); 297 } 298 } 299 break; 300 case DTM.CDATA_SECTION_NODE : 301 { 302 boolean isLexH = (m_contentHandler instanceof LexicalHandler ); 303 LexicalHandler lh = isLexH 304 ? ((LexicalHandler ) this.m_contentHandler) : null; 305 306 if (isLexH) 307 { 308 lh.startCDATA(); 309 } 310 311 dispatachChars(node); 312 313 { 314 if (isLexH) 315 { 316 lh.endCDATA(); 317 } 318 } 319 } 320 break; 321 case DTM.TEXT_NODE : 322 { 323 if (nextIsRaw) 324 { 325 nextIsRaw = false; 326 327 m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, ""); 328 dispatachChars(node); 329 m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, ""); 330 } 331 else 332 { 333 dispatachChars(node); 334 } 335 } 336 break; 337 case DTM.ENTITY_REFERENCE_NODE : 338 { 339 if (m_contentHandler instanceof LexicalHandler ) 340 { 341 ((LexicalHandler ) this.m_contentHandler).startEntity( 342 m_dtm.getNodeName(node)); 343 } 344 else 345 { 346 347 } 349 } 350 break; 351 default : 352 } 353 } 354 355 363 protected void endNode(int node) throws org.xml.sax.SAXException 364 { 365 366 switch (m_dtm.getNodeType(node)) 367 { 368 case DTM.DOCUMENT_NODE : 369 this.m_contentHandler.endDocument(); 370 break; 371 case DTM.ELEMENT_NODE : 372 String ns = m_dtm.getNamespaceURI(node); 373 if(null == ns) 374 ns = ""; 375 this.m_contentHandler.endElement(ns, 376 m_dtm.getLocalName(node), 377 m_dtm.getNodeName(node)); 378 379 for (int nsn = m_dtm.getFirstNamespaceNode(node, true); DTM.NULL != nsn; 380 nsn = m_dtm.getNextNamespaceNode(node, nsn, true)) 381 { 382 String prefix = m_dtm.getNodeNameX(nsn); 384 385 this.m_contentHandler.endPrefixMapping(prefix); 386 } 387 break; 388 case DTM.CDATA_SECTION_NODE : 389 break; 390 case DTM.ENTITY_REFERENCE_NODE : 391 { 392 if (m_contentHandler instanceof LexicalHandler ) 393 { 394 LexicalHandler lh = ((LexicalHandler ) this.m_contentHandler); 395 396 lh.endEntity(m_dtm.getNodeName(node)); 397 } 398 } 399 break; 400 default : 401 } 402 } 403 } 405 | Popular Tags |