| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
package org.webmacro.directive; |
| 28 | |
|
| 29 | |
import org.webmacro.Context; |
| 30 | |
import org.webmacro.Macro; |
| 31 | |
import org.webmacro.PropertyException; |
| 32 | |
import org.webmacro.TemplateVisitor; |
| 33 | |
import org.webmacro.engine.BuildContext; |
| 34 | |
import org.webmacro.engine.BuildException; |
| 35 | |
import org.webmacro.engine.StringMacro; |
| 36 | |
import org.webmacro.engine.Variable; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public class EvalDirective extends org.webmacro.directive.Directive |
| 50 | |
{ |
| 51 | |
|
| 52 | |
private static final int EVAL_EXPR = 1; |
| 53 | |
private static final int EVAL_USING = 2; |
| 54 | |
private static final int EVAL_MAP = 3; |
| 55 | |
private static final int MAX_RECURSION_DEPTH = 100; |
| 56 | |
|
| 57 | |
|
| 58 | |
private Object _evalTarget; |
| 59 | 0 | private Object _mapExpr = null; |
| 60 | |
|
| 61 | 2 | private static final ArgDescriptor[] myArgs = |
| 62 | |
new ArgDescriptor[] { |
| 63 | |
new RValueArg(EVAL_EXPR), |
| 64 | |
new OptionalGroup(2), |
| 65 | |
new KeywordArg(EVAL_USING, "using"), |
| 66 | |
new RValueArg(EVAL_MAP)}; |
| 67 | |
|
| 68 | 2 | private static final DirectiveDescriptor myDescr = |
| 69 | |
new DirectiveDescriptor("eval", null, myArgs, null); |
| 70 | |
|
| 71 | |
public static DirectiveDescriptor getDescriptor() |
| 72 | |
{ |
| 73 | 6 | return myDescr; |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
public EvalDirective() |
| 78 | 0 | { |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
public Object build(DirectiveBuilder builder, BuildContext bc) |
| 82 | |
throws BuildException |
| 83 | |
{ |
| 84 | 0 | _evalTarget = builder.getArg(EVAL_EXPR, bc); |
| 85 | 0 | if (builder.getArg(EVAL_USING) != null) |
| 86 | |
{ |
| 87 | |
|
| 88 | 0 | _mapExpr = builder.getArg(EVAL_MAP, bc); |
| 89 | |
} |
| 90 | |
|
| 91 | 0 | return this; |
| 92 | |
} |
| 93 | |
|
| 94 | |
public void write(org.webmacro.FastWriter out, org.webmacro.Context context) |
| 95 | |
throws org.webmacro.PropertyException, java.io.IOException |
| 96 | |
{ |
| 97 | |
try |
| 98 | |
{ |
| 99 | 0 | Macro macro = null; |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | 0 | Object targ = _evalTarget; |
| 106 | 0 | if (targ instanceof Variable) |
| 107 | |
{ |
| 108 | |
|
| 109 | 0 | targ = ((Variable) _evalTarget).getValue(context); |
| 110 | |
} |
| 111 | 0 | if (targ instanceof String) |
| 112 | |
{ |
| 113 | |
|
| 114 | 0 | targ = new StringMacro((String) targ); |
| 115 | |
} |
| 116 | 0 | if (targ instanceof Macro) |
| 117 | |
{ |
| 118 | 0 | macro = (Macro) targ; |
| 119 | |
} |
| 120 | |
else |
| 121 | |
{ |
| 122 | |
|
| 123 | 0 | throw new PropertyException("Invalid argument to #eval directive. First arg must be a Macro or a String."); |
| 124 | |
} |
| 125 | 0 | if (_mapExpr == null) |
| 126 | |
{ |
| 127 | |
|
| 128 | 0 | macro.write(out, context); |
| 129 | |
} |
| 130 | |
else |
| 131 | |
{ |
| 132 | 0 | Object argmap = _mapExpr; |
| 133 | 0 | if (argmap instanceof Macro) |
| 134 | |
{ |
| 135 | 0 | argmap = ((Macro) argmap).evaluate(context); |
| 136 | |
} |
| 137 | 0 | if (!(argmap instanceof java.util.Map)) |
| 138 | |
{ |
| 139 | 0 | throw new PropertyException("The supplied expression did not evaluate to a java.util.Map instance."); |
| 140 | |
} |
| 141 | |
|
| 142 | 0 | int recursionDepth = 0; |
| 143 | 0 | if (context.containsKey("EvalDepth")) |
| 144 | |
{ |
| 145 | |
try |
| 146 | |
{ |
| 147 | 0 | recursionDepth = |
| 148 | |
((Integer) context.get("EvalDepth")).intValue(); |
| 149 | 0 | recursionDepth++; |
| 150 | |
} |
| 151 | 0 | catch (Exception e) |
| 152 | |
{ |
| 153 | |
|
| 154 | 0 | } |
| 155 | 0 | if (recursionDepth > MAX_RECURSION_DEPTH) |
| 156 | |
{ |
| 157 | 0 | throw new PropertyException( |
| 158 | |
"ERROR: A recursive call to #eval exceeded the maximum depth of " |
| 159 | |
+ MAX_RECURSION_DEPTH); |
| 160 | |
} |
| 161 | |
} |
| 162 | 0 | java.util.Map outerVars = null; |
| 163 | 0 | if (context.containsKey("OuterVars")) |
| 164 | |
{ |
| 165 | |
try |
| 166 | |
{ |
| 167 | 0 | outerVars = (java.util.Map) context.get("OuterVars"); |
| 168 | |
} |
| 169 | 0 | catch (Exception e) |
| 170 | |
{ |
| 171 | |
|
| 172 | 0 | } |
| 173 | |
} |
| 174 | 0 | if (outerVars == null) |
| 175 | 0 | outerVars = context.getMap(); |
| 176 | 0 | Context c = new Context(context.getBroker()); |
| 177 | |
|
| 178 | 0 | c.setMap((java.util.Map) argmap); |
| 179 | |
|
| 180 | 0 | c.put("EvalDepth", recursionDepth); |
| 181 | |
|
| 182 | 0 | c.put("OuterVars", outerVars); |
| 183 | |
|
| 184 | 0 | c.put("Self", macro); |
| 185 | 0 | macro.write(out, c); |
| 186 | |
} |
| 187 | |
} |
| 188 | 0 | catch (Exception e) |
| 189 | |
{ |
| 190 | 0 | if (e instanceof PropertyException) |
| 191 | 0 | throw (PropertyException) e; |
| 192 | 0 | throw new PropertyException("#eval: Unable to evaluate macro.", e); |
| 193 | 0 | } |
| 194 | 0 | } |
| 195 | |
|
| 196 | |
public void accept(TemplateVisitor v) |
| 197 | |
{ |
| 198 | 0 | v.beginDirective(myDescr.name); |
| 199 | 0 | v.visitDirectiveArg("EvalTarget", _evalTarget); |
| 200 | 0 | if (_mapExpr != null) |
| 201 | |
{ |
| 202 | 0 | v.visitDirectiveArg("EvalKeyword", "using"); |
| 203 | 0 | v.visitDirectiveArg("EvalMap", _mapExpr); |
| 204 | |
} |
| 205 | 0 | v.endDirective(); |
| 206 | 0 | } |
| 207 | |
|
| 208 | |
} |