KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > EL


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35 package com.micronova.jsp.tag;
36
37 import com.micronova.util.*;
38 import java.util.*;
39 import javax.servlet.jsp.*;
40 import java.lang.reflect.*;
41 import java.util.regex.*;
42
43 /**
44    EL evaluation utilities. Also used to indicate that translation from "#{" to "${" should be made for a tag attribute.
45 */

46
47 public class EL extends ELRoot implements ObjectSource, ObjectTarget
48 {
49     public static final String JavaDoc DEFAULT = "default";
50
51     /** precompiled patterns */
52
53     public static final String JavaDoc DEFAULTPATTERNEVAL = "[@]\\{([^}]*)\\}";
54     public static final String JavaDoc DEFAULTPATTERNQUERY = "[%]\\{([^\\}]*)\\}";
55     public static final String JavaDoc DEFAULTPATTERNNAME = "__";
56
57     public static final Pattern defaultPatternEval = Pattern.compile(DEFAULTPATTERNEVAL);
58     public static final Pattern defaultPatternQuery = Pattern.compile(DEFAULTPATTERNQUERY);
59
60     public static final Pattern defaultPatternName = Pattern.compile(DEFAULTPATTERNNAME);
61
62     public static final Pattern getPattern(Object JavaDoc patternSpec)
63     {
64         if (DEFAULTPATTERNEVAL.equals(patternSpec))
65         {
66             return defaultPatternEval;
67         }
68         else if (DEFAULTPATTERNQUERY.equals(patternSpec))
69         {
70             return defaultPatternQuery;
71         }
72         else if (patternSpec instanceof Pattern)
73         {
74             return (Pattern)patternSpec;
75         }
76         else if (patternSpec != null)
77         {
78             String JavaDoc patternString = (String JavaDoc)patternSpec;
79
80             if (patternString.indexOf('(') == -1)
81             {
82                 patternString = patternString + "\\{([^}]*)\\}";
83             }
84
85             return Pattern.compile(patternString);
86         }
87         else
88         {
89             return null;
90         }
91     }
92
93     protected PageContext _pageContext;
94     protected String JavaDoc _codec;
95     protected String JavaDoc _nameCodec;
96     protected String JavaDoc _valueCodec;
97
98     /** built-in codec class prefix */
99
100     public static final String JavaDoc CODECPATH = "com.micronova.util.codec.Codec";
101
102     /** page-scoped variable name for the object a codec is applied on (for codec) */
103
104     public static final String JavaDoc OPERANDVAR = "_operand";
105
106     /** sets a page attribute named 'name' to 'value'. When 'value' is null, then the attribute is removed. */
107
108     public static void setPageAttribute(PageContext pageContext, String JavaDoc name, Object JavaDoc value)
109     {
110         if (name != null)
111         {
112             if (value == null)
113             {
114                 pageContext.removeAttribute(name, PageContext.PAGE_SCOPE);
115             }
116             else
117             {
118                 pageContext.setAttribute(name, value, PageContext.PAGE_SCOPE);
119             }
120         }
121     }
122
123     /** gets a page attribute named 'name' */
124
125     public static Object JavaDoc getPageAttribute(PageContext pageContext, String JavaDoc name)
126     {
127         return pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
128     }
129
130     /** replace "@{" with "${" in a StringBuffer */
131
132     public final static StringBuffer JavaDoc replaceEvalEscape(StringBuffer JavaDoc buffer)
133     {
134         boolean isBeforeBrace = false;
135         
136         for (int i = buffer.length(); --i >= 0;)
137         {
138             char c = buffer.charAt(i);
139             
140             if (isBeforeBrace)
141             {
142                 if (c == '@')
143                 {
144                     buffer.setCharAt(i, '$');
145                 }
146                 
147                 isBeforeBrace = false;
148             }
149             else
150             {
151                 if (c == '{')
152                 {
153                     isBeforeBrace = true;
154                 }
155             }
156         }
157
158         return buffer;
159     }
160
161     /** String version of replaceEvalEscape() */
162
163     public final static String JavaDoc replaceEvalEscape(String JavaDoc expression)
164     {
165         if (expression != null)
166         {
167             expression = replaceEvalEscape(new StringBuffer JavaDoc(expression)).toString();
168         }
169
170         return expression;
171     }
172
173     /** applies codecs to an Object */
174
175     public static Object JavaDoc applyCodec(PageContext pageContext, String JavaDoc codecs, Object JavaDoc object) throws Exception JavaDoc
176     {
177         if ((codecs != null) && (codecs.length() > 0))
178         {
179             List codecList = StringUtil.split(codecs, '|');
180
181             for (int i = 0; i < codecList.size(); i ++)
182             {
183                 String JavaDoc codec = (String JavaDoc)codecList.get(i);
184
185                 setPageAttribute(pageContext, OPERANDVAR, object);
186
187                 List partList = StringUtil.split(codec, ':');
188
189                 int partCount = partList.size();
190
191                 if (partCount < 2)
192                 {
193                     object = evaluateExpression(pageContext, "codecExpression", codec.replaceAll("\\\\\\{", "{"), Object JavaDoc.class);
194                 }
195                 else
196                 {
197                     String JavaDoc codecName = (String JavaDoc)partList.get(0);
198
199                     List implicitArg = StringUtil.split((String JavaDoc)partList.get(1), ';');
200
201                     boolean doesUseObject = true;
202
203                     if (codecName.startsWith("_"))
204                     {
205                         codecName = codecName.substring(1);
206                         implicitArg.add(1, pageContext);
207                     }
208
209                     if (codecName.indexOf('.') < 0)
210                     {
211                         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
212
213                         buffer.append(CODECPATH);
214                         buffer.append(codecName);
215
216                         codecName = buffer.toString();
217                     }
218
219                     String JavaDoc codecMethod = (String JavaDoc)implicitArg.get(0);
220
221                     if (codecMethod.endsWith("_"))
222                     {
223                         codecMethod = codecMethod.substring(0, codecMethod.length() - 1);
224                         doesUseObject = false;
225                     }
226
227                     int implicitArgCount = implicitArg.size() - 1;
228                     int numArgs = implicitArgCount + partCount - 1;
229
230                     if (!doesUseObject)
231                     {
232                         numArgs --;
233                     }
234
235                     Class JavaDoc[] types = new Class JavaDoc[numArgs];
236                     Object JavaDoc[] args = new Object JavaDoc[numArgs];
237
238                     int j = 0;
239
240                     for (int k = 1; j < implicitArgCount; j ++, k ++)
241                     {
242                         types[j] = Object JavaDoc.class;
243
244                         Object JavaDoc arg = implicitArg.get(k);
245
246                         if (arg instanceof String JavaDoc)
247                         {
248                             arg = evaluateExpression(pageContext, "codecArgument", (String JavaDoc)arg, Object JavaDoc.class);
249                         }
250
251                         args[j] = arg;
252                     }
253
254                     if (doesUseObject)
255                     {
256                         types[j] = Object JavaDoc.class;
257                         args[j] = object;
258                         j ++;
259                     }
260
261                     for (int k = 2; j < numArgs; j ++, k ++)
262                     {
263                         types[j] = Object JavaDoc.class;
264                         args[j] = evaluateExpression(pageContext, "codecArgument", (String JavaDoc)partList.get(k), Object JavaDoc.class);
265                     }
266
267                     Class JavaDoc c = Class.forName(codecName);
268                     Method m = c.getDeclaredMethod(codecMethod, types);
269
270                     try
271                     {
272                         object = m.invoke(null, args);
273                     }
274                     catch (Exception JavaDoc e)
275                     {
276                         Throwable JavaDoc cause = e.getCause();
277
278                         if (cause instanceof Exception JavaDoc)
279                         {
280                             throw (Exception JavaDoc)cause;
281                         }
282                         else
283                         {
284                             throw e;
285                         }
286                     }
287                 }
288             }
289         }
290             
291         return object;
292     }
293
294     /** to be used as ObjectSource */
295
296     public EL(PageContext pageContext, String JavaDoc codec)
297     {
298         _pageContext = pageContext;
299         _codec = codec;
300     }
301
302     /** to be used as ObjectTarget */
303
304     public EL(PageContext pageContext, String JavaDoc nameCodec, String JavaDoc valueCodec)
305     {
306         _pageContext = pageContext;
307         _nameCodec = nameCodec;
308         _valueCodec = valueCodec;
309     }
310
311     /** ObjectSource implementation */
312
313     public Object JavaDoc getObject(Object JavaDoc client, Object JavaDoc key)
314     {
315         try
316         {
317             PageContext pageContext = _pageContext;
318
319             return applyCodec(pageContext, _codec, evaluateExpression(pageContext, "eval", "${" + key.toString() + "}", Object JavaDoc.class));
320         }
321         catch (Exception JavaDoc e)
322         {
323             throw new RuntimeException JavaDoc(e);
324         }
325     }
326
327     /** ObjectTarget implementation (key/value filter for Map client) */
328
329     public Object JavaDoc putObject(Object JavaDoc client, Object JavaDoc key, Object JavaDoc value)
330     {
331         try
332         {
333             String JavaDoc nameCodec = _nameCodec;
334             String JavaDoc valueCodec = _valueCodec;
335
336             PageContext pageContext = _pageContext;
337
338             if (nameCodec == DEFAULT)
339             {
340                 key = StringUtil.applyPattern(key.toString(), defaultPatternName, null);
341             }
342             else if (!TypeUtil.isEmptyString(nameCodec))
343             {
344                 key = (String JavaDoc)(applyCodec(pageContext, valueCodec, key.toString()));
345             }
346             
347             if (value instanceof String JavaDoc)
348             {
349                 if (valueCodec == DEFAULT)
350                 {
351                     value = XMLUtil.encode(value.toString());
352                 }
353                 else if (!TypeUtil.isEmptyString(valueCodec))
354                 {
355                     value = (String JavaDoc)applyCodec(pageContext, valueCodec, value.toString());
356                 }
357             }
358             
359             return ((Map)client).put(key, value);
360         }
361         catch (Exception JavaDoc e)
362         {
363             throw new RuntimeException JavaDoc(e);
364         }
365     }
366
367     /** EL filter */
368
369     /** applies filter */
370
371     public static final String JavaDoc INDEXVAR = "_index";
372     public static final String JavaDoc ELEMENTVAR = "_element";
373     public static final String JavaDoc LENGTHVAR = "_length";
374     public static final String JavaDoc INCLUDE = "include";
375     public static final String JavaDoc APPLYCODEC = "applyCodec";
376     public static final String JavaDoc APPLY = "apply";
377     public static final String JavaDoc BREAK = "break";
378
379     public static Object JavaDoc applyFilter(PageContext pageContext, Object JavaDoc object, String JavaDoc include, String JavaDoc breakExpression, String JavaDoc apply, String JavaDoc applyCodec) throws Exception JavaDoc
380     {
381         if (object != null)
382         {
383             Object JavaDoc xVar = EL.getPageAttribute(pageContext, ELEMENTVAR);
384             Object JavaDoc iVar = EL.getPageAttribute(pageContext, INDEXVAR);
385             Object JavaDoc lVar = EL.getPageAttribute(pageContext, LENGTHVAR);
386             
387             try
388             {
389                 if (object instanceof String JavaDoc)
390                 {
391                     String JavaDoc string = (String JavaDoc)object;
392                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
393                     
394                     int length = string.length();
395
396                     EL.setPageAttribute(pageContext, LENGTHVAR, new Integer JavaDoc(length));
397                     
398                     for (int i = 0; i < length; i ++)
399                     {
400                         char x = string.charAt(i);
401                         
402                         EL.setPageAttribute(pageContext, ELEMENTVAR, "" + x);
403                         
404                         EL.setPageAttribute(pageContext, INDEXVAR, new Integer JavaDoc(i));
405                         
406                         Boolean JavaDoc b = (Boolean JavaDoc)EL.evaluateExpression(pageContext, "include", include, Boolean JavaDoc.class);
407                         
408                         if (b.booleanValue())
409                         {
410                             if (apply == null)
411                             {
412                                 if (applyCodec == null)
413                                 {
414                                     buffer.append(x);
415                                 }
416                                 else
417                                 {
418                                     buffer.append(EL.applyCodec(pageContext, applyCodec, new Character JavaDoc(x)));
419                                 }
420                             }
421                             else
422                             {
423                                 Object JavaDoc applyObject = EL.evaluateExpression(pageContext, APPLY, apply, Object JavaDoc.class);
424                                 
425                                 if (applyCodec != null)
426                                 {
427                                     applyObject = EL.applyCodec(pageContext, applyCodec, applyObject);
428                                 }
429                                 
430                                 buffer.append(applyObject);
431                             }
432                         }
433                         
434                         if (breakExpression != null)
435                         {
436                             Boolean JavaDoc bBreak = (Boolean JavaDoc)EL.evaluateExpression(pageContext, BREAK, breakExpression, Boolean JavaDoc.class);
437                             
438                             if (bBreak.booleanValue())
439                             {
440                                 break;
441                             }
442                         }
443                     }
444                     
445                     object = buffer.toString();
446                 }
447                 else if (object instanceof Map)
448                 {
449                     Map map = (Map)object;
450                     Map newMap = new NestedMap();
451                     
452                     Iterator iterator = map.entrySet().iterator();
453                     
454                     int length = map.size();
455                     
456                     EL.setPageAttribute(pageContext, LENGTHVAR, new Integer JavaDoc(length));
457                     
458                     for (int i = 0; iterator.hasNext(); i ++)
459                     {
460                         Map.Entry x = (Map.Entry)iterator.next();
461                         
462                         EL.setPageAttribute(pageContext, ELEMENTVAR, x);
463                         
464                         EL.setPageAttribute(pageContext, INDEXVAR, new Integer JavaDoc(i));
465                         
466                         Boolean JavaDoc b = (Boolean JavaDoc)EL.evaluateExpression(pageContext, INCLUDE, include, Boolean JavaDoc.class);
467                         
468                         if (b.booleanValue())
469                         {
470                             Object JavaDoc key = x.getKey();
471                             Object JavaDoc keyValue = x.getValue();
472                             
473                             if (apply != null)
474                             {
475                                 keyValue = EL.evaluateExpression(pageContext, APPLY, apply, Object JavaDoc.class);
476                             }
477                             
478                             if (applyCodec != null)
479                             {
480                                 keyValue = EL.applyCodec(pageContext, applyCodec, keyValue);
481                             }
482                             
483                             newMap.put(key, keyValue);
484                         }
485                         
486                         if (breakExpression != null)
487                         {
488                             Boolean JavaDoc bBreak = (Boolean JavaDoc)EL.evaluateExpression(pageContext, BREAK, breakExpression, Boolean JavaDoc.class);
489                             
490                             if (bBreak.booleanValue())
491                             {
492                                 break;
493                             }
494                         }
495                     }
496                     
497                     object = newMap;
498                 }
499                 else
500                 {
501                     List list = TypeUtil.isList(object);
502
503                     if (list != null)
504                     {
505                         List newList = new SparseList();
506                         int length = list.size();
507                     
508                         EL.setPageAttribute(pageContext, LENGTHVAR, new Integer JavaDoc(length));
509                     
510                         for (int i = 0; i < length; i ++)
511                         {
512                             Object JavaDoc x = list.get(i);
513                             
514                             EL.setPageAttribute(pageContext, ELEMENTVAR, x);
515                             
516                             EL.setPageAttribute(pageContext, INDEXVAR, new Integer JavaDoc(i));
517                             
518                             Boolean JavaDoc b = (Boolean JavaDoc)EL.evaluateExpression(pageContext, INCLUDE, include, Boolean JavaDoc.class);
519                             
520                             if (b.booleanValue())
521                             {
522                                 if (apply != null)
523                                 {
524                                     x = EL.evaluateExpression(pageContext, APPLY, apply, Object JavaDoc.class);
525                                 }
526                                 
527                                 if (applyCodec != null)
528                                 {
529                                     x = EL.applyCodec(pageContext, applyCodec, x);
530                                 }
531                                 
532                                 newList.add(x);
533                             }
534                             
535                             if (breakExpression != null)
536                             {
537                                 Boolean JavaDoc bBreak = (Boolean JavaDoc)EL.evaluateExpression(pageContext, BREAK, breakExpression, Boolean JavaDoc.class);
538                                 
539                                 if (bBreak.booleanValue())
540                                 {
541                                     break;
542                                 }
543                             }
544                         }
545                         
546                         object = newList;
547                     }
548                     else
549                     {
550                         throw new Exception JavaDoc("unsupported filter object type:" + object.getClass().getName());
551                     }
552                 }
553             }
554             finally
555             {
556                 EL.setPageAttribute(pageContext, ELEMENTVAR, xVar);
557                 EL.setPageAttribute(pageContext, INDEXVAR, iVar);
558                 EL.setPageAttribute(pageContext, LENGTHVAR, lVar);
559             }
560         }
561         
562         return object;
563     }
564 }
565
Popular Tags