Skip to content

Commit 335befc

Browse files
committed
Small refactoring
Switched to stripes layouts Switched to AJAX and client-side download UI cleanup
1 parent d5ba7e2 commit 335befc

16 files changed

+603
-281
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@
6666
<artifactId>guice-servlet</artifactId>
6767
<version>3.0</version>
6868
</dependency>
69+
<dependency>
70+
<groupId>com.google.code.gson</groupId>
71+
<artifactId>gson</artifactId>
72+
<version>2.3.1</version>
73+
</dependency>
74+
6975

7076
<!-- Test Dependencies -->
7177
<dependency>

src/main/java/fr/airv/web/soyonline/actions/Compile.java

-159
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package fr.airv.web.soyonline.actions;
2+
3+
4+
import net.sourceforge.stripes.action.DefaultHandler;
5+
import net.sourceforge.stripes.action.HandlesEvent;
6+
import net.sourceforge.stripes.action.Resolution;
7+
import net.sourceforge.stripes.action.UrlBinding;
8+
import fr.airv.web.soyonline.model.AjaxResult;
9+
import fr.airv.web.soyonline.services.CompilationException;
10+
import fr.airv.web.soyonline.services.CompilationService;
11+
import fr.airv.web.soyonline.stripesext.JSONResolution;
12+
13+
/**
14+
* Main compilation action bean class. Exposes 3 entry points:
15+
* <ol>
16+
* <li>Main display</li>
17+
* <li>Compiling</li>
18+
* <li>Compiling and downloading result</li>
19+
* </ol>
20+
*
21+
* @author Hervé Labas
22+
*
23+
*/
24+
@UrlBinding("/action/compile/{$event}")
25+
public class CompileActionBean extends SoyOnlineActionBean {
26+
27+
/**
28+
* Soy input source to compile.
29+
*/
30+
private String inputSource;
31+
32+
/**
33+
* Compilation service. TODO: initialise using Guice.
34+
*/
35+
private CompilationService compilationService = new CompilationService();
36+
37+
/**
38+
* Compiles the current source input and renders the compiled result.
39+
*
40+
* @return The javascript resolution containing the compilation result.
41+
*/
42+
@HandlesEvent("verify")
43+
@DefaultHandler
44+
public Resolution compile() {
45+
AjaxResult compilationResult = new AjaxResult();
46+
try {
47+
String compiled = compilationService.compileSource(getInputSource());
48+
compilationResult.setContent(compiled);
49+
compilationResult.setSuccess(true);
50+
} catch (CompilationException ex) {
51+
compilationResult.setContent(ex.getMessage());
52+
compilationResult.setSuccess(false);
53+
}
54+
return new JSONResolution(compilationResult);
55+
}
56+
57+
public String getInputSource() {
58+
return inputSource;
59+
}
60+
61+
public void setInputSource(String inputSource) {
62+
this.inputSource = inputSource;
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package fr.airv.web.soyonline.actions;
2+
3+
import net.sourceforge.stripes.action.DefaultHandler;
4+
import net.sourceforge.stripes.action.ForwardResolution;
5+
import net.sourceforge.stripes.action.Resolution;
6+
import net.sourceforge.stripes.action.UrlBinding;
7+
8+
@UrlBinding(SoyOnlineActionBean.BASE_PATH)
9+
public class HomeActionBean extends SoyOnlineActionBean {
10+
11+
/**
12+
* Displays the main compilation page.
13+
*
14+
* @return The main JSP resolution.
15+
*/
16+
@DefaultHandler
17+
public Resolution showCompiler() {
18+
return new ForwardResolution("/jsp/home.jsp");
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package fr.airv.web.soyonline.actions;
2+
3+
import net.sourceforge.stripes.action.ActionBean;
4+
import net.sourceforge.stripes.action.ActionBeanContext;
5+
6+
/**
7+
* Base class for the app's action beans.
8+
* @author herve
9+
*/
10+
public class SoyOnlineActionBean implements ActionBean {
11+
12+
protected static final String BASE_PATH = "/action/";
13+
14+
/**
15+
* The ActionBeanContext instance.
16+
*/
17+
protected ActionBeanContext ctx;
18+
19+
@Override
20+
public void setContext(ActionBeanContext context) {
21+
ctx = context;
22+
}
23+
24+
@Override
25+
public ActionBeanContext getContext() {
26+
return ctx;
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package fr.airv.web.soyonline.model;
2+
3+
4+
import java.io.Serializable;
5+
6+
/**
7+
* AJAX request result container.
8+
*
9+
* @author herve
10+
*
11+
*/
12+
public class AjaxResult {
13+
14+
/**
15+
* Flag telling if the result is a success.
16+
*/
17+
private boolean success;
18+
19+
/**
20+
* Content of the response.
21+
*/
22+
private Serializable content;
23+
24+
/**
25+
* Constructor with content.
26+
* @param content Content to send.
27+
*/
28+
public AjaxResult(Serializable content) {
29+
this.setContent(content);
30+
}
31+
32+
/**
33+
* Default constructor.
34+
*/
35+
public AjaxResult() {
36+
}
37+
38+
public boolean isSuccess() {
39+
return success;
40+
}
41+
42+
public void setSuccess(boolean success) {
43+
this.success = success;
44+
}
45+
46+
public Object getContent() {
47+
return content;
48+
}
49+
50+
public void setContent(Serializable content) {
51+
this.content = content;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package fr.airv.web.soyonline.services;
2+
3+
4+
import com.google.template.soy.base.SoySyntaxException;
5+
6+
/**
7+
* Compilation error exception.
8+
* @author herve
9+
*/
10+
public class CompilationException extends Exception {
11+
12+
/**
13+
* Serial UID.
14+
*/
15+
private static final long serialVersionUID = 3102386191894455147L;
16+
17+
/**
18+
* Source file where the error was detected.
19+
*/
20+
private String source;
21+
22+
/**
23+
* Line number for the error.
24+
*/
25+
private int lineNumber;
26+
27+
/**
28+
* Builds a compilation error.
29+
* @param soyEx The original compilation error.
30+
*/
31+
public CompilationException(SoySyntaxException soyEx) {
32+
super(soyEx.getMessage(), soyEx);
33+
this.setSource(soyEx.getSourceLocation().getFilePath());
34+
this.setLineNumber(soyEx.getSourceLocation().getLineNumber());
35+
}
36+
37+
public int getLineNumber() {
38+
return lineNumber;
39+
}
40+
41+
public void setLineNumber(int lineNumber) {
42+
this.lineNumber = lineNumber;
43+
}
44+
45+
public String getSource() {
46+
return source;
47+
}
48+
49+
public void setSource(String source) {
50+
this.source = source;
51+
}
52+
}

0 commit comments

Comments
 (0)