KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class ELMap extends MapBean
44 {
45     public static final String JavaDoc PAGECONTEXT = "pageContext";
46     public static final String JavaDoc ENVVAR = "_";
47     public static final String JavaDoc KEY = "key";
48     public static final String JavaDoc VALUE = "value";
49     public static final String JavaDoc GET = "get";
50     public static final String JavaDoc PUT = "put";
51     public static final String JavaDoc GETCODEC = "getCodec";
52     public static final String JavaDoc PUTCODEC = "putCodec";
53
54     protected PageContext _pageContext;
55     protected NestedMap _environment;
56
57     public ELMap(PageContext pageContext, Object JavaDoc environmentSource) throws Exception JavaDoc
58     {
59         super();
60
61         _pageContext = pageContext;
62         _environment = new NestedMap(environmentSource);
63     }
64
65     /** ObjectSource implementation */
66
67     public Object JavaDoc getObject(Object JavaDoc client, Object JavaDoc key)
68     {
69         Object JavaDoc envVar = null;
70         PageContext pageContext = _pageContext;
71
72         try
73         {
74             NestedMap environment = _environment;
75
76             envVar = EL.getPageAttribute(pageContext, ENVVAR);
77
78             EL.setPageAttribute(pageContext, ENVVAR, environment);
79
80             String JavaDoc getCodec = EL.replaceEvalEscape(environment.getString(GETCODEC));
81             environment.put(KEY, key);
82
83             return EL.applyCodec(pageContext, getCodec, environment);
84         }
85         catch (Exception JavaDoc e)
86         {
87             throw new RuntimeException JavaDoc(e);
88         }
89         finally
90         {
91             if (envVar != null)
92             {
93                 EL.setPageAttribute(pageContext, ENVVAR, envVar);
94             }
95         }
96     }
97
98     /** ObjectTarget implementation (key/value filter for Map client) */
99
100     public Object JavaDoc putObject(Object JavaDoc client, Object JavaDoc key, Object JavaDoc value)
101     {
102
103         Object JavaDoc envVar = null;
104         PageContext pageContext = _pageContext;
105
106         try
107         {
108             NestedMap environment = _environment;
109
110             envVar = EL.getPageAttribute(pageContext, ENVVAR);
111
112             EL.setPageAttribute(pageContext, ENVVAR, environment);
113
114             String JavaDoc putCodec = EL.replaceEvalEscape(environment.getString(PUTCODEC));
115             environment.put(KEY, key);
116             environment.put(VALUE, value);
117
118             return EL.applyCodec(pageContext, putCodec, environment);
119         }
120         catch (Exception JavaDoc e)
121         {
122             throw new RuntimeException JavaDoc(e);
123         }
124         finally
125         {
126             if (envVar != null)
127             {
128                 EL.setPageAttribute(pageContext, ENVVAR, envVar);
129             }
130         }
131     }
132 }
133
Popular Tags