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