KickJava   Java API By Example, From Geeks To Geeks.

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


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: ElemChoose.java,v 1.18 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.res.XSLTErrorResources;
24 import org.apache.xalan.transformer.TransformerImpl;
25 import org.apache.xpath.XPathContext;
26 import org.apache.xpath.objects.XObject;
27
28 /**
29  * Implement xsl:choose.
30  * <pre>
31  * <!ELEMENT xsl:choose (xsl:when+, xsl:otherwise?)>
32  * <!ATTLIST xsl:choose %space-att;>
33  * </pre>
34  * @see <a HREF="http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:choose">XXX in XSLT Specification</a>
35  * @xsl.usage advanced
36  */

37 public class ElemChoose extends ElemTemplateElement
38 {
39
40   /**
41    * Get an int constant identifying the type of element.
42    * @see org.apache.xalan.templates.Constants
43    *
44    * @return The token ID for this element
45    */

46   public int getXSLToken()
47   {
48     return Constants.ELEMNAME_CHOOSE;
49   }
50
51   /**
52    * Return the node name.
53    *
54    * @return The element's name
55    */

56   public String JavaDoc getNodeName()
57   {
58     return Constants.ELEMNAME_CHOOSE_STRING;
59   }
60
61   /**
62    * Constructor ElemChoose
63    *
64    */

65   public ElemChoose(){}
66
67   /**
68    * Execute the xsl:choose transformation.
69    *
70    *
71    * @param transformer non-null reference to the the current transform-time state.
72    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
73    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
74    *
75    * @throws TransformerException
76    */

77   public void execute(TransformerImpl transformer) throws TransformerException JavaDoc
78   {
79
80     if (TransformerImpl.S_DEBUG)
81       transformer.getTraceManager().fireTraceEvent(this);
82
83     boolean found = false;
84
85     for (ElemTemplateElement childElem = getFirstChildElem();
86             childElem != null; childElem = childElem.getNextSiblingElem())
87     {
88       int type = childElem.getXSLToken();
89
90       if (Constants.ELEMNAME_WHEN == type)
91       {
92         found = true;
93
94         ElemWhen when = (ElemWhen) childElem;
95
96         // must be xsl:when
97
XPathContext xctxt = transformer.getXPathContext();
98         int sourceNode = xctxt.getCurrentNode();
99         
100         // System.err.println("\""+when.getTest().getPatternString()+"\"");
101

102         // if(when.getTest().getPatternString().equals("COLLECTION/icuser/ictimezone/LITERAL='GMT +13:00 Pacific/Tongatapu'"))
103
// System.err.println("Found COLLECTION/icuser/ictimezone/LITERAL");
104

105         if (TransformerImpl.S_DEBUG)
106         {
107           XObject test = when.getTest().execute(xctxt, sourceNode, when);
108
109           if (TransformerImpl.S_DEBUG)
110             transformer.getTraceManager().fireSelectedEvent(sourceNode, when,
111                     "test", when.getTest(), test);
112
113           if (test.bool())
114           {
115             transformer.getTraceManager().fireTraceEvent(when);
116             
117             transformer.executeChildTemplates(when, true);
118
119             transformer.getTraceManager().fireTraceEndEvent(when);
120                       
121             return;
122           }
123
124         }
125         else if (when.getTest().bool(xctxt, sourceNode, when))
126         {
127           transformer.executeChildTemplates(when, true);
128
129           return;
130         }
131       }
132       else if (Constants.ELEMNAME_OTHERWISE == type)
133       {
134         found = true;
135
136         if (TransformerImpl.S_DEBUG)
137           transformer.getTraceManager().fireTraceEvent(childElem);
138
139         // xsl:otherwise
140
transformer.executeChildTemplates(childElem, true);
141
142         if (TransformerImpl.S_DEBUG)
143           transformer.getTraceManager().fireTraceEndEvent(childElem);
144         return;
145       }
146     }
147
148     if (!found)
149       transformer.getMsgMgr().error(
150         this, XSLTErrorResources.ER_CHOOSE_REQUIRES_WHEN);
151         
152     if (TransformerImpl.S_DEBUG)
153       transformer.getTraceManager().fireTraceEndEvent(this);
154   }
155
156   /**
157    * Add a child to the child list.
158    *
159    * @param newChild Child to add to this node's child list
160    *
161    * @return The child that was just added to the child list
162    *
163    * @throws DOMException
164    */

165   public ElemTemplateElement appendChild(ElemTemplateElement newChild)
166   {
167
168     int type = ((ElemTemplateElement) newChild).getXSLToken();
169
170     switch (type)
171     {
172     case Constants.ELEMNAME_WHEN :
173     case Constants.ELEMNAME_OTHERWISE :
174
175       // TODO: Positional checking
176
break;
177     default :
178       error(XSLTErrorResources.ER_CANNOT_ADD,
179             new Object JavaDoc[]{ newChild.getNodeName(),
180                           this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
181

182     //" to " + this.m_elemName);
183
}
184
185     return super.appendChild(newChild);
186   }
187   
188   /**
189    * Tell if this element can accept variable declarations.
190    * @return true if the element can accept and process variable declarations.
191    */

192   public boolean canAcceptVariables()
193   {
194     return false;
195   }
196
197 }
198
Popular Tags