KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > templates > ElemWithParam


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ElemWithParam.java,v 1.14 2004/02/16 20:32:33 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.xalan.res.XSLTErrorResources;
24 import org.apache.xalan.transformer.TransformerImpl;
25 import org.apache.xml.utils.QName;
26 import org.apache.xpath.XPath;
27 import org.apache.xpath.XPathContext;
28 import org.apache.xpath.objects.XObject;
29 import org.apache.xpath.objects.XRTreeFrag;
30 import org.apache.xpath.objects.XString;
31
32 /**
33  * Implement xsl:with-param. xsl:with-param is allowed within
34  * both xsl:call-template and xsl:apply-templates.
35  * <pre>
36  * <!ELEMENT xsl:with-param %template;>
37  * <!ATTLIST xsl:with-param
38  * name %qname; #REQUIRED
39  * select %expr; #IMPLIED
40  * >
41  * </pre>
42  * @see <a HREF="http://www.w3.org/TR/xslt#element-with-param">element-with-param in XSLT Specification</a>
43  * @xsl.usage advanced
44  */

45 public class ElemWithParam extends ElemTemplateElement
46 {
47   /**
48    * This is the index to the stack frame being called, <emph>not</emph> the
49    * stack frame that contains this element.
50    */

51   int m_index;
52
53   /**
54    * The "select" attribute, which specifies the value of the
55    * argument, if element content is not specified.
56    * @serial
57    */

58   private XPath m_selectPattern = null;
59
60   /**
61    * Set the "select" attribute.
62    * The "select" attribute specifies the value of the
63    * argument, if element content is not specified.
64    *
65    * @param v Value to set for the "select" attribute.
66    */

67   public void setSelect(XPath v)
68   {
69     m_selectPattern = v;
70   }
71
72   /**
73    * Get the "select" attribute.
74    * The "select" attribute specifies the value of the
75    * argument, if element content is not specified.
76    *
77    * @return Value of the "select" attribute.
78    */

79   public XPath getSelect()
80   {
81     return m_selectPattern;
82   }
83
84   /**
85    * The required name attribute specifies the name of the
86    * parameter (the variable the value of whose binding is
87    * to be replaced). The value of the name attribute is a QName,
88    * which is expanded as described in [2.4 Qualified Names].
89    * @serial
90    */

91   private QName m_qname = null;
92   
93   int m_qnameID;
94
95   /**
96    * Set the "name" attribute.
97    * DJD
98    *
99    * @param v Value to set for the "name" attribute.
100    */

101   public void setName(QName v)
102   {
103     m_qname = v;
104   }
105
106   /**
107    * Get the "name" attribute.
108    * DJD
109    *
110    * @return Value of the "name" attribute.
111    */

112   public QName getName()
113   {
114     return m_qname;
115   }
116
117   /**
118    * Get an integer representation of the element type.
119    *
120    * @return An integer representation of the element, defined in the
121    * Constants class.
122    * @see org.apache.xalan.templates.Constants
123    */

124   public int getXSLToken()
125   {
126     return Constants.ELEMNAME_WITHPARAM;
127   }
128
129
130   /**
131    * Return the node name.
132    *
133    * @return the node name.
134    */

135   public String JavaDoc getNodeName()
136   {
137     return Constants.ELEMNAME_WITHPARAM_STRING;
138   }
139   
140   /**
141    * This function is called after everything else has been
142    * recomposed, and allows the template to set remaining
143    * values that may be based on some other property that
144    * depends on recomposition.
145    */

146   public void compose(StylesheetRoot sroot) throws TransformerException JavaDoc
147   {
148     // See if we can reduce an RTF to a select with a string expression.
149
if(null == m_selectPattern
150        && org.apache.xalan.processor.TransformerFactoryImpl.m_optimize)
151     {
152       XPath newSelect = ElemVariable.rewriteChildToExpression(this);
153       if(null != newSelect)
154         m_selectPattern = newSelect;
155     }
156     m_qnameID = sroot.getComposeState().getQNameID(m_qname);
157     super.compose(sroot);
158     
159     java.util.Vector JavaDoc vnames = sroot.getComposeState().getVariableNames();
160     if(null != m_selectPattern)
161       m_selectPattern.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
162       
163     // m_index must be resolved by ElemApplyTemplates and ElemCallTemplate!
164
}
165   
166   /**
167    * Set the parent as an ElemTemplateElement.
168    *
169    * @param parent This node's parent as an ElemTemplateElement
170    */

171   public void setParentElem(ElemTemplateElement p)
172   {
173     super.setParentElem(p);
174     p.m_hasVariableDecl = true;
175   }
176   
177   /**
178    * Get the XObject representation of the variable.
179    *
180    * @param transformer non-null reference to the the current transform-time state.
181    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
182    *
183    * @return the XObject representation of the variable.
184    *
185    * @throws TransformerException
186    */

187   public XObject getValue(TransformerImpl transformer, int sourceNode)
188           throws TransformerException JavaDoc
189   {
190
191     XObject var;
192     XPathContext xctxt = transformer.getXPathContext();
193
194     xctxt.pushCurrentNode(sourceNode);
195
196     try
197     {
198       if (null != m_selectPattern)
199       {
200         var = m_selectPattern.execute(xctxt, sourceNode, this);
201
202         var.allowDetachToRelease(false);
203
204         if (TransformerImpl.S_DEBUG)
205           transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
206                   "select", m_selectPattern, var);
207       }
208       else if (null == getFirstChildElem())
209       {
210         var = XString.EMPTYSTRING;
211       }
212       else
213       {
214
215         // Use result tree fragment
216
int df = transformer.transformToRTF(this);
217
218         var = new XRTreeFrag(df, xctxt, this);
219       }
220     }
221     finally
222     {
223       xctxt.popCurrentNode();
224     }
225
226     return var;
227   }
228   
229   /**
230    * Call the children visitors.
231    * @param visitor The visitor whose appropriate method will be called.
232    */

233   protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs)
234   {
235     if(callAttrs && (null != m_selectPattern))
236         m_selectPattern.getExpression().callVisitors(m_selectPattern, visitor);
237     super.callChildVisitors(visitor, callAttrs);
238   }
239   
240   /**
241    * Add a child to the child list. If the select attribute
242    * is present, an error will be raised.
243    *
244    * @param elem New element to append to this element's children list
245    *
246    * @return null if the select attribute was present, otherwise the
247    * child just added to the child list
248    */

249   public ElemTemplateElement appendChild(ElemTemplateElement elem)
250   {
251     // cannot have content and select
252
if (m_selectPattern != null)
253     {
254       error(XSLTErrorResources.ER_CANT_HAVE_CONTENT_AND_SELECT,
255           new Object JavaDoc[]{"xsl:" + this.getNodeName()});
256       return null;
257     }
258     return super.appendChild(elem);
259   }
260
261
262 }
263
Popular Tags