KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > arguments > ArgumentLoader


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.arguments;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
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   // --------------------------------------------------------------------------
39
// Implementation of the Loader interface
40
// --------------------------------------------------------------------------
41

42   public Definition load (final String JavaDoc name, final Map JavaDoc context)
43     throws ADLException
44   {
45     Definition d = clientLoader.load(name, context);
46     Map JavaDoc values = new HashMap JavaDoc();
47     if (d instanceof ArgumentDefinition) {
48       String JavaDoc args = ((ArgumentDefinition)d).getArguments();
49       if (args != null) {
50         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(args, ",");
51         int i = 0;
52         while (st.hasMoreTokens()) {
53           String JavaDoc arg = st.nextToken();
54           Object JavaDoc 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   // --------------------------------------------------------------------------
73
// Argument evaluation methods
74
// --------------------------------------------------------------------------
75

76   void evaluate (final Node node, final Map JavaDoc context)
77     throws ADLException
78   {
79     Map JavaDoc attrs = node.astGetAttributes();
80     Iterator JavaDoc i = attrs.keySet().iterator();
81     while (i.hasNext()) {
82       String JavaDoc attr = (String JavaDoc)i.next();
83       String JavaDoc value = (String JavaDoc)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 JavaDoc[] 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 JavaDoc evaluate (final String JavaDoc s, final Map JavaDoc 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 JavaDoc arg = s.substring(i + 2, j);
117     String JavaDoc value = (String JavaDoc)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