KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > el > ELText


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.el;
16
17 import java.io.IOException JavaDoc;
18 import java.io.StringWriter JavaDoc;
19 import java.io.Writer JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.el.ELContext;
24 import javax.el.ELException;
25 import javax.el.ExpressionFactory;
26 import javax.el.ValueExpression;
27 import javax.faces.context.ResponseWriter;
28
29 import com.sun.facelets.util.FastWriter;
30
31 /**
32  * Handles parsing EL Strings in accordance with the EL-API Specification. The
33  * parser accepts either <code>${..}</code> or <code>#{..}</code>.
34  *
35  * @author Jacob Hookom
36  * @version $Id: ELText.java,v 1.5 2006/05/09 06:25:39 jhook Exp $
37  */

38 public class ELText {
39
40     private static final class LiteralValueExpression extends ValueExpression {
41
42         /**
43          *
44          */

45         private static final long serialVersionUID = 1L;
46
47         private final String JavaDoc text;
48
49         public LiteralValueExpression(String JavaDoc text) {
50             this.text = text;
51         }
52
53         public boolean isLiteralText() {
54             return false;
55         }
56
57         public int hashCode() {
58             return 0;
59         }
60
61         public String JavaDoc getExpressionString() {
62             return this.text;
63         }
64
65         public boolean equals(Object JavaDoc obj) {
66             return false;
67         }
68
69         public void setValue(ELContext context, Object JavaDoc value) {
70         }
71
72         public boolean isReadOnly(ELContext context) {
73             return false;
74         }
75
76         public Object JavaDoc getValue(ELContext context) {
77             return null;
78         }
79
80         public Class JavaDoc getType(ELContext context) {
81             return null;
82         }
83
84         public Class JavaDoc getExpectedType() {
85             return null;
86         }
87
88     }
89
90     private static final class ELTextComposite extends ELText {
91         private final ELText[] txt;
92
93         public ELTextComposite(ELText[] txt) {
94             super(null);
95             this.txt = txt;
96         }
97
98         public void write(Writer JavaDoc out, ELContext ctx) throws ELException,
99                 IOException JavaDoc {
100             for (int i = 0; i < this.txt.length; i++) {
101                 this.txt[i].write(out, ctx);
102             }
103         }
104
105         public void writeText(ResponseWriter out, ELContext ctx)
106                 throws ELException, IOException JavaDoc {
107             for (int i = 0; i < this.txt.length; i++) {
108                 this.txt[i].writeText(out, ctx);
109             }
110         }
111
112         public String JavaDoc toString(ELContext ctx) {
113             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
114             for (int i = 0; i < this.txt.length; i++) {
115                 sb.append(this.txt[i].toString(ctx));
116             }
117             return sb.toString();
118         }
119
120         /*
121          * public String toString(ELContext ctx) { StringBuffer sb = new
122          * StringBuffer(); for (int i = 0; i < this.txt.length; i++) {
123          * sb.append(this.txt[i].toString(ctx)); } return sb.toString(); }
124          */

125
126         public String JavaDoc toString() {
127             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
128             for (int i = 0; i < this.txt.length; i++) {
129                 sb.append(this.txt[i].toString());
130             }
131             return sb.toString();
132         }
133
134         public boolean isLiteral() {
135             return false;
136         }
137
138         public ELText apply(ExpressionFactory factory, ELContext ctx) {
139             int len = this.txt.length;
140             ELText[] nt = new ELText[len];
141             for (int i = 0; i < len; i++) {
142                 nt[i] = this.txt[i].apply(factory, ctx);
143             }
144             return new ELTextComposite(nt);
145         }
146     }
147
148     private static final class ELTextVariable extends ELText {
149         private final ValueExpression ve;
150
151         public ELTextVariable(ValueExpression ve) {
152             super(ve.getExpressionString());
153             this.ve = ve;
154         }
155
156         public boolean isLiteral() {
157             return false;
158         }
159
160         public ELText apply(ExpressionFactory factory, ELContext ctx) {
161             return new ELTextVariable(factory.createValueExpression(ctx,
162                     this.ve.getExpressionString(), String JavaDoc.class));
163         }
164
165         public void write(Writer JavaDoc out, ELContext ctx) throws ELException,
166                 IOException JavaDoc {
167             Object JavaDoc v = this.ve.getValue(ctx);
168             if (v != null) {
169                 out.write((String JavaDoc) v);
170             }
171         }
172
173         public String JavaDoc toString(ELContext ctx) throws ELException {
174             Object JavaDoc v = this.ve.getValue(ctx);
175             if (v != null) {
176                 return v.toString();
177             }
178
179             return null;
180         }
181
182         public void writeText(ResponseWriter out, ELContext ctx)
183                 throws ELException, IOException JavaDoc {
184             Object JavaDoc v = this.ve.getValue(ctx);
185             if (v != null) {
186                 out.writeText((String JavaDoc) v, null);
187             }
188         }
189     }
190
191     protected final String JavaDoc literal;
192
193     public ELText(String JavaDoc literal) {
194         this.literal = literal;
195     }
196
197     /**
198      * If it's literal text
199      *
200      * @return true if the String is literal (doesn't contain <code>#{..}</code>
201      * or <code>${..}</code>)
202      */

203     public boolean isLiteral() {
204         return true;
205     }
206
207     /**
208      * Return an instance of <code>this</code> that is applicable given the
209      * ELContext and ExpressionFactory state.
210      *
211      * @param factory
212      * the ExpressionFactory to use
213      * @param ctx
214      * the ELContext to use
215      * @return an ELText instance
216      */

217     public ELText apply(ExpressionFactory factory, ELContext ctx) {
218         return this;
219     }
220
221     /**
222      * Allow this instance to write to the passed Writer, given the ELContext
223      * state
224      *
225      * @param out
226      * Writer to write to
227      * @param ctx
228      * current ELContext state
229      * @throws ELException
230      * @throws IOException
231      */

232     public void write(Writer JavaDoc out, ELContext ctx) throws ELException,
233             IOException JavaDoc {
234         out.write(this.literal);
235     }
236
237     public void writeText(ResponseWriter out, ELContext ctx)
238             throws ELException, IOException JavaDoc {
239         out.writeText(this.literal, null);
240     }
241
242     /**
243      * Evaluates the ELText to a String
244      *
245      * @param ctx
246      * current ELContext state
247      * @throws ELException
248      * @return the evaluated String
249      */

250     public String JavaDoc toString(ELContext ctx) throws ELException {
251         return this.literal;
252     }
253
254     public String JavaDoc toString() {
255         return this.literal;
256     }
257
258     /**
259      * Parses the passed string to determine if it's literal or not
260      *
261      * @param in
262      * input String
263      * @return true if the String is literal (doesn't contain <code>#{..}</code>
264      * or <code>${..}</code>)
265      */

266     public static boolean isLiteral(String JavaDoc in) {
267         ELText txt = parse(in);
268         return txt == null || txt.isLiteral();
269     }
270
271     /**
272      * Factory method for creating an unvalidated ELText instance. NOTE: All
273      * expressions in the passed String are treated as
274      * {@link com.sun.facelets.el.LiteralValueExpression LiteralValueExpressions}.
275      *
276      * @param in
277      * String to parse
278      * @return ELText instance that knows if the String was literal or not
279      * @throws javax.el.ELException
280      */

281     public static ELText parse(String JavaDoc in) throws ELException {
282         return parse(null, null, in);
283     }
284
285     /**
286      * Factory method for creating a validated ELText instance. When an
287      * Expression is hit, it will use the ExpressionFactory to create a
288      * ValueExpression instance, resolving any functions at that time. <p/>
289      * Variables and properties will not be evaluated.
290      *
291      * @param fact
292      * ExpressionFactory to use
293      * @param ctx
294      * ELContext to validate against
295      * @param in
296      * String to parse
297      * @return ELText that can be re-applied later
298      * @throws javax.el.ELException
299      */

300     public static ELText parse(ExpressionFactory fact, ELContext ctx, String JavaDoc in)
301             throws ELException {
302         char[] ca = in.toCharArray();
303         int i = 0;
304         char c = 0;
305         int len = ca.length;
306         int end = len - 1;
307         boolean esc = false;
308         int vlen = 0;
309
310         StringBuffer JavaDoc buff = new StringBuffer JavaDoc(128);
311         List JavaDoc text = new ArrayList JavaDoc();
312         ELText t = null;
313         ValueExpression ve = null;
314
315         while (i < len) {
316             c = ca[i];
317             if ('\\' == c) {
318                 esc = !esc;
319                 if (esc && i < end && ca[i + 1] == '$' || ca[i + 1] == '#') {
320                     i++;
321                     continue;
322                 }
323             } else if (!esc && ('$' == c || '#' == c)) {
324                 if (i < end) {
325                     if ('{' == ca[i + 1]) {
326                         if (buff.length() > 0) {
327                             text.add(new ELText(buff.toString()));
328                             buff.setLength(0);
329                         }
330                         vlen = findVarLength(ca, i);
331                         if (ctx != null && fact != null) {
332                             ve = fact.createValueExpression(ctx, new String JavaDoc(ca,
333                                     i, vlen), String JavaDoc.class);
334                             t = new ELTextVariable(ve);
335                         } else {
336                             t = new ELTextVariable(new LiteralValueExpression(
337                                     new String JavaDoc(ca, i, vlen)));
338                         }
339                         text.add(t);
340                         i += vlen;
341                         continue;
342                     }
343                 }
344             }
345             esc = false;
346             buff.append(c);
347             i++;
348         }
349
350         if (buff.length() > 0) {
351             text.add(new ELText(new String JavaDoc(buff.toString())));
352             buff.setLength(0);
353         }
354
355         if (text.size() == 0) {
356             return null;
357         } else if (text.size() == 1) {
358             return (ELText) text.get(0);
359         } else {
360             ELText[] ta = (ELText[]) text.toArray(new ELText[text.size()]);
361             return new ELTextComposite(ta);
362         }
363     }
364
365     private static int findVarLength(char[] ca, int s) throws ELException {
366         int i = s;
367         int len = ca.length;
368         char c = 0;
369         int str = 0;
370         boolean esc = false;
371         while (i < len) {
372             c = ca[i];
373             if ('\\' == c) {
374                 esc = true;
375             } else if (!esc && ('\'' == c || '"' == c)) {
376                 if (str == c) {
377                     str = 0;
378                 } else {
379                     str = c;
380                 }
381             } else if (!esc && str == 0 && ('}' == c)) {
382                 return i - s + 1;
383             }
384             i++;
385         }
386         throw new ELException("EL Expression Unbalanced: ... "
387                 + new String JavaDoc(ca, s, i - s));
388     }
389
390 }
391
Popular Tags