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:choose.
  71. * <pre>
  72. * <!ELEMENT xsl:choose (xsl:when+, xsl:otherwise?)>
  73. * <!ATTLIST xsl:choose %space-att;>
  74. * </pre>
  75. * @see <a href="http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:choose">XXX in XSLT Specification</a>
  76. */
  77. public class ElemChoose extends ElemTemplateElement
  78. {
  79. /**
  80. * Get an int constant identifying the type of element.
  81. * @see org.apache.xalan.templates.Constants
  82. *
  83. * @return The token ID for this element
  84. */
  85. public int getXSLToken()
  86. {
  87. return Constants.ELEMNAME_CHOOSE;
  88. }
  89. /**
  90. * Return the node name.
  91. *
  92. * @return The element's name
  93. */
  94. public String getNodeName()
  95. {
  96. return Constants.ELEMNAME_CHOOSE_STRING;
  97. }
  98. /**
  99. * Constructor ElemChoose
  100. *
  101. */
  102. public ElemChoose(){}
  103. /**
  104. * Execute the xsl:choose transformation.
  105. *
  106. *
  107. * @param transformer non-null reference to the the current transform-time state.
  108. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  109. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  110. *
  111. * @throws TransformerException
  112. */
  113. public void execute(TransformerImpl transformer) throws TransformerException
  114. {
  115. if (TransformerImpl.S_DEBUG)
  116. transformer.getTraceManager().fireTraceEvent(this);
  117. boolean found = false;
  118. for (ElemTemplateElement childElem = getFirstChildElem();
  119. childElem != null; childElem = childElem.getNextSiblingElem())
  120. {
  121. int type = childElem.getXSLToken();
  122. if (Constants.ELEMNAME_WHEN == type)
  123. {
  124. found = true;
  125. ElemWhen when = (ElemWhen) childElem;
  126. // must be xsl:when
  127. XPathContext xctxt = transformer.getXPathContext();
  128. int sourceNode = xctxt.getCurrentNode();
  129. // System.err.println("\""+when.getTest().getPatternString()+"\"");
  130. // if(when.getTest().getPatternString().equals("COLLECTION/icuser/ictimezone/LITERAL='GMT +13:00 Pacific/Tongatapu'"))
  131. // System.err.println("Found COLLECTION/icuser/ictimezone/LITERAL");
  132. if (TransformerImpl.S_DEBUG)
  133. {
  134. XObject test = when.getTest().execute(xctxt, sourceNode, when);
  135. if (TransformerImpl.S_DEBUG)
  136. transformer.getTraceManager().fireSelectedEvent(sourceNode, when,
  137. "test", when.getTest(), test);
  138. if (test.bool())
  139. {
  140. transformer.getTraceManager().fireTraceEvent(when);
  141. transformer.executeChildTemplates(when, true);
  142. transformer.getTraceManager().fireTraceEndEvent(when);
  143. return;
  144. }
  145. }
  146. else if (when.getTest().bool(xctxt, sourceNode, when))
  147. {
  148. transformer.executeChildTemplates(when, true);
  149. return;
  150. }
  151. }
  152. else if (Constants.ELEMNAME_OTHERWISE == type)
  153. {
  154. found = true;
  155. if (TransformerImpl.S_DEBUG)
  156. transformer.getTraceManager().fireTraceEvent(childElem);
  157. // xsl:otherwise
  158. transformer.executeChildTemplates(childElem, true);
  159. if (TransformerImpl.S_DEBUG)
  160. transformer.getTraceManager().fireTraceEndEvent(childElem);
  161. return;
  162. }
  163. }
  164. if (!found)
  165. transformer.getMsgMgr().error(
  166. this, XSLTErrorResources.ER_CHOOSE_REQUIRES_WHEN);
  167. if (TransformerImpl.S_DEBUG)
  168. transformer.getTraceManager().fireTraceEndEvent(this);
  169. }
  170. /**
  171. * Add a child to the child list.
  172. *
  173. * @param newChild Child to add to this node's child list
  174. *
  175. * @return The child that was just added to the child list
  176. *
  177. * @throws DOMException
  178. */
  179. public ElemTemplateElement appendChild(ElemTemplateElement newChild)
  180. {
  181. int type = ((ElemTemplateElement) newChild).getXSLToken();
  182. switch (type)
  183. {
  184. case Constants.ELEMNAME_WHEN :
  185. case Constants.ELEMNAME_OTHERWISE :
  186. // TODO: Positional checking
  187. break;
  188. default :
  189. error(XSLTErrorResources.ER_CANNOT_ADD,
  190. new Object[]{ newChild.getNodeName(),
  191. this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
  192. //" to " + this.m_elemName);
  193. }
  194. return super.appendChild(newChild);
  195. }
  196. /**
  197. * Tell if this element can accept variable declarations.
  198. * @return true if the element can accept and process variable declarations.
  199. */
  200. public boolean canAcceptVariables()
  201. {
  202. return false;
  203. }
  204. }