1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.transformer;
  58. import org.w3c.dom.Node;
  59. import org.apache.xml.dtm.DTM;
  60. import org.xml.sax.*;
  61. import org.apache.xml.dtm.ref.DTMTreeWalker;
  62. import org.apache.xml.utils.MutableAttrListImpl;
  63. import org.apache.xalan.templates.ElemTemplateElement;
  64. import org.apache.xpath.DOMHelper;
  65. import org.apache.xpath.XPathContext;
  66. /**
  67. * <meta name="usage" content="internal"/>
  68. * Handle a walk of a tree, but screen out attributes for
  69. * the result tree.
  70. */
  71. public class TreeWalker2Result extends DTMTreeWalker
  72. {
  73. /** The transformer instance */
  74. TransformerImpl m_transformer;
  75. /** The result tree handler */
  76. ResultTreeHandler m_handler;
  77. /** Node where to start the tree walk */
  78. int m_startNode;
  79. /**
  80. * Constructor.
  81. *
  82. * @param transformer Non-null transformer instance
  83. * @param handler The Result tree handler to use
  84. */
  85. public TreeWalker2Result(TransformerImpl transformer,
  86. ResultTreeHandler handler)
  87. {
  88. super(handler, null);
  89. m_transformer = transformer;
  90. m_handler = handler;
  91. }
  92. /**
  93. * Perform a pre-order traversal non-recursive style.
  94. *
  95. * @param pos Start node for traversal
  96. *
  97. * @throws TransformerException
  98. */
  99. public void traverse(int pos) throws org.xml.sax.SAXException
  100. {
  101. m_dtm = m_transformer.getXPathContext().getDTM(pos);
  102. m_startNode = pos;
  103. super.traverse(pos);
  104. }
  105. /**
  106. * End processing of given node
  107. *
  108. *
  109. * @param node Node we just finished processing
  110. *
  111. * @throws org.xml.sax.SAXException
  112. */
  113. protected void endNode(int node) throws org.xml.sax.SAXException
  114. {
  115. super.endNode(node);
  116. if(DTM.ELEMENT_NODE == m_dtm.getNodeType(node))
  117. {
  118. m_transformer.getXPathContext().popCurrentNode();
  119. }
  120. }
  121. /**
  122. * Start traversal of the tree at the given node
  123. *
  124. *
  125. * @param node Starting node for traversal
  126. *
  127. * @throws TransformerException
  128. */
  129. protected void startNode(int node) throws org.xml.sax.SAXException
  130. {
  131. XPathContext xcntxt = m_transformer.getXPathContext();
  132. try
  133. {
  134. if (DTM.ELEMENT_NODE == m_dtm.getNodeType(node))
  135. {
  136. xcntxt.pushCurrentNode(node);
  137. if(m_startNode != node)
  138. {
  139. super.startNode(node);
  140. }
  141. else
  142. {
  143. String elemName = m_dtm.getNodeName(node);
  144. String localName = m_dtm.getLocalName(node);
  145. String namespace = m_dtm.getNamespaceURI(node);
  146. //xcntxt.pushCurrentNode(node);
  147. m_handler.startElement(namespace, localName, elemName, null);
  148. boolean hasNSDecls = false;
  149. DTM dtm = m_dtm;
  150. for (int ns = dtm.getFirstNamespaceNode(node, true);
  151. DTM.NULL != ns; ns = dtm.getNextNamespaceNode(node, ns, true))
  152. {
  153. m_handler.ensureNamespaceDeclDeclared(dtm, ns);
  154. }
  155. // %REVIEW% This flag is apparently never set true. Is that
  156. // a bug, or should this code be phased out?
  157. if(hasNSDecls)
  158. {
  159. m_handler.addNSDeclsToAttrs();
  160. }
  161. for (int attr = dtm.getFirstAttribute(node);
  162. DTM.NULL != attr; attr = dtm.getNextAttribute(attr))
  163. {
  164. m_handler.addAttribute(attr);
  165. }
  166. }
  167. }
  168. else
  169. {
  170. xcntxt.pushCurrentNode(node);
  171. super.startNode(node);
  172. xcntxt.popCurrentNode();
  173. }
  174. }
  175. catch(javax.xml.transform.TransformerException te)
  176. {
  177. throw new org.xml.sax.SAXException(te);
  178. }
  179. }
  180. }