PrintWriter pw = context.tryCreate(logger, packageName, simpleSourceName);
if (pw == null) {
return packageName + "." + simpleSourceName;
}
ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleSourceName);
factory.addImplementedInterface(typeName);
// imports
factory.addImport(Name.getSourceNameForClass(GWT.class));
factory.addImport(Name.getSourceNameForClass(SafeHtml.class));
factory.addImport(Name.getSourceNameForClass(SafeHtmlBuilder.class));
// Loop through the formatters declared for this type and supertypes
FormatCollector formatters = new FormatCollector(context, logger, toGenerate);
MethodCollector invokables = new MethodCollector(context, logger, toGenerate);
SourceWriter sw = factory.createSourceWriter(context, pw);
for (JMethod method : toGenerate.getMethods()) {
TreeLogger l = logger.branch(Type.DEBUG, "Creating XTemplate method " + method.getName());
final String template;
XTemplate marker = method.getAnnotation(XTemplate.class);
if (marker == null) {
l.log(Type.ERROR, "Unable to create template for method " + method.getReadableDeclaration()
+ ", this may cause other failures.");
continue;
} else {
if (marker.source().length() != 0) {
if (marker.value().length() != 0) {
l.log(Type.WARN, "Found both source file and inline template, using source file");
}
InputStream stream = getTemplateResource(context, toGenerate, l, marker.source());
if (stream == null) {
l.log(Type.ERROR, "No data could be loaded - no data at path " + marker.source());
throw new UnableToCompleteException();
}
template = Util.readStreamAsString(stream);
} else if (marker.value().length() != 0) {
template = marker.value();
} else {
l.log(Type.ERROR,
"XTemplate annotation found with no contents, cannot generate method " + method.getName()
+ ", this may cause other failures.");
continue;
}
}
XTemplateParser p = new XTemplateParser(l.branch(Type.DEBUG,
"Parsing provided template for " + method.getReadableDeclaration()));
TemplateModel m = p.parse(template);
SafeHtmlTemplatesCreator safeHtml = new SafeHtmlTemplatesCreator(context, l.branch(Type.DEBUG,
"Building SafeHtmlTemplates"), method);
sw.println(method.getReadableDeclaration(false, true, true, false, true) + "{");
sw.indent();
Map<String, JType> params = new HashMap<String, JType>();
for (JParameter param : method.getParameters()) {
params.put(param.getName(), param.getType());
}
Context scopeContext = new Context(context, l, params, formatters);
// if there is only one parameter, wrap the scope up so that properties
// can be accessed directly
if (method.getParameters().length == 1) {
JParameter param = method.getParameters()[0];
scopeContext = new Context(scopeContext, param.getName(), param.getType());
}
String outerSHVar = scopeContext.declareLocalVariable("outer");
sw.println("SafeHtml %1$s;", outerSHVar);
buildSafeHtmlTemplates(outerSHVar, sw, m, safeHtml, scopeContext, invokables);
sw.println("return %1$s;", outerSHVar);
sw.outdent();
sw.println("}");
safeHtml.create();
}
// Save the file and return its type name
sw.commit(logger);
return factory.getCreatedClassName();
}