KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > el > EL


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.el;
31
32 import com.caucho.loader.EnvironmentLocal;
33 import com.caucho.util.L10N;
34
35 import javax.el.ELContext;
36 import javax.el.ELException;
37 import java.util.HashMap JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Abstract implementation class for an expression.
42  */

43 public class EL {
44   private static final Logger JavaDoc log = Logger.getLogger(EL.class.getName());
45   private static final L10N L = new L10N(EL.class);
46   
47   private static EnvironmentLocal<ELContext> _elEnvironment
48     = new EnvironmentLocal<ELContext>();
49   
50   private static EnvironmentLocal<HashMap JavaDoc<String JavaDoc,Object JavaDoc>> _envVar
51     = new EnvironmentLocal<HashMap JavaDoc<String JavaDoc,Object JavaDoc>>();
52
53   public final static Object JavaDoc NULL = new Object JavaDoc();
54
55   public static ELContext getEnvironment()
56   {
57     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
58     
59     return getEnvironment(loader);
60   }
61
62   public static ELContext getEnvironment(ClassLoader JavaDoc loader)
63   {
64     ELContext context = _elEnvironment.getLevel(loader);
65
66     if (context == null) {
67       context = new EnvironmentContext(loader);
68       
69       _elEnvironment.set(context, loader);
70     }
71
72     return context;
73   }
74   
75   public static void setEnvironment(ELContext env)
76   {
77     _elEnvironment.set(env);
78   }
79   
80   public static void setEnvironment(ELContext env,
81                                     ClassLoader JavaDoc loader)
82   {
83     _elEnvironment.set(env, loader);
84   }
85
86   /**
87    * Sets the environment map.
88    */

89   public static void setVariableMap(HashMap JavaDoc<String JavaDoc,Object JavaDoc> map,
90                     ClassLoader JavaDoc loader)
91   {
92     _envVar.set(map, loader);
93   }
94
95   /**
96    * Gets a var from the specified level.
97    */

98   public static Object JavaDoc getLevelVar(String JavaDoc name, ClassLoader JavaDoc loader)
99   {
100     HashMap JavaDoc<String JavaDoc,Object JavaDoc> varMap = _envVar.getLevel(loader);
101
102     if (varMap == null)
103       return null;
104     else
105       return varMap.get(name);
106   }
107
108   /**
109    * Puts an environment value.
110    */

111   public static Object JavaDoc putVar(String JavaDoc name, Object JavaDoc value)
112   {
113     return putVar(name, value, Thread.currentThread().getContextClassLoader());
114   }
115
116   /**
117    * Puts an environment value.
118    */

119   public static Object JavaDoc putVar(String JavaDoc name, Object JavaDoc value, ClassLoader JavaDoc loader)
120   {
121     HashMap JavaDoc<String JavaDoc,Object JavaDoc> varMap = _envVar.getLevel(loader);
122
123     if (varMap == null) {
124       varMap = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
125       _envVar.set(varMap, loader);
126     }
127
128     return varMap.put(name, value);
129   }
130
131   public static Object JavaDoc evalObject(String JavaDoc value)
132     throws ELParseException, ELException
133   {
134     ELParser parser = new ELParser(getEnvironment(), value);
135
136     Expr expr = parser.parse();
137
138     return expr.getValue(getEnvironment());
139   }
140
141   public static Object JavaDoc evalObject(String JavaDoc value, ELContext env)
142     throws ELParseException, ELException
143   {
144     ELParser parser = new ELParser(getEnvironment(), value);
145
146     Expr expr = parser.parse();
147
148     return expr.getValue(env);
149   }
150
151   public static String JavaDoc evalString(String JavaDoc value, ELContext env)
152     throws ELParseException, ELException
153   {
154     ELParser parser = new ELParser(getEnvironment(), value);
155
156     Expr expr = parser.parse();
157
158     return expr.evalString(env);
159   }
160 }
161
Popular Tags