| 1 | |
package org.webslinger.macros; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.Writer; |
| 5 | |
import java.util.ArrayList; |
| 6 | |
import java.util.List; |
| 7 | |
import java.util.Map; |
| 8 | |
|
| 9 | |
import org.apache.bsf.BSFException; |
| 10 | |
|
| 11 | |
import org.webslinger.PathContext; |
| 12 | |
import org.webslinger.Webslinger; |
| 13 | |
import org.webslinger.template.TemplateMacro; |
| 14 | |
import org.webslinger.template.TemplateManager; |
| 15 | |
|
| 16 | |
public abstract class AbstractSimpleMacro implements TemplateMacro { |
| 17 | |
private final String name; |
| 18 | |
private final boolean withTemplate; |
| 19 | |
private final boolean isBlock; |
| 20 | |
|
| 21 | 300 | protected AbstractSimpleMacro(String name, boolean withTemplate, boolean isBlock) { |
| 22 | 300 | this.name = name; |
| 23 | 300 | this.withTemplate = withTemplate; |
| 24 | 300 | this.isBlock = isBlock; |
| 25 | 300 | } |
| 26 | |
|
| 27 | |
public String getName() { |
| 28 | 600 | return name; |
| 29 | |
} |
| 30 | |
|
| 31 | |
public boolean isBlock() { |
| 32 | 84 | return isBlock; |
| 33 | |
} |
| 34 | |
|
| 35 | |
public boolean withTemplate() { |
| 36 | 270 | return withTemplate; |
| 37 | |
} |
| 38 | |
|
| 39 | |
public boolean render(TemplateManager manager, Object parent, Writer writer, Args args, Map<String, Object> context, Body body) throws IOException, BSFException { |
| 40 | 274 | return render(manager, (Webslinger) parent, writer, args, context, body); |
| 41 | |
} |
| 42 | |
|
| 43 | |
protected abstract boolean render(TemplateManager manager, Webslinger parent, Writer writer, Args args, Map<String, Object> context, Body body) throws IOException, BSFException; |
| 44 | |
|
| 45 | |
public TemplateMacro newInstance(TemplateManager manager, Object parent, Args args) throws IOException, BSFException { |
| 46 | 0 | return this; |
| 47 | |
} |
| 48 | |
|
| 49 | |
protected PathContext[] getTemplates(Webslinger parent, Args args, int templateIndex) throws IOException, BSFException { |
| 50 | 270 | if (!withTemplate()) return new PathContext[0]; |
| 51 | 270 | Object[] found = args.getIndexed("template", templateIndex); |
| 52 | 270 | List<PathContext> pcs = new ArrayList<PathContext>(found.length); |
| 53 | 270 | for (int i = 0; i < found.length; i++) { |
| 54 | 0 | pcs.add(parent.resolvePath(found[i])); |
| 55 | |
} |
| 56 | 270 | return pcs.toArray(new PathContext[pcs.size()]); |
| 57 | |
} |
| 58 | |
|
| 59 | |
protected PathContext getTemplate(Webslinger parent, Args args, int templateIndex) throws IOException, BSFException { |
| 60 | 0 | return parent.resolvePath(withTemplate() ? args.get("template", templateIndex) : null); |
| 61 | |
} |
| 62 | |
} |