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;
019:
020: import static org.mvel.MVELRuntime.execute;
021: import org.mvel.integration.VariableResolverFactory;
022: import org.mvel.optimizers.AccessorOptimizer;
023: import org.mvel.optimizers.OptimizerFactory;
024: import static org.mvel.util.ParseTools.handleParserEgress;
025:
026: import java.io.Serializable;
027:
028: public class CompiledExpression implements Serializable,
029: ExecutableStatement {
030: private ASTIterator tokens;
031:
032: private Class knownEgressType;
033: private Class knownIngressType;
034:
035: private boolean convertableIngressEgress;
036: private boolean optimized = false;
037:
038: private Class<? extends AccessorOptimizer> accessorOptimizer;
039:
040: private String sourceName;
041:
042: private ParserContext parserContext;
043:
044: public CompiledExpression(ASTIterator astMap, String sourceName) {
045: this .tokens = astMap;
046: this .sourceName = sourceName;
047: }
048:
049: public ASTIterator getTokens() {
050: return new ASTLinkedList(tokens.firstNode(), tokens.size());
051: }
052:
053: public void setTokens(ASTIterator tokens) {
054: this .tokens = tokens;
055: }
056:
057: public Class getKnownEgressType() {
058: return knownEgressType;
059: }
060:
061: public void setKnownEgressType(Class knownEgressType) {
062: this .knownEgressType = knownEgressType;
063: }
064:
065: public Class getKnownIngressType() {
066: return knownIngressType;
067: }
068:
069: public void setKnownIngressType(Class knownIngressType) {
070: this .knownIngressType = knownIngressType;
071: }
072:
073: public boolean isConvertableIngressEgress() {
074: return convertableIngressEgress;
075: }
076:
077: public void setConvertableIngressEgress(
078: boolean convertableIngressEgress) {
079: this .convertableIngressEgress = convertableIngressEgress;
080: }
081:
082: public void computeTypeConversionRule() {
083: if (knownIngressType != null && knownEgressType != null) {
084: convertableIngressEgress = knownIngressType
085: .isAssignableFrom(knownEgressType);
086: }
087: }
088:
089: public Object getValue(Object ctx, Object elCtx,
090: VariableResolverFactory variableFactory) {
091: if (!optimized)
092: setupOptimizers();
093: return getValue(ctx, variableFactory);
094: }
095:
096: public Object getValue(Object staticContext,
097: VariableResolverFactory factory) {
098: if (!optimized)
099: setupOptimizers();
100: return handleParserEgress(execute(false, this , staticContext,
101: factory), false);
102: }
103:
104: public Object getDirectValue(Object staticContext,
105: VariableResolverFactory factory) {
106: return execute(false, this , staticContext, factory);
107: }
108:
109: private void setupOptimizers() {
110: OptimizerFactory.setThreadAccessorOptimizer(accessorOptimizer);
111: optimized = true;
112: }
113:
114: public ASTIterator getTokenIterator() {
115: //return new ASTArrayList(tokens);
116: return tokens;
117: }
118:
119: public boolean isOptimized() {
120: return optimized;
121: }
122:
123: public void setOptimized(boolean optimized) {
124: this .optimized = optimized;
125: }
126:
127: public Class<? extends AccessorOptimizer> getAccessorOptimizer() {
128: return accessorOptimizer;
129: }
130:
131: public void setAccessorOptimizer(
132: Class<? extends AccessorOptimizer> accessorOptimizer) {
133: this .accessorOptimizer = accessorOptimizer;
134: }
135:
136: public String getSourceName() {
137: return sourceName;
138: }
139:
140: public void setSourceName(String sourceName) {
141: this .sourceName = sourceName;
142: }
143:
144: public boolean intOptimized() {
145: return false;
146: }
147:
148: public ParserContext getParserContext() {
149: return parserContext;
150: }
151:
152: public void setParserContext(ParserContext parserContext) {
153: this .parserContext = parserContext;
154: }
155:
156: public Object setValue(Object ctx, Object elCtx,
157: VariableResolverFactory variableFactory, Object value) {
158: return null;
159: }
160: }
|