001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.jasper.compiler;
018:
019: import java.util.*;
020: import javax.servlet.jsp.tagext.FunctionInfo;
021: import org.apache.jasper.JasperException;
022:
023: /**
024: * This class defines internal representation for an EL Expression
025: *
026: * It currently only defines functions. It can be expanded to define
027: * all the components of an EL expression, if need to.
028: *
029: * @author Kin-man Chung
030: */
031:
032: abstract class ELNode {
033:
034: abstract public void accept(Visitor v) throws JasperException;
035:
036: /**
037: * Child classes
038: */
039:
040: /**
041: * Represents an EL expression: anything in ${ and }.
042: */
043: public static class Root extends ELNode {
044:
045: private ELNode.Nodes expr;
046:
047: Root(ELNode.Nodes expr) {
048: this .expr = expr;
049: }
050:
051: public void accept(Visitor v) throws JasperException {
052: v.visit(this );
053: }
054:
055: public ELNode.Nodes getExpression() {
056: return expr;
057: }
058: }
059:
060: /**
061: * Represents text outside of EL expression.
062: */
063: public static class Text extends ELNode {
064:
065: private String text;
066:
067: Text(String text) {
068: this .text = text;
069: }
070:
071: public void accept(Visitor v) throws JasperException {
072: v.visit(this );
073: }
074:
075: public String getText() {
076: return text;
077: }
078: }
079:
080: /**
081: * Represents anything in EL expression, other than functions, including
082: * function arguments etc
083: */
084: public static class ELText extends ELNode {
085:
086: private String text;
087:
088: ELText(String text) {
089: this .text = text;
090: }
091:
092: public void accept(Visitor v) throws JasperException {
093: v.visit(this );
094: }
095:
096: public String getText() {
097: return text;
098: }
099: }
100:
101: /**
102: * Represents a function
103: * Currently only include the prefix and function name, but not its
104: * arguments.
105: */
106: public static class Function extends ELNode {
107:
108: private String prefix;
109: private String name;
110: private String uri;
111: private FunctionInfo functionInfo;
112: private String methodName;
113: private String[] parameters;
114:
115: Function(String prefix, String name) {
116: this .prefix = prefix;
117: this .name = name;
118: }
119:
120: public void accept(Visitor v) throws JasperException {
121: v.visit(this );
122: }
123:
124: public String getPrefix() {
125: return prefix;
126: }
127:
128: public String getName() {
129: return name;
130: }
131:
132: public void setUri(String uri) {
133: this .uri = uri;
134: }
135:
136: public String getUri() {
137: return uri;
138: }
139:
140: public void setFunctionInfo(FunctionInfo f) {
141: this .functionInfo = f;
142: }
143:
144: public FunctionInfo getFunctionInfo() {
145: return functionInfo;
146: }
147:
148: public void setMethodName(String methodName) {
149: this .methodName = methodName;
150: }
151:
152: public String getMethodName() {
153: return methodName;
154: }
155:
156: public void setParameters(String[] parameters) {
157: this .parameters = parameters;
158: }
159:
160: public String[] getParameters() {
161: return parameters;
162: }
163: }
164:
165: /**
166: * An ordered list of ELNode.
167: */
168: public static class Nodes {
169:
170: /* Name used for creating a map for the functions in this
171: EL expression, for communication to Generator.
172: */
173: String mapName = null; // The function map associated this EL
174: private List list;
175:
176: public Nodes() {
177: list = new ArrayList();
178: }
179:
180: public void add(ELNode en) {
181: list.add(en);
182: }
183:
184: /**
185: * Visit the nodes in the list with the supplied visitor
186: * @param v The visitor used
187: */
188: public void visit(Visitor v) throws JasperException {
189: Iterator iter = list.iterator();
190: while (iter.hasNext()) {
191: ELNode n = (ELNode) iter.next();
192: n.accept(v);
193: }
194: }
195:
196: public Iterator iterator() {
197: return list.iterator();
198: }
199:
200: public boolean isEmpty() {
201: return list.size() == 0;
202: }
203:
204: /**
205: * @return true if the expression contains a ${...}
206: */
207: public boolean containsEL() {
208: Iterator iter = list.iterator();
209: while (iter.hasNext()) {
210: ELNode n = (ELNode) iter.next();
211: if (n instanceof Root) {
212: return true;
213: }
214: }
215: return false;
216: }
217:
218: public void setMapName(String name) {
219: this .mapName = name;
220: }
221:
222: public String getMapName() {
223: return mapName;
224: }
225: }
226:
227: /*
228: * A visitor class for traversing ELNodes
229: */
230: public static class Visitor {
231:
232: public void visit(Root n) throws JasperException {
233: n.getExpression().visit(this );
234: }
235:
236: public void visit(Function n) throws JasperException {
237: }
238:
239: public void visit(Text n) throws JasperException {
240: }
241:
242: public void visit(ELText n) throws JasperException {
243: }
244: }
245: }
|