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.xml.sax.*;
  60. import org.apache.xpath.*;
  61. import org.apache.xpath.objects.XString;
  62. import org.apache.xpath.objects.XObject;
  63. import org.apache.xpath.objects.XRTreeFrag;
  64. import org.apache.xml.utils.QName;
  65. import org.apache.xalan.res.XSLTErrorResources;
  66. import org.apache.xalan.transformer.TransformerImpl;
  67. import javax.xml.transform.TransformerException;
  68. /**
  69. * <meta name="usage" content="advanced"/>
  70. * Implement xsl:with-param. xsl:with-param is allowed within
  71. * both xsl:call-template and xsl:apply-templates.
  72. * <pre>
  73. * <!ELEMENT xsl:with-param %template;>
  74. * <!ATTLIST xsl:with-param
  75. * name %qname; #REQUIRED
  76. * select %expr; #IMPLIED
  77. * >
  78. * </pre>
  79. * @see <a href="http://www.w3.org/TR/xslt#element-with-param">element-with-param in XSLT Specification</a>
  80. */
  81. public class ElemWithParam extends ElemTemplateElement
  82. {
  83. /**
  84. * This is the index to the stack frame being called, <emph>not</emph> the
  85. * stack frame that contains this element.
  86. */
  87. int m_index;
  88. /**
  89. * The "select" attribute, which specifies the value of the
  90. * argument, if element content is not specified.
  91. * @serial
  92. */
  93. private XPath m_selectPattern = null;
  94. /**
  95. * Set the "select" attribute.
  96. * The "select" attribute specifies the value of the
  97. * argument, if element content is not specified.
  98. *
  99. * @param v Value to set for the "select" attribute.
  100. */
  101. public void setSelect(XPath v)
  102. {
  103. m_selectPattern = v;
  104. }
  105. /**
  106. * Get the "select" attribute.
  107. * The "select" attribute specifies the value of the
  108. * argument, if element content is not specified.
  109. *
  110. * @return Value of the "select" attribute.
  111. */
  112. public XPath getSelect()
  113. {
  114. return m_selectPattern;
  115. }
  116. /**
  117. * The required name attribute specifies the name of the
  118. * parameter (the variable the value of whose binding is
  119. * to be replaced). The value of the name attribute is a QName,
  120. * which is expanded as described in [2.4 Qualified Names].
  121. * @serial
  122. */
  123. private QName m_qname = null;
  124. int m_qnameID;
  125. /**
  126. * Set the "name" attribute.
  127. * DJD
  128. *
  129. * @param v Value to set for the "name" attribute.
  130. */
  131. public void setName(QName v)
  132. {
  133. m_qname = v;
  134. }
  135. /**
  136. * Get the "name" attribute.
  137. * DJD
  138. *
  139. * @return Value of the "name" attribute.
  140. */
  141. public QName getName()
  142. {
  143. return m_qname;
  144. }
  145. /**
  146. * Get an integer representation of the element type.
  147. *
  148. * @return An integer representation of the element, defined in the
  149. * Constants class.
  150. * @see org.apache.xalan.templates.Constants
  151. */
  152. public int getXSLToken()
  153. {
  154. return Constants.ELEMNAME_WITHPARAM;
  155. }
  156. /**
  157. * Return the node name.
  158. *
  159. * @return the node name.
  160. */
  161. public String getNodeName()
  162. {
  163. return Constants.ELEMNAME_WITHPARAM_STRING;
  164. }
  165. /**
  166. * This function is called after everything else has been
  167. * recomposed, and allows the template to set remaining
  168. * values that may be based on some other property that
  169. * depends on recomposition.
  170. */
  171. public void compose(StylesheetRoot sroot) throws TransformerException
  172. {
  173. // See if we can reduce an RTF to a select with a string expression.
  174. if(null == m_selectPattern
  175. && org.apache.xalan.processor.TransformerFactoryImpl.m_optimize)
  176. {
  177. XPath newSelect = ElemVariable.rewriteChildToExpression(this);
  178. if(null != newSelect)
  179. m_selectPattern = newSelect;
  180. }
  181. m_qnameID = sroot.getComposeState().getQNameID(m_qname);
  182. super.compose(sroot);
  183. java.util.Vector vnames = sroot.getComposeState().getVariableNames();
  184. if(null != m_selectPattern)
  185. m_selectPattern.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
  186. // m_index must be resolved by ElemApplyTemplates and ElemCallTemplate!
  187. }
  188. /**
  189. * Set the parent as an ElemTemplateElement.
  190. *
  191. * @param parent This node's parent as an ElemTemplateElement
  192. */
  193. public void setParentElem(ElemTemplateElement p)
  194. {
  195. super.setParentElem(p);
  196. p.m_hasVariableDecl = true;
  197. }
  198. /**
  199. * Get the XObject representation of the variable.
  200. *
  201. * @param transformer non-null reference to the the current transform-time state.
  202. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  203. *
  204. * @return the XObject representation of the variable.
  205. *
  206. * @throws TransformerException
  207. */
  208. public XObject getValue(TransformerImpl transformer, int sourceNode)
  209. throws TransformerException
  210. {
  211. XObject var;
  212. XPathContext xctxt = transformer.getXPathContext();
  213. xctxt.pushCurrentNode(sourceNode);
  214. try
  215. {
  216. if (null != m_selectPattern)
  217. {
  218. var = m_selectPattern.execute(xctxt, sourceNode, this);
  219. var.allowDetachToRelease(false);
  220. if (TransformerImpl.S_DEBUG)
  221. transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
  222. "select", m_selectPattern, var);
  223. }
  224. else if (null == getFirstChildElem())
  225. {
  226. var = XString.EMPTYSTRING;
  227. }
  228. else
  229. {
  230. // Use result tree fragment
  231. int df = transformer.transformToRTF(this);
  232. var = new XRTreeFrag(df, xctxt, this);
  233. }
  234. }
  235. finally
  236. {
  237. xctxt.popCurrentNode();
  238. }
  239. return var;
  240. }
  241. /**
  242. * Call the children visitors.
  243. * @param visitor The visitor whose appropriate method will be called.
  244. */
  245. protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs)
  246. {
  247. if(callAttrs && (null != m_selectPattern))
  248. m_selectPattern.getExpression().callVisitors(m_selectPattern, visitor);
  249. super.callChildVisitors(visitor, callAttrs);
  250. }
  251. }