1.1 restoration
[mir.git] / source / mir / util / ParameterExpander.java
index 769f6fb..734bec9 100755 (executable)
@@ -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