View Javadoc

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.struts2.jasper.compiler;
19  
20  import org.apache.struts2.jasper.JasperException;
21  
22  import javax.servlet.jsp.tagext.FunctionInfo;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  /***
28   * This class defines internal representation for an EL Expression
29   * <p/>
30   * It currently only defines functions.  It can be expanded to define
31   * all the components of an EL expression, if need to.
32   *
33   * @author Kin-man Chung
34   */
35  
36  abstract class ELNode {
37  
38      abstract public void accept(Visitor v) throws JasperException;
39  
40      /***
41       * Child classes
42       */
43  
44  
45      /***
46       * Represents an EL expression: anything in ${ and }.
47       */
48      public static class Root extends ELNode {
49  
50          private ELNode.Nodes expr;
51  
52          Root(ELNode.Nodes expr) {
53              this.expr = expr;
54          }
55  
56          public void accept(Visitor v) throws JasperException {
57              v.visit(this);
58          }
59  
60          public ELNode.Nodes getExpression() {
61              return expr;
62          }
63      }
64  
65      /***
66       * Represents text outside of EL expression.
67       */
68      public static class Text extends ELNode {
69  
70          private String text;
71  
72          Text(String text) {
73              this.text = text;
74          }
75  
76          public void accept(Visitor v) throws JasperException {
77              v.visit(this);
78          }
79  
80          public String getText() {
81              return text;
82          }
83      }
84  
85      /***
86       * Represents anything in EL expression, other than functions, including
87       * function arguments etc
88       */
89      public static class ELText extends ELNode {
90  
91          private String text;
92  
93          ELText(String text) {
94              this.text = text;
95          }
96  
97          public void accept(Visitor v) throws JasperException {
98              v.visit(this);
99          }
100 
101         public String getText() {
102             return text;
103         }
104     }
105 
106     /***
107      * Represents a function
108      * Currently only include the prefix and function name, but not its
109      * arguments.
110      */
111     public static class Function extends ELNode {
112 
113         private String prefix;
114         private String name;
115         private String uri;
116         private FunctionInfo functionInfo;
117         private String methodName;
118         private String[] parameters;
119 
120         Function(String prefix, String name) {
121             this.prefix = prefix;
122             this.name = name;
123         }
124 
125         public void accept(Visitor v) throws JasperException {
126             v.visit(this);
127         }
128 
129         public String getPrefix() {
130             return prefix;
131         }
132 
133         public String getName() {
134             return name;
135         }
136 
137         public void setUri(String uri) {
138             this.uri = uri;
139         }
140 
141         public String getUri() {
142             return uri;
143         }
144 
145         public void setFunctionInfo(FunctionInfo f) {
146             this.functionInfo = f;
147         }
148 
149         public FunctionInfo getFunctionInfo() {
150             return functionInfo;
151         }
152 
153         public void setMethodName(String methodName) {
154             this.methodName = methodName;
155         }
156 
157         public String getMethodName() {
158             return methodName;
159         }
160 
161         public void setParameters(String[] parameters) {
162             this.parameters = parameters;
163         }
164 
165         public String[] getParameters() {
166             return parameters;
167         }
168     }
169 
170     /***
171      * An ordered list of ELNode.
172      */
173     public static class Nodes {
174 
175         /* Name used for creating a map for the functions in this
176          EL expression, for communication to Generator.
177        */
178         String mapName = null;    // The function map associated this EL
179         private List list;
180 
181         public Nodes() {
182             list = new ArrayList();
183         }
184 
185         public void add(ELNode en) {
186             list.add(en);
187         }
188 
189         /***
190          * Visit the nodes in the list with the supplied visitor
191          *
192          * @param v The visitor used
193          */
194         public void visit(Visitor v) throws JasperException {
195             Iterator iter = list.iterator();
196             while (iter.hasNext()) {
197                 ELNode n = (ELNode) iter.next();
198                 n.accept(v);
199             }
200         }
201 
202         public Iterator iterator() {
203             return list.iterator();
204         }
205 
206         public boolean isEmpty() {
207             return list.size() == 0;
208         }
209 
210         /***
211          * @return true if the expression contains a ${...}
212          */
213         public boolean containsEL() {
214             Iterator iter = list.iterator();
215             while (iter.hasNext()) {
216                 ELNode n = (ELNode) iter.next();
217                 if (n instanceof Root) {
218                     return true;
219                 }
220             }
221             return false;
222         }
223 
224         public void setMapName(String name) {
225             this.mapName = name;
226         }
227 
228         public String getMapName() {
229             return mapName;
230         }
231     }
232 
233     /*
234      * A visitor class for traversing ELNodes
235      */
236     public static class Visitor {
237 
238         public void visit(Root n) throws JasperException {
239             n.getExpression().visit(this);
240         }
241 
242         public void visit(Function n) throws JasperException {
243         }
244 
245         public void visit(Text n) throws JasperException {
246         }
247 
248         public void visit(ELText n) throws JasperException {
249         }
250     }
251 }
252