1 23 24 package org.objectweb.fractal.adl.arguments; 25 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.StringTokenizer ; 30 31 import org.objectweb.fractal.adl.ADLException; 32 import org.objectweb.fractal.adl.AbstractLoader; 33 import org.objectweb.fractal.adl.Definition; 34 import org.objectweb.fractal.adl.Node; 35 36 public class ArgumentLoader extends AbstractLoader { 37 38 42 public Definition load (final String name, final Map context) 43 throws ADLException 44 { 45 Definition d = clientLoader.load(name, context); 46 Map values = new HashMap (); 47 if (d instanceof ArgumentDefinition) { 48 String args = ((ArgumentDefinition)d).getArguments(); 49 if (args != null) { 50 StringTokenizer st = new StringTokenizer (args, ","); 51 int i = 0; 52 while (st.hasMoreTokens()) { 53 String arg = st.nextToken(); 54 Object value = context.get(arg); 55 if (value == null) { 56 value = context.get(" arg " + i); 57 } 58 if (value == null) { 59 throw new ADLException( 60 "No value defined for argument '" + arg + "'", (Node)d, null); 61 } 62 values.put(arg, value); 63 ++i; 64 } 65 ((ArgumentDefinition)d).setArguments(null); 66 } 67 } 68 evaluate((Node)d, values); 69 return d; 70 } 71 72 76 void evaluate (final Node node, final Map context) 77 throws ADLException 78 { 79 Map attrs = node.astGetAttributes(); 80 Iterator i = attrs.keySet().iterator(); 81 while (i.hasNext()) { 82 String attr = (String )i.next(); 83 String value = (String )attrs.get(attr); 84 if (value != null) { 85 try { 86 value = evaluate(value, context); 87 } catch (ADLException e) { 88 throw new ADLException(e.getMessage(), node, null); 89 } 90 attrs.put(attr, value); 91 } 92 } 93 node.astSetAttributes(attrs); 94 95 String [] nodeTypes = node.astGetNodeTypes(); 96 for (int j = 0; j < nodeTypes.length; ++j) { 97 Node[] nodes = node.astGetNodes(nodeTypes[j]); 98 for (int k = 0; k < nodes.length; ++k) { 99 Node n = nodes[k]; 100 if (n != null) { 101 evaluate(n, context); 102 } 103 } 104 } 105 } 106 107 String evaluate (final String s, final Map context) throws ADLException { 108 int i = s.indexOf("${"); 109 if (i == -1) { 110 return s; 111 } 112 int j = s.indexOf('}', i); 113 if (j == -1) { 114 throw new ADLException("Malformed variable reference in '" + s + "'", null); 115 } 116 String arg = s.substring(i + 2, j); 117 String value = (String )context.get(arg); 118 if (value == null) { 119 throw new ADLException("Undefined variable '" + arg + "'", null); 120 } 121 return evaluate(s.substring(0, i) + value + s.substring(j + 1), context); 122 } 123 } 124 | Popular Tags |