import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.beanutils.PropertyUtils;
+/**
+ * Class to work with expressions. Will be gradually phased out and replaced
+ * with {@link mir.util.expressions.ExpressionParser}
+ */
public class ParameterExpander {
final static String NODE_SEPARATOR = ".";
final static char STRING_ESCAPE_CHARACTER = '\\';
+ /**
+ * Fundamental method to retrieve a field of an object. Supported are
+ * maps, beans and objects with a generic get method
+ */
+ public static Object getObjectField(Object anObject, Object aField) {
+ if (anObject instanceof Map) {
+ return ((Map) anObject).get(aField);
+ }
+ else if ((aField instanceof String) && PropertyUtils.isReadable(anObject, (String) aField)) {
+ try {
+ return PropertyUtils.getProperty(anObject, (String) aField);
+ }
+ catch (Throwable t) {
+ throw new RuntimeException(t.getMessage());
+ }
+ }
+ else {
+ try {
+ return MethodUtils.invokeExactMethod(anObject, "get", aField);
+ }
+ catch (Throwable t) {
+ throw new RuntimeException("Invalid reference of " + aField + " into " + anObject);
+ }
+ }
+ }
+
private static Object findNode(String aKey, Map aMap, List aParts, boolean aMakeIfNotPresent) throws Exception {
Iterator i;
String location = "";
((Map) node).put(part, newNode);
}
else
- throw new ParameterExpanderExc( "Can't expand key {1}: {2} does not exist", new Object[]{aKey,location} );
+ throw new ParameterExpanderExc( "Can't expand key " + aKey + ": " + location + " does not exist");
node = newNode;
}
Object expandedValue = findValueForKey(aMap, aKey);
if (!(expandedValue instanceof String))
- throw new ParameterExpanderExc( "Value of key is not a string but a {1}", new Object[]{expandedValue.getClass().getName()} );
+ throw new ParameterExpanderExc( "Value of key is not a string but a " + expandedValue.getClass().getName());
return (String) expandedValue;
}
Object node=findNode(aKey, aMap, parts, true);
+ // todo: bean support
if (node instanceof Map) {
((Map) node).put(key, aValue);
}
else
- throw new ParameterExpanderExc( "Can't set key {1}: not inside a Map", new Object[]{aKey} );
+ throw new ParameterExpanderExc( "Can't set key " + aKey + " : not inside a Map");
}
public static String expandExpression(Object aContext, String anExpression) throws Exception {
endOfExpressionPosition++;
}
if (endOfExpressionPosition>=anExpression.length()) {
- throw new ParameterExpanderExc("Unterminated string in {1}",new Object[]{anExpression});
+ throw new ParameterExpanderExc("Unterminated string in '" +anExpression+"'");
}
}
endOfExpressionPosition++;
previousPosition=endOfExpressionPosition+1;
}
else {
- throw new ParameterExpanderExc("Missing } in {1}",new Object[]{anExpression});
+ throw new ParameterExpanderExc("Missing } in " + anExpression);
}
}
else
return result;
}
- private Object evaluateObjectField(Object anObject, Object aField) {
- if (anObject instanceof Map) {
- return ((Map) anObject).get(aField);
- }
- else if ((aField instanceof String) && PropertyUtils.isReadable(anObject, (String) aField)) {
- try {
- return PropertyUtils.getProperty(anObject, (String) aField);
- }
- catch (Throwable t) {
- throw new RuntimeException(t.getMessage());
- }
- }
- else {
- try {
- return MethodUtils.invokeExactMethod(anObject, "get", aField);
- }
- catch (Throwable t) {
- throw new RuntimeException("Invalid reference of " + aField + " into " + anObject);
- }
- }
- }
-
private Object parseVariable() {
boolean done;
Token token;
if (!(token instanceof RightSquareBraceToken))
throw new RuntimeException("] expected");
- currentValue = evaluateObjectField(currentValue, qualifier);
+ currentValue = getObjectField(currentValue, qualifier);
}
else if (token instanceof IdentifierToken) {
scanner.scan();
qualifier = ((IdentifierToken) token).getName();
- currentValue = evaluateObjectField(currentValue, qualifier);
+ currentValue = getObjectField(currentValue, qualifier);
}
else if (token instanceof LeftParenthesisToken) {
- if (currentValue instanceof Generator.GeneratorFunction) {
+ if (currentValue instanceof Generator.Function) {
parameters = parseList();
try {
- currentValue = ((Generator.GeneratorFunction) currentValue).perform(parameters);
+ currentValue = ((Generator.Function) currentValue).perform(parameters);
}
catch (GeneratorExc t) {
throw new RuntimeException(t.getMessage());
if (aValue instanceof Boolean)
return ((Boolean) aValue).booleanValue();
+ if (aValue instanceof RewindableIterator) {
+ ((RewindableIterator) aValue).rewind();
+ }
+
+ if (aValue instanceof Iterator) {
+ return ((Iterator) aValue).hasNext();
+ }
+
+ if (aValue instanceof List) {
+ return ((List) aValue).size()>0;
+ }
+
+ if (aValue instanceof String) {
+ return ((String) aValue).length()>0;
+ }
+
return aValue!=null;
}
}
public static class ParameterExpanderExc extends Exc {
- public ParameterExpanderExc(String msg, Object[] objects) {
- super(msg, objects);
+ public ParameterExpanderExc(String msg) {
+ super(msg);
}
}
}
\ No newline at end of file