1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: DTMTreeWalker.java,v 1.6 2004/02/16 23:06:11 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.dtm.ref;
  20. import com.sun.org.apache.xml.internal.dtm.DTM;
  21. import com.sun.org.apache.xml.internal.utils.NodeConsumer;
  22. import com.sun.org.apache.xml.internal.utils.XMLString;
  23. import org.xml.sax.ContentHandler;
  24. import org.xml.sax.ext.LexicalHandler;
  25. /**
  26. * This class does a pre-order walk of the DTM tree, calling a ContentHandler
  27. * interface as it goes. As such, it's more like the Visitor design pattern
  28. * than like the DOM's TreeWalker.
  29. *
  30. * I think normally this class should not be needed, because
  31. * of DTM#dispatchToEvents.
  32. * @xsl.usage advanced
  33. */
  34. public class DTMTreeWalker
  35. {
  36. /** Local reference to a ContentHandler */
  37. private ContentHandler m_contentHandler = null;
  38. /** DomHelper for this TreeWalker */
  39. protected DTM m_dtm;
  40. /**
  41. * Set the DTM to be traversed.
  42. *
  43. * @param dtm The Document Table Model to be used.
  44. */
  45. public void setDTM(DTM dtm)
  46. {
  47. m_dtm = dtm;
  48. }
  49. /**
  50. * Get the ContentHandler used for the tree walk.
  51. *
  52. * @return the ContentHandler used for the tree walk
  53. */
  54. public ContentHandler getcontentHandler()
  55. {
  56. return m_contentHandler;
  57. }
  58. /**
  59. * Set the ContentHandler used for the tree walk.
  60. *
  61. * @param ch the ContentHandler to be the result of the tree walk.
  62. */
  63. public void setcontentHandler(ContentHandler ch)
  64. {
  65. m_contentHandler = ch;
  66. }
  67. /**
  68. * Constructor.
  69. * @param contentHandler The implemention of the
  70. * contentHandler operation (toXMLString, digest, ...)
  71. */
  72. public DTMTreeWalker()
  73. {
  74. }
  75. /**
  76. * Constructor.
  77. * @param contentHandler The implemention of the
  78. * contentHandler operation (toXMLString, digest, ...)
  79. */
  80. public DTMTreeWalker(ContentHandler contentHandler, DTM dtm)
  81. {
  82. this.m_contentHandler = contentHandler;
  83. m_dtm = dtm;
  84. }
  85. /** Perform a non-recursive pre-order/post-order traversal,
  86. * operating as a Visitor. startNode (preorder) and endNode
  87. * (postorder) are invoked for each node as we traverse over them,
  88. * with the result that the node is written out to m_contentHandler.
  89. *
  90. * @param pos Node in the tree at which to start (and end) traversal --
  91. * in other words, the root of the subtree to traverse over.
  92. *
  93. * @throws TransformerException */
  94. public void traverse(int pos) throws org.xml.sax.SAXException
  95. {
  96. // %REVIEW% Why isn't this just traverse(pos,pos)?
  97. int top = pos; // Remember the root of this subtree
  98. while (DTM.NULL != pos)
  99. {
  100. startNode(pos);
  101. int nextNode = m_dtm.getFirstChild(pos);
  102. while (DTM.NULL == nextNode)
  103. {
  104. endNode(pos);
  105. if (top == pos)
  106. break;
  107. nextNode = m_dtm.getNextSibling(pos);
  108. if (DTM.NULL == nextNode)
  109. {
  110. pos = m_dtm.getParent(pos);
  111. if ((DTM.NULL == pos) || (top == pos))
  112. {
  113. // %REVIEW% This condition isn't tested in traverse(pos,top)
  114. // -- bug?
  115. if (DTM.NULL != pos)
  116. endNode(pos);
  117. nextNode = DTM.NULL;
  118. break;
  119. }
  120. }
  121. }
  122. pos = nextNode;
  123. }
  124. }
  125. /** Perform a non-recursive pre-order/post-order traversal,
  126. * operating as a Visitor. startNode (preorder) and endNode
  127. * (postorder) are invoked for each node as we traverse over them,
  128. * with the result that the node is written out to m_contentHandler.
  129. *
  130. * @param pos Node in the tree where to start traversal
  131. * @param top Node in the tree where to end traversal.
  132. * If top==DTM.NULL, run through end of document.
  133. *
  134. * @throws TransformerException
  135. */
  136. public void traverse(int pos, int top) throws org.xml.sax.SAXException
  137. {
  138. // %OPT% Can we simplify the loop conditionals by adding:
  139. // if(top==DTM.NULL) top=0
  140. // -- or by simply ignoring this case and relying on the fact that
  141. // pos will never equal DTM.NULL until we're ready to exit?
  142. while (DTM.NULL != pos)
  143. {
  144. startNode(pos);
  145. int nextNode = m_dtm.getFirstChild(pos);
  146. while (DTM.NULL == nextNode)
  147. {
  148. endNode(pos);
  149. if ((DTM.NULL != top) && top == pos)
  150. break;
  151. nextNode = m_dtm.getNextSibling(pos);
  152. if (DTM.NULL == nextNode)
  153. {
  154. pos = m_dtm.getParent(pos);
  155. if ((DTM.NULL == pos) || ((DTM.NULL != top) && (top == pos)))
  156. {
  157. nextNode = DTM.NULL;
  158. break;
  159. }
  160. }
  161. }
  162. pos = nextNode;
  163. }
  164. }
  165. /** Flag indicating whether following text to be processed is raw text */
  166. boolean nextIsRaw = false;
  167. /**
  168. * Optimized dispatch of characters.
  169. */
  170. private final void dispatachChars(int node)
  171. throws org.xml.sax.SAXException
  172. {
  173. m_dtm.dispatchCharactersEvents(node, m_contentHandler, false);
  174. }
  175. /**
  176. * Start processing given node
  177. *
  178. *
  179. * @param node Node to process
  180. *
  181. * @throws org.xml.sax.SAXException
  182. */
  183. protected void startNode(int node) throws org.xml.sax.SAXException
  184. {
  185. if (m_contentHandler instanceof NodeConsumer)
  186. {
  187. // %TBD%
  188. // ((NodeConsumer) m_contentHandler).setOriginatingNode(node);
  189. }
  190. switch (m_dtm.getNodeType(node))
  191. {
  192. case DTM.COMMENT_NODE :
  193. {
  194. XMLString data = m_dtm.getStringValue(node);
  195. if (m_contentHandler instanceof LexicalHandler)
  196. {
  197. LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
  198. data.dispatchAsComment(lh);
  199. }
  200. }
  201. break;
  202. case DTM.DOCUMENT_FRAGMENT_NODE :
  203. // ??;
  204. break;
  205. case DTM.DOCUMENT_NODE :
  206. this.m_contentHandler.startDocument();
  207. break;
  208. case DTM.ELEMENT_NODE :
  209. DTM dtm = m_dtm;
  210. for (int nsn = dtm.getFirstNamespaceNode(node, true); DTM.NULL != nsn;
  211. nsn = dtm.getNextNamespaceNode(node, nsn, true))
  212. {
  213. // String prefix = dtm.getPrefix(nsn);
  214. String prefix = dtm.getNodeNameX(nsn);
  215. this.m_contentHandler.startPrefixMapping(prefix, dtm.getNodeValue(nsn));
  216. }
  217. // System.out.println("m_dh.getNamespaceOfNode(node): "+m_dh.getNamespaceOfNode(node));
  218. // System.out.println("m_dh.getLocalNameOfNode(node): "+m_dh.getLocalNameOfNode(node));
  219. String ns = dtm.getNamespaceURI(node);
  220. if(null == ns)
  221. ns = "";
  222. // %OPT% !!
  223. org.xml.sax.helpers.AttributesImpl attrs =
  224. new org.xml.sax.helpers.AttributesImpl();
  225. for (int i = dtm.getFirstAttribute(node);
  226. i != DTM.NULL;
  227. i = dtm.getNextAttribute(i))
  228. {
  229. attrs.addAttribute(dtm.getNamespaceURI(i),
  230. dtm.getLocalName(i),
  231. dtm.getNodeName(i),
  232. "CDATA",
  233. dtm.getNodeValue(i));
  234. }
  235. this.m_contentHandler.startElement(ns,
  236. m_dtm.getLocalName(node),
  237. m_dtm.getNodeName(node),
  238. attrs);
  239. break;
  240. case DTM.PROCESSING_INSTRUCTION_NODE :
  241. {
  242. String name = m_dtm.getNodeName(node);
  243. // String data = pi.getData();
  244. if (name.equals("xslt-next-is-raw"))
  245. {
  246. nextIsRaw = true;
  247. }
  248. else
  249. {
  250. this.m_contentHandler.processingInstruction(name,
  251. m_dtm.getNodeValue(node));
  252. }
  253. }
  254. break;
  255. case DTM.CDATA_SECTION_NODE :
  256. {
  257. boolean isLexH = (m_contentHandler instanceof LexicalHandler);
  258. LexicalHandler lh = isLexH
  259. ? ((LexicalHandler) this.m_contentHandler) : null;
  260. if (isLexH)
  261. {
  262. lh.startCDATA();
  263. }
  264. dispatachChars(node);
  265. {
  266. if (isLexH)
  267. {
  268. lh.endCDATA();
  269. }
  270. }
  271. }
  272. break;
  273. case DTM.TEXT_NODE :
  274. {
  275. if (nextIsRaw)
  276. {
  277. nextIsRaw = false;
  278. m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
  279. dispatachChars(node);
  280. m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
  281. }
  282. else
  283. {
  284. dispatachChars(node);
  285. }
  286. }
  287. break;
  288. case DTM.ENTITY_REFERENCE_NODE :
  289. {
  290. if (m_contentHandler instanceof LexicalHandler)
  291. {
  292. ((LexicalHandler) this.m_contentHandler).startEntity(
  293. m_dtm.getNodeName(node));
  294. }
  295. else
  296. {
  297. // warning("Can not output entity to a pure SAX ContentHandler");
  298. }
  299. }
  300. break;
  301. default :
  302. }
  303. }
  304. /**
  305. * End processing of given node
  306. *
  307. *
  308. * @param node Node we just finished processing
  309. *
  310. * @throws org.xml.sax.SAXException
  311. */
  312. protected void endNode(int node) throws org.xml.sax.SAXException
  313. {
  314. switch (m_dtm.getNodeType(node))
  315. {
  316. case DTM.DOCUMENT_NODE :
  317. this.m_contentHandler.endDocument();
  318. break;
  319. case DTM.ELEMENT_NODE :
  320. String ns = m_dtm.getNamespaceURI(node);
  321. if(null == ns)
  322. ns = "";
  323. this.m_contentHandler.endElement(ns,
  324. m_dtm.getLocalName(node),
  325. m_dtm.getNodeName(node));
  326. for (int nsn = m_dtm.getFirstNamespaceNode(node, true); DTM.NULL != nsn;
  327. nsn = m_dtm.getNextNamespaceNode(node, nsn, true))
  328. {
  329. // String prefix = m_dtm.getPrefix(nsn);
  330. String prefix = m_dtm.getNodeNameX(nsn);
  331. this.m_contentHandler.endPrefixMapping(prefix);
  332. }
  333. break;
  334. case DTM.CDATA_SECTION_NODE :
  335. break;
  336. case DTM.ENTITY_REFERENCE_NODE :
  337. {
  338. if (m_contentHandler instanceof LexicalHandler)
  339. {
  340. LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
  341. lh.endEntity(m_dtm.getNodeName(node));
  342. }
  343. }
  344. break;
  345. default :
  346. }
  347. }
  348. } //TreeWalker