KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > core > IteratorBlock


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.core;
54
55 import java.io.IOException JavaDoc;
56 import java.util.HashSet JavaDoc;
57 import java.util.Set JavaDoc;
58 import freemarker.template.*;
59
60 /**
61  * An instruction that processes a list or foreach block
62  */

63 final class IteratorBlock extends TemplateElement {
64
65     private Expression listExpression;
66     private String JavaDoc indexName;
67     private boolean isForEach;
68
69     /**
70      * @param listVariable a variable referring to a sequence or collection
71      * @param indexName an arbitrary index variable name
72      * @param nestedBlock the nestedBlock to iterate over
73      */

74     IteratorBlock(Expression listExpression,
75                   String JavaDoc indexName,
76                   TemplateElement nestedBlock,
77                   boolean isForEach)
78     {
79         this.listExpression = listExpression;
80         this.indexName = indexName;
81         this.isForEach = isForEach;
82         this.nestedBlock = nestedBlock;
83     }
84
85     void accept(Environment env) throws TemplateException, IOException JavaDoc
86     {
87         TemplateModel baseModel = listExpression.getAsTemplateModel(env);
88         if (baseModel == null) {
89             if (env.isClassicCompatible()) {
90                 // Classic behavior of simply ignoring null references.
91
return;
92             }
93             assertNonNull(baseModel, listExpression, env);
94         }
95         env.visit(new Context(baseModel));
96     }
97
98     public String JavaDoc getCanonicalForm() {
99         if (isForEach) {
100             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<#foreach ");
101             buf.append(indexName);
102             buf.append(" in ");
103             buf.append(listExpression.getCanonicalForm());
104             buf.append(">");
105             if (nestedBlock != null) {
106                 buf.append(nestedBlock.getCanonicalForm());
107             }
108             buf.append("</#foreach>");
109             return buf.toString();
110         }
111         else {
112             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<#list ");
113             buf.append(listExpression.getCanonicalForm());
114             buf.append(" as ");
115             buf.append(indexName);
116             buf.append(">");
117             if (nestedBlock != null) {
118                 buf.append(nestedBlock.getCanonicalForm());
119             }
120             buf.append("</#list>");
121             return buf.toString();
122         }
123     }
124
125     public String JavaDoc getDescription() {
126         if (isForEach) {
127             return "foreach " + indexName + " in " + listExpression;
128
129         }
130         else {
131             return "list " + listExpression + " as " + indexName;
132         }
133     }
134
135     /**
136      * A helper class that holds the context of the loop.
137      */

138
139     class Context implements LocalContext {
140         private boolean hasNext;
141         private TemplateModel loopVar;
142         private int index;
143         private Set JavaDoc variableNames = null;
144         private TemplateModel list;
145         
146         Context(TemplateModel list) {
147             this.list = list;
148         }
149         
150         
151         void runLoop(Environment env) throws TemplateException, IOException JavaDoc {
152             if (list instanceof TemplateCollectionModel) {
153                 TemplateCollectionModel baseListModel = (TemplateCollectionModel)list;
154                 TemplateModelIterator it = baseListModel.iterator();
155                 hasNext = it.hasNext();
156                 while (hasNext) {
157                     loopVar = it.next();
158                     hasNext = it.hasNext();
159                     if (nestedBlock != null) {
160                         env.visit(nestedBlock);
161                     }
162                     index++;
163                 }
164             }
165             else if (list instanceof TemplateSequenceModel) {
166                 TemplateSequenceModel tsm = (TemplateSequenceModel) list;
167                 int size = tsm.size();
168                 for (index =0; index <size; index++) {
169                     loopVar = tsm.get(index);
170                     hasNext = (size > index +1);
171                     if (nestedBlock != null) {
172                         env.visit(nestedBlock);
173                     }
174                 }
175             }
176             else if (env.isClassicCompatible()) {
177                 loopVar = list;
178                 if (nestedBlock != null) {
179                     env.visit(nestedBlock);
180                 }
181             }
182             else {
183                 throw invalidTypeException(list, listExpression, env, "collection or sequence");
184             }
185         }
186
187         public TemplateModel getLocalVariable(String JavaDoc name) {
188             if (name.startsWith(indexName)) {
189                 switch(name.length() - indexName.length()) {
190                     case 0:
191                         return loopVar;
192                     case 6:
193                         if(name.endsWith("_index")) {
194                             return new SimpleNumber(index);
195                         }
196                         break;
197                     case 9:
198                         if(name.endsWith("_has_next")) {
199                             return hasNext ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
200                         }
201                         break;
202                 }
203             }
204             return null;
205         }
206         
207         public Set JavaDoc getLocalVariableNames() {
208             if(variableNames == null) {
209                 variableNames = new HashSet JavaDoc();
210                 variableNames.add(indexName);
211                 variableNames.add(indexName + "_index");
212                 variableNames.add(indexName + "_has_next");
213             }
214             return variableNames;
215         }
216     }
217 }
218
Popular Tags