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.ast;
019:
020: import org.mvel.MVEL;
021: import static org.mvel.MVEL.compileSetExpression;
022: import org.mvel.compiler.CompiledSetExpression;
023: import org.mvel.compiler.ExecutableStatement;
024: import org.mvel.compiler.AbstractParser;
025: import org.mvel.integration.VariableResolverFactory;
026: import static org.mvel.util.ArrayTools.findFirst;
027: import static org.mvel.util.ParseTools.*;
028: import static org.mvel.util.PropertyTools.find;
029:
030: /**
031: * @author Christopher Brock
032: */
033: public class AssignmentNode extends ASTNode implements Assignment {
034: private String varName;
035: private transient CompiledSetExpression setExpr;
036:
037: private char[] indexTarget;
038: private char[] index;
039:
040: private char[] stmt;
041: private ExecutableStatement statement;
042:
043: private boolean col = false;
044:
045: // private String index;
046:
047: public AssignmentNode(char[] expr, int fields, int operation,
048: String name) {
049: super (expr, fields);
050:
051: int assignStart;
052:
053: if (operation != -1) {
054: checkNameSafety(this .varName = name.trim());
055:
056: this .egressType = (statement = (ExecutableStatement) subCompileExpression(stmt = createShortFormOperativeAssignment(
057: name, expr, operation))).getKnownEgressType();
058: } else if ((assignStart = find(expr, '=')) != -1) {
059: this .varName = new String(expr, 0, assignStart).trim();
060: stmt = subset(expr, assignStart + 1);
061:
062: if ((fields & COMPILE_IMMEDIATE) != 0) {
063: this .egressType = (statement = (ExecutableStatement) subCompileExpression(stmt))
064: .getKnownEgressType();
065: }
066:
067: if (col = ((endOfName = findFirst('[',
068: indexTarget = this .varName.toCharArray())) > 0)) {
069: if (((this .fields |= COLLECTION) & COMPILE_IMMEDIATE) != 0) {
070: setExpr = (CompiledSetExpression) compileSetExpression(indexTarget);
071: }
072:
073: this .varName = new String(expr, 0, endOfName);
074: index = subset(indexTarget, endOfName,
075: indexTarget.length - endOfName);
076: }
077:
078: checkNameSafety(this .varName);
079: } else {
080: checkNameSafety(this .varName = new String(expr));
081: }
082:
083: if ((fields & COMPILE_IMMEDIATE) != 0) {
084: AbstractParser.getCurrentThreadParserContext().addVariable(
085: this .varName, egressType);
086: }
087:
088: this .name = this .varName.toCharArray();
089: }
090:
091: public AssignmentNode(char[] expr, int fields) {
092: this (expr, fields, -1, null);
093: }
094:
095: public Object getReducedValueAccelerated(Object ctx,
096: Object this Value, VariableResolverFactory factory) {
097: if (setExpr == null) {
098: setExpr = (CompiledSetExpression) compileSetExpression(indexTarget);
099: // statement = (ExecutableStatement) subCompileExpression(stmt);
100: }
101:
102: // Object o;
103:
104: if (col) {
105: setExpr.setValue(ctx, factory, ctx = statement.getValue(
106: ctx, this Value, factory));
107: } else if (statement != null) {
108: factory.createVariable(varName, ctx = statement.getValue(
109: ctx, this Value, factory));
110: } else {
111: factory.createVariable(varName, null);
112: return Void.class;
113: }
114:
115: return ctx;
116: }
117:
118: public Object getReducedValue(Object ctx, Object this Value,
119: VariableResolverFactory factory) {
120: // Object o;
121:
122: checkNameSafety(varName);
123:
124: if (col) {
125: MVEL.setProperty(factory.getVariableResolver(varName)
126: .getValue(), new String(index), ctx = MVEL.eval(
127: stmt, ctx, factory));
128: } else {
129: factory.createVariable(varName, ctx = MVEL.eval(stmt, ctx,
130: factory));
131: }
132:
133: return ctx;
134: }
135:
136: public String getAssignmentVar() {
137: return varName;
138: }
139:
140: public char[] getExpression() {
141: return stmt;
142: }
143:
144: public boolean isNewDeclaration() {
145: return false;
146: }
147:
148: }
|