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.w3c.dom.traversal.NodeIterator;
  60. import org.apache.xml.dtm.DTM;
  61. import org.apache.xml.dtm.DTMIterator;
  62. import org.apache.xml.dtm.ref.DTMTreeWalker;
  63. import org.xml.sax.*;
  64. import org.apache.xpath.*;
  65. import org.apache.xpath.objects.XObject;
  66. import org.apache.xalan.trace.SelectionEvent;
  67. import org.apache.xalan.res.XSLTErrorResources;
  68. import org.apache.xml.utils.QName;
  69. import org.apache.xalan.transformer.TreeWalker2Result;
  70. import org.apache.xalan.transformer.TransformerImpl;
  71. import org.apache.xalan.transformer.ResultTreeHandler;
  72. import javax.xml.transform.TransformerException;
  73. /**
  74. * <meta name="usage" content="advanced"/>
  75. * Implement xsl:copy-of.
  76. * <pre>
  77. * <!ELEMENT xsl:copy-of EMPTY>
  78. * <!ATTLIST xsl:copy-of select %expr; #REQUIRED>
  79. * </pre>
  80. * @see <a href="http://www.w3.org/TR/xslt#copy-of">copy-of in XSLT Specification</a>
  81. */
  82. public class ElemCopyOf extends ElemTemplateElement
  83. {
  84. /**
  85. * The required select attribute contains an expression.
  86. * @serial
  87. */
  88. public XPath m_selectExpression = null;
  89. /**
  90. * Set the "select" attribute.
  91. * The required select attribute contains an expression.
  92. *
  93. * @param expr Expression for select attribute
  94. */
  95. public void setSelect(XPath expr)
  96. {
  97. m_selectExpression = expr;
  98. }
  99. /**
  100. * Get the "select" attribute.
  101. * The required select attribute contains an expression.
  102. *
  103. * @return Expression for select attribute
  104. */
  105. public XPath getSelect()
  106. {
  107. return m_selectExpression;
  108. }
  109. /**
  110. * This function is called after everything else has been
  111. * recomposed, and allows the template to set remaining
  112. * values that may be based on some other property that
  113. * depends on recomposition.
  114. */
  115. public void compose(StylesheetRoot sroot) throws TransformerException
  116. {
  117. super.compose(sroot);
  118. StylesheetRoot.ComposeState cstate = sroot.getComposeState();
  119. m_selectExpression.fixupVariables(cstate.getVariableNames(), cstate.getGlobalsSize());
  120. }
  121. /**
  122. * Get an int constant identifying the type of element.
  123. * @see org.apache.xalan.templates.Constants
  124. *
  125. * @return The token ID for this element
  126. */
  127. public int getXSLToken()
  128. {
  129. return Constants.ELEMNAME_COPY_OF;
  130. }
  131. /**
  132. * Return the node name.
  133. *
  134. * @return The element's name
  135. */
  136. public String getNodeName()
  137. {
  138. return Constants.ELEMNAME_COPY_OF_STRING;
  139. }
  140. /**
  141. * The xsl:copy-of element can be used to insert a result tree
  142. * fragment into the result tree, without first converting it to
  143. * a string as xsl:value-of does (see [7.6.1 Generating Text with
  144. * xsl:value-of]).
  145. *
  146. * @param transformer non-null reference to the the current transform-time state.
  147. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  148. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  149. *
  150. * @throws TransformerException
  151. */
  152. public void execute(
  153. TransformerImpl transformer)
  154. throws TransformerException
  155. {
  156. if (TransformerImpl.S_DEBUG)
  157. transformer.getTraceManager().fireTraceEvent(this);
  158. try
  159. {
  160. XPathContext xctxt = transformer.getXPathContext();
  161. int sourceNode = xctxt.getCurrentNode();
  162. XObject value = m_selectExpression.execute(xctxt, sourceNode, this);
  163. if (TransformerImpl.S_DEBUG)
  164. transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
  165. "select", m_selectExpression, value);
  166. ResultTreeHandler handler = transformer.getResultTreeHandler();
  167. if (null != value)
  168. {
  169. int type = value.getType();
  170. String s;
  171. switch (type)
  172. {
  173. case XObject.CLASS_BOOLEAN :
  174. case XObject.CLASS_NUMBER :
  175. case XObject.CLASS_STRING :
  176. s = value.str();
  177. handler.characters(s.toCharArray(), 0, s.length());
  178. break;
  179. case XObject.CLASS_NODESET :
  180. // System.out.println(value);
  181. DTMIterator nl = value.iter();
  182. // Copy the tree.
  183. DTMTreeWalker tw = new TreeWalker2Result(transformer, handler);
  184. int pos;
  185. while (DTM.NULL != (pos = nl.nextNode()))
  186. {
  187. DTM dtm = xctxt.getDTMManager().getDTM(pos);
  188. short t = dtm.getNodeType(pos);
  189. // If we just copy the whole document, a startDoc and endDoc get
  190. // generated, so we need to only walk the child nodes.
  191. if (t == DTM.DOCUMENT_NODE)
  192. {
  193. for (int child = dtm.getFirstChild(pos); child != DTM.NULL;
  194. child = dtm.getNextSibling(child))
  195. {
  196. tw.traverse(child);
  197. }
  198. }
  199. else if (t == DTM.ATTRIBUTE_NODE)
  200. {
  201. handler.addAttribute(pos);
  202. }
  203. else
  204. {
  205. tw.traverse(pos);
  206. }
  207. }
  208. // nl.detach();
  209. break;
  210. case XObject.CLASS_RTREEFRAG :
  211. handler.outputResultTreeFragment(value,
  212. transformer.getXPathContext());
  213. break;
  214. default :
  215. s = value.str();
  216. handler.characters(s.toCharArray(), 0, s.length());
  217. break;
  218. }
  219. }
  220. // I don't think we want this. -sb
  221. // if (TransformerImpl.S_DEBUG)
  222. // transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
  223. // "endSelect", m_selectExpression, value);
  224. }
  225. catch(org.xml.sax.SAXException se)
  226. {
  227. throw new TransformerException(se);
  228. }
  229. finally
  230. {
  231. if (TransformerImpl.S_DEBUG)
  232. transformer.getTraceManager().fireTraceEndEvent(this);
  233. }
  234. }
  235. /**
  236. * Add a child to the child list.
  237. *
  238. * @param newChild Child to add to this node's child list
  239. *
  240. * @return Child just added to child list
  241. */
  242. public ElemTemplateElement appendChild(ElemTemplateElement newChild)
  243. {
  244. error(XSLTErrorResources.ER_CANNOT_ADD,
  245. new Object[]{ newChild.getNodeName(),
  246. this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
  247. //" to " + this.m_elemName);
  248. return null;
  249. }
  250. /**
  251. * Call the children visitors.
  252. * @param visitor The visitor whose appropriate method will be called.
  253. */
  254. protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs)
  255. {
  256. if(callAttrs)
  257. m_selectExpression.getExpression().callVisitors(m_selectExpression, visitor);
  258. super.callChildVisitors(visitor, callAttrs);
  259. }
  260. }