001: /* XelMVELResolver.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Sun Sep 2 23:23:12 2007, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zkmax.xel.mvel;
020:
021: import java.util.Collections;
022: import java.util.Set;
023: import java.util.Map;
024: import java.util.HashMap;
025:
026: import org.mvel.CompileException;
027: import org.mvel.integration.VariableResolver;
028: import org.mvel.integration.impl.BaseVariableResolverFactory;
029: import org.mvel.DataConversion;
030:
031: /**
032: * The MVEL variable resolver that is implemnted on top of XEL
033: * variable resolver.
034: *
035: * @author tomyeh
036: * @since 3.0.0
037: */
038: /*package*/class XelMVELResolver extends BaseVariableResolverFactory {
039: private final org.zkoss.xel.VariableResolver _resolver;
040: /** A map of (String,Object) that was added by createVariable.
041: * It is used because _resolver is readonly.
042: */
043: private Map _values;
044:
045: /*package*/XelMVELResolver(org.zkoss.xel.VariableResolver resolver) {
046: _resolver = resolver;
047: }
048:
049: //VariableResolverFactory//
050: public VariableResolver getVariableResolver(String name) {
051: if (_values != null
052: && _values.containsKey(name)
053: || (_resolver != null && _resolver
054: .resolveVariable(name) != null)) {
055: return this .variableResolvers != null
056: && this .variableResolvers.containsKey(name) ? (VariableResolver) this .variableResolvers
057: .get(name)
058: : new VarResolver(name);
059: }
060: return this .nextFactory != null ? this .nextFactory
061: .getVariableResolver(name) : null;
062: }
063:
064: public boolean isResolveable(String name) {
065: return (this .variableResolvers != null && this .variableResolvers
066: .containsKey(name))
067: || (_values != null && _values.containsKey(name))
068: || (_resolver != null && _resolver
069: .resolveVariable(name) != null)
070: || (this .nextFactory != null && this .nextFactory
071: .isResolveable(name));
072: }
073:
074: public boolean isTarget(String name) {
075: return this .variableResolvers.containsKey(name);
076: }
077:
078: public Set getKnownVariables() {
079: return this .nextFactory != null ? this .nextFactory
080: .getKnownVariables() : Collections.EMPTY_SET;
081: //No way to retrieve all variables from XEL variable resolver
082: }
083:
084: public VariableResolver createVariable(String name, Object value) {
085: VariableResolver vr = getVariableResolver(name);
086: if (vr == null)
087: vr = new VarResolver(name);
088:
089: vr.setValue(value);
090: return vr;
091: }
092:
093: public VariableResolver createVariable(String name, Object value,
094: Class type) {
095: VariableResolver vr = getVariableResolver(name);
096: if (vr != null && vr.getType() != null)
097: throw new CompileException(
098: "variable already defined within scope: "
099: + vr.getType() + " " + name);
100:
101: addResolver(name, vr = new VarResolver(name, type));
102: vr.setValue(value);
103: return vr;
104: }
105:
106: private void addResolver(String name, VariableResolver vr) {
107: if (this .variableResolvers == null)
108: this .variableResolvers = new HashMap();
109: this .variableResolvers.put(name, vr);
110: }
111:
112: //Supporting Class//
113: private class VarResolver implements VariableResolver {
114: private final String _name;
115: private Class _type;
116:
117: private VarResolver(String name) {
118: _name = name;
119: }
120:
121: private VarResolver(String name, Class type) {
122: _name = name;
123: _type = type;
124: }
125:
126: public String getName() {
127: return _name;
128: }
129:
130: public Class getType() {
131: return _type;
132: }
133:
134: public void setStaticType(Class type) {
135: _type = type;
136: }
137:
138: public int getFlags() {
139: return 0;
140: }
141:
142: public Object getValue() {
143: if (_values != null) {
144: final Object o = _values.get(_name);
145: if (o != null || _values.containsKey(_name))
146: return o;
147: }
148: return _resolver.resolveVariable(_name);
149: }
150:
151: public void setValue(Object value) {
152: if (_type != null && value != null
153: && value.getClass() != _type) {
154: if (!DataConversion.canConvert(_type, value.getClass()))
155: throw new CompileException("cannot assign "
156: + value.getClass().getName() + " to type: "
157: + _type.getName());
158:
159: try {
160: value = DataConversion.convert(value, _type);
161: } catch (Exception e) {
162: throw new CompileException(
163: "cannot convert value of "
164: + value.getClass().getName()
165: + " to: " + _type.getName());
166: }
167: }
168:
169: if (_values == null)
170: _values = new HashMap();
171: _values.put(_name, value);
172: }
173: }
174: }
|