Browse Source

Using precompiled templates with SoySauce (#2839)

* Using precompiled templates with SoySauce instead of Tofu runtime interpreter

* Use renderHtml() instead of renderText()

Co-authored-by: Tobias Rogg <26879902+roggnroll@users.noreply.github.com>

Co-authored-by: Tobias Rogg <26879902+roggnroll@users.noreply.github.com>
pull/2841/head
Tijana Vislavski Gradina 3 years ago
committed by GitHub
parent
commit
ee50f94c4f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 55 deletions
  1. +53
    -27
      java/demo/pom.xml
  2. +0
    -1
      java/demo/src/main/java/com/google/phonenumbers/demo/render/ErrorRenderer.java
  3. +0
    -4
      java/demo/src/main/java/com/google/phonenumbers/demo/render/InputFormRenderer.java
  4. +4
    -21
      java/demo/src/main/java/com/google/phonenumbers/demo/render/LibPhoneNumberRenderer.java
  5. +0
    -1
      java/demo/src/main/java/com/google/phonenumbers/demo/render/ResultFileRenderer.java
  6. +0
    -1
      java/demo/src/main/java/com/google/phonenumbers/demo/render/ResultRenderer.java

+ 53
- 27
java/demo/pom.xml View File

@ -20,7 +20,9 @@
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation> <maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
<archiveClasses>true</archiveClasses> <archiveClasses>true</archiveClasses>
<soy.root>src/main/resources/com/google/phonenumbers/demo</soy.root>
<soy.root>${project.basedir}/src/main/resources/com/google/phonenumbers/demo</soy.root>
<soy.srcs>${soy.root}/input_form.soy,${soy.root}/result.soy,${soy.root}/result_error.soy,${soy.root}/result_file.soy</soy.srcs>
<soy.output.dir>${project.build.directory}/${project.build.finalName}/WEB-INF/lib/</soy.output.dir>
<soy.package>com.google.phonenumbers.demo.template</soy.package> <soy.package>com.google.phonenumbers.demo.template</soy.package>
</properties> </properties>
@ -83,32 +85,6 @@
<!-- for hot reload of the web application--> <!-- for hot reload of the web application-->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins> <plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.google.template.soy.SoyParseInfoGenerator</mainClass>
<arguments>
<argument>--generateInvocationBuilders</argument>
<argument>--javaPackage</argument>
<argument>${soy.package}</argument>
<argument>--javaClassNameSource</argument>
<argument>filename</argument>
<argument>--srcs</argument>
<argument>${soy.root}/input_form.soy,${soy.root}/result.soy,${soy.root}/result_error.soy,${soy.root}/result_file.soy</argument>
<argument>--outputDirectory</argument>
<argument>src/main/java/com/google/phonenumbers/demo/template/</argument>
</arguments>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>com.google.cloud.tools</groupId> <groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId> <artifactId>appengine-maven-plugin</artifactId>
@ -124,6 +100,56 @@
</configuration> </configuration>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
</plugin> </plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<!-- soy-parser generates template classes in
com.google.phonenumbers.demo.template package and isn't tied to a
build lifecycle, i.e. it needs to be run manually with
"mvn exec:java@soy-parser" -->
<id>soy-parser</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.google.template.soy.SoyParseInfoGenerator</mainClass>
<arguments>
<argument>--generateInvocationBuilders</argument>
<argument>--javaPackage</argument>
<argument>${soy.package}</argument>
<argument>--javaClassNameSource</argument>
<argument>filename</argument>
<argument>--srcs</argument>
<argument>${soy.srcs}</argument>
<argument>--outputDirectory</argument>
<argument>${project.basedir}/src/main/java/com/google/phonenumbers/demo/template/</argument>
</arguments>
</configuration>
</execution>
<execution>
<!-- soy-compiler compiles templates so that we can use SoySauce
instead of Tofu and it's tied to the build lifecycle (it's part of
install phase). -->
<id>soy-compiler</id>
<goals>
<goal>java</goal>
</goals>
<phase>install</phase>
<configuration>
<mainClass>com.google.template.soy.SoyToJbcSrcCompiler</mainClass>
<arguments>
<argument>--srcs</argument>
<argument>${soy.srcs}</argument>
<argument>--output</argument>
<argument>${soy.output.dir}/soy-templates.jar</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>


+ 0
- 1
java/demo/src/main/java/com/google/phonenumbers/demo/render/ErrorRenderer.java View File

