2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 * $Id: ElemWithParam.java 468643 2006-10-28 06:56:03Z minchau $
21 package org.apache.xalan.templates;
23 import javax.xml.transform.TransformerException;
25 import org.apache.xalan.res.XSLTErrorResources;
26 import org.apache.xalan.transformer.TransformerImpl;
27 import org.apache.xml.utils.QName;
28 import org.apache.xpath.XPath;
29 import org.apache.xpath.XPathContext;
30 import org.apache.xpath.objects.XObject;
31 import org.apache.xpath.objects.XRTreeFrag;
32 import org.apache.xpath.objects.XString;
35 * Implement xsl:with-param. xsl:with-param is allowed within
36 * both xsl:call-template and xsl:apply-templates.
38 * <!ELEMENT xsl:with-param %template;>
39 * <!ATTLIST xsl:with-param
40 * name %qname; #REQUIRED
41 * select %expr; #IMPLIED
44 * @see <a href="http://www.w3.org/TR/xslt#element-with-param">element-with-param in XSLT Specification</a>
47 public class ElemWithParam extends ElemTemplateElement
49 static final long serialVersionUID = -1070355175864326257L;
51 * This is the index to the stack frame being called, <emph>not</emph> the
52 * stack frame that contains this element.
57 * The "select" attribute, which specifies the value of the
58 * argument, if element content is not specified.
61 private XPath m_selectPattern = null;
64 * Set the "select" attribute.
65 * The "select" attribute specifies the value of the
66 * argument, if element content is not specified.
68 * @param v Value to set for the "select" attribute.
70 public void setSelect(XPath v)
76 * Get the "select" attribute.
77 * The "select" attribute specifies the value of the
78 * argument, if element content is not specified.
80 * @return Value of the "select" attribute.
82 public XPath getSelect()
84 return m_selectPattern;
88 * The required name attribute specifies the name of the
89 * parameter (the variable the value of whose binding is
90 * to be replaced). The value of the name attribute is a QName,
91 * which is expanded as described in [2.4 Qualified Names].
94 private QName m_qname = null;
99 * Set the "name" attribute.
102 * @param v Value to set for the "name" attribute.
104 public void setName(QName v)
110 * Get the "name" attribute.
113 * @return Value of the "name" attribute.
115 public QName getName()
121 * Get an integer representation of the element type.
123 * @return An integer representation of the element, defined in the
125 * @see org.apache.xalan.templates.Constants
127 public int getXSLToken()
129 return Constants.ELEMNAME_WITHPARAM;
134 * Return the node name.
136 * @return the node name.
138 public String getNodeName()
140 return Constants.ELEMNAME_WITHPARAM_STRING;
144 * This function is called after everything else has been
145 * recomposed, and allows the template to set remaining
146 * values that may be based on some other property that
147 * depends on recomposition.
149 public void compose(StylesheetRoot sroot) throws TransformerException
151 // See if we can reduce an RTF to a select with a string expression.
152 if(null == m_selectPattern
153 && sroot.getOptimizer())
155 XPath newSelect = ElemVariable.rewriteChildToExpression(this);
156 if(null != newSelect)
157 m_selectPattern = newSelect;
159 m_qnameID = sroot.getComposeState().getQNameID(m_qname);
160 super.compose(sroot);
162 java.util.Vector vnames = sroot.getComposeState().getVariableNames();
163 if(null != m_selectPattern)
164 m_selectPattern.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
166 // m_index must be resolved by ElemApplyTemplates and ElemCallTemplate!
170 * Set the parent as an ElemTemplateElement.
172 * @param p This node's parent as an ElemTemplateElement
174 public void setParentElem(ElemTemplateElement p)
176 super.setParentElem(p);
177 p.m_hasVariableDecl = true;
181 * Get the XObject representation of the variable.
183 * @param transformer non-null reference to the the current transform-time state.
184 * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
186 * @return the XObject representation of the variable.
188 * @throws TransformerException
190 public XObject getValue(TransformerImpl transformer, int sourceNode)
191 throws TransformerException
195 XPathContext xctxt = transformer.getXPathContext();
197 xctxt.pushCurrentNode(sourceNode);
201 if (null != m_selectPattern)
203 var = m_selectPattern.execute(xctxt, sourceNode, this);
205 var.allowDetachToRelease(false);
207 else if (null == getFirstChildElem())
209 var = XString.EMPTYSTRING;
214 // Use result tree fragment
215 int df = transformer.transformToRTF(this);
217 var = new XRTreeFrag(df, xctxt, this);
222 xctxt.popCurrentNode();
229 * Call the children visitors.
230 * @param visitor The visitor whose appropriate method will be called.
232 protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs)
234 if(callAttrs && (null != m_selectPattern))
235 m_selectPattern.getExpression().callVisitors(m_selectPattern, visitor);
236 super.callChildVisitors(visitor, callAttrs);
240 * Add a child to the child list. If the select attribute
241 * is present, an error will be raised.
243 * @param elem New element to append to this element's children list
245 * @return null if the select attribute was present, otherwise the
246 * child just added to the child list
248 public ElemTemplateElement appendChild(ElemTemplateElement elem)
250 // cannot have content and select
251 if (m_selectPattern != null)
253 error(XSLTErrorResources.ER_CANT_HAVE_CONTENT_AND_SELECT,
254 new Object[]{"xsl:" + this.getNodeName()});
257 return super.appendChild(elem);