1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.templates;
  58. //import org.w3c.dom.*;
  59. import org.apache.xml.dtm.DTM;
  60. import org.xml.sax.*;
  61. import org.apache.xpath.*;
  62. import org.apache.xpath.objects.XObject;
  63. import org.apache.xalan.trace.SelectionEvent;
  64. import org.apache.xml.utils.QName;
  65. import org.apache.xalan.res.XSLTErrorResources;
  66. import org.apache.xalan.transformer.TransformerImpl;
  67. import javax.xml.transform.TransformerException;
  68. /**
  69. * <meta name="usage" content="advanced"/>
  70. * Implement xsl:if.
  71. * <pre>
  72. * <!ELEMENT xsl:if %template;>
  73. * <!ATTLIST xsl:if
  74. * test %expr; #REQUIRED
  75. * %space-att;
  76. * >
  77. * </pre>
  78. * @see <a href="http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:if">XXX in XSLT Specification</a>
  79. */
  80. public class ElemIf extends ElemTemplateElement
  81. {
  82. /**
  83. * The xsl:if element must have a test attribute, which specifies an expression.
  84. * @serial
  85. */
  86. private XPath m_test = null;
  87. /**
  88. * Set the "test" attribute.
  89. * The xsl:if element must have a test attribute, which specifies an expression.
  90. *
  91. * @param v test attribute to set
  92. */
  93. public void setTest(XPath v)
  94. {
  95. m_test = v;
  96. }
  97. /**
  98. * Get the "test" attribute.
  99. * The xsl:if element must have a test attribute, which specifies an expression.
  100. *
  101. * @return the "test" attribute for this element.
  102. */
  103. public XPath getTest()
  104. {
  105. return m_test;
  106. }
  107. /**
  108. * This function is called after everything else has been
  109. * recomposed, and allows the template to set remaining
  110. * values that may be based on some other property that
  111. * depends on recomposition.
  112. *
  113. * @param sroot The root stylesheet.
  114. *
  115. * @throws TransformerException
  116. */
  117. public void compose(StylesheetRoot sroot) throws TransformerException
  118. {
  119. super.compose(sroot);
  120. java.util.Vector vnames = sroot.getComposeState().getVariableNames();
  121. if (null != m_test)
  122. m_test.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
  123. }
  124. /**
  125. * Get an int constant identifying the type of element.
  126. * @see org.apache.xalan.templates.Constants
  127. *
  128. * @return The token ID for this element
  129. */
  130. public int getXSLToken()
  131. {
  132. return Constants.ELEMNAME_IF;
  133. }
  134. /**
  135. * Return the node name.
  136. *
  137. * @return the element's name
  138. */
  139. public String getNodeName()
  140. {
  141. return Constants.ELEMNAME_IF_STRING;
  142. }
  143. /**
  144. * Conditionally execute a sub-template.
  145. * The expression is evaluated and the resulting object is converted
  146. * to a boolean as if by a call to the boolean function. If the result
  147. * is true, then the content template is instantiated; otherwise, nothing
  148. * is created.
  149. *
  150. * @param transformer non-null reference to the the current transform-time state.
  151. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  152. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  153. *
  154. * @throws TransformerException
  155. */
  156. public void execute(TransformerImpl transformer) throws TransformerException
  157. {
  158. XPathContext xctxt = transformer.getXPathContext();
  159. int sourceNode = xctxt.getCurrentNode();
  160. if (TransformerImpl.S_DEBUG)
  161. {
  162. XObject test = m_test.execute(xctxt, sourceNode, this);
  163. if (TransformerImpl.S_DEBUG)
  164. transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
  165. "test", m_test, test);
  166. if (test.bool())
  167. {
  168. // Note that there was a bad incompatibility with xsl:if...
  169. // which was called whether the test was successful or not.
  170. // With the current logic of xsl:for-each,
  171. // if the node-set is empty trace will not be called at all.
  172. // So I've changed xsl:if to match the xsl:for-each behavior.
  173. // -sb
  174. if (TransformerImpl.S_DEBUG)
  175. transformer.getTraceManager().fireTraceEvent(this);
  176. transformer.executeChildTemplates(this, true);
  177. if (TransformerImpl.S_DEBUG)
  178. transformer.getTraceManager().fireTraceEndEvent(this);
  179. }
  180. // I don't think we want this. -sb
  181. // if (TransformerImpl.S_DEBUG)
  182. // transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
  183. // "endTest", m_test, test);
  184. }
  185. else if (m_test.bool(xctxt, sourceNode, this))
  186. {
  187. transformer.executeChildTemplates(this, true);
  188. }
  189. }
  190. /**
  191. * Call the children visitors.
  192. * @param visitor The visitor whose appropriate method will be called.
  193. */
  194. protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs)
  195. {
  196. if(callAttrs)
  197. m_test.getExpression().callVisitors(m_test, visitor);
  198. super.callChildVisitors(visitor, callAttrs);
  199. }
  200. }