/** * Add a SafeHtml template and an instance method for invoking the template to * the generated BinderImpl class. These templates are declared at the * beginning of the class and instantiated with a GWT.create(Template.class * call. * <p> * Note that the UiBinder#tokenator is used to determine the arguments to the * generated SafeHtml template. * * @return the object that models this template method */ public HtmlTemplateMethodWriter addSafeHtmlTemplate(String html, Tokenator t) throws IllegalArgumentException { if (html == null) { throw new IllegalArgumentException("Template html cannot be null"); } if (t == null) { throw new IllegalArgumentException("Template tokenator cannot be null"); } HtmlTemplateMethodWriter method = new HtmlTemplateMethodWriter(html, t, this); htmlTemplates.add(method); return method; }
public HtmlTemplateMethodWriter(String html, Tokenator tokenator, HtmlTemplatesWriter templates) throws IllegalArgumentException { assertNotNull("html", html); assertNotNull("tokenator", tokenator); assertNotNull("templates", templates); this.templates = templates; methodName = "html" + this.templates.nextTemplateId(); this.html = html; this.tokenator = tokenator; }
public String getDirectTemplateCall() { ensureArgumentsResolved(); return String.format("template.%s(%s)", methodName, getTemplateCallArguments()); }
/** * Writes a new SafeHtml template to the generated BinderImpl. * * @return The invocation of the SafeHtml template function with the arguments * filled in */ public String declareTemplateCall(String html, String fieldName) throws IllegalArgumentException { if (!useSafeHtmlTemplates) { return '"' + html + '"'; } FieldWriter w = fieldManager.lookup(fieldName); HtmlTemplateMethodWriter templateMethod = htmlTemplates.addSafeHtmlTemplate(html, tokenator); if (useLazyWidgetBuilders) { w.setHtml(templateMethod.getIndirectTemplateCall()); } else { w.setHtml(templateMethod.getDirectTemplateCall()); } return w.getHtml(); }
/** * Creates the template method invocation. * * @param w * * @return String the template method call with parameters */ public void writeTemplateCaller(IndentedWriter w) { ensureArgumentsResolved(); w.write("SafeHtml template_%s() {", methodName); w.indent(); w.write("return %s;", getDirectTemplateCall()); w.outdent(); w.write("}"); }
private void ensureArgumentsResolved() { if (argumentsResolved) { return; } if (tokenator != null) { List<ValueAndInfo> valuesAndSources = tokenator.getOrderedValues(html); for (ValueAndInfo valueAndSource : valuesAndSources) { XMLElement source = (XMLElement) valueAndSource.info; String expression = valueAndSource.value; if (templates.isSafeConstant(expression)) { methodArgs.add(new Argument(source, ArgumentType.HTML, expression)); } else if (templates.isUri(expression)) { methodArgs.add(new Argument(source, ArgumentType.URI, expression)); } else { // Nasty. Chop off the "" + stuff surrounding spring expressions String guts = expression.substring(4, expression.length() - 4); methodArgs.add(new Argument(source, ArgumentType.STRING, guts)); } } } strings.add("@Template(\"" + addTemplatePlaceholders(html) + "\")"); strings.add("SafeHtml " + methodName + "(" + addTemplateParameters() + ");"); strings.add(" "); argumentsResolved = true; }
private String processArgExpression(Argument arg) { String raw = arg.expression; if (arg.type == ArgumentType.URI) { if (isStringReference(arg)) { return SafeUriAttributeParser.wrapUnsafeStringAndWarn(templates.getLogger(), arg.getSource(), raw); } } return raw; } }
/** * Writes all templates to the provided {@link IndentedWriter}. * * @param w the writer to write the template to */ public void writeTemplateMethod(IndentedWriter w) { ensureArgumentsResolved(); for (String s : strings) { w.write(s); } }
/** * Retrieves the arguments for SafeHtml template function call from the * {@link Tokenator}. */ private String getTemplateCallArguments() { StringBuilder b = new StringBuilder(); for (Argument arg : methodArgs) { if (b.length() > 0) { b.append(", "); } String argExpression = processArgExpression(arg); b.append(argExpression); } return b.toString(); }
/** * Writes a new SafeHtml template to the generated BinderImpl. * * @return The invocation of the SafeHtml template function with the arguments * filled in */ public String declareTemplateCall(String html, String fieldName) throws IllegalArgumentException { if (!useSafeHtmlTemplates) { return '"' + html + '"'; } FieldWriter w = fieldManager.lookup(fieldName); HtmlTemplateMethodWriter templateMethod = htmlTemplates.addSafeHtmlTemplate(html, tokenator); if (useLazyWidgetBuilders) { w.setHtml(templateMethod.getIndirectTemplateCall()); } else { w.setHtml(templateMethod.getDirectTemplateCall()); } return w.getHtml(); }
/** * Creates the template method invocation. * * @param w * * @return String the template method call with parameters */ public void writeTemplateCaller(IndentedWriter w) { ensureArgumentsResolved(); w.write("SafeHtml template_%s() {", methodName); w.indent(); w.write("return %s;", getDirectTemplateCall()); w.outdent(); w.write("}"); }
private void ensureArgumentsResolved() { if (argumentsResolved) { return; } if (tokenator != null) { List<ValueAndInfo> valuesAndSources = tokenator.getOrderedValues(html); for (ValueAndInfo valueAndSource : valuesAndSources) { XMLElement source = (XMLElement) valueAndSource.info; String expression = valueAndSource.value; if (templates.isSafeConstant(expression)) { methodArgs.add(new Argument(source, ArgumentType.HTML, expression)); } else if (templates.isUri(expression)) { methodArgs.add(new Argument(source, ArgumentType.URI, expression)); } else { // Nasty. Chop off the "" + stuff surrounding spring expressions String guts = expression.substring(4, expression.length() - 4); methodArgs.add(new Argument(source, ArgumentType.STRING, guts)); } } } strings.add("@Template(\"" + addTemplatePlaceholders(html) + "\")"); strings.add("SafeHtml " + methodName + "(" + addTemplateParameters() + ");"); strings.add(" "); argumentsResolved = true; }
private String processArgExpression(Argument arg) { String raw = arg.expression; if (arg.type == ArgumentType.URI) { if (isStringReference(arg)) { return SafeUriAttributeParser.wrapUnsafeStringAndWarn(templates.getLogger(), arg.getSource(), raw); } } return raw; } }
/** * Writes all templates to the provided {@link IndentedWriter}. * * @param w the writer to write the template to */ public void writeTemplateMethod(IndentedWriter w) { ensureArgumentsResolved(); for (String s : strings) { w.write(s); } }
/** * Retrieves the arguments for SafeHtml template function call from the * {@link Tokenator}. */ private String getTemplateCallArguments() { StringBuilder b = new StringBuilder(); for (Argument arg : methodArgs) { if (b.length() > 0) { b.append(", "); } String argExpression = processArgExpression(arg); b.append(argExpression); } return b.toString(); }
/** * Writes a new SafeHtml template to the generated BinderImpl. * * @return The invocation of the SafeHtml template function with the arguments * filled in */ public String declareTemplateCall(String html, String fieldName) throws IllegalArgumentException { if (!useSafeHtmlTemplates) { return '"' + html + '"'; } FieldWriter w = fieldManager.lookup(fieldName); HtmlTemplateMethodWriter templateMethod = htmlTemplates.addSafeHtmlTemplate(html, tokenator); if (useLazyWidgetBuilders) { w.setHtml(templateMethod.getIndirectTemplateCall()); } else { w.setHtml(templateMethod.getDirectTemplateCall()); } return w.getHtml(); }
public String getDirectTemplateCall() { ensureArgumentsResolved(); return String.format("template.%s(%s)", methodName, getTemplateCallArguments()); }
public HtmlTemplateMethodWriter(String html, Tokenator tokenator, HtmlTemplatesWriter templates) throws IllegalArgumentException { assertNotNull("html", html); assertNotNull("tokenator", tokenator); assertNotNull("templates", templates); this.templates = templates; methodName = "html" + this.templates.nextTemplateId(); this.html = html; this.tokenator = tokenator; }
/** * Add a SafeHtml template and an instance method for invoking the template to * the generated BinderImpl class. These templates are declared at the * beginning of the class and instantiated with a GWT.create(Template.class * call. * <p> * Note that the UiBinder#tokenator is used to determine the arguments to the * generated SafeHtml template. * * @return the object that models this template method */ public HtmlTemplateMethodWriter addSafeHtmlTemplate(String html, Tokenator t) throws IllegalArgumentException { if (html == null) { throw new IllegalArgumentException("Template html cannot be null"); } if (t == null) { throw new IllegalArgumentException("Template tokenator cannot be null"); } HtmlTemplateMethodWriter method = new HtmlTemplateMethodWriter(html, t, this); htmlTemplates.add(method); return method; }
/** * Writes a new SafeHtml template to the generated BinderImpl. * * @return The invocation of the SafeHtml template function with the arguments * filled in */ public String declareTemplateCall(String html, String fieldName) throws IllegalArgumentException { if (!useSafeHtmlTemplates) { return '"' + html + '"'; } FieldWriter w = fieldManager.lookup(fieldName); HtmlTemplateMethodWriter templateMethod = htmlTemplates.addSafeHtmlTemplate(html, tokenator); if (useLazyWidgetBuilders) { w.setHtml(templateMethod.getIndirectTemplateCall()); } else { w.setHtml(templateMethod.getDirectTemplateCall()); } return w.getHtml(); }