X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Futil%2FParameterExpander.java;h=0a5876b0f65906b7565a7dd7afb0af93166060f6;hb=598f26d339b398917c2aa14c766c0da5d1be8512;hp=bb677d75e7cc19d01b0d394921ce6a2c6a1d343f;hpb=6a5286a1e7f2db55a7d312bb9b1ef40453b3612d;p=mir.git diff --git a/source/mir/util/ParameterExpander.java b/source/mir/util/ParameterExpander.java index bb677d75..0a5876b0 100755 --- a/source/mir/util/ParameterExpander.java +++ b/source/mir/util/ParameterExpander.java @@ -1,5 +1,8 @@ package mir.util; +import multex.Failure; +import multex.Exc; + import java.util.*; public class ParameterExpander { @@ -23,15 +26,12 @@ public class ParameterExpander { return result; } - public static String findValueForKey(Map aMap, String aKey) throws Exception { - Object node; + private static Object findNode(String aKey, Map aMap, List aParts) throws Exception { Iterator i; - List parts = splitString(aKey, NODE_SEPARATOR); String location = ""; + Object node = aMap; - node = aMap; - - i = parts.iterator(); + i = aParts.iterator(); while(i.hasNext()) { String part = (String) i.next(); @@ -49,18 +49,44 @@ public class ParameterExpander { location = location + part; if (node == null) { - throw new Exception( "Can't expand key " + aKey + ": "+ location + " does not exist" ); + throw new ParameterExpanderExc( "Can't expand key {1}: {2} does not exist", new Object[]{aKey,location} ); } } - if (node instanceof Map) { - node = ((Map) node).get(DEFAULT_KEY); - } + return node; + } + + public static Object findValueForKey(Map aMap, String aKey) throws Exception { + Object node; + List parts = splitString(aKey, NODE_SEPARATOR); + + node = findNode(aKey, aMap, parts); + + return node; + } - if (!(node instanceof String)) - throw new Exception( "Can't expand key " + aKey + ": "+ location + " is not a string but a " + node.getClass().getName() ); + public static String findStringForKey(Map aMap, String aKey) throws Exception { + Object expandedValue = findValueForKey(aMap, aKey); - return (String) node; + if (!(expandedValue instanceof String)) + throw new ParameterExpanderExc( "Value of key is not a string but a {1}", new Object[]{expandedValue.getClass().getName()} ); + + return (String) expandedValue; + } + + public static void setValueForKey(Map aMap, String aKey, Object aValue) throws Exception { + List parts = splitString(aKey, NODE_SEPARATOR); + + String key = (String) parts.get(parts.size()-1); + parts.remove(parts.size()-1); + + Object node=findNode(aKey, aMap, parts); + + 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} ); } public static String expandExpression(Map aMap, String anExpression) throws Exception { @@ -81,11 +107,11 @@ public class ParameterExpander { if (anExpression.charAt(position+1) == '{') { endOfNamePosition=anExpression.indexOf('}', position); if (endOfNamePosition>=0) { - result.append(findValueForKey(aMap, anExpression.substring(position+2, endOfNamePosition))); + result.append(findStringForKey(aMap, anExpression.substring(position+2, endOfNamePosition))); previousPosition=endOfNamePosition+1; } else { - throw new Exception("Missing } in " + anExpression); + throw new ParameterExpanderExc("Missing } in {1}",new Object[]{anExpression}); } } else @@ -99,6 +125,13 @@ public class ParameterExpander { return result.toString(); } + + + public static class ParameterExpanderExc extends Exc { + public ParameterExpanderExc(String msg, Object[] objects) { + super(msg, objects); + } + } }