Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

compile to Java sources

  • Loading branch information...
commit 0da4b7bab13b64a7a6edfaf9fabd477424150704 1 parent eb6e1af
@splix authored
View
77 src/main/java/com/the6hours/hsoytemplates/HsoyBaseCompiler.java
@@ -0,0 +1,77 @@
+package com.the6hours.hsoytemplates;
+
+import com.google.template.soy.SoyFileSet;
+import com.google.template.soy.base.SoySyntaxException;
+
+import java.io.*;
+import java.util.List;
+
+/**
+ * @author Igor Artamonov (http://igorartamonov.com)
+ * @since 02.01.13
+ */
+public abstract class HsoyBaseCompiler {
+
+ private String sourceEncoding = "UTF-8";
+
+ public String compileToString(String input) throws HsoyFormatException, SoySyntaxException {
+ return compileToString(input, "unknown.hsoy");
+ }
+
+ public String compileToString(String input, String name) throws HsoyFormatException, SoySyntaxException {
+ HsoyParser parser = new HsoyParser();
+ HsoyDocument document = parser.parse(input);
+ String soy = document.getSoy();
+ SoyFileSet.Builder sfs = new SoyFileSet.Builder();
+ sfs.add(soy, name);
+
+ SoyFileSet soyFileSet = sfs.build();
+ return compileToString(soyFileSet);
+ }
+
+ public abstract String compileToString(SoyFileSet fileSet) throws HsoyFormatException, SoySyntaxException;
+
+ public SoyFileSet build(List<File> files) throws HsoyFormatException, IOException {
+ HsoyParser parser = new HsoyParser();
+
+ SoyFileSet.Builder sfs = new SoyFileSet.Builder();
+ for (File f: files) {
+ try {
+ HsoyDocument document = parser.parse(read(f));
+ sfs.add(document.getSoy(), f.getName());
+ } catch (HsoyFormatException e) {
+ throw new HsoyFormatException(String.format("Invalid format for file %s", f.getName()), e);
+ }
+ }
+
+ return sfs.build();
+ }
+
+ public SoyFileSet build(String input, String filename) throws HsoyFormatException {
+ HsoyParser parser = new HsoyParser();
+ HsoyDocument document = parser.parse(input);
+
+ SoyFileSet.Builder sfs = new SoyFileSet.Builder();
+ sfs.add(document.getSoy(), filename);
+ return sfs.build();
+ }
+
+
+ public String read(File file) throws IOException {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), sourceEncoding));
+ String line;
+ StringBuilder buf = new StringBuilder((int)file.length());
+ while ((line = reader.readLine()) != null) {
+ buf.append(line).append('\n');
+ }
+ return buf.toString();
+ }
+
+ public String getSourceEncoding() {
+ return sourceEncoding;
+ }
+
+ public void setSourceEncoding(String sourceEncoding) {
+ this.sourceEncoding = sourceEncoding;
+ }
+}
View
65 src/main/java/com/the6hours/hsoytemplates/HsoyJavaCompiler.java
@@ -0,0 +1,65 @@
+package com.the6hours.hsoytemplates;
+
+import com.google.template.soy.SoyFileSet;
+import com.google.template.soy.base.SoySyntaxException;
+import com.google.template.soy.javasrc.SoyJavaSrcOptions;
+import com.google.template.soy.jssrc.SoyJsSrcOptions;
+
+import java.util.List;
+
+/**
+ * @author Igor Artamonov (http://igorartamonov.com)
+ * @since 02.01.13
+ */
+public class HsoyJavaCompiler extends HsoyBaseCompiler {
+
+ private String targetPackage;
+ private String targetClass;
+ private String targetExtends;
+
+ public String compileToString(SoyFileSet soyFileSet) throws HsoyFormatException, SoySyntaxException {
+ SoyJavaSrcOptions javaSrcOptions = new SoyJavaSrcOptions();
+ javaSrcOptions.setCodeStyle(SoyJavaSrcOptions.CodeStyle.STRINGBUILDER);
+ String compiledSrcs = soyFileSet.compileToJavaSrc(javaSrcOptions, null);
+ StringBuilder buf = new StringBuilder();
+ if (targetPackage != null) {
+ buf.append("package ").append(targetPackage).append(";\n");
+ }
+ if (targetClass != null) {
+ buf.append("public class ").append(targetClass).append(' ');
+ if (targetExtends != null) {
+ buf.append("extends ").append(targetExtends);
+ }
+ buf.append("{\n\n");
+ }
+ buf.append(compiledSrcs);
+ if (targetClass != null) {
+ buf.append("\n}");
+ }
+ return buf.toString();
+ }
+
+ public String getTargetPackage() {
+ return targetPackage;
+ }
+
+ public void setTargetPackage(String targetPackage) {
+ this.targetPackage = targetPackage;
+ }
+
+ public String getTargetClass() {
+ return targetClass;
+ }
+
+ public void setTargetClass(String targetClass) {
+ this.targetClass = targetClass;
+ }
+
+ public String getTargetExtends() {
+ return targetExtends;
+ }
+
+ public void setTargetExtends(String targetExtends) {
+ this.targetExtends = targetExtends;
+ }
+}
View
72 src/main/java/com/the6hours/hsoytemplates/HsoyJsCompiler.java
@@ -11,62 +11,40 @@
* @author Igor Artamonov (http://igorartamonov.com)
* @since 23.09.12
*/
-public class HsoyJsCompiler {
+public class HsoyJsCompiler extends HsoyBaseCompiler {
- public String compileToString(String input) throws HsoyFormatException, SoySyntaxException {
- return compileToString(input, "unknown.hsoy");
- }
-
- public String compileToString(String input, String name) throws HsoyFormatException, SoySyntaxException {
- HsoyParser parser = new HsoyParser();
- HsoyDocument document = parser.parse(input);
- String soy = document.getSoy();
- SoyFileSet.Builder sfs = new SoyFileSet.Builder();
- sfs.add(soy, name);
-
- SoyFileSet soyFileSet = sfs.build();
+ private boolean shouldProvideRequireSoyNamespaces = false;
+ private boolean shouldGenerateJsdoc = false;
+ @Override
+ public String compileToString(SoyFileSet fileSet) throws HsoyFormatException, SoySyntaxException {
SoyJsSrcOptions jsSrcOptions = new SoyJsSrcOptions();
- jsSrcOptions.setShouldProvideRequireSoyNamespaces(false);
- jsSrcOptions.setShouldGenerateJsdoc(false);
- List<String> compiledSrcs = soyFileSet.compileToJsSrc(jsSrcOptions, null);
- assert compiledSrcs.size() == 1;
- return compiledSrcs.get(0);
- }
-
- public SoyFileSet build(List<File> files) throws HsoyFormatException, IOException {
- HsoyParser parser = new HsoyParser();
-
- SoyFileSet.Builder sfs = new SoyFileSet.Builder();
- for (File f: files) {
- try {
- HsoyDocument document = parser.parse(read(f));
- sfs.add(document.getSoy(), f.getName());
- } catch (HsoyFormatException e) {
- throw new HsoyFormatException(String.format("Invalid format for file %s", f.getName()), e);
- }
+ jsSrcOptions.setShouldProvideRequireSoyNamespaces(shouldProvideRequireSoyNamespaces);
+ jsSrcOptions.setShouldGenerateJsdoc(shouldGenerateJsdoc);
+ List<String> compiledSrcs = fileSet.compileToJsSrc(jsSrcOptions, null);
+ if (compiledSrcs.size() == 1) {
+ return compiledSrcs.get(0);
}
-
- return sfs.build();
+ StringBuilder buf = new StringBuilder();
+ for (String js: compiledSrcs) {
+ buf.append(js).append('\n');
+ }
+ return buf.toString();
}
- public SoyFileSet build(String input, String filename) throws HsoyFormatException {
- HsoyParser parser = new HsoyParser();
- HsoyDocument document = parser.parse(input);
+ public boolean isShouldProvideRequireSoyNamespaces() {
+ return shouldProvideRequireSoyNamespaces;
+ }
- SoyFileSet.Builder sfs = new SoyFileSet.Builder();
- sfs.add(document.getSoy(), filename);
- return sfs.build();
+ public void setShouldProvideRequireSoyNamespaces(boolean shouldProvideRequireSoyNamespaces) {
+ this.shouldProvideRequireSoyNamespaces = shouldProvideRequireSoyNamespaces;
}
- private String read(File file) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
- String line;
- StringBuilder buf = new StringBuilder((int)file.length());
- while ((line = reader.readLine()) != null) {
- buf.append(line).append('\n');
- }
- return buf.toString();
+ public boolean isShouldGenerateJsdoc() {
+ return shouldGenerateJsdoc;
}
+ public void setShouldGenerateJsdoc(boolean shouldGenerateJsdoc) {
+ this.shouldGenerateJsdoc = shouldGenerateJsdoc;
+ }
}
View
26 src/test/groovy/com/the6hours/hsoytemplates/HsoyJavaCompilerTest.groovy
@@ -0,0 +1,26 @@
+package com.the6hours.hsoytemplates
+
+import spock.lang.Specification
+
+/**
+ *
+ * @author Igor Artamonov (http://igorartamonov.com)
+ * @since 02.01.13
+ */
+class HsoyJavaCompilerTest extends Specification {
+
+ def "Can compile" () {
+ setup:
+ String hsoy = this.class.classLoader.getResourceAsStream('hamlsoy/test-complex.hsoy').text
+ HsoyJavaCompiler compiler = new HsoyJavaCompiler()
+ compiler.targetClass = 'CompiledHsoy'
+ compiler.targetPackage = 'com.the6hours.tests'
+ when:
+ String act = compiler.compileToString(hsoy)
+ println act
+ then:
+ act
+ act.length() > 0
+ act.startsWith("package")
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.