Skip to content
This repository was archived by the owner on Jan 20, 2022. It is now read-only.

Commit 7fe8d3a

Browse files
committed
Merge pull request #17 from asebak/code-standard-cleanup
code clean up
2 parents 51abcab + 6243367 commit 7fe8d3a

30 files changed

Lines changed: 158 additions & 179 deletions

src/com/atsebak/ui5/AppType.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.atsebak.ui5;
2+
3+
public enum AppType {
4+
DESKTOP,
5+
MOBILE
6+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.atsebak.ui5.config;
1+
package com.atsebak.ui5;
22

3-
public enum UI5Type {
3+
public enum FileType {
44
JS,
55
XML,
66
HTML,
Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.atsebak.ui5.autogeneration;
22

3-
import com.atsebak.ui5.config.UI5Library;
3+
import com.atsebak.ui5.AppType;
44
import org.jetbrains.annotations.NotNull;
55

66
import java.util.HashMap;
@@ -14,110 +14,93 @@ public final class CodeGenerator {
1414
* @return
1515
*/
1616
public String createControllerCode(@NotNull final String root) {
17-
return Template.builder().name("templates/controller.js.ftl").classContext(this.getClass())
18-
.data(new HashMap<String, Object>() {{
19-
put("root", root);
20-
}})
21-
.build()
22-
.toString();
17+
return createTemplate("templates/controller.js.ftl", new HashMap<String, Object>() {{
18+
put("root", root);
19+
}});
2320
}
2421

2522
/**
2623
* Index.html code
2724
*
28-
* @param ui5Library
25+
* @param appType
2926
* @param rootModuleName
3027
* @param intialViewExt
3128
* @return
3229
*/
33-
public String createIndexCode(@NotNull UI5Library ui5Library, @NotNull final String rootModuleName, @NotNull final String intialViewExt) {
34-
String templateLocation = "";
35-
switch (ui5Library) {
36-
case DESKTOP:
37-
templateLocation = "templates/desktop/index.html.ftl";
38-
break;
39-
case MOBILE:
40-
templateLocation = "templates/mobile/index.html.ftl";
41-
break;
42-
}
43-
44-
return Template.builder().name(templateLocation).classContext(this.getClass())
45-
.data(new HashMap<String, Object>() {{
46-
put("rootModuleName", rootModuleName);
47-
put("intialViewExt", intialViewExt.toUpperCase());
48-
}})
49-
.build()
50-
.toString();
30+
public String createIndexCode(@NotNull AppType appType, @NotNull final String rootModuleName, @NotNull final String intialViewExt) {
31+
return createTemplate("templates/" + appType.name().toLowerCase() + "/index.html.ftl", new HashMap<String, Object>() {{
32+
put("rootModuleName", rootModuleName);
33+
put("intialViewExt", intialViewExt.toUpperCase());
34+
}});
5135
}
5236

5337
/**
5438
* HTML Code
5539
*
56-
* @param ui5Library
40+
* @param appType
5741
* @param controllerPath
5842
* @return
5943
*/
60-
public String createHtmlViewCode(@NotNull UI5Library ui5Library, @NotNull String controllerPath) {
61-
return getGeneratedCodeForView(ui5Library, controllerPath, "html");
44+
public String createHtmlViewCode(@NotNull AppType appType, @NotNull String controllerPath) {
45+
return getGeneratedCodeForView(appType, controllerPath, "html");
6246
}
6347

6448
/**
6549
* JS Code
6650
*
67-
* @param ui5Library
51+
* @param appType
6852
* @param controllerPath
6953
* @return
7054
*/
71-
public String createJavascriptViewCode(@NotNull UI5Library ui5Library, @NotNull String controllerPath){
72-
return getGeneratedCodeForView(ui5Library, controllerPath, "js");
55+
public String createJavascriptViewCode(@NotNull AppType appType, @NotNull String controllerPath){
56+
return getGeneratedCodeForView(appType, controllerPath, "js");
7357
}
7458

7559
/**
7660
* XML Code
7761
*
78-
* @param ui5Library
62+
* @param appType
7963
* @param controllerPath
8064
* @return
8165
*/
82-
public String createXmlViewCode(@NotNull UI5Library ui5Library, @NotNull String controllerPath) {
83-
return getGeneratedCodeForView(ui5Library, controllerPath, "xml");
66+
public String createXmlViewCode(@NotNull AppType appType, @NotNull String controllerPath) {
67+
return getGeneratedCodeForView(appType, controllerPath, "xml");
8468
}
8569

8670
/**
8771
* JSON Code
8872
*
89-
* @param ui5Library
73+
* @param appType
9074
* @param controllerPath
9175
* @return
9276
*/
93-
public String createJsonViewCode(@NotNull UI5Library ui5Library,@NotNull String controllerPath) {
94-
return getGeneratedCodeForView(ui5Library, controllerPath, "json");
77+
public String createJsonViewCode(@NotNull AppType appType,@NotNull String controllerPath) {
78+
return getGeneratedCodeForView(appType, controllerPath, "json");
9579
}
9680

9781
/**
9882
* Generic method to autogenerate code based on file template extension
9983
*
100-
* @param ui5Library
84+
* @param appType
10185
* @param controllerPath
10286
* @param ext
10387
* @return
10488
*/
105-
private String getGeneratedCodeForView(@NotNull UI5Library ui5Library, @NotNull final String controllerPath,@NotNull String ext) {
106-
String templateLocation = "";
107-
switch (ui5Library) {
108-
case DESKTOP:
109-
templateLocation = "templates/desktop/view." + ext + ".ftl";
110-
break;
111-
case MOBILE:
112-
templateLocation = "templates/mobile/view." + ext + ".ftl";
113-
break;
114-
}
89+
private String getGeneratedCodeForView(@NotNull AppType appType, @NotNull final String controllerPath,@NotNull String ext) {
90+
return createTemplate("templates/" + appType.name().toLowerCase() + "/view." + ext + ".ftl", new HashMap<String, Object>() {{
91+
put("controllerPath", controllerPath);
92+
}});
93+
}
11594

116-
return Template.builder().name(templateLocation).classContext(this.getClass())
117-
.data(new HashMap<String, Object>() {{
118-
put("controllerPath", controllerPath);
119-
}})
120-
.build()
95+
/**
96+
* Template Builder class
97+
* @param templateName
98+
* @param replacements
99+
* @return
100+
*/
101+
private String createTemplate(String templateName, HashMap<String, Object> replacements) {
102+
return Template.builder().name(templateName).classContext(this.getClass())
103+
.data(replacements).build()
121104
.toString();
122105
}
123106
}

src/com/atsebak/ui5/autogeneration/HTMLView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.atsebak.ui5.autogeneration;
22

3-
import com.atsebak.ui5.config.UI5Library;
3+
import com.atsebak.ui5.AppType;
44
import org.jetbrains.annotations.NotNull;
55

66
public class HTMLView extends View implements UI5View {
@@ -11,7 +11,7 @@ public String getExtension() {
1111

1212
@NotNull
1313
@Override
14-
public String autogenerateCode(@NotNull UI5Library ui5Library, @NotNull String controllerPath) {
14+
public String autogenerateCode(@NotNull AppType ui5Library, @NotNull String controllerPath) {
1515
return getCodeGenerator().createHtmlViewCode(ui5Library, controllerPath);
1616
}
1717

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.atsebak.ui5.autogeneration;
22

3-
import com.atsebak.ui5.config.UI5Library;
3+
import com.atsebak.ui5.AppType;
44
import org.jetbrains.annotations.NotNull;
55

66
public class Index extends View {
77
@NotNull
8-
public String createIndexCode(@NotNull UI5Library ui5Library, @NotNull String rootModuleName, @NotNull String initialViewExt) {
8+
public String createIndexCode(@NotNull AppType ui5Library, @NotNull String rootModuleName, @NotNull String initialViewExt) {
99
return getCodeGenerator().createIndexCode(ui5Library, rootModuleName, initialViewExt);
1010
}
1111
}

src/com/atsebak/ui5/autogeneration/JSONView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.atsebak.ui5.autogeneration;
22

3-
import com.atsebak.ui5.config.UI5Library;
3+
import com.atsebak.ui5.AppType;
44
import org.jetbrains.annotations.NotNull;
55

66
public class JSONView extends View implements UI5View {
@@ -11,7 +11,7 @@ public String getExtension() {
1111

1212
@NotNull
1313
@Override
14-
public String autogenerateCode(@NotNull UI5Library ui5Library, @NotNull String controllerPath) {
14+
public String autogenerateCode(@NotNull AppType ui5Library, @NotNull String controllerPath) {
1515
return getCodeGenerator().createJsonViewCode(ui5Library, controllerPath);
1616
}
1717

src/com/atsebak/ui5/autogeneration/JSView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.atsebak.ui5.autogeneration;
22

3-
import com.atsebak.ui5.config.UI5Library;
3+
import com.atsebak.ui5.AppType;
44
import org.jetbrains.annotations.NotNull;
55

66
public class JSView extends View implements UI5View {
@@ -11,7 +11,7 @@ public String getExtension() {
1111

1212
@NotNull
1313
@Override
14-
public String autogenerateCode(@NotNull UI5Library ui5Library, @NotNull String controllerPath) {
14+
public String autogenerateCode(@NotNull AppType ui5Library, @NotNull String controllerPath) {
1515
return getCodeGenerator().createJavascriptViewCode(ui5Library, controllerPath);
1616
}
1717

src/com/atsebak/ui5/autogeneration/Template.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import freemarker.template.Configuration;
55
import lombok.Builder;
6+
import lombok.SneakyThrows;
67

78
import java.io.StringWriter;
89
import java.io.Writer;
@@ -15,20 +16,15 @@ public class Template {
1516
private Class<?> classContext;
1617
private String name;
1718

19+
@SneakyThrows(Exception.class)
1820
@Override
1921
public String toString() {
2022
Configuration configuration = new Configuration();
2123
configuration.setClassForTemplateLoading(classContext, "/");
22-
try {
23-
freemarker.template.Template template = configuration.getTemplate(name);
24-
Writer writer = new StringWriter();
25-
template.process(data, writer);
26-
return writer.toString();
27-
28-
} catch (Exception e) {
29-
30-
}
31-
return "";
24+
freemarker.template.Template template = configuration.getTemplate(name);
25+
Writer writer = new StringWriter();
26+
template.process(data, writer);
27+
return writer.toString();
3228
}
3329

3430
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.atsebak.ui5.autogeneration;
22

3-
import com.atsebak.ui5.config.UI5Library;
3+
import com.atsebak.ui5.AppType;
44

55
public interface UI5View {
66
String getExtension();
7-
String autogenerateCode(UI5Library ui5Library, String controllerPath);
7+
String autogenerateCode(AppType ui5Library, String controllerPath);
88
}

src/com/atsebak/ui5/autogeneration/XMLView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.atsebak.ui5.autogeneration;
22

3-
import com.atsebak.ui5.config.UI5Library;
3+
import com.atsebak.ui5.AppType;
44
import org.jetbrains.annotations.NotNull;
55

66

@@ -11,7 +11,7 @@ public String getExtension() {
1111
}
1212

1313
@Override
14-
public String autogenerateCode(@NotNull UI5Library ui5Library, @NotNull String controllerPath) {
14+
public String autogenerateCode(@NotNull AppType ui5Library, @NotNull String controllerPath) {
1515
return getCodeGenerator().createXmlViewCode(ui5Library, controllerPath);
1616
}
1717

0 commit comments

Comments
 (0)