001: /**
002: * MVEL (The MVFLEX Expression Language)
003: *
004: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */package org.mvel.integration.impl;
019:
020: import org.mvel.CompileException;
021: import org.mvel.integration.VariableResolver;
022: import org.mvel.integration.VariableResolverFactory;
023:
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.Map;
027: import java.util.Set;
028:
029: @SuppressWarnings({"unchecked"})
030: public class MapVariableResolverFactory extends
031: BaseVariableResolverFactory {
032: /**
033: * Holds the instance of the variables.
034: */
035: protected Map<String, Object> variables;
036:
037: // private VariableResolverFactory nextFactory;
038:
039: private boolean cachingSafe = false;
040:
041: public MapVariableResolverFactory(Map variables) {
042: this .variables = variables;
043: }
044:
045: public MapVariableResolverFactory(Map<String, Object> variables,
046: VariableResolverFactory nextFactory) {
047: this .variables = variables;
048: this .nextFactory = nextFactory;
049: }
050:
051: public MapVariableResolverFactory(Map<String, Object> variables,
052: boolean cachingSafe) {
053: this .variables = variables;
054: this .cachingSafe = cachingSafe;
055: }
056:
057: public VariableResolver createVariable(String name, Object value) {
058: VariableResolver vr = getVariableResolver(name);
059: if (vr != null) {
060: vr.setValue(value);
061: return vr;
062: } else {
063: (vr = new MapVariableResolver(variables, name, cachingSafe))
064: .setValue(value);
065: return vr;
066: }
067: }
068:
069: public VariableResolver createVariable(String name, Object value,
070: Class<?> type) {
071: VariableResolver vr = getVariableResolver(name);
072: if (vr != null && vr.getType() != null) {
073: throw new CompileException(
074: "variable already defined within scope: "
075: + vr.getType() + " " + name);
076: } else {
077: addResolver(name, vr = new MapVariableResolver(variables,
078: name, type, cachingSafe));
079: vr.setValue(value);
080: return vr;
081: }
082: }
083:
084: public VariableResolver getVariableResolver(String name) {
085: if (variables != null && variables.containsKey(name)) {
086: return variableResolvers != null
087: && variableResolvers.containsKey(name) ? variableResolvers
088: .get(name)
089: : new MapVariableResolver(variables, name,
090: cachingSafe);
091: } else if (nextFactory != null) {
092: return nextFactory.getVariableResolver(name);
093: }
094: return null;
095: }
096:
097: public boolean isResolveable(String name) {
098: if (variableResolvers != null
099: && variableResolvers.containsKey(name)) {
100: return true;
101: } else if (variables != null && variables.containsKey(name)) {
102: return true;
103: } else if (nextFactory != null) {
104: return nextFactory.isResolveable(name);
105: }
106: return false;
107: }
108:
109: protected void addResolver(String name, VariableResolver vr) {
110: if (variableResolvers == null)
111: variableResolvers = new HashMap<String, VariableResolver>();
112: variableResolvers.put(name, vr);
113: }
114:
115: public boolean isTarget(String name) {
116: return variableResolvers != null
117: && variableResolvers.containsKey(name);
118: }
119:
120: public Set<String> getKnownVariables() {
121: Set<String> knownVars = new HashSet<String>();
122:
123: if (nextFactory == null) {
124: if (variables != null)
125: knownVars.addAll(variables.keySet());
126: return knownVars;
127: } else {
128: if (variables != null)
129: knownVars.addAll(variables.keySet());
130: knownVars.addAll(nextFactory.getKnownVariables());
131: return knownVars;
132: }
133: }
134: }
|