Skip to content

Commit d5ba7e2

Browse files
committed
Update README with basic info.
Formated and commented main action class.
1 parent a12aa81 commit d5ba7e2

File tree

3 files changed

+158
-85
lines changed

3 files changed

+158
-85
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.project
22
.classpath
33
.settings/
4+
eclipse-launch-profiles
45
target/
56
pom.xml.tag
67
pom.xml.releaseBackup

README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
1-
soy-online
1+
Soy online
22
==========
33

4-
Soy templates online compiler
4+
Soy templates online compiler.
5+
Wraps the [Soy compiler](https://developers.google.com/closure/templates/docs/helloworld_js) in a Java webapp to ease the Soy compiling process.
6+
7+
Usage
8+
------
9+
10+
Type in your Soy code in the input source field, click "Compile" and that's it!
11+
12+
Running the source code
13+
------
14+
15+
This project runs on Google App Engine and leverages the [Stripes framework](https://stripesframework.atlassian.net/wiki/display/STRIPES/Quick+Start+Guide) as an MVC controller.
16+
17+
[Maven](http://maven.apache.org) is used mainly for dependency management.
18+
19+
> *Note:*
20+
> The SoyToJsSrcCompiler.jar being outside of Maven official repository, you'll need to add it manually to compile, like this:
21+
22+
```shell
23+
mvn install:install-file -DgroupId=com.google.template.soy -DartifactId=soy-js-compiler -Dversion=1.0-SNAPSHOT -Dfile=SoyToJsSrcCompiler.jar
24+
```
25+
26+
I've used eclipse Luna with the [Google Plugin](https://cloud.google.com/appengine/docs/java/tools/eclipse#Running_the_Project) without any major issue.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.airv.web.soyonline.actions;
22

3+
34
import java.util.List;
45

56
import net.sourceforge.stripes.action.ActionBean;
@@ -20,90 +21,139 @@
2021
import com.google.template.soy.jssrc.SoyJsSrcOptions.CodeStyle;
2122
import com.google.template.soy.jssrc.internal.JsSrcModule;
2223

24+
/**
25+
* Main compilation action bean class.
26+
* Exposes 3 entry points:
27+
* <ol>
28+
* <li>Main display</li>
29+
* <li>Compiling</li>
30+
* <li>Compiling and downloading result</li>
31+
* </ol>
32+
*
33+
* @author Hervé Labas
34+
*
35+
*/
2336
@UrlBinding("/action/")
2437
public class Compile implements ActionBean {
25-
private ActionBeanContext context;
26-
private String inputSource;
27-
private String compiled;
28-
private SoySyntaxException compileError;
29-
30-
@Override
31-
public ActionBeanContext getContext() {
32-
return context;
33-
}
34-
35-
@Override
36-
public void setContext(ActionBeanContext context) {
37-
this.context = context;
38-
}
39-
40-
@DefaultHandler
41-
public Resolution showCompiler() {
42-
return new ForwardResolution("/jsp/compiled.jsp");
43-
}
44-
45-
public Resolution compile() {
46-
doCompile();
47-
return new ForwardResolution("/jsp/compiled.jsp");
48-
}
49-
50-
public Resolution download() {
51-
doCompile();
52-
if (this.compiled != null) {
53-
StreamingResolution res = new StreamingResolution("application/javascript", this.compiled);
54-
res.setAttachment(true);
55-
res.setFilename("compiled.js");
56-
return res;
57-
}
58-
return new ForwardResolution("/jsp/compiled.jsp");
59-
}
60-
61-
private void doCompile() {
62-
List<Module> guiceModules = Lists.newArrayListWithCapacity(1);
63-
guiceModules.add(new JsSrcModule());
64-
Injector injector = Guice.createInjector(guiceModules);
65-
66-
SoyFileSet.Builder sfsBuilder = injector.getInstance(SoyFileSet.Builder.class);
67-
sfsBuilder.add(getInputSource(), "");
68-
SoyJsSrcOptions jsSrcOptions = new SoyJsSrcOptions();
69-
jsSrcOptions.setIsUsingIjData(false);
70-
jsSrcOptions.setCodeStyle(CodeStyle.CONCAT);
71-
jsSrcOptions.setShouldGenerateJsdoc(false);
72-
jsSrcOptions.setShouldProvideRequireSoyNamespaces(false);
73-
jsSrcOptions.setShouldDeclareTopLevelNamespaces(true);
74-
jsSrcOptions.setShouldGenerateGoogMsgDefs(false);
75-
jsSrcOptions.setGoogMsgsAreExternal(false);
76-
try {
77-
List<String> result = sfsBuilder.build().compileToJsSrc(jsSrcOptions, null);
78-
setCompiled(result.get(0));
79-
}
80-
catch (SoySyntaxException ex) {
81-
setCompileError(ex);
82-
}
83-
}
84-
85-
public String getCompiled() {
86-
return compiled;
87-
}
88-
89-
public void setCompiled(String compiled) {
90-
this.compiled = compiled;
91-
}
92-
93-
public SoySyntaxException getCompileError() {
94-
return compileError;
95-
}
96-
97-
public void setCompileError(SoySyntaxException compileError) {
98-
this.compileError = compileError;
99-
}
100-
101-
public String getInputSource() {
102-
return inputSource;
103-
}
104-
105-
public void setInputSource(String inputSource) {
106-
this.inputSource = inputSource;
107-
}
38+
39+
/**
40+
* Stripes context.
41+
*/
42+
private ActionBeanContext context;
43+
44+
/**
45+
* Soy input source to compile.
46+
*/
47+
private String inputSource;
48+
49+
/**
50+
* Source compiled as JS code.
51+
*/
52+
private String compiled;
53+
54+
/**
55+
* Compilation error.
56+
*/
57+
private SoySyntaxException compileError;
58+
59+
@Override
60+
public ActionBeanContext getContext() {
61+
return context;
62+
}
63+
64+
@Override
65+
public void setContext(ActionBeanContext context) {
66+
this.context = context;
67+
}
68+
69+
/**
70+
* Displays the main compilation page.
71+
* @return The main JSP resolution.
72+
*/
73+
@DefaultHandler
74+
public Resolution showCompiler() {
75+
return new ForwardResolution("/jsp/compiled.jsp");
76+
}
77+
78+
/**
79+
* Compiles the current source input and renders the compiled result.
80+
* @return The main JSP resolution.
81+
*/
82+
public Resolution compile() {
83+
doCompile();
84+
return new ForwardResolution("/jsp/compiled.jsp");
85+
}
86+
87+
/**
88+
* Compiles and then streams the result back to the client as a file attachment.
89+
* @return The main JSP resolution.
90+
*/
91+
public Resolution download() {
92+
doCompile();
93+
if (this.compiled != null) {
94+
StreamingResolution res = new StreamingResolution("application/javascript", this.compiled);
95+
res.setAttachment(true);
96+
res.setFilename("compiled.js");
97+
return res;
98+
}
99+
return new ForwardResolution("/jsp/compiled.jsp");
100+
}
101+
102+
/**
103+
* Proceeds to the actual compilation of the source input.
104+
*/
105+
private void doCompile() {
106+
// Initialise Guice
107+
List<Module> guiceModules = Lists.newArrayListWithCapacity(1);
108+
guiceModules.add(new JsSrcModule());
109+
Injector injector = Guice.createInjector(guiceModules);
110+
111+
// Create file builder to add the source entry
112+
SoyFileSet.Builder sfsBuilder = injector.getInstance(SoyFileSet.Builder.class);
113+
sfsBuilder.add(getInputSource(), "");
114+
115+
// Default options
116+
// TODO: propose to adjust some options from the UI
117+
SoyJsSrcOptions jsSrcOptions = new SoyJsSrcOptions();
118+
jsSrcOptions.setIsUsingIjData(false);
119+
jsSrcOptions.setCodeStyle(CodeStyle.CONCAT);
120+
jsSrcOptions.setShouldGenerateJsdoc(false);
121+
jsSrcOptions.setShouldProvideRequireSoyNamespaces(false);
122+
jsSrcOptions.setShouldDeclareTopLevelNamespaces(true);
123+
jsSrcOptions.setShouldGenerateGoogMsgDefs(false);
124+
jsSrcOptions.setGoogMsgsAreExternal(false);
125+
126+
// Compile
127+
try {
128+
List<String> result = sfsBuilder.build().compileToJsSrc(jsSrcOptions, null);
129+
setCompiled(result.get(0));
130+
} catch (SoySyntaxException ex) {
131+
setCompileError(ex);
132+
}
133+
}
134+
135+
public String getCompiled() {
136+
return compiled;
137+
}
138+
139+
public void setCompiled(String compiled) {
140+
this.compiled = compiled;
141+
}
142+
143+
public SoySyntaxException getCompileError() {
144+
return compileError;
145+
}
146+
147+
public void setCompileError(SoySyntaxException compileError) {
148+
this.compileError = compileError;
149+
}
150+
151+
public String getInputSource() {
152+
return inputSource;
153+
}
154+
155+
public void setInputSource(String inputSource) {
156+
this.inputSource = inputSource;
157+
}
108158

109159
}

0 commit comments

Comments
 (0)