KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > ELNode


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 package org.apache.jasper.compiler;
19
20 import java.util.*;
21 import javax.servlet.jsp.tagext.FunctionInfo JavaDoc;
22 import org.apache.jasper.JasperException;
23
24 /**
25  * This class defines internal representation for an EL Expression
26  *
27  * It currently only defines functions. It can be expanded to define
28  * all the components of an EL expression, if need to.
29  *
30  * @author Kin-man Chung
31  */

32
33 abstract class ELNode {
34
35     abstract public void accept(Visitor v) throws JasperException;
36
37     /**
38      * Child classes
39      */

40
41
42     /**
43      * Represents an EL expression: anything in ${ and }.
44      */

45     public static class Root extends ELNode {
46
47     private ELNode.Nodes expr;
48     private char type;
49
50     Root(ELNode.Nodes expr, char type) {
51         this.expr = expr;
52         this.type = type;
53     }
54
55     public void accept(Visitor v) throws JasperException {
56         v.visit(this);
57     }
58
59     public ELNode.Nodes getExpression() {
60         return expr;
61     }
62
63     public char getType() {
64         return type;
65     }
66     }
67
68     /**
69      * Represents text outside of EL expression.
70      */

71     public static class Text extends ELNode {
72
73     private String JavaDoc text;
74
75     Text(String JavaDoc text) {
76         this.text = text;
77     }
78
79     public void accept(Visitor v) throws JasperException {
80         v.visit(this);
81     }
82
83     public String JavaDoc getText() {
84         return text;
85     }
86     }
87
88     /**
89      * Represents anything in EL expression, other than functions, including
90      * function arguments etc
91      */

92     public static class ELText extends ELNode {
93
94     private String JavaDoc text;
95
96     ELText(String JavaDoc text) {
97         this.text = text;
98     }
99
100     public void accept(Visitor v) throws JasperException {
101         v.visit(this);
102     }
103
104     public String JavaDoc getText() {
105         return text;
106     }
107     }
108
109     /**
110      * Represents a function
111      * Currently only include the prefix and function name, but not its
112      * arguments.
113      */

114     public static class Function extends ELNode {
115
116     private String JavaDoc prefix;
117     private String JavaDoc name;
118     private String JavaDoc uri;
119     private FunctionInfo JavaDoc functionInfo;
120     private String JavaDoc methodName;
121     private String JavaDoc[] parameters;
122
123     Function(String JavaDoc prefix, String JavaDoc name) {
124         this.prefix = prefix;
125         this.name = name;
126     }
127
128     public void accept(Visitor v) throws JasperException {
129         v.visit(this);
130     }
131
132     public String JavaDoc getPrefix() {
133         return prefix;
134     }
135
136     public String JavaDoc getName() {
137         return name;
138     }
139
140     public void setUri(String JavaDoc uri) {
141         this.uri = uri;
142     }
143
144     public String JavaDoc getUri() {
145         return uri;
146     }
147
148     public void setFunctionInfo(FunctionInfo JavaDoc f) {
149         this.functionInfo = f;
150     }
151
152     public FunctionInfo JavaDoc getFunctionInfo() {
153         return functionInfo;
154     }
155
156     public void setMethodName(String JavaDoc methodName) {
157         this.methodName = methodName;
158     }
159
160     public String JavaDoc getMethodName() {
161         return methodName;
162     }
163
164     public void setParameters(String JavaDoc[] parameters) {
165         this.parameters = parameters;
166     }
167
168     public String JavaDoc[] getParameters() {
169         return parameters;
170     }
171     }
172
173     /**
174      * An ordered list of ELNode.
175      */

176     public static class Nodes {
177
178     /* Name used for creating a map for the functions in this
179        EL expression, for communication to Generator.
180      */

181     String JavaDoc mapName = null; // The function map associated this EL
182
private List<ELNode> list;
183
184     public Nodes() {
185         list = new ArrayList<ELNode>();
186     }
187
188     public void add(ELNode en) {
189         list.add(en);
190     }
191
192     /**
193      * Visit the nodes in the list with the supplied visitor
194      * @param v The visitor used
195      */

196     public void visit(Visitor v) throws JasperException {
197         Iterator<ELNode> iter = list.iterator();
198         while (iter.hasNext()) {
199             ELNode n = iter.next();
200             n.accept(v);
201         }
202     }
203
204     public Iterator<ELNode> iterator() {
205         return list.iterator();
206     }
207
208     public boolean isEmpty() {
209         return list.size() == 0;
210     }
211
212     /**
213      * @return true if the expression contains a ${...}
214      */

215     public boolean containsEL() {
216         Iterator<ELNode> iter = list.iterator();
217         while (iter.hasNext()) {
218             ELNode n = iter.next();
219             if (n instanceof Root) {
220                 return true;
221             }
222         }
223         return false;
224     }
225
226     public void setMapName(String JavaDoc name) {
227         this.mapName = name;
228     }
229
230     public String JavaDoc getMapName() {
231         return mapName;
232     }
233     
234     }
235
236     /*
237      * A visitor class for traversing ELNodes
238      */

239     public static class Visitor {
240
241     public void visit(Root n) throws JasperException {
242         n.getExpression().visit(this);
243     }
244
245     public void visit(Function n) throws JasperException {
246     }
247
248     public void visit(Text n) throws JasperException {
249     }
250
251     public void visit(ELText n) throws JasperException {
252     }
253     }
254 }
255
256
Popular Tags