public
class
TemplateInterpreter {
TerminalNodeProcessor[] terminalNodeProcessors =
new
TerminalNodeProcessor[
200
];
Map<Class<ParserRuleContext>, ContextProcessor> contextProcessorMap =
new
HashMap<Class<ParserRuleContext>, ContextProcessor>();
OtherTerminalNodeProcessor otherNodeProcessor =
new
OtherTerminalNodeProcessor();
public
void
addTerminalNodeProcessor(TerminalNodeProcessor processor) {
terminalNodeProcessors[processor.getType()] = processor;
}
public
void
addContextProcessor(ContextProcessor contextProcessor) {
contextProcessorMap.put(contextProcessor.getType(), contextProcessor);
}
public
TinyTemplateParser.TemplateContext parserTemplateTree(String sourceName, String templateString) {
char
[] source = templateString.toCharArray();
ANTLRInputStream is =
new
ANTLRInputStream(source, source.length);
is.name = sourceName;
TinyTemplateParser parser =
new
TinyTemplateParser(
new
CommonTokenStream(
new
TinyTemplateLexer(is)));
return
parser.template();
}
public
void
interpret(TemplateEngineDefault engine, TemplateFromContext templateFromContext, String templateString, String sourceName, TemplateContext pageContext, TemplateContext context, Writer writer)
throws
Exception {
interpret(engine, templateFromContext, parserTemplateTree(sourceName, templateString), pageContext, context, writer);
writer.flush();
}
public
void
interpret(TemplateEngineDefault engine, TemplateFromContext templateFromContext, TinyTemplateParser.TemplateContext templateParseTree, TemplateContext pageContext, TemplateContext context, Writer writer)
throws
Exception {
for
(
int
i =
0
; i < templateParseTree.getChildCount(); i++) {
interpretTree(engine, templateFromContext, templateParseTree.getChild(i), pageContext, context, writer);
}
}
public
Object interpretTree(TemplateEngineDefault engine, TemplateFromContext templateFromContext, ParseTree tree, TemplateContext pageContext, TemplateContext context, Writer writer)
throws
Exception {
Object returnValue =
null
;
if
(tree
instanceof
TerminalNode) {
TerminalNode terminalNode = (TerminalNode) tree;
TerminalNodeProcessor processor = terminalNodeProcessors[terminalNode.getSymbol().getType()];
if
(processor !=
null
) {
returnValue = processor.process(terminalNode, context, writer);
}
else
{
returnValue = otherNodeProcessor.process(terminalNode, context, writer);
}
}
else
if
(tree
instanceof
ParserRuleContext) {
ContextProcessor processor = contextProcessorMap.get(tree.getClass());
if
(processor !=
null
) {
returnValue = processor.process(
this
, templateFromContext, (ParserRuleContext) tree, pageContext, context, engine, writer);
}
if
(processor ==
null
|| processor !=
null
&& processor.processChildren()) {
for
(
int
i =
0
; i < tree.getChildCount(); i++) {
Object value = interpretTree(engine, templateFromContext, tree.getChild(i), pageContext, context, writer);
if
(value !=
null
) {
returnValue = value;
}
}
}
}
else
{
for
(
int
i =
0
; i < tree.getChildCount(); i++) {
Object value = interpretTree(engine, templateFromContext, tree.getChild(i), pageContext, context, writer);
if
(returnValue ==
null
&& value !=
null
) {
returnValue = value;
}
}
}
return
returnValue;
}
public
static
void
write(Writer writer, Object object)
throws
IOException {
if
(object !=
null
) {
writer.write(object.toString());
writer.flush();
}
}
}