1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package org.webmacro.engine; |
23 | |
|
24 | |
import org.webmacro.Context; |
25 | |
import org.webmacro.FastWriter; |
26 | |
import org.webmacro.Macro; |
27 | |
import org.webmacro.PropertyException; |
28 | |
|
29 | |
import java.io.IOException; |
30 | |
import java.util.HashMap; |
31 | |
import java.util.Iterator; |
32 | |
import java.util.Map; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class MapBuilder extends HashMap implements Builder |
47 | |
{ |
48 | |
private static final long serialVersionUID = 1L; |
49 | |
|
50 | |
public Object build (BuildContext pc) throws BuildException |
51 | |
{ |
52 | 0 | Map ret = new HashMap(size()); |
53 | 0 | boolean isMacro = false; |
54 | 0 | for (Iterator itr = entrySet().iterator(); itr.hasNext(); ) |
55 | |
{ |
56 | 0 | Map.Entry entry = (Map.Entry) itr.next(); |
57 | 0 | Object key = entry.getKey(); |
58 | 0 | Object value = entry.getValue(); |
59 | |
|
60 | 0 | if (key instanceof Builder) |
61 | 0 | key = ((Builder) key).build(pc); |
62 | 0 | if (value instanceof Builder) |
63 | 0 | value = ((Builder) value).build(pc); |
64 | |
|
65 | 0 | if (!isMacro) |
66 | 0 | isMacro = key instanceof Macro || value instanceof Macro; |
67 | |
|
68 | 0 | ret.put(key, value); |
69 | 0 | } |
70 | |
|
71 | 0 | if (isMacro) |
72 | 0 | return new MapMacro(ret); |
73 | |
else |
74 | 0 | return ret; |
75 | |
} |
76 | |
} |
77 | |
|
78 | |
class MapMacro implements Macro |
79 | |
{ |
80 | |
private final Map _map; |
81 | |
|
82 | |
MapMacro (Map map) |
83 | 0 | { |
84 | 0 | _map = map; |
85 | 0 | } |
86 | |
|
87 | |
public void write (FastWriter out, Context context) throws PropertyException, IOException |
88 | |
{ |
89 | 0 | out.write(evaluate(context).toString()); |
90 | 0 | } |
91 | |
|
92 | |
public String toString () |
93 | |
{ |
94 | 0 | return _map.toString(); |
95 | |
} |
96 | |
|
97 | |
public Object evaluate (Context context) throws PropertyException |
98 | |
{ |
99 | 0 | Map ret = new HashMap(_map.size()); |
100 | 0 | for (Iterator itr = _map.entrySet().iterator(); itr.hasNext(); ) |
101 | |
{ |
102 | 0 | Map.Entry entry = (Map.Entry) itr.next(); |
103 | 0 | Object key = entry.getKey(); |
104 | 0 | Object value = entry.getValue(); |
105 | |
|
106 | 0 | if (key instanceof Macro) |
107 | 0 | key = ((Macro) key).evaluate(context); |
108 | 0 | if (value instanceof Macro) |
109 | 0 | value = ((Macro) value).evaluate(context); |
110 | 0 | ret.put(key, value); |
111 | 0 | } |
112 | |
|
113 | 0 | return ret; |
114 | |
} |
115 | |
} |