KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > tagplugins > jstl > core > ForTokens


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.jasper.tagplugins.jstl.core;
20
21 import org.apache.jasper.compiler.tagplugin.TagPlugin;
22 import org.apache.jasper.compiler.tagplugin.TagPluginContext;
23
24 public class ForTokens implements TagPlugin {
25     
26     public void doTag(TagPluginContext ctxt) {
27         boolean hasVar, hasVarStatus, hasBegin, hasEnd, hasStep;
28         
29         //init the flags
30
hasVar = ctxt.isAttributeSpecified("var");
31         hasVarStatus = ctxt.isAttributeSpecified("varStatus");
32         hasBegin = ctxt.isAttributeSpecified("begin");
33         hasEnd = ctxt.isAttributeSpecified("end");
34         hasStep = ctxt.isAttributeSpecified("step");
35         
36         if(hasVarStatus){
37             ctxt.dontUseTagPlugin();
38             return;
39         }
40         
41         //define all the temp variables' names
42
String JavaDoc itemsName = ctxt.getTemporaryVariableName();
43         String JavaDoc delimsName = ctxt.getTemporaryVariableName();
44         String JavaDoc stName = ctxt.getTemporaryVariableName();
45         String JavaDoc beginName = ctxt.getTemporaryVariableName();
46         String JavaDoc endName = ctxt.getTemporaryVariableName();
47         String JavaDoc stepName = ctxt.getTemporaryVariableName();
48         String JavaDoc index = ctxt.getTemporaryVariableName();
49         String JavaDoc temp = ctxt.getTemporaryVariableName();
50         String JavaDoc tokensCountName = ctxt.getTemporaryVariableName();
51         
52         //get the value of the "items" attribute
53
ctxt.generateJavaSource("String " + itemsName + " = (String)");
54         ctxt.generateAttribute("items");
55         ctxt.generateJavaSource(";");
56         
57         //get the value of the "delim" attribute
58
ctxt.generateJavaSource("String " + delimsName + " = (String)");
59         ctxt.generateAttribute("delims");
60         ctxt.generateJavaSource(";");
61         
62         //new a StringTokenizer Object according to the "items" and the "delim"
63
ctxt.generateJavaSource("java.util.StringTokenizer " + stName + " = " +
64                 "new java.util.StringTokenizer(" + itemsName + ", " + delimsName + ");");
65         
66         //if "begin" specified, move the token to the "begin" place
67
//and record the begin index. default begin place is 0.
68
ctxt.generateJavaSource("int " + tokensCountName + " = " + stName + ".countTokens();");
69         if(hasBegin){
70             ctxt.generateJavaSource("int " + beginName + " = " );
71             ctxt.generateAttribute("begin");
72             ctxt.generateJavaSource(";");
73             ctxt.generateJavaSource("for(int " + index + " = 0; " + index + " < " + beginName + " && " + stName + ".hasMoreTokens(); " + index + "++, " + stName + ".nextToken()){}");
74         }else{
75             ctxt.generateJavaSource("int " + beginName + " = 0;");
76         }
77         
78         //when "end" is specified, if the "end" is more than the last index,
79
//record the end place as the last index, otherwise, record it as "end";
80
//default end place is the last index
81
if(hasEnd){
82             ctxt.generateJavaSource("int " + endName + " = 0;" );
83             ctxt.generateJavaSource("if((" + tokensCountName + " - 1) < ");
84             ctxt.generateAttribute("end");
85             ctxt.generateJavaSource("){");
86             ctxt.generateJavaSource(" " + endName + " = " + tokensCountName + " - 1;");
87             ctxt.generateJavaSource("}else{");
88             ctxt.generateJavaSource(" " + endName + " = ");
89             ctxt.generateAttribute("end");
90             ctxt.generateJavaSource(";}");
91         }else{
92             ctxt.generateJavaSource("int " + endName + " = " + tokensCountName + " - 1;");
93         }
94         
95         //get the step value from "step" if specified.
96
//default step value is 1.
97
if(hasStep){
98             ctxt.generateJavaSource("int " + stepName + " = " );
99             ctxt.generateAttribute("step");
100             ctxt.generateJavaSource(";");
101         }else{
102             ctxt.generateJavaSource("int " + stepName + " = 1;");
103         }
104         
105         //the loop
106
ctxt.generateJavaSource("for(int " + index + " = " + beginName + "; " + index + " <= " + endName + "; " + index + "++){");
107         ctxt.generateJavaSource(" String " + temp + " = " + stName + ".nextToken();");
108         ctxt.generateJavaSource(" if(((" + index + " - " + beginName + ") % " + stepName + ") == 0){");
109         //if var specified, put the current token into the attribute "var" defines.
110
if(hasVar){
111             String JavaDoc strVar = ctxt.getConstantAttribute("var");
112             ctxt.generateJavaSource(" pageContext.setAttribute(\"" + strVar + "\", " + temp + ");");
113         }
114         ctxt.generateBody();
115         ctxt.generateJavaSource(" }");
116         ctxt.generateJavaSource("}");
117     }
118     
119 }
120
Popular Tags