public static Vector<Variable> extractVariables(String anExpression) throws ParseException, TypeMismatchException { final Hashtable<String, Variable> returnedHash = new Hashtable<String, Variable>(); try { Expression e = ExpressionParser.parse(anExpression); e.transform( new ExpressionTransformer() { @Override public Expression performTransformation(Expression e) throws TransformException { if (e instanceof BindingValueAsExpression) { String variableName = ((BindingValueAsExpression) e).toString(); Variable returned = returnedHash.get(variableName); if (returned == null) { returned = new Variable(variableName); returnedHash.put(variableName, returned); } return returned; } return e; } }); } catch (ParseException e1) { throw e1; } catch (TransformException e) { e.printStackTrace(); } /*DefaultExpressionParser parser = new DefaultExpressionParser(); Expression expression = parser.parse(anExpression, bindable); expression.evaluate(new EvaluationContext(new ExpressionParser.DefaultConstantFactory(), new VariableFactory() { @Override public Expression makeVariable(Word value, Bindable bindable) { Variable returned = returnedHash.get(value.getValue()); if (returned == null) { returned = new Variable(value.getValue()); returnedHash.put(value.getValue(), returned); } return returned; } }, new ExpressionParser.DefaultFunctionFactory()), bindable);*/ Vector<Variable> returned = new Vector<Variable>(); for (String v : returnedHash.keySet()) { returned.add(returnedHash.get(v)); } return returned; }
public static Vector<Expression> extractPrimitives(String anExpression) throws ParseException, TypeMismatchException { final Hashtable<String, Expression> returnedHash = new Hashtable<String, Expression>(); try { Expression e = ExpressionParser.parse(anExpression); e.transform( new ExpressionTransformer() { @Override public Expression performTransformation(Expression e) throws TransformException { if (e instanceof BindingValueAsExpression) { String variableName = ((BindingValueAsExpression) e).toString(); Expression returned = returnedHash.get(variableName); if (returned == null) { returned = new Variable(variableName); returnedHash.put(variableName, returned); } return returned; } return e; } }); } catch (org.openflexo.antar.expr.parser.ParseException e1) { e1.printStackTrace(); } catch (TransformException e) { e.printStackTrace(); } /*DefaultExpressionParser parser = new DefaultExpressionParser(); Expression expression = parser.parse(anExpression, bindable); expression.evaluate(new EvaluationContext(new ExpressionParser.DefaultConstantFactory(), new VariableFactory() { @Override public Expression makeVariable(Word value, Bindable bindable) { Expression returned = returnedHash.get(value.getValue()); if (returned == null) { returned = new Variable(value.getValue()); returnedHash.put(value.getValue(), returned); } return returned; } }, new FunctionFactory() { @Override public Expression makeFunction(String functionName, List<Expression> args, Bindable bindable) { StringBuffer key = new StringBuffer(); key.append(functionName + "("); for (int i = 0; i < args.size(); i++) { key.append((i > 0 ? "," : "") + "arg" + i); } key.append(")"); Expression returned = returnedHash.get(key); if (returned == null) { returned = new Function(functionName, args); returnedHash.put(key.toString(), returned); } return returned; } }), bindable); */ Vector<Expression> returned = new Vector<Expression>(); for (String v : returnedHash.keySet()) { returned.add(returnedHash.get(v)); } return returned; }