1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
package org.webmacro.directive; |
24 | |
|
25 | |
import java.io.IOException; |
26 | |
|
27 | |
import org.webmacro.Context; |
28 | |
import org.webmacro.FastWriter; |
29 | |
import org.webmacro.Macro; |
30 | |
import org.webmacro.PropertyException; |
31 | |
import org.webmacro.TemplateVisitor; |
32 | |
import org.webmacro.engine.BuildContext; |
33 | |
import org.webmacro.engine.BuildException; |
34 | |
import org.webmacro.engine.Variable; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class SetblockDirective extends Directive |
40 | |
{ |
41 | |
|
42 | |
private static final int SETBLOCK_AS = 1; |
43 | |
private static final int SETBLOCK_MACRO = 2; |
44 | |
private static final int SETBLOCK_TARGET = 3; |
45 | |
private static final int SETBLOCK_RESULT = 4; |
46 | |
|
47 | |
private Variable target; |
48 | |
private Object result; |
49 | 44 | private boolean asMacro = false; |
50 | |
|
51 | |
private static final ArgDescriptor[] |
52 | 40 | myArgs = new ArgDescriptor[]{ |
53 | |
new OptionalGroup(2), |
54 | |
new KeywordArg(SETBLOCK_AS, "as"), |
55 | |
new KeywordArg(SETBLOCK_MACRO, "macro"), |
56 | |
new LValueArg(SETBLOCK_TARGET), |
57 | |
new BlockArg(SETBLOCK_RESULT) |
58 | |
}; |
59 | |
|
60 | |
private static final DirectiveDescriptor |
61 | 40 | myDescr = new DirectiveDescriptor("setblock", null, myArgs, null); |
62 | |
|
63 | |
public static DirectiveDescriptor getDescriptor () |
64 | |
{ |
65 | 63 | return myDescr; |
66 | |
} |
67 | |
|
68 | |
public SetblockDirective () |
69 | 44 | { |
70 | 44 | } |
71 | |
|
72 | |
public Object build (DirectiveBuilder builder, |
73 | |
BuildContext bc) |
74 | |
throws BuildException |
75 | |
{ |
76 | |
try |
77 | |
{ |
78 | 44 | target = (Variable) builder.getArg(SETBLOCK_TARGET, bc); |
79 | |
} |
80 | 0 | catch (ClassCastException e) |
81 | |
{ |
82 | 0 | throw new NotVariableBuildException(myDescr.name, e); |
83 | 44 | } |
84 | 44 | Object macroKeyword = builder.getArg(SETBLOCK_MACRO, bc); |
85 | 44 | asMacro = (macroKeyword != null); |
86 | 44 | result = builder.getArg(SETBLOCK_RESULT, bc); |
87 | 44 | return this; |
88 | |
} |
89 | |
|
90 | |
public void write (FastWriter out, Context context) |
91 | |
throws PropertyException, IOException |
92 | |
{ |
93 | |
|
94 | |
try |
95 | |
{ |
96 | 44 | if (result instanceof Macro && !asMacro) |
97 | 22 | target.setValue(context, ((Macro) result).evaluate(context)); |
98 | |
else |
99 | 22 | target.setValue(context, result); |
100 | |
} |
101 | 0 | catch (PropertyException e) |
102 | |
{ |
103 | 0 | throw e; |
104 | |
} |
105 | 0 | catch (Exception e) |
106 | |
{ |
107 | 0 | String errorText = "#setblock: Unable to set " + target; |
108 | 0 | writeWarning(errorText, context, out); |
109 | 44 | } |
110 | 44 | } |
111 | |
|
112 | |
public void accept (TemplateVisitor v) |
113 | |
{ |
114 | 0 | v.beginDirective(myDescr.name); |
115 | 0 | if (asMacro) |
116 | |
{ |
117 | 0 | v.visitDirectiveArg("SetblockKeyword", "as"); |
118 | 0 | v.visitDirectiveArg("SetblockKeyword", "macro"); |
119 | |
} |
120 | 0 | v.visitDirectiveArg("SetblockTarget", target); |
121 | 0 | v.visitDirectiveArg("SetblockValue", result); |
122 | 0 | v.endDirective(); |
123 | 0 | } |
124 | |
|
125 | |
} |