KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.el;
30
31 import javax.el.ELContext;
32 import javax.el.ELResolver;
33 import java.beans.FeatureDescriptor JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37
38 /**
39  * Variable resolver using an underlying Map.
40  */

41 public class MapVariableResolver extends ELResolver {
42   private Map JavaDoc<String JavaDoc,Object JavaDoc> _map;
43   
44   /**
45    * Creates the resolver
46    */

47   public MapVariableResolver(Map JavaDoc<String JavaDoc,Object JavaDoc> map)
48   {
49     if (map == null)
50       map = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
51
52     _map = map;
53   }
54   
55   /**
56    * Creates the resolver
57    */

58   public MapVariableResolver()
59   {
60     this(new HashMap JavaDoc<String JavaDoc,Object JavaDoc>());
61   }
62   
63   /**
64    * Returns the named variable value.
65    */

66   @Override JavaDoc
67   public Object JavaDoc getValue(ELContext context, Object JavaDoc base, Object JavaDoc property)
68   {
69     if (base != null || ! (property instanceof String JavaDoc))
70       return null;
71
72     String JavaDoc var = (String JavaDoc) property;
73     
74     Object JavaDoc value = _map.get(var);
75
76     if (value != null) {
77       context.setPropertyResolved(true);
78       
79       return value;
80     }
81
82     return null;
83   }
84   
85   /**
86    * Returns the named variable value.
87    */

88   @Override JavaDoc
89   public void setValue(ELContext context,
90                Object JavaDoc base,
91                Object JavaDoc property,
92                Object JavaDoc value)
93   {
94     if (base != null || ! (property instanceof String JavaDoc))
95       return;
96     
97     String JavaDoc var = (String JavaDoc) property;
98
99     context.setPropertyResolved(true);
100     
101     _map.put(var, value);
102   }
103   
104   /**
105    * Sets the named variable value.
106    */

107   public Object JavaDoc put(String JavaDoc var, Object JavaDoc value)
108   {
109     return _map.put(var, value);
110   }
111
112   /**
113    * Returns true for read-only.
114    */

115   @Override JavaDoc
116   public boolean isReadOnly(ELContext context, Object JavaDoc base, Object JavaDoc property)
117   {
118     if (property != null || ! (base instanceof String JavaDoc))
119       return true;
120
121     context.setPropertyResolved(true);
122
123     return false;
124   }
125   
126   /**
127    * Returns the named variable value.
128    */

129   @Override JavaDoc
130   public Class JavaDoc<?> getType(ELContext context,
131             Object JavaDoc base,
132             Object JavaDoc property)
133   {
134     Object JavaDoc value = getValue(context, base, property);
135
136     if (value != null)
137       return value.getClass();
138     else
139       return null;
140   }
141
142   public Class JavaDoc<?> getCommonPropertyType(ELContext context,
143                     Object JavaDoc base)
144   {
145     return null;
146   }
147
148   public Iterator JavaDoc<FeatureDescriptor JavaDoc>
149     getFeatureDescriptors(ELContext context, Object JavaDoc base)
150   {
151     return null;
152   }
153
154   /**
155    * Gets the map.
156    */

157   public Map JavaDoc<String JavaDoc,Object JavaDoc> getMap()
158   {
159     return _map;
160   }
161
162   /**
163    * Sets the map.
164    */

165   public void setMap(Map JavaDoc<String JavaDoc,Object JavaDoc> map)
166   {
167     _map = map;
168   }
169
170   public String JavaDoc toString()
171   {
172     return "MapVariableResolver[" + _map + "]";
173   }
174 }
175
Popular Tags