X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Futil%2FParameterExpander.java;h=734bec98b0b39d3d003d12d4ac7eb42f04fa82e1;hb=63e0ee1fb8038eb6d8f0190cf38c3b3ab2727216;hp=769f6fb148b5bbc513bb420900771ee537fc9ae0;hpb=8b91e8d8bf4a31a88440a404e83238dcf32f8f4a;p=mir.git diff --git a/source/mir/util/ParameterExpander.java b/source/mir/util/ParameterExpander.java index 769f6fb1..734bec98 100755 --- a/source/mir/util/ParameterExpander.java +++ b/source/mir/util/ParameterExpander.java @@ -42,10 +42,40 @@ import multex.Exc; 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 = ""; @@ -74,7 +104,7 @@ public class ParameterExpander { ((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; } @@ -95,7 +125,7 @@ public class ParameterExpander { 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; } @@ -108,11 +138,12 @@ public class ParameterExpander { 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 { @@ -143,7 +174,7 @@ public class ParameterExpander { endOfExpressionPosition++; } if (endOfExpressionPosition>=anExpression.length()) { - throw new ParameterExpanderExc("Unterminated string in {1}",new Object[]{anExpression}); + throw new ParameterExpanderExc("Unterminated string in '" +anExpression+"'"); } } endOfExpressionPosition++; @@ -153,7 +184,7 @@ public class ParameterExpander { previousPosition=endOfExpressionPosition+1; } else { - throw new ParameterExpanderExc("Missing } in {1}",new Object[]{anExpression}); + throw new ParameterExpanderExc("Missing } in " + anExpression); } } else @@ -573,28 +604,6 @@ public class ParameterExpander { 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; @@ -612,19 +621,19 @@ public class ParameterExpander { 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()); @@ -781,6 +790,22 @@ public class ParameterExpander { 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; } @@ -877,8 +902,8 @@ public class ParameterExpander { } 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