@ -30,7 +30,6 @@ public class ErrorRenderer extends LibPhoneNumberRenderer<Error> {
public ErrorRenderer( public ErrorRenderer(
String phoneNumber, String defaultCountry, Locale geocodingLocale, String error) { String phoneNumber, String defaultCountry, Locale geocodingLocale, String error) {
super("result_error.soy", "com.google.phonenumbers.demo.error");
this.phoneNumber = phoneNumber; this.phoneNumber = phoneNumber;
this.defaultCountry = defaultCountry; this.defaultCountry = defaultCountry;
this.geocodingLocale = geocodingLocale; this.geocodingLocale = geocodingLocale;


+ 0
- 4
java/demo/src/main/java/com/google/phonenumbers/demo/render/InputFormRenderer.java View File

@ -23,10 +23,6 @@ import com.google.phonenumbers.demo.template.InputFormTemplates.InputForm;
public class InputFormRenderer extends LibPhoneNumberRenderer<InputForm> { public class InputFormRenderer extends LibPhoneNumberRenderer<InputForm> {
public InputFormRenderer() {
super("input_form.soy", "com.google.phonenumbers.demo.inputForm");
}
@Override @Override
public String genHtml() { public String genHtml() {
return super.render( return super.render(


+ 4
- 21
java/demo/src/main/java/com/google/phonenumbers/demo/render/LibPhoneNumberRenderer.java View File

@ -18,32 +18,15 @@
package com.google.phonenumbers.demo.render; package com.google.phonenumbers.demo.render;
import com.google.template.soy.SoyFileSet;
import com.google.template.soy.data.SoyTemplate; import com.google.template.soy.data.SoyTemplate;
import com.google.template.soy.tofu.SoyTofu;
import java.net.URL;
import com.google.template.soy.jbcsrc.api.SoySauce;
import com.google.template.soy.jbcsrc.api.SoySauceBuilder;
public abstract class LibPhoneNumberRenderer<T extends SoyTemplate> { public abstract class LibPhoneNumberRenderer<T extends SoyTemplate> {
private final String template;
private final String namespace;
public LibPhoneNumberRenderer(String template, String namespace) {
this.template = template;
this.namespace = namespace;
}
private URL getResource() {
return LibPhoneNumberRenderer.class.getResource("../" + template);
}
String render(T soyTemplate) { String render(T soyTemplate) {
SoyFileSet sfs = SoyFileSet.builder().add(getResource()).build();
SoyTofu tofu = sfs.compileToTofu();
// For convenience, create another SoyTofu object that has a
// namespace specified, so you can pass partial template names to
// the newRenderer() method.
SoyTofu simpleTofu = tofu.forNamespace(namespace);
return simpleTofu.newRenderer(soyTemplate).render();
SoySauce soySauce = new SoySauceBuilder().build();
return soySauce.newRenderer(soyTemplate).renderHtml().get().toString();
} }
public abstract String genHtml(); public abstract String genHtml();


+ 0
- 1
java/demo/src/main/java/com/google/phonenumbers/demo/render/ResultFileRenderer.java View File

@ -31,7 +31,6 @@ public class ResultFileRenderer extends LibPhoneNumberRenderer<File> {
private final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); private final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
public ResultFileRenderer(String defaultCountry, String fileContents) { public ResultFileRenderer(String defaultCountry, String fileContents) {
super("result_file.soy", "com.google.phonenumbers.demo.file");
this.fileContents = fileContents; this.fileContents = fileContents;
this.defaultCountry = defaultCountry; this.defaultCountry = defaultCountry;
} }


+ 0
- 1
java/demo/src/main/java/com/google/phonenumbers/demo/render/ResultRenderer.java View File

@ -43,7 +43,6 @@ public class ResultRenderer extends LibPhoneNumberRenderer<SingleNumber> {
public ResultRenderer( public ResultRenderer(
String phoneNumber, String defaultCountry, Locale geocodingLocale, PhoneNumber number) { String phoneNumber, String defaultCountry, Locale geocodingLocale, PhoneNumber number) {
super("result.soy", "com.google.phonenumbers.demo.singleNumber");
this.phoneNumber = phoneNumber; this.phoneNumber = phoneNumber;
this.defaultCountry = defaultCountry; this.defaultCountry = defaultCountry;
this.geocodingLocale = geocodingLocale; this.geocodingLocale = geocodingLocale;


Loading…
Cancel
Save