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.apache.xalan.templates.Stylesheet;
  59. import org.apache.xml.dtm.DTM;
  60. import org.apache.xml.dtm.DTMIterator;
  61. import org.apache.xml.dtm.DTMFilter;
  62. import org.apache.xml.utils.XMLString;
  63. import javax.xml.transform.TransformerException;
  64. import org.xml.sax.Attributes;
  65. import org.apache.xpath.XPathContext;
  66. import org.apache.xalan.res.XSLTErrorResources;
  67. /**
  68. * <meta name="usage" content="internal"/>
  69. * Class used to clone a node, possibly including its children to
  70. * a result tree.
  71. */
  72. public class ClonerToResultTree
  73. {
  74. /** Result tree handler for the cloned tree */
  75. private ResultTreeHandler m_rth;
  76. /** Transformer instance to use for cloning */
  77. private TransformerImpl m_transformer;
  78. /**
  79. * Constructor ClonerToResultTree
  80. *
  81. *
  82. * @param transformer non-null transformer instance to use for the cloning
  83. * @param rth non-null result tree handler for the cloned tree
  84. */
  85. public ClonerToResultTree(TransformerImpl transformer,
  86. ResultTreeHandler rth)
  87. {
  88. m_rth = rth;
  89. m_transformer = transformer;
  90. }
  91. // /**
  92. // * Clone an element with or without children.
  93. // * TODO: Fix or figure out node clone failure!
  94. // * the error condition is severe enough to halt processing.
  95. // *
  96. // * @param node The node to clone
  97. // * @param shouldCloneAttributes Flag indicating whether to
  98. // * clone children attributes
  99. // *
  100. // * @throws TransformerException
  101. // */
  102. // public void cloneToResultTree(int node, boolean shouldCloneAttributes)
  103. // throws TransformerException
  104. // {
  105. //
  106. // try
  107. // {
  108. // XPathContext xctxt = m_transformer.getXPathContext();
  109. // DTM dtm = xctxt.getDTM(node);
  110. //
  111. // int type = dtm.getNodeType(node);
  112. // switch (type)
  113. // {
  114. // case DTM.TEXT_NODE :
  115. // dtm.dispatchCharactersEvents(node, m_rth, false);
  116. // break;
  117. // case DTM.DOCUMENT_FRAGMENT_NODE :
  118. // case DTM.DOCUMENT_NODE :
  119. //
  120. // // Can't clone a document, but refrain from throwing an error
  121. // // so that copy-of will work
  122. // break;
  123. // case DTM.ELEMENT_NODE :
  124. // {
  125. // Attributes atts;
  126. //
  127. // if (shouldCloneAttributes)
  128. // {
  129. // m_rth.addAttributes(node);
  130. // m_rth.processNSDecls(node, type, dtm);
  131. // }
  132. //
  133. // String ns = dtm.getNamespaceURI(node);
  134. // String localName = dtm.getLocalName(node);
  135. //
  136. // m_rth.startElement(ns, localName, dtm.getNodeNameX(node), null);
  137. // }
  138. // break;
  139. // case DTM.CDATA_SECTION_NODE :
  140. // m_rth.startCDATA();
  141. // dtm.dispatchCharactersEvents(node, m_rth, false);
  142. // m_rth.endCDATA();
  143. // break;
  144. // case DTM.ATTRIBUTE_NODE :
  145. // m_rth.addAttribute(node);
  146. // break;
  147. // case DTM.COMMENT_NODE :
  148. // XMLString xstr = dtm.getStringValue (node);
  149. // xstr.dispatchAsComment(m_rth);
  150. // break;
  151. // case DTM.ENTITY_REFERENCE_NODE :
  152. // m_rth.entityReference(dtm.getNodeNameX(node));
  153. // break;
  154. // case DTM.PROCESSING_INSTRUCTION_NODE :
  155. // {
  156. // // %REVIEW% Is the node name the same as the "target"?
  157. // m_rth.processingInstruction(dtm.getNodeNameX(node),
  158. // dtm.getNodeValue(node));
  159. // }
  160. // break;
  161. // default :
  162. // //"Can not create item in result tree: "+node.getNodeName());
  163. // m_transformer.getMsgMgr().error(null,
  164. // XSLTErrorResources.ER_CANT_CREATE_ITEM,
  165. // new Object[]{ dtm.getNodeName(node) });
  166. // }
  167. // }
  168. // catch(org.xml.sax.SAXException se)
  169. // {
  170. // throw new TransformerException(se);
  171. // }
  172. // } // end cloneToResultTree function
  173. /**
  174. * Clone an element with or without children.
  175. * TODO: Fix or figure out node clone failure!
  176. * the error condition is severe enough to halt processing.
  177. *
  178. * @param node The node to clone
  179. * @param shouldCloneAttributes Flag indicating whether to
  180. * clone children attributes
  181. *
  182. * @throws TransformerException
  183. */
  184. public static void cloneToResultTree(int node, int nodeType, DTM dtm,
  185. ResultTreeHandler rth,
  186. boolean shouldCloneAttributes)
  187. throws TransformerException
  188. {
  189. try
  190. {
  191. switch (nodeType)
  192. {
  193. case DTM.TEXT_NODE :
  194. dtm.dispatchCharactersEvents(node, rth, false);
  195. break;
  196. case DTM.DOCUMENT_FRAGMENT_NODE :
  197. case DTM.DOCUMENT_NODE :
  198. // Can't clone a document, but refrain from throwing an error
  199. // so that copy-of will work
  200. break;
  201. case DTM.ELEMENT_NODE :
  202. {
  203. // Note: SAX apparently expects "no namespace" to be
  204. // represented as "" rather than null.
  205. String ns = dtm.getNamespaceURI(node);
  206. if (ns==null) ns="";
  207. String localName = dtm.getLocalName(node);
  208. rth.startElement(ns, localName, dtm.getNodeNameX(node), null);
  209. // If outputting attrs as separate events, they must
  210. // _follow_ the startElement event. (Think of the
  211. // xsl:attribute directive.)
  212. if (shouldCloneAttributes)
  213. {
  214. rth.addAttributes(node);
  215. rth.processNSDecls(node, nodeType, dtm);
  216. }
  217. }
  218. break;
  219. case DTM.CDATA_SECTION_NODE :
  220. rth.startCDATA();
  221. dtm.dispatchCharactersEvents(node, rth, false);
  222. rth.endCDATA();
  223. break;
  224. case DTM.ATTRIBUTE_NODE :
  225. rth.addAttribute(node);
  226. break;
  227. case DTM.NAMESPACE_NODE:
  228. // %REVIEW% Normally, these should have been handled with element.
  229. // It's possible that someone may write a stylesheet that tries to
  230. // clone them explicitly. If so, we need the equivalent of
  231. // rth.addAttribute().
  232. rth.processNSDecls(node,DTM.NAMESPACE_NODE,dtm);
  233. break;
  234. case DTM.COMMENT_NODE :
  235. XMLString xstr = dtm.getStringValue (node);
  236. xstr.dispatchAsComment(rth);
  237. break;
  238. case DTM.ENTITY_REFERENCE_NODE :
  239. rth.entityReference(dtm.getNodeNameX(node));
  240. break;
  241. case DTM.PROCESSING_INSTRUCTION_NODE :
  242. {
  243. // %REVIEW% Is the node name the same as the "target"?
  244. rth.processingInstruction(dtm.getNodeNameX(node),
  245. dtm.getNodeValue(node));
  246. }
  247. break;
  248. default :
  249. //"Can not create item in result tree: "+node.getNodeName());
  250. throw new TransformerException(
  251. "Can't clone node: "+dtm.getNodeName(node));
  252. }
  253. }
  254. catch(org.xml.sax.SAXException se)
  255. {
  256. throw new TransformerException(se);
  257. }
  258. } // end cloneToResultTree function
  259. }