| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package org.webmacro.directive; |
| 23 | |
import java.io.IOException; |
| 24 | |
import java.util.ArrayList; |
| 25 | |
import java.util.Iterator; |
| 26 | |
import java.util.List; |
| 27 | |
|
| 28 | |
import org.webmacro.Context; |
| 29 | |
import org.webmacro.FastWriter; |
| 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.Variable; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | 0 | public class AlternateDirective extends Directive |
| 87 | |
{ |
| 88 | |
private static final int ALTERNATE_TARGET = 1; |
| 89 | |
private static final int ALTERNATE_THROUGH = 2; |
| 90 | |
private static final int ALTERNATE_LIST = 3; |
| 91 | |
private Variable target; |
| 92 | |
private Object list; |
| 93 | |
private static final ArgDescriptor[] |
| 94 | 2 | myArgs = new ArgDescriptor[] { |
| 95 | |
new LValueArg(ALTERNATE_TARGET), |
| 96 | |
new KeywordArg(ALTERNATE_THROUGH, "through"), |
| 97 | |
new RValueArg(ALTERNATE_LIST) |
| 98 | |
}; |
| 99 | |
private static final DirectiveDescriptor |
| 100 | 2 | myDescr = new DirectiveDescriptor("alternate", null, myArgs, null); |
| 101 | |
public static DirectiveDescriptor getDescriptor() |
| 102 | |
{ |
| 103 | 6 | return myDescr; |
| 104 | |
} |
| 105 | |
public Object build(DirectiveBuilder builder, |
| 106 | |
BuildContext bc) |
| 107 | |
throws BuildException |
| 108 | |
{ |
| 109 | |
try |
| 110 | |
{ |
| 111 | 0 | target = (Variable) builder.getArg(ALTERNATE_TARGET, bc); |
| 112 | |
} |
| 113 | 0 | catch (ClassCastException e) |
| 114 | |
{ |
| 115 | 0 | throw new NotVariableBuildException(myDescr.name, e); |
| 116 | 0 | } |
| 117 | 0 | list = builder.getArg(ALTERNATE_LIST, bc); |
| 118 | 0 | return this; |
| 119 | |
} |
| 120 | |
public void write(FastWriter out, Context context) |
| 121 | |
throws PropertyException, IOException |
| 122 | |
{ |
| 123 | 0 | Object l = null; |
| 124 | |
try |
| 125 | |
{ |
| 126 | 0 | if (list instanceof Macro) |
| 127 | 0 | l = ((Macro) list).evaluate(context); |
| 128 | |
else |
| 129 | 0 | l = list; |
| 130 | 0 | Iterator itr = context.getBroker()._propertyOperators |
| 131 | |
.getIterator(l); |
| 132 | 0 | target.setValue(context, new IteratorAlternator(itr)); |
| 133 | |
} |
| 134 | 0 | catch (Exception e) |
| 135 | |
{ |
| 136 | 0 | String warning = "#alternate: list argument is not a list: " + l; |
| 137 | 0 | writeWarning(warning, context, out); |
| 138 | 0 | return; |
| 139 | 0 | } |
| 140 | 0 | } |
| 141 | |
public void accept(TemplateVisitor v) |
| 142 | |
{ |
| 143 | 0 | v.beginDirective(myDescr.name); |
| 144 | 0 | v.visitDirectiveArg("AlternateTarget", target); |
| 145 | 0 | v.visitDirectiveArg("AlternateList", list); |
| 146 | 0 | v.endDirective(); |
| 147 | 0 | } |
| 148 | |
} |
| 149 | 0 | abstract class Alternator implements Macro |
| 150 | |
{ |
| 151 | |
public abstract Object evaluate(Context context); |
| 152 | |
public void write(FastWriter out, Context context) |
| 153 | |
throws PropertyException, IOException |
| 154 | |
{ |
| 155 | 0 | Object o = evaluate(context); |
| 156 | 0 | if (o != null) |
| 157 | 0 | out.write(o.toString()); |
| 158 | 0 | } |
| 159 | |
} |
| 160 | |
class IteratorAlternator extends Alternator |
| 161 | |
{ |
| 162 | |
private Iterator<Object> itr; |
| 163 | |
private List<Object> list; |
| 164 | 0 | private int index = -1; |
| 165 | |
public IteratorAlternator(Iterator<Object> itr) |
| 166 | 0 | { |
| 167 | 0 | this.itr = itr; |
| 168 | 0 | this.list = new ArrayList<Object>(); |
| 169 | 0 | } |
| 170 | |
public Object evaluate(Context context) |
| 171 | |
{ |
| 172 | |
Object o; |
| 173 | 0 | if (index == -1 && itr.hasNext()) |
| 174 | |
{ |
| 175 | 0 | o = itr.next(); |
| 176 | 0 | list.add(o); |
| 177 | |
} |
| 178 | |
else |
| 179 | |
{ |
| 180 | 0 | index++; |
| 181 | 0 | if (index == list.size()) |
| 182 | 0 | index = 0; |
| 183 | 0 | o = list.get(index); |
| 184 | |
} |
| 185 | 0 | return o; |
| 186 | |
} |
| 187 | |
} |