Name pkg = Name.parse("foo");
Name name = Name.parse("index");
Name fqn = pkg.append(name);
Path.Absolute absolute = Path.absolute(fqn, ".gtmpl");
Path.Relative relative = Path.relative(name, ".gtmpl");
GroovyTemplateEmitter generator = new GroovyTemplateEmitter(fqn);
try {
ProcessPhase processPhase = new ProcessPhase(new SimpleProcessContext(Collections.<Path.Absolute, TemplateModel<?>>emptyMap()) {
@Override
public Path.Absolute resolveTemplate(Path path) throws TemplateException {
return null;
}
@Override
public TagHandler resolveTagHandler(String name) {
if ("title".equals(name)) {
return new TitleTag();
} else {
return null;
}
}
@Override
public MethodInvocation resolveMethodInvocation(String typeName, String methodName, Map<String, String> parameterMap) throws ProcessingException {
if (parameterMap.size() > 0) {
throw failure("Unexpected non empty parameter map");
}
Class clazz = AbstractTemplateTestCase.this.getClass();
try {
Method m = clazz.getMethod(methodName);
return new MethodInvocation(clazz.getName(), m.getName(), Collections.<String>emptyList());
}
catch (NoSuchMethodException e) {
// Should we thrown a CompilationException instead ?
throw failure(e);
}
}
});
TemplateModel<ASTNode.Template> templateModel = new TemplateModel<ASTNode.Template>(
ASTNode.Template.parse(text),
absolute,
0,
0);
processPhase.process(templateModel);
// Emit
EmitPhase emitPhase = new EmitPhase(new EmitContext(){
@Override
public TagHandler resolveTagHandler(String name) {
if ("title".equals(name)) {
return new TitleTag();
} else {
return null;
}
}
public void createResource(Path.Absolute path, CharSequence content) throws IOException {
throw new UnsupportedOperationException();
}
});
emitPhase.emit(generator, templateModel.getModel());
}
catch (juzu.impl.template.spi.juzu.ast.ParseException e) {
throw failure(e);
}
GroovyTemplateStub stub = generator.build(fqn.toString());
stub.init();
return stub;
}