1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
package org.webmacro.engine; |
25 | |
|
26 | |
import java.util.HashMap; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
import org.webmacro.Broker; |
30 | |
import org.webmacro.Context; |
31 | |
import org.webmacro.Macro; |
32 | |
import org.webmacro.NotFoundException; |
33 | |
import org.webmacro.ResourceException; |
34 | |
import org.webmacro.Template; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public class BuildContext extends Context |
48 | |
{ |
49 | |
|
50 | 224 | private final Map _types = new HashMap(); |
51 | 224 | private final Map _macros = new HashMap(); |
52 | |
|
53 | |
public BuildContext (Broker b) |
54 | |
{ |
55 | 224 | super(b); |
56 | 224 | } |
57 | |
|
58 | |
public final Parser getParser (String pname) |
59 | |
throws NotFoundException |
60 | |
{ |
61 | |
try |
62 | |
{ |
63 | 0 | return (Parser) getBroker().get("parser", pname); |
64 | |
} |
65 | 0 | catch (NotFoundException e) |
66 | |
{ |
67 | 0 | throw e; |
68 | |
} |
69 | 0 | catch (ResourceException e) |
70 | |
{ |
71 | 0 | throw new NotFoundException(e.toString(), e); |
72 | |
} |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public Object getVariableType (String name) |
80 | |
{ |
81 | 5834 | Object ret = _types.get(name); |
82 | 5834 | return (ret == null) ? Variable.PROPERTY_TYPE : ret; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public void setVariableType (String name, Object type) |
90 | |
{ |
91 | 0 | if (name == null) |
92 | |
{ |
93 | 0 | return; |
94 | |
} |
95 | 0 | if (type == null) |
96 | |
{ |
97 | 0 | _types.remove(name); |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 0 | _types.put(name, type); |
102 | |
} |
103 | 0 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public void putMacro (String name, MacroDefinition macro) |
109 | |
{ |
110 | 0 | _macros.put(name, macro); |
111 | 0 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public MacroDefinition getMacro (String name) |
117 | |
{ |
118 | 0 | return (MacroDefinition) _macros.get(name); |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public void mergeConstants (Template t) |
126 | |
{ |
127 | 0 | Map macros = t.getMacros(); |
128 | 0 | Map params = t.getParameters(); |
129 | 0 | if (macros != null) |
130 | 0 | _macros.putAll(macros); |
131 | 0 | if (params != null) |
132 | 0 | super.putAll(params); |
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public Map getMacros () |
139 | |
{ |
140 | 224 | return _macros; |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
Object resolveVariableReference (Object names[]) |
148 | |
throws BuildException |
149 | |
{ |
150 | 5834 | Object v = null; |
151 | |
|
152 | 5834 | if (names.length < 1) |
153 | 0 | throw new BuildException("Variable with name of length zero!"); |
154 | |
|
155 | 5834 | Object c[] = new Object[names.length]; |
156 | 16818 | for (int i = 0; i < c.length; i++) |
157 | |
{ |
158 | 10984 | c[i] = (names[i] instanceof Builder) ? |
159 | |
((Builder) names[i]).build(this) : names[i]; |
160 | |
} |
161 | 5834 | String firstName = c[0].toString(); |
162 | 5834 | Object type = null; |
163 | 5834 | if (c[0] instanceof FunctionCall) |
164 | |
{ |
165 | 0 | type = FunctionVariable.TYPE; |
166 | |
} |
167 | |
else |
168 | |
{ |
169 | 5834 | type = getVariableType(firstName); |
170 | |
} |
171 | 5834 | if (type == Variable.PROPERTY_TYPE) |
172 | |
{ |
173 | 5834 | if (containsKey(firstName)) |
174 | |
{ |
175 | 0 | Object expansion = get(firstName); |
176 | 0 | if (expansion instanceof Macro) |
177 | |
{ |
178 | 0 | v = (c.length == 1) ? expansion |
179 | |
: new MacroPropertyVariable((Macro) expansion, c); |
180 | |
} |
181 | |
else |
182 | |
{ |
183 | 0 | v = (c.length == 1) ? expansion |
184 | |
: new ConstantPropertyVariable(expansion, c); |
185 | |
} |
186 | 0 | } |
187 | |
else |
188 | |
{ |
189 | 5834 | v = (c.length == 1) |
190 | |
? (Object) new SimplePropertyVariable(c) |
191 | |
: (Object) new PropertyVariable(c); |
192 | |
} |
193 | |
} |
194 | 0 | else if (type == Variable.LOCAL_TYPE) |
195 | |
{ |
196 | 0 | v = new GlobalVariable(c); |
197 | |
} |
198 | 0 | else if (type == FunctionVariable.TYPE) |
199 | |
{ |
200 | 0 | v = new FunctionVariable(c); |
201 | |
} |
202 | |
else |
203 | |
{ |
204 | 0 | throw new BuildException("Unrecognized Variable Type: " + type); |
205 | |
} |
206 | 5834 | return v; |
207 | |
} |
208 | |
|
209 | |
} |
210 | |
|
211 | |
|