KickJava   Java API By Example, From Geeks To Geeks.

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


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: ElemTemplate.java,v 1.20 2004/02/16 20:32:32 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import javax.xml.transform.SourceLocator JavaDoc;
22 import javax.xml.transform.TransformerException JavaDoc;
23
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
29 /**
30  * Implement xsl:template.
31  * <pre>
32  * <!ELEMENT xsl:template
33  * (#PCDATA
34  * %instructions;
35  * %result-elements;
36  * | xsl:param)
37  * >
38  *
39  * <!ATTLIST xsl:template
40  * match %pattern; #IMPLIED
41  * name %qname; #IMPLIED
42  * priority %priority; #IMPLIED
43  * mode %qname; #IMPLIED
44  * %space-att;
45  * >
46  * </pre>
47  * @see <a HREF="http://www.w3.org/TR/xslt#section-Defining-Template-Rules">section-Defining-Template-Rules in XSLT Specification</a>
48  * @xsl.usage advanced
49  */

50 public class ElemTemplate extends ElemTemplateElement
51 {
52   /** The public identifier for the current document event.
53    * @serial */

54   private String JavaDoc m_publicId;
55
56   /** The system identifier for the current document event.
57    * @serial */

58   private String JavaDoc m_systemId;
59
60   /**
61    * Return the public identifier for the current document event.
62    * <p>This will be the public identifier
63    * @return A string containing the public identifier, or
64    * null if none is available.
65    * @see #getSystemId
66    */

67   public String JavaDoc getPublicId()
68   {
69     return m_publicId;
70   }
71
72   /**
73    * Return the system identifier for the current document event.
74    *
75    * <p>If the system identifier is a URL, the parser must resolve it
76    * fully before passing it to the application.</p>
77    *
78    * @return A string containing the system identifier, or null
79    * if none is available.
80    * @see #getPublicId
81    */

82   public String JavaDoc getSystemId()
83   {
84     return m_systemId;
85   }
86
87   /**
88    * Set the location information for this element.
89    *
90    * @param locator SourceLocator holding location information
91    */

92   public void setLocaterInfo(SourceLocator JavaDoc locator)
93   {
94
95     m_publicId = locator.getPublicId();
96     m_systemId = locator.getSystemId();
97
98     super.setLocaterInfo(locator);
99   }
100
101   /**
102    * The owning stylesheet.
103    * (Should this only be put on the template element, to
104    * conserve space?)
105    * @serial
106    */

107   private Stylesheet m_stylesheet;
108
109   /**
110    * Get the stylesheet composed (resolves includes and
111    * imports and has methods on it that return "composed" properties.
112    *
113    * @return The stylesheet composed.
114    */

115   public StylesheetComposed getStylesheetComposed()
116   {
117     return m_stylesheet.getStylesheetComposed();
118   }
119
120   /**
121    * Get the owning stylesheet.
122    *
123    * @return The owning stylesheet.
124    */

125   public Stylesheet getStylesheet()
126   {
127     return m_stylesheet;
128   }
129
130   /**
131    * Set the owning stylesheet.
132    *
133    * @param sheet The owning stylesheet for this element
134    */

135   public void setStylesheet(Stylesheet sheet)
136   {
137     m_stylesheet = sheet;
138   }
139
140   /**
141    * Get the root stylesheet.
142    *
143    * @return The root stylesheet for this element
144    */

145   public StylesheetRoot getStylesheetRoot()
146   {
147     return m_stylesheet.getStylesheetRoot();
148   }
149
150   /**
151    * The match attribute is a Pattern that identifies the source
152    * node or nodes to which the rule applies.
153    * @serial
154    */

155   private XPath m_matchPattern = null;
156
157   /**
158    * Set the "match" attribute.
159    * The match attribute is a Pattern that identifies the source
160    * node or nodes to which the rule applies. The match attribute
161    * is required unless the xsl:template element has a name
162    * attribute (see [6 Named Templates]). It is an error for the
163    * value of the match attribute to contain a VariableReference.
164    * @see <a HREF="http://www.w3.org/TR/xslt#patterns">patterns in XSLT Specification</a>
165    *
166    * @param v Value to set for the "match" attribute
167    */

168   public void setMatch(XPath v)
169   {
170     m_matchPattern = v;
171   }
172
173   /**
174    * Get the "match" attribute.
175    * The match attribute is a Pattern that identifies the source
176    * node or nodes to which the rule applies. The match attribute
177    * is required unless the xsl:template element has a name
178    * attribute (see [6 Named Templates]). It is an error for the
179    * value of the match attribute to contain a VariableReference.
180    * @see <a HREF="http://www.w3.org/TR/xslt#patterns">patterns in XSLT Specification</a>
181    *
182    * @return Value of the "match" attribute
183    */

184   public XPath getMatch()
185   {
186     return m_matchPattern;
187   }
188
189   /**
190    * An xsl:template element with a name attribute specifies a named template.
191    * @serial
192    */

193   private QName m_name = null;
194
195   /**
196    * Set the "name" attribute.
197    * An xsl:template element with a name attribute specifies a named template.
198    * If an xsl:template element has a name attribute, it may, but need not,
199    * also have a match attribute.
200    * @see <a HREF="http://www.w3.org/TR/xslt#named-templates">named-templates in XSLT Specification</a>
201    *
202    * @param v Value to set the "name" attribute
203    */

204   public void setName(QName v)
205   {
206     m_name = v;
207   }
208
209   /**
210    * Get the "name" attribute.
211    * An xsl:template element with a name attribute specifies a named template.
212    * If an xsl:template element has a name attribute, it may, but need not,
213    * also have a match attribute.
214    * @see <a HREF="http://www.w3.org/TR/xslt#named-templates">named-templates in XSLT Specification</a>
215    *
216    * @return Value of the "name" attribute
217    */

218   public QName getName()
219   {
220     return m_name;
221   }
222
223   /**
224    * Modes allow an element to be processed multiple times,
225    * each time producing a different result.
226    * @serial
227    */

228   private QName m_mode;
229
230   /**
231    * Set the "mode" attribute.
232    * Modes allow an element to be processed multiple times,
233    * each time producing a different result. If xsl:template
234    * does not have a match attribute, it must not have a mode attribute.
235    * @see <a HREF="http://www.w3.org/TR/xslt#modes">modes in XSLT Specification</a>
236    *
237    * @param v Value to set the "mode" attribute
238    */

239   public void setMode(QName v)
240   {
241     m_mode = v;
242   }
243
244   /**
245    * Get the "mode" attribute.
246    * Modes allow an element to be processed multiple times,
247    * each time producing a different result. If xsl:template
248    * does not have a match attribute, it must not have a mode attribute.
249    * @see <a HREF="http://www.w3.org/TR/xslt#modes">modes in XSLT Specification</a>
250    *
251    * @return Value of the "mode" attribute
252    */

253   public QName getMode()
254   {
255     return m_mode;
256   }
257
258   /**
259    * The priority of a template rule is specified by the priority
260    * attribute on the template rule.
261    * @serial
262    */

263   private double m_priority = XPath.MATCH_SCORE_NONE;
264
265   /**
266    * Set the "priority" attribute.
267    * The priority of a template rule is specified by the priority
268    * attribute on the template rule. The value of this must be a
269    * real number (positive or negative), matching the production
270    * Number with an optional leading minus sign (-).
271    * @see <a HREF="http://www.w3.org/TR/xslt#conflict">conflict in XSLT Specification</a>
272    *
273    * @param v The value to set for the "priority" attribute
274    */

275   public void setPriority(double v)
276   {
277     m_priority = v;
278   }
279
280   /**
281    * Get the "priority" attribute.
282    * The priority of a template rule is specified by the priority
283    * attribute on the template rule. The value of this must be a
284    * real number (positive or negative), matching the production
285    * Number with an optional leading minus sign (-).
286    * @see <a HREF="http://www.w3.org/TR/xslt#conflict">conflict in XSLT Specification</a>
287    *
288    * @return The value of the "priority" attribute
289    */

290   public double getPriority()
291   {
292     return m_priority;
293   }
294
295   /**
296    * Get an int constant identifying the type of element.
297    * @see org.apache.xalan.templates.Constants
298    *
299    * @return The token ID for the element
300    */

301   public int getXSLToken()
302   {
303     return Constants.ELEMNAME_TEMPLATE;
304   }
305
306   /**
307    * Return the node name.
308    *
309    * @return The element's name
310    */

311   public String JavaDoc getNodeName()
312   {
313     return Constants.ELEMNAME_TEMPLATE_STRING;
314   }
315   
316   /**
317    * The stack frame size for this template, which is equal to the maximum number
318    * of params and variables that can be declared in the template at one time.
319    */

320   public int m_frameSize;
321   
322   /**
323    * The size of the portion of the stack frame that can hold parameter
324    * arguments.
325    */

326   int m_inArgsSize;
327   
328   /**
329    * List of namespace/local-name pairs, DTM style, that are unique
330    * qname identifiers for the arguments. The position of a given qname
331    * in the list is the argument ID, and thus the position in the stack
332    * frame.
333    */

334   private int[] m_argsQNameIDs;
335   
336   /**
337    * This function is called after everything else has been
338    * recomposed, and allows the template to set remaining
339    * values that may be based on some other property that
340    * depends on recomposition.
341    */

342   public void compose(StylesheetRoot sroot) throws TransformerException JavaDoc
343   {
344     super.compose(sroot);
345     StylesheetRoot.ComposeState cstate = sroot.getComposeState();
346     java.util.Vector JavaDoc vnames = cstate.getVariableNames();
347     if(null != m_matchPattern)
348       m_matchPattern.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
349       
350     cstate.resetStackFrameSize();
351     m_inArgsSize = 0;
352   }
353   
354   /**
355    * This after the template's children have been composed.
356    */

357   public void endCompose(StylesheetRoot sroot) throws TransformerException JavaDoc
358   {
359     StylesheetRoot.ComposeState cstate = sroot.getComposeState();
360     super.endCompose(sroot);
361     m_frameSize = cstate.getFrameSize();
362     
363     cstate.resetStackFrameSize();
364   }
365
366   /**
367    * Copy the template contents into the result tree.
368    * The content of the xsl:template element is the template
369    * that is instantiated when the template rule is applied.
370    *
371    * @param transformer non-null reference to the the current transform-time state.
372    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
373    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
374    *
375    * @throws TransformerException
376    */

377   public void execute(
378           TransformerImpl transformer)
379             throws TransformerException JavaDoc
380   {
381     XPathContext xctxt = transformer.getXPathContext();
382     
383     transformer.getStackGuard().checkForInfinateLoop();
384     
385     xctxt.pushRTFContext();
386
387     if (TransformerImpl.S_DEBUG)
388       transformer.getTraceManager().fireTraceEvent(this);
389
390       // %REVIEW% commenting out of the code below.
391
// if (null != sourceNode)
392
// {
393
transformer.executeChildTemplates(this, true);
394 // }
395
// else // if(null == sourceNode)
396
// {
397
// transformer.getMsgMgr().error(this,
398
// this, sourceNode,
399
// XSLTErrorResources.ER_NULL_SOURCENODE_HANDLEAPPLYTEMPLATES);
400
//
401
// //"sourceNode is null in handleApplyTemplatesInstruction!");
402
// }
403

404     if (TransformerImpl.S_DEBUG)
405       transformer.getTraceManager().fireTraceEndEvent(this);
406
407     xctxt.popRTFContext();
408     }
409
410   /**
411    * This function is called during recomposition to
412    * control how this element is composed.
413    * @param root The root stylesheet for this transformation.
414    */

415   public void recompose(StylesheetRoot root)
416   {
417     root.recomposeTemplates(this);
418   }
419
420 }
421
Popular Tags