1 /*
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18 /*
19  * $Id: ElemIf.java 468643 2006-10-28 06:56:03Z minchau $
20  */
21 package org.apache.xalan.templates;
22 
23 import javax.xml.transform.TransformerException;
24 
25 import org.apache.xalan.transformer.TransformerImpl;
26 import org.apache.xpath.XPath;
27 import org.apache.xpath.XPathContext;
28 import org.apache.xpath.objects.XObject;
29 
30 /**
31  * Implement xsl:if.
32  * <pre>
33  * <!ELEMENT xsl:if %template;>
34  * <!ATTLIST xsl:if
35  *   test %expr; #REQUIRED
36  *   %space-att;
37  * >
38  * </pre>
39  * @see <a href="http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:if">XXX in XSLT Specification</a>
40  * @xsl.usage advanced
41  */
42 public class ElemIf extends ElemTemplateElement
43 {
44     static final long serialVersionUID = 2158774632427453022L;
45 
46   /**
47    * The xsl:if element must have a test attribute, which specifies an expression.
48    * @serial
49    */
50   private XPath m_test = null;
51 
52   /**
53    * Set the "test" attribute.
54    * The xsl:if element must have a test attribute, which specifies an expression.
55    *
56    * @param v test attribute to set
57    */
setTest(XPath v)58   public void setTest(XPath v)
59   {
60     m_test = v;
61   }
62 
63   /**
64    * Get the "test" attribute.
65    * The xsl:if element must have a test attribute, which specifies an expression.
66    *
67    * @return the "test" attribute for this element.
68    */
getTest()69   public XPath getTest()
70   {
71     return m_test;
72   }
73 
74   /**
75    * This function is called after everything else has been
76    * recomposed, and allows the template to set remaining
77    * values that may be based on some other property that
78    * depends on recomposition.
79    *
80    * @param sroot The root stylesheet.
81    *
82    * @throws TransformerException
83    */
compose(StylesheetRoot sroot)84   public void compose(StylesheetRoot sroot) throws TransformerException
85   {
86 
87     super.compose(sroot);
88 
89     java.util.Vector vnames = sroot.getComposeState().getVariableNames();
90 
91     if (null != m_test)
92       m_test.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
93   }
94 
95   /**
96    * Get an int constant identifying the type of element.
97    * @see org.apache.xalan.templates.Constants
98    *
99    * @return The token ID for this element
100    */
getXSLToken()101   public int getXSLToken()
102   {
103     return Constants.ELEMNAME_IF;
104   }
105 
106   /**
107    * Return the node name.
108    *
109    * @return the element's name
110    */
getNodeName()111   public String getNodeName()
112   {
113     return Constants.ELEMNAME_IF_STRING;
114   }
115 
116   /**
117    * Conditionally execute a sub-template.
118    * The expression is evaluated and the resulting object is converted
119    * to a boolean as if by a call to the boolean function. If the result
120    * is true, then the content template is instantiated; otherwise, nothing
121    * is created.
122    *
123    * @param transformer non-null reference to the the current transform-time state.
124    *
125    * @throws TransformerException
126    */
execute(TransformerImpl transformer)127   public void execute(TransformerImpl transformer) throws TransformerException
128   {
129 
130     XPathContext xctxt = transformer.getXPathContext();
131     int sourceNode = xctxt.getCurrentNode();
132 
133       if (m_test.bool(xctxt, sourceNode, this)) {
134           transformer.executeChildTemplates(this, true);
135       }
136 
137   }
138 
139   /**
140    * Call the children visitors.
141    * @param visitor The visitor whose appropriate method will be called.
142    */
callChildVisitors(XSLTVisitor visitor, boolean callAttrs)143   protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs)
144   {
145   	if(callAttrs)
146   		m_test.getExpression().callVisitors(m_test, visitor);
147     super.callChildVisitors(visitor, callAttrs);
148   }
149 
150 }
151