001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.el;
030:
031: import javax.el.ELContext;
032: import javax.el.ELResolver;
033: import java.beans.FeatureDescriptor;
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.Map;
037:
038: /**
039: * Variable resolver using an underlying Map.
040: */
041: public class MapVariableResolver extends ELResolver {
042: private Map<String, Object> _map;
043:
044: /**
045: * Creates the resolver
046: */
047: public MapVariableResolver(Map<String, Object> map) {
048: if (map == null)
049: map = new HashMap<String, Object>();
050:
051: _map = map;
052: }
053:
054: /**
055: * Creates the resolver
056: */
057: public MapVariableResolver() {
058: this (new HashMap<String, Object>());
059: }
060:
061: /**
062: * Returns the named variable value.
063: */
064: @Override
065: public Object getValue(ELContext context, Object base,
066: Object property) {
067: if (base != null || !(property instanceof String))
068: return null;
069:
070: String var = (String) property;
071:
072: Object value = _map.get(var);
073:
074: if (value != null) {
075: context.setPropertyResolved(true);
076:
077: return value;
078: }
079:
080: return null;
081: }
082:
083: /**
084: * Returns the named variable value.
085: */
086: @Override
087: public void setValue(ELContext context, Object base,
088: Object property, Object value) {
089: if (!(base instanceof String) || property != null)
090: return;
091:
092: String var = (String) base;
093:
094: context.setPropertyResolved(true);
095:
096: _map.put(var, value);
097: }
098:
099: /**
100: * Sets the named variable value.
101: */
102: public Object put(String var, Object value) {
103: return _map.put(var, value);
104: }
105:
106: /**
107: * Returns true for read-only.
108: */
109: @Override
110: public boolean isReadOnly(ELContext context, Object base,
111: Object property) {
112: if (property != null || !(base instanceof String))
113: return true;
114:
115: context.setPropertyResolved(true);
116:
117: return false;
118: }
119:
120: /**
121: * Returns the named variable value.
122: */
123: @Override
124: public Class<?> getType(ELContext context, Object base,
125: Object property) {
126: Object value = getValue(context, base, property);
127:
128: if (value != null)
129: return value.getClass();
130: else
131: return null;
132: }
133:
134: public Class<?> getCommonPropertyType(ELContext context, Object base) {
135: return null;
136: }
137:
138: public Iterator<FeatureDescriptor> getFeatureDescriptors(
139: ELContext context, Object base) {
140: return null;
141: }
142:
143: /**
144: * Gets the map.
145: */
146: public Map<String, Object> getMap() {
147: return _map;
148: }
149:
150: /**
151: * Sets the map.
152: */
153: public void setMap(Map<String, Object> map) {
154: _map = map;
155: }
156:
157: public String toString() {
158: return "MapVariableResolver[" + _map + "]";
159: }
160: }
|