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 javax.xml.transform.SourceLocator;
  59. import javax.xml.transform.TransformerException;
  60. import org.apache.xalan.res.XSLTErrorResources;
  61. import org.apache.xalan.transformer.TransformerImpl;
  62. import org.apache.xml.utils.QName;
  63. import org.apache.xpath.VariableStack;
  64. import org.apache.xpath.XPathContext;
  65. import org.apache.xpath.objects.XObject;
  66. /**
  67. * <meta name="usage" content="advanced"/>
  68. * Implement xsl:call-template.
  69. * <pre>
  70. * &!ELEMENT xsl:call-template (xsl:with-param)*>
  71. * &!ATTLIST xsl:call-template
  72. * name %qname; #REQUIRED
  73. * &
  74. * </pre>
  75. * @see <a href="http://www.w3.org/TR/xslt#named-templates">named-templates in XSLT Specification</a>
  76. */
  77. public class ElemCallTemplate extends ElemForEach
  78. {
  79. /**
  80. * An xsl:call-template element invokes a template by name;
  81. * it has a required name attribute that identifies the template to be invoked.
  82. * @serial
  83. */
  84. public QName m_templateName = null;
  85. /**
  86. * Set the "name" attribute.
  87. * An xsl:call-template element invokes a template by name;
  88. * it has a required name attribute that identifies the template to be invoked.
  89. *
  90. * @param name Name attribute to set
  91. */
  92. public void setName(QName name)
  93. {
  94. m_templateName = name;
  95. }
  96. /**
  97. * Get the "name" attribute.
  98. * An xsl:call-template element invokes a template by name;
  99. * it has a required name attribute that identifies the template to be invoked.
  100. *
  101. * @return Name attribute of this element
  102. */
  103. public QName getName()
  104. {
  105. return m_templateName;
  106. }
  107. /**
  108. * The template which is named by QName.
  109. * @serial
  110. */
  111. private ElemTemplate m_template = null;
  112. /**
  113. * Get an int constant identifying the type of element.
  114. * @see org.apache.xalan.templates.Constants
  115. *
  116. * @return The token ID for this element
  117. */
  118. public int getXSLToken()
  119. {
  120. return Constants.ELEMNAME_CALLTEMPLATE;
  121. }
  122. /**
  123. * Return the node name.
  124. *
  125. * @return The name of this element
  126. */
  127. public String getNodeName()
  128. {
  129. return Constants.ELEMNAME_CALLTEMPLATE_STRING;
  130. }
  131. /**
  132. * This function is called after everything else has been
  133. * recomposed, and allows the template to set remaining
  134. * values that may be based on some other property that
  135. * depends on recomposition.
  136. */
  137. public void compose(StylesheetRoot sroot) throws TransformerException
  138. {
  139. super.compose(sroot);
  140. // Call compose on each param no matter if this is apply-templates
  141. // or call templates.
  142. int length = getParamElemCount();
  143. for (int i = 0; i < length; i++)
  144. {
  145. ElemWithParam ewp = getParamElem(i);
  146. ewp.compose(sroot);
  147. }
  148. if ((null != m_templateName) && (null == m_template))
  149. {
  150. m_template =
  151. this.getStylesheetRoot().getTemplateComposed(m_templateName);
  152. if(null == m_template)
  153. return; // %REVIEW% error?
  154. length = getParamElemCount();
  155. for (int i = 0; i < length; i++)
  156. {
  157. ElemWithParam ewp = getParamElem(i);
  158. ewp.m_index = -1;
  159. // Find the position of the param in the template being called,
  160. // and set the index of the param slot.
  161. int etePos = 0;
  162. for (ElemTemplateElement ete = m_template.getFirstChildElem();
  163. null != ete; ete = ete.getNextSiblingElem())
  164. {
  165. if(ete.getXSLToken() == Constants.ELEMNAME_PARAMVARIABLE)
  166. {
  167. ElemParam ep = (ElemParam)ete;
  168. if(ep.getName().equals(ewp.getName()))
  169. {
  170. ewp.m_index = etePos;
  171. }
  172. }
  173. else
  174. break;
  175. etePos++;
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * This after the template's children have been composed.
  182. */
  183. public void endCompose(StylesheetRoot sroot) throws TransformerException
  184. {
  185. int length = getParamElemCount();
  186. for (int i = 0; i < length; i++)
  187. {
  188. ElemWithParam ewp = getParamElem(i);
  189. ewp.endCompose(sroot);
  190. }
  191. super.endCompose(sroot);
  192. }
  193. /**
  194. * Invoke a named template.
  195. * @see <a href="http://www.w3.org/TR/xslt#named-templates">named-templates in XSLT Specification</a>
  196. *
  197. * @param transformer non-null reference to the the current transform-time state.
  198. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  199. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  200. *
  201. * @throws TransformerException
  202. */
  203. public void execute(
  204. TransformerImpl transformer)
  205. throws TransformerException
  206. {
  207. if (TransformerImpl.S_DEBUG)
  208. transformer.getTraceManager().fireTraceEvent(this);
  209. if (null != m_template)
  210. {
  211. XPathContext xctxt = transformer.getXPathContext();
  212. VariableStack vars = xctxt.getVarStack();
  213. int thisframe = vars.getStackFrame();
  214. int nextFrame = vars.link(m_template.m_frameSize);
  215. // We have to clear the section of the stack frame that has params
  216. // so that the default param evaluation will work correctly.
  217. if(m_template.m_inArgsSize > 0)
  218. {
  219. vars.clearLocalSlots(0, m_template.m_inArgsSize);
  220. if(null != m_paramElems)
  221. {
  222. int currentNode = xctxt.getCurrentNode();
  223. vars.setStackFrame(thisframe);
  224. int size = m_paramElems.length;
  225. for (int i = 0; i < size; i++)
  226. {
  227. ElemWithParam ewp = m_paramElems[i];
  228. if(ewp.m_index >= 0)
  229. {
  230. XObject obj = ewp.getValue(transformer, currentNode);
  231. // Note here that the index for ElemWithParam must have been
  232. // statically made relative to the xsl:template being called,
  233. // NOT this xsl:template.
  234. vars.setLocalVariable(ewp.m_index, obj, nextFrame);
  235. }
  236. }
  237. vars.setStackFrame(nextFrame);
  238. }
  239. }
  240. SourceLocator savedLocator = xctxt.getSAXLocator();
  241. try
  242. {
  243. xctxt.setSAXLocator(m_template);
  244. // template.executeChildTemplates(transformer, sourceNode, mode, true);
  245. transformer.pushElemTemplateElement(m_template);
  246. m_template.execute(transformer);
  247. }
  248. finally
  249. {
  250. transformer.popElemTemplateElement();
  251. xctxt.setSAXLocator(savedLocator);
  252. // When we entered this function, the current
  253. // frame buffer (cfb) index in the variable stack may
  254. // have been manually set. If we just call
  255. // unlink(), however, it will restore the cfb to the
  256. // previous link index from the link stack, rather than
  257. // the manually set cfb. So,
  258. // the only safe solution is to restore it back
  259. // to the same position it was on entry, since we're
  260. // really not working in a stack context here. (Bug4218)
  261. vars.unlink(thisframe);
  262. }
  263. }
  264. else
  265. {
  266. transformer.getMsgMgr().error(this, XSLTErrorResources.ER_TEMPLATE_NOT_FOUND,
  267. new Object[]{ m_templateName }); //"Could not find template named: '"+templateName+"'");
  268. }
  269. if (TransformerImpl.S_DEBUG)
  270. transformer.getTraceManager().fireTraceEndEvent(this);
  271. }
  272. /** Vector of xsl:param elements associated with this element.
  273. * @serial */
  274. protected ElemWithParam[] m_paramElems = null;
  275. /**
  276. * Get the count xsl:param elements associated with this element.
  277. * @return The number of xsl:param elements.
  278. */
  279. public int getParamElemCount()
  280. {
  281. return (m_paramElems == null) ? 0 : m_paramElems.length;
  282. }
  283. /**
  284. * Get a xsl:param element associated with this element.
  285. *
  286. * @param i Index of element to find
  287. *
  288. * @return xsl:param element at given index
  289. */
  290. public ElemWithParam getParamElem(int i)
  291. {
  292. return m_paramElems[i];
  293. }
  294. /**
  295. * Set a xsl:param element associated with this element.
  296. *
  297. * @param ParamElem xsl:param element to set.
  298. */
  299. public void setParamElem(ElemWithParam ParamElem)
  300. {
  301. if (null == m_paramElems)
  302. {
  303. m_paramElems = new ElemWithParam[1];
  304. m_paramElems[0] = ParamElem;
  305. }
  306. else
  307. {
  308. // Expensive 1 at a time growth, but this is done at build time, so
  309. // I think it's OK.
  310. int length = m_paramElems.length;
  311. ElemWithParam[] ewp = new ElemWithParam[length + 1];
  312. System.arraycopy(m_paramElems, 0, ewp, 0, length);
  313. m_paramElems = ewp;
  314. ewp[length] = ParamElem;
  315. }
  316. }
  317. /**
  318. * Add a child to the child list.
  319. * <!ELEMENT xsl:apply-templates (xsl:sort|xsl:with-param)*>
  320. * <!ATTLIST xsl:apply-templates
  321. * select %expr; "node()"
  322. * mode %qname; #IMPLIED
  323. * >
  324. *
  325. * @param newChild Child to add to this node's children list
  326. *
  327. * @return The child that was just added the children list
  328. *
  329. * @throws DOMException
  330. */
  331. public ElemTemplateElement appendChild(ElemTemplateElement newChild)
  332. {
  333. int type = ((ElemTemplateElement) newChild).getXSLToken();
  334. if (Constants.ELEMNAME_WITHPARAM == type)
  335. {
  336. setParamElem((ElemWithParam) newChild);
  337. }
  338. // You still have to append, because this element can
  339. // contain a for-each, and other elements.
  340. return super.appendChild(newChild);
  341. }
  342. /**
  343. * Call the children visitors.
  344. * @param visitor The visitor whose appropriate method will be called.
  345. */
  346. public void callChildVisitors(XSLTVisitor visitor, boolean callAttrs)
  347. {
  348. // if (null != m_paramElems)
  349. // {
  350. // int size = m_paramElems.length;
  351. //
  352. // for (int i = 0; i < size; i++)
  353. // {
  354. // ElemWithParam ewp = m_paramElems[i];
  355. // ewp.callVisitors(visitor);
  356. // }
  357. // }
  358. super.callChildVisitors(visitor, callAttrs);
  359. }
  360. }