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.drools.base.mvel;
019:
020: import org.mvel.CompileException;
021: import org.mvel.DataConversion;
022: import org.mvel.integration.VariableResolver;
023:
024: import java.util.Map;
025:
026: public class LocalVariableResolver implements VariableResolver {
027: private String name;
028: private Class knownType;
029: private DroolsMVELFactory factory;
030:
031: private boolean cache = false;
032:
033: public LocalVariableResolver(DroolsMVELFactory factory, String name) {
034: this .factory = factory;
035: this .name = name;
036: }
037:
038: public LocalVariableResolver(DroolsMVELFactory factory,
039: String name, Class knownType) {
040: this .name = name;
041: this .knownType = knownType;
042: this .factory = factory;
043: }
044:
045: public LocalVariableResolver(DroolsMVELFactory factory,
046: String name, boolean cache) {
047: this .factory = factory;
048: this .name = name;
049: this .cache = cache;
050: }
051:
052: public LocalVariableResolver(DroolsMVELFactory factory,
053: String name, Class knownType, boolean cache) {
054: this .name = name;
055: this .knownType = knownType;
056: this .factory = factory;
057: this .cache = cache;
058: }
059:
060: public void setName(String name) {
061: this .name = name;
062: }
063:
064: public void setStaticType(Class knownType) {
065: this .knownType = knownType;
066: }
067:
068: public String getName() {
069: return name;
070: }
071:
072: public Class getType() {
073: return knownType;
074: }
075:
076: public void setValue(Object value) {
077: if (knownType != null && value != null
078: && value.getClass() != knownType) {
079: if (!DataConversion.canConvert(knownType, value.getClass())) {
080: throw new CompileException("cannot assign "
081: + value.getClass().getName() + " to type: "
082: + knownType.getName());
083: }
084: try {
085: value = DataConversion.convert(value, knownType);
086: } catch (Exception e) {
087: throw new CompileException("cannot convert value of "
088: + value.getClass().getName() + " to: "
089: + knownType.getName());
090: }
091: }
092:
093: this .factory.setLocalValue(this .name, value);
094: }
095:
096: public Object getValue() {
097: return this .factory.getLocalValue(this .name);
098: }
099:
100: public int getFlags() {
101: return 0;
102: }
103:
104: public boolean isCache() {
105: return cache;
106: }
107:
108: public void setCache(boolean cache) {
109: this.cache = cache;
110: }
111: }
|