| 1 | |
package org.webslinger.template; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.Writer; |
| 5 | |
import java.util.ArrayList; |
| 6 | |
import java.util.Arrays; |
| 7 | |
import java.util.List; |
| 8 | |
import java.util.Map; |
| 9 | |
|
| 10 | |
import org.apache.bsf.BSFException; |
| 11 | |
|
| 12 | |
public interface TemplateMacro { |
| 13 | |
String getName(); |
| 14 | |
boolean isBlock(); |
| 15 | |
boolean render(TemplateManager manager, Object parent, Writer writer, Args args, Map<String, Object> context, Body body) throws IOException, BSFException; |
| 16 | |
TemplateMacro newInstance(TemplateManager manager, Object parent, Args args) throws IOException, BSFException; |
| 17 | |
|
| 18 | |
final class Args { |
| 19 | |
public final Object[] pList; |
| 20 | |
public final Map<String, Object> pMap; |
| 21 | |
|
| 22 | 418 | public Args(Object[] pList, Map<String, Object> pMap) { |
| 23 | 418 | this.pList = pList; |
| 24 | 418 | this.pMap = pMap; |
| 25 | 418 | if (pList == null && pMap == null) throw new InternalError("pList and pMap both null"); |
| 26 | 418 | } |
| 27 | |
|
| 28 | |
public Object get(String name, int index) { |
| 29 | 148 | return pList != null ? (index < pList.length ? pList[index] : null) : pMap.get(name); |
| 30 | |
} |
| 31 | |
|
| 32 | |
public Object[] getIndexed(String name, int index) { |
| 33 | 270 | ArrayList found = new ArrayList(); |
| 34 | 270 | if (pList != null) { |
| 35 | 270 | while (index < pList.length) found.add(pList[index++]); |
| 36 | |
} else { |
| 37 | 0 | int count = 0; |
| 38 | 0 | String key = name; |
| 39 | 0 | while (pMap.containsKey(key)) { |
| 40 | 0 | found.add(pMap.get(key)); |
| 41 | 0 | key = name + count; |
| 42 | 0 | count++; |
| 43 | |
} |
| 44 | |
} |
| 45 | 270 | return found.toArray(); |
| 46 | |
} |
| 47 | |
|
| 48 | |
public String toString() { |
| 49 | 0 | return "Args(" + (pList != null ? Arrays.asList(pList).toString() : pMap.toString()) + ")"; |
| 50 | |
} |
| 51 | |
} |
| 52 | |
|
| 53 | |
interface Body { |
| 54 | |
boolean render(Writer writer, Map<String, Object> context) throws IOException; |
| 55 | |
Object getSource(); |
| 56 | |
} |
| 57 | |
} |