KickJava   Java API By Example, From Geeks To Geeks.

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


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
36 package com.micronova.jsp.tag;
37
38 import com.micronova.util.*;
39 import com.micronova.util.codec.CodecJSP;
40 import java.util.regex.*;
41 import java.util.*;
42
43 /** Evaluates the tagValue as EL */
44
45 public class EvalTag extends YuzuTag implements ObjectSource
46 {
47     protected Object JavaDoc _pattern;
48     protected boolean _recursive;
49     protected boolean _allowGroup;
50     protected String JavaDoc _evalCodec;
51     protected Object JavaDoc _environment;
52
53     protected void init()
54     {
55         super.init();
56
57         _export = DEFAULT;
58
59         _pattern = EL.DEFAULTPATTERNEVAL;
60         _recursive = false;
61         _allowGroup = false;
62         _evalCodec = null;
63         _environment = null;
64     }
65
66     protected Object JavaDoc evaluate(Object JavaDoc tagValue, Pattern pattern) throws Exception JavaDoc
67     {
68         return Template.render(tagValue.toString(), pattern, 1, this, this, _recursive, _allowGroup);
69     }
70
71     public Object JavaDoc processValue(Object JavaDoc tagValue) throws Exception JavaDoc
72     {
73         if (tagValue instanceof String JavaDoc)
74         {
75             Pattern pattern = EL.getPattern(_pattern);
76
77             Object JavaDoc environment = _environment;
78
79             Object JavaDoc valueVarSaved = null;
80
81             if (environment != null)
82             {
83                 valueVarSaved = pageContext.getAttribute(YuzuTag.VALUEVAR);
84                 setPageAttribute(VALUEVAR, environment);
85             }
86
87             tagValue = evaluate(tagValue, pattern);
88
89             if (environment != null)
90             {
91                 EL.setPageAttribute(pageContext, YuzuTag.VALUEVAR, valueVarSaved);
92             }
93         }
94         else
95         {
96             tagValue =CodecJSP.eval(pageContext, tagValue, EL.getPattern(_pattern), _environment, _evalCodec, _recursive, _allowGroup);
97         }
98
99         return tagValue;
100     }
101
102     public Object JavaDoc getObject(Object JavaDoc client, Object JavaDoc key)
103     {
104         try
105         {
106             return applyCodec(_evalCodec, evaluateExpression("eval", "${" + key.toString() + "}", Object JavaDoc.class));
107         }
108         catch (Exception JavaDoc e)
109         {
110             throw new RuntimeException JavaDoc(e);
111         }
112     }
113
114     public void setPattern(Object JavaDoc expression) throws Exception JavaDoc
115     {
116         _pattern = evaluateAttribute("pattern", expression, Object JavaDoc.class);
117     }
118
119     public void setRecursive(Object JavaDoc expression) throws Exception JavaDoc
120     {
121         _recursive = ((Boolean JavaDoc)evaluateAttribute("recursive", expression, Boolean JavaDoc.class)).booleanValue();
122     }
123
124     public void setAllowGroup(Object JavaDoc expression) throws Exception JavaDoc
125     {
126         _allowGroup = ((Boolean JavaDoc)evaluateAttribute("allowGroup", expression, Boolean JavaDoc.class)).booleanValue();
127     }
128
129     public void setEvalCodec(Object JavaDoc expression) throws Exception JavaDoc
130     {
131         _evalCodec = (String JavaDoc)evaluateAttribute("evalCodec", expression, EL.class);
132     }
133
134     public void setEnvironment(Object JavaDoc expression) throws Exception JavaDoc
135     {
136         _environment = evaluateAttribute("environment", expression, Object JavaDoc.class);
137     }
138 }
139
Popular Tags