|
1 | 1 | package fr.airv.web.soyonline.actions;
|
2 | 2 |
|
| 3 | + |
3 | 4 | import java.util.List;
|
4 | 5 |
|
5 | 6 | import net.sourceforge.stripes.action.ActionBean;
|
|
20 | 21 | import com.google.template.soy.jssrc.SoyJsSrcOptions.CodeStyle;
|
21 | 22 | import com.google.template.soy.jssrc.internal.JsSrcModule;
|
22 | 23 |
|
| 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 | + */ |
23 | 36 | @UrlBinding("/action/")
|
24 | 37 | 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 | + } |
108 | 158 |
|
109 | 159 | }
|
0 commit comments