/*
* (c) Copyright 2010-2011 AgileBirds
*
* This file is part of OpenFlexo.
*
* OpenFlexo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenFlexo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.openflexo.antar.expr;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class BindingValueAsExpression extends Expression {
public static abstract class AbstractBindingPathElement {
}
public static class NormalBindingPathElement extends AbstractBindingPathElement {
public String property;
public NormalBindingPathElement(String aProperty) {
property = aProperty;
}
@Override
public String toString() {
return "Normal[" + property + "]";
}
@Override
public boolean equals(Object obj) {
if (obj instanceof NormalBindingPathElement) {
NormalBindingPathElement e = (NormalBindingPathElement) obj;
return property.equals(e.property);
}
return super.equals(obj);
}
}
public static class MethodCallBindingPathElement extends AbstractBindingPathElement {
public String method;
public List<Expression> args;
public MethodCallBindingPathElement(String aMethod, List<Expression> someArgs) {
method = aMethod;
args = someArgs;
}
@Override
public String toString() {
return "Call[" + method + "(" + args + ")" + "]";
}
@Override
public boolean equals(Object obj) {
if (obj instanceof MethodCallBindingPathElement) {
MethodCallBindingPathElement e = (MethodCallBindingPathElement) obj;
return method.equals(e.method) && args.equals(e.args);
}
return super.equals(obj);
}
}
private List<AbstractBindingPathElement> bindingPath;
public BindingValueAsExpression(List<AbstractBindingPathElement> aBindingPath) {
super();
this.bindingPath = aBindingPath;
}
@Override
public int getDepth() {
return 0;
}
public List<AbstractBindingPathElement> getBindingPath() {
return bindingPath;
}
public boolean containsAMethodCall() {
for (AbstractBindingPathElement e : getBindingPath()) {
if (e instanceof MethodCallBindingPathElement) {
return true;
}
}
return false;
}
public boolean isSimpleVariable() {
return getBindingPath().size() == 1 && getBindingPath().get(0) instanceof NormalBindingPathElement;
}
/*@Override
public Expression evaluate(EvaluationContext context) throws TypeMismatchException {
if (containsAMethodCall()) {
ArrayList<AbstractBindingPathElement> newBindingPath = new ArrayList<AbstractBindingPathElement>();
for (AbstractBindingPathElement e : getBindingPath()) {
if (e instanceof NormalBindingPathElement) {
newBindingPath.add(new NormalBindingPathElement(((NormalBindingPathElement) e).property));
} else if (e instanceof MethodCallBindingPathElement) {
ArrayList<Expression> newArgs = new ArrayList<Expression>();
for (Expression arg : ((MethodCallBindingPathElement) e).args) {
newArgs.add(arg.evaluate(context));
}
newBindingPath.add(new MethodCallBindingPathElement(((MethodCallBindingPathElement) e).method, newArgs));
}
}
return new BindingValueAsExpression(newBindingPath);
}
return this;
}*/
@Override
public Expression transform(ExpressionTransformer transformer) throws TransformException {
ArrayList<AbstractBindingPathElement> newBindingPath = new ArrayList<AbstractBindingPathElement>();
for (AbstractBindingPathElement e : getBindingPath()) {
if (e instanceof NormalBindingPathElement) {
newBindingPath.add(new NormalBindingPathElement(((NormalBindingPathElement) e).property));
} else if (e instanceof MethodCallBindingPathElement) {
ArrayList<Expression> newArgs = new ArrayList<Expression>();
for (Expression arg : ((MethodCallBindingPathElement) e).args) {
newArgs.add(arg.transform(transformer));
}
newBindingPath.add(new MethodCallBindingPathElement(((MethodCallBindingPathElement) e).method, newArgs));
}
}
BindingValueAsExpression bv = new BindingValueAsExpression(newBindingPath);
return transformer.performTransformation(bv);
}
@Override
public EvaluationType getEvaluationType() {
return EvaluationType.LITERAL;
}
@Override
protected Vector<Expression> getChilds() {
return null;
}
public boolean isValid() {
return true;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof BindingValueAsExpression) {
BindingValueAsExpression e = (BindingValueAsExpression) obj;
return getBindingPath().equals(e.getBindingPath());
}
return super.equals(obj);
}
}