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