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 SetDirective extends Directive |
40 | |
{ |
41 | |
|
42 | |
private static final int SET_TARGET = 1; |
43 | |
private static final int SET_RESULT = 2; |
44 | |
|
45 | |
private Variable target; |
46 | |
private Object result; |
47 | |
|
48 | |
private static final ArgDescriptor[] |
49 | 2 | myArgs = new ArgDescriptor[]{ |
50 | |
new LValueArg(SET_TARGET), |
51 | |
new AssignmentArg(), |
52 | |
new RValueArg(SET_RESULT) |
53 | |
}; |
54 | |
|
55 | |
private static final DirectiveDescriptor |
56 | 2 | myDescr = new DirectiveDescriptor("set", null, myArgs, null); |
57 | |
|
58 | |
public static DirectiveDescriptor getDescriptor () |
59 | |
{ |
60 | 6 | return myDescr; |
61 | |
} |
62 | |
|
63 | |
public SetDirective () |
64 | 360 | { |
65 | 360 | } |
66 | |
|
67 | |
public SetDirective (Variable target, Object result) |
68 | 0 | { |
69 | 0 | this.target = target; |
70 | 0 | this.result = result; |
71 | 0 | } |
72 | |
|
73 | |
public Object build (DirectiveBuilder builder, |
74 | |
BuildContext bc) |
75 | |
throws BuildException |
76 | |
{ |
77 | |
try |
78 | |
{ |
79 | 360 | target = (Variable) builder.getArg(SET_TARGET, bc); |
80 | |
} |
81 | 0 | catch (ClassCastException e) |
82 | |
{ |
83 | 0 | throw new NotVariableBuildException(myDescr.name, e); |
84 | 360 | } |
85 | 360 | result = builder.getArg(SET_RESULT, bc); |
86 | 360 | return this; |
87 | |
} |
88 | |
|
89 | |
public void write (FastWriter out, Context context) |
90 | |
throws PropertyException, IOException |
91 | |
{ |
92 | |
|
93 | |
try |
94 | |
{ |
95 | 8582 | if (result instanceof Macro) |
96 | 7404 | target.setValue(context, ((Macro) result).evaluate(context)); |
97 | |
else |
98 | 1178 | target.setValue(context, result); |
99 | |
} |
100 | 0 | catch (PropertyException e) |
101 | |
{ |
102 | 0 | throw e; |
103 | |
} |
104 | 0 | catch (Exception e) |
105 | |
{ |
106 | 0 | String errorText = "#set: Unable to set value: " + target; |
107 | 0 | writeWarning(errorText, context, out); |
108 | 8582 | } |
109 | 8582 | } |
110 | |
|
111 | |
public void accept (TemplateVisitor v) |
112 | |
{ |
113 | 0 | v.beginDirective(myDescr.name); |
114 | 0 | v.visitDirectiveArg("SetTarget", target); |
115 | 0 | v.visitDirectiveArg("SetValue", result); |
116 | 0 | v.endDirective(); |
117 | 0 | } |
118 | |
|
119 | |
} |