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 org.webmacro.Context; |
27 | |
import org.webmacro.FastWriter; |
28 | |
import org.webmacro.Macro; |
29 | |
import org.webmacro.PropertyException; |
30 | |
|
31 | |
import java.io.IOException; |
32 | |
import java.util.Vector; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 2132 | public final class ListBuilder extends Vector implements Builder |
43 | |
{ |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
private static final long serialVersionUID = 1L; |
49 | |
|
50 | |
public final Object[] buildAsArray (BuildContext bc) throws BuildException |
51 | |
{ |
52 | 2128 | Object c[] = new Object[size()]; |
53 | |
|
54 | 3944 | for (int i = 0; i < c.length; i++) |
55 | |
{ |
56 | 1816 | Object elem = elementAt(i); |
57 | 1816 | c[i] = (elem instanceof Builder) ? |
58 | |
((Builder) elem).build(bc) : elem; |
59 | |
} |
60 | 2128 | return c; |
61 | |
} |
62 | |
|
63 | |
public final Object build (BuildContext bc) throws BuildException |
64 | |
{ |
65 | 2128 | boolean isMacro = false; |
66 | 2128 | Object[] c = buildAsArray(bc); |
67 | 3944 | for (int i = 0; i < c.length; i++) |
68 | 1816 | if (c[i] instanceof Macro) |
69 | 1664 | isMacro = true; |
70 | |
|
71 | 2128 | return (isMacro) ? (Object) new ListMacro(c) : c; |
72 | |
} |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
class ListMacro implements Macro |
82 | |
{ |
83 | |
|
84 | |
final private Object[] _content; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
ListMacro (Object[] content) |
90 | 1588 | { |
91 | 1588 | _content = content; |
92 | 1588 | } |
93 | |
|
94 | |
public void write (FastWriter out, Context context) |
95 | |
throws PropertyException, IOException |
96 | |
{ |
97 | 0 | out.write(evaluate(context).toString()); |
98 | 0 | } |
99 | |
|
100 | |
public String toString () |
101 | |
{ |
102 | 2 | StringBuffer sb = new StringBuffer(); |
103 | 2 | sb.append("("); |
104 | 4 | for (int i = 0; i < _content.length; i++) |
105 | |
{ |
106 | 2 | if (i != 0) |
107 | |
{ |
108 | 0 | sb.append(", "); |
109 | |
} |
110 | 2 | sb.append(_content[i] == null ? "null" : _content[i].toString()); |
111 | |
} |
112 | 2 | sb.append(")"); |
113 | 2 | return sb.toString(); |
114 | |
} |
115 | |
|
116 | |
public Object evaluate (Context context) |
117 | |
throws PropertyException |
118 | |
{ |
119 | 42696 | Object[] ret = new Object[_content.length]; |
120 | 95962 | for (int i = 0; i < _content.length; i++) |
121 | |
{ |
122 | 53286 | Object m = _content[i]; |
123 | 53286 | if (m instanceof Macro) |
124 | |
{ |
125 | 48010 | m = ((Macro) m).evaluate(context); |
126 | |
} |
127 | 53266 | ret[i] = m; |
128 | |
} |
129 | 42676 | return ret; |
130 | |
} |
131 | |
} |