KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 package org.apache.xalan.templates;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.xalan.transformer.TransformerImpl;
24 import org.apache.xpath.XPath;
25 import org.apache.xpath.XPathContext;
26 import org.apache.xpath.objects.XObject;
27
28 /**
29  * Implement xsl:if.
30  * <pre>
31  * <!ELEMENT xsl:if %template;>
32  * <!ATTLIST xsl:if
33  * test %expr; #REQUIRED
34  * %space-att;
35  * >
36  * </pre>
37  * @see <a HREF="http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:if">XXX in XSLT Specification</a>
38  * @xsl.usage advanced
39  */

40 public class ElemIf extends ElemTemplateElement
41 {
42
43   /**
44    * The xsl:if element must have a test attribute, which specifies an expression.
45    * @serial
46    */

47   private XPath m_test = null;
48
49   /**
50    * Set the "test" attribute.
51    * The xsl:if element must have a test attribute, which specifies an expression.
52    *
53    * @param v test attribute to set
54    */

55   public void setTest(XPath v)
56   {
57     m_test = v;
58   }
59
60   /**
61    * Get the "test" attribute.
62    * The xsl:if element must have a test attribute, which specifies an expression.
63    *
64    * @return the "test" attribute for this element.
65    */

66   public XPath getTest()
67   {
68     return m_test;
69   }
70
71   /**
72    * This function is called after everything else has been
73    * recomposed, and allows the template to set remaining
74    * values that may be based on some other property that
75    * depends on recomposition.
76    *
77    * @param sroot The root stylesheet.
78    *
79    * @throws TransformerException
80    */

81   public void compose(StylesheetRoot sroot) throws TransformerException JavaDoc
82   {
83
84     super.compose(sroot);
85
86     java.util.Vector JavaDoc vnames = sroot.getComposeState().getVariableNames();
87
88     if (null != m_test)
89       m_test.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
90   }
91
92   /**
93    * Get an int constant identifying the type of element.
94    * @see org.apache.xalan.templates.Constants
95    *
96    * @return The token ID for this element
97    */

98   public int getXSLToken()
99   {
100     return Constants.ELEMNAME_IF;
101   }
102
103   /**
104    * Return the node name.
105    *
106    * @return the element's name
107    */

108   public String JavaDoc getNodeName()
109   {
110     return Constants.ELEMNAME_IF_STRING;
111   }
112
113   /**
114    * Conditionally execute a sub-template.
115    * The expression is evaluated and the resulting object is converted
116    * to a boolean as if by a call to the boolean function. If the result
117    * is true, then the content template is instantiated; otherwise, nothing
118    * is created.
119    *
120    * @param transformer non-null reference to the the current transform-time state.
121    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
122    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
123    *
124    * @throws TransformerException
125    */

126   public void execute(TransformerImpl transformer) throws TransformerException JavaDoc
127   {
128
129     XPathContext xctxt = transformer.getXPathContext();
130     int sourceNode = xctxt.getCurrentNode();
131
132     if (TransformerImpl.S_DEBUG)
133     {
134       XObject test = m_test.execute(xctxt, sourceNode, this);
135
136       if (TransformerImpl.S_DEBUG)
137         transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
138                 "test", m_test, test);
139     
140       // xsl:for-each now fires one trace event + one for every
141
// iteration; changing xsl:if to fire one regardless of true/false
142

143       if (TransformerImpl.S_DEBUG)
144         transformer.getTraceManager().fireTraceEvent(this);
145
146       if (test.bool())
147       {
148         transformer.executeChildTemplates(this, true);
149       }
150
151       if (TransformerImpl.S_DEBUG)
152         transformer.getTraceManager().fireTraceEndEvent(this);
153
154       // I don't think we want this. -sb
155
// if (TransformerImpl.S_DEBUG)
156
// transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
157
// "endTest", m_test, test);
158
}
159     else if (m_test.bool(xctxt, sourceNode, this))
160     {
161       transformer.executeChildTemplates(this, true);
162     }
163     
164   }
165   
166   /**
167    * Call the children visitors.
168    * @param visitor The visitor whose appropriate method will be called.
169    */

170   protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs)
171   {
172     if(callAttrs)
173         m_test.getExpression().callVisitors(m_test, visitor);
174     super.callChildVisitors(visitor, callAttrs);
175   }
176
177 }
178
Popular Tags