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.templates;
  58. //import org.w3c.dom.*;
  59. import org.apache.xml.dtm.DTM;
  60. import org.xml.sax.*;
  61. import org.apache.xpath.*;
  62. import java.util.*;
  63. import org.apache.xml.utils.QName;
  64. import org.apache.xalan.trace.*;
  65. import org.apache.xalan.res.XSLTErrorResources;
  66. import org.apache.xalan.transformer.TransformerImpl;
  67. import org.apache.xalan.transformer.ResultTreeHandler;
  68. import org.apache.xalan.transformer.ClonerToResultTree;
  69. import javax.xml.transform.TransformerException;
  70. /**
  71. * <meta name="usage" content="advanced"/>
  72. * Implement xsl:copy.
  73. * <pre>
  74. * <!ELEMENT xsl:copy %template;>
  75. * <!ATTLIST xsl:copy
  76. * %space-att;
  77. * use-attribute-sets %qnames; #IMPLIED
  78. * >
  79. * </pre>
  80. * @see <a href="http://www.w3.org/TR/xslt#copying">copying in XSLT Specification</a>
  81. */
  82. public class ElemCopy extends ElemUse
  83. {
  84. /**
  85. * Get an int constant identifying the type of element.
  86. * @see org.apache.xalan.templates.Constants
  87. *
  88. * @return The token ID for this element
  89. */
  90. public int getXSLToken()
  91. {
  92. return Constants.ELEMNAME_COPY;
  93. }
  94. /**
  95. * Return the node name.
  96. *
  97. * @return This element's name
  98. */
  99. public String getNodeName()
  100. {
  101. return Constants.ELEMNAME_COPY_STRING;
  102. }
  103. /**
  104. * The xsl:copy element provides an easy way of copying the current node.
  105. * Executing this function creates a copy of the current node into the
  106. * result tree.
  107. * <p>The namespace nodes of the current node are automatically
  108. * copied as well, but the attributes and children of the node are not
  109. * automatically copied. The content of the xsl:copy element is a
  110. * template for the attributes and children of the created node;
  111. * the content is instantiated only for nodes of types that can have
  112. * attributes or children (i.e. root nodes and element nodes).</p>
  113. * <p>The root node is treated specially because the root node of the
  114. * result tree is created implicitly. When the current node is the
  115. * root node, xsl:copy will not create a root node, but will just use
  116. * the content template.</p>
  117. *
  118. * @param transformer non-null reference to the the current transform-time state.
  119. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  120. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  121. *
  122. * @throws TransformerException
  123. */
  124. public void execute(
  125. TransformerImpl transformer)
  126. throws TransformerException
  127. {
  128. XPathContext xctxt = transformer.getXPathContext();
  129. try
  130. {
  131. int sourceNode = xctxt.getCurrentNode();
  132. xctxt.pushCurrentNode(sourceNode);
  133. DTM dtm = xctxt.getDTM(sourceNode);
  134. short nodeType = dtm.getNodeType(sourceNode);
  135. if ((DTM.DOCUMENT_NODE != nodeType) && (DTM.DOCUMENT_FRAGMENT_NODE != nodeType))
  136. {
  137. ResultTreeHandler rthandler = transformer.getResultTreeHandler();
  138. if (TransformerImpl.S_DEBUG)
  139. transformer.getTraceManager().fireTraceEvent(this);
  140. // TODO: Process the use-attribute-sets stuff
  141. ClonerToResultTree.cloneToResultTree(sourceNode, nodeType, dtm,
  142. rthandler, false);
  143. if (DTM.ELEMENT_NODE == nodeType)
  144. {
  145. super.execute(transformer);
  146. rthandler.processNSDecls(sourceNode, nodeType, dtm);
  147. transformer.executeChildTemplates(this, true);
  148. String ns = dtm.getNamespaceURI(sourceNode);
  149. String localName = dtm.getLocalName(sourceNode);
  150. transformer.getResultTreeHandler().endElement(ns, localName,
  151. dtm.getNodeName(sourceNode));
  152. }
  153. if (TransformerImpl.S_DEBUG)
  154. transformer.getTraceManager().fireTraceEndEvent(this);
  155. }
  156. else
  157. {
  158. if (TransformerImpl.S_DEBUG)
  159. transformer.getTraceManager().fireTraceEvent(this);
  160. super.execute(transformer);
  161. transformer.executeChildTemplates(this, true);
  162. if (TransformerImpl.S_DEBUG)
  163. transformer.getTraceManager().fireTraceEndEvent(this);
  164. }
  165. }
  166. catch(org.xml.sax.SAXException se)
  167. {
  168. throw new TransformerException(se);
  169. }
  170. finally
  171. {
  172. xctxt.popCurrentNode();
  173. }
  174. }
  175. }