Skip to content

Commit 8bec621

Browse files
committed
Add Javascript multi modules resource
1 parent 9e93739 commit 8bec621

File tree

4 files changed

+263
-53
lines changed

4 files changed

+263
-53
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package io.github.jeemv.springboot.vuejs.utilities.resources;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.regex.Matcher;
7+
import java.util.regex.Pattern;
8+
9+
/**
10+
* A single Javascript file (from resources/static/js) with many modules and java variables.
11+
* JavascriptMultiModulesResource
12+
* This class is part of springBoot-VueJS
13+
* @author jc
14+
* @version 1.0.0
15+
*
16+
*/
17+
public class JavascriptMultiModulesResource {
18+
public static String moduleSequenceStart="//";
19+
public static String moduleSequenceEnd="//";
20+
private Map<String, JsResourceElement> jsResourceElements;
21+
private String jsContent;
22+
23+
public JavascriptMultiModulesResource() {
24+
jsResourceElements=new HashMap<>();
25+
}
26+
27+
private String getModuleCode(String moduleName) throws Exception {
28+
Pattern pattern = Pattern.compile(Pattern.quote(moduleSequenceStart)+"(?:.*?)"+moduleName+"(?:.*?)"+Pattern.quote("\r\n")+"(.*?)"+Pattern.quote("\r\n"+moduleSequenceEnd)+"(?:.*?)"+moduleName,Pattern.DOTALL);
29+
Matcher matcher = pattern.matcher(jsContent);
30+
if (matcher.find()) {
31+
String s=matcher.group(1);
32+
Pattern p = Pattern.compile("(?i)^("+Pattern.quote("(function() {")+")+");
33+
Matcher m = p.matcher(s);
34+
if (m.find()) {
35+
s = m.replaceAll("");
36+
Pattern pl = Pattern.compile("(?i)("+Pattern.quote("})();")+")$");
37+
Matcher ml = pl.matcher(s);
38+
if(ml.find()) {
39+
s = ml.replaceAll("");
40+
}else {
41+
throw new Exception("The module "+moduleName+" contains malformed scripts!");
42+
}
43+
}
44+
return s;
45+
}
46+
throw new Exception("module "+moduleName+"not found!");
47+
48+
}
49+
50+
/**
51+
* Gets a module by its name
52+
* @param moduleName The module name, defined by // moduleName
53+
* @return
54+
*/
55+
public JsResourceElement getModule(String moduleName) {
56+
if(!jsResourceElements.containsKey(moduleName)) {
57+
jsResourceElements.put(moduleName, new JsResourceElement());
58+
}
59+
return jsResourceElements.get(moduleName);
60+
}
61+
62+
public String parseContent(String moduleName) throws Exception {
63+
JsResourceElement element=jsResourceElements.get(moduleName);
64+
element.setJsContent(getModuleCode(moduleName));
65+
return element.parseContent();
66+
}
67+
68+
/**
69+
* Loads a javascript file.
70+
*
71+
* @param filename
72+
* The name of the js file
73+
* @throws IOException
74+
*/
75+
public void loadFile(String filename) throws IOException {
76+
JsResourceLoader loader=new JsResourceLoader();
77+
jsContent=loader.loadFile(filename);
78+
}
79+
80+
/**
81+
* Creates and returns a javascript multi modules resource.
82+
*
83+
* @param filename
84+
* @return The javascript file to load
85+
* @throws IOException
86+
*/
87+
public static JavascriptMultiModulesResource create(String filename) throws IOException {
88+
JavascriptMultiModulesResource jsFile = new JavascriptMultiModulesResource();
89+
jsFile.loadFile(filename);
90+
return jsFile;
91+
}
92+
}

src/main/java/io/github/jeemv/springboot/vuejs/utilities/JavascriptResource.java renamed to src/main/java/io/github/jeemv/springboot/vuejs/utilities/resources/JavascriptResource.java

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,64 @@
1-
package io.github.jeemv.springboot.vuejs.utilities;
1+
package io.github.jeemv.springboot.vuejs.utilities.resources;
22

3-
import java.io.ByteArrayOutputStream;
43
import java.io.IOException;
5-
import java.io.InputStream;
64
import java.util.HashMap;
75
import java.util.Map;
86

9-
import org.springframework.core.io.ClassPathResource;
10-
import org.springframework.core.io.Resource;
117

12-
import com.fasterxml.jackson.core.type.TypeReference;
13-
import com.fasterxml.jackson.databind.ObjectMapper;
148

159
/**
1610
* JavascriptResource This class is part of springBoot-VueJS Allows to
1711
* Externalize a js script (from resources/static/js) and to use java variables
1812
* in it.
1913
*
2014
* @author jcheron myaddressmail@gmail.com
21-
* @version 1.0.0
15+
* @version 1.1.0
2216
*
2317
*/
2418
public class JavascriptResource {
25-
private static final String ROOT_FOLDER = "static/js/";
26-
private String jsContent;
27-
private Map<String, Object> variables;
28-
19+
private JsResourceElement jsResourceElement;
2920
public JavascriptResource() {
3021
this(new HashMap<>());
3122
}
3223

3324
public JavascriptResource(Map<String, Object> variables) {
34-
this.variables = variables;
25+
this.jsResourceElement=new JsResourceElement(variables);
3526
}
3627

3728
/**
3829
* Adds java content identified by its name.
3930
*
40-
* @param name The name of the java content (use ${name} in the js file to
41-
* refer to it.
42-
* @param value The java content
31+
* @param name
32+
* The name of the java content (use ${name} in the js file to
33+
* refer to it.
34+
* @param value
35+
* The java content
4336
* @return the previous value associated with name, or null if there was no
4437
* mapping for name.
4538
*/
4639
public Object put(String name, Object value) {
47-
return variables.put(name, value);
40+
return jsResourceElement.put(name, value);
4841
}
4942

5043
/**
5144
* Adds an array of names,values to the resource.
5245
*
53-
* @param keyValues Sample new Object[][] { {"id",5},{"name","doe"} }
46+
* @param keyValues
47+
* Sample new Object[][] { {"id",5},{"name","doe"} }
5448
*/
5549
public void addVariables(Object[][] keyValues) {
56-
for (Object[] array : keyValues) {
57-
if (array.length == 2) {
58-
variables.put(array[0] + "", array[1]);
59-
}
60-
}
50+
jsResourceElement.addVariables(keyValues);
6151
}
6252

6353
/**
6454
* Adds names,values of contents to the resource from a json array.
6555
*
66-
* @param jsonString sample "{'id':5}"
56+
* @param jsonString
57+
* sample "{'id':5}"
6758
* @throws IOException
6859
*/
6960
public void addVariables(String jsonString) throws IOException {
70-
ObjectMapper mapper = new ObjectMapper();
71-
Map<String, Object> map = mapper.readValue(jsonString, new TypeReference<Map<String, Object>>() {
72-
});
73-
for (String k : map.keySet()) {
74-
variables.put(k, map.get(k));
75-
}
61+
jsResourceElement.addVariables(jsonString);
7662
}
7763

7864
/**
@@ -82,26 +68,19 @@ public void addVariables(String jsonString) throws IOException {
8268
* @throws IOException
8369
*/
8470
public void addVariables(Object o) throws IOException {
85-
addVariables(new ObjectMapper().writeValueAsString(o));
71+
jsResourceElement.addVariables(o);
8672
}
8773

8874
/**
8975
* Loads a javascript file.
9076
*
91-
* @param filename The name of the js file
77+
* @param filename
78+
* The name of the js file
9279
* @throws IOException
9380
*/
9481
public void loadFile(String filename) throws IOException {
95-
filename = ROOT_FOLDER + filename + ".js";
96-
Resource resource = new ClassPathResource(filename);
97-
InputStream resourceInputStream = resource.getInputStream();
98-
ByteArrayOutputStream result = new ByteArrayOutputStream();
99-
byte[] buffer = new byte[1024];
100-
int length;
101-
while ((length = resourceInputStream.read(buffer)) != -1) {
102-
result.write(buffer, 0, length);
103-
}
104-
jsContent = result.toString("UTF-8");
82+
JsResourceLoader loader=new JsResourceLoader();
83+
jsResourceElement.setJsContent(loader.loadFile(filename));
10584
}
10685

10786
/**
@@ -110,20 +89,17 @@ public void loadFile(String filename) throws IOException {
11089
* @return The javascript parsed with the java contents
11190
*/
11291
public String parseContent() {
113-
String res = jsContent;
114-
for (String k : variables.keySet()) {
115-
Object v = variables.get(k);
116-
res = res.replaceAll("\\$\\{" + k + "\\}", v + "");
117-
}
118-
return res;
92+
return jsResourceElement.parseContent();
11993
}
12094

12195
/**
12296
* Returns the javascript content of a javascript file parsed with java
12397
* contents.
12498
*
125-
* @param filename The javascript file to load
126-
* @param variables The java contents to be parsed.
99+
* @param filename
100+
* The javascript file to load
101+
* @param variables
102+
* The java contents to be parsed.
127103
* @return The javascript content of the file parsed with the java contents
128104
* @throws IOException
129105
*/
@@ -136,9 +112,11 @@ public static String load(String filename, Map<String, Object> variables) throws
136112
/**
137113
* Creates and returns a javascript resource.
138114
*
139-
* @param filename The javascript file to load
140-
* @param variables The javascript content of the file parsed with the java
141-
* contents
115+
* @param filename
116+
* The javascript file to load
117+
* @param variables
118+
* The javascript content of the file parsed with the java
119+
* contents
142120
* @return A Javascript resource
143121
* @throws IOException
144122
*/
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package io.github.jeemv.springboot.vuejs.utilities.resources;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import com.fasterxml.jackson.core.type.TypeReference;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
10+
/**
11+
* JsResourceElement
12+
* This class is part of springBoot-VueJS
13+
* @author jc
14+
* @version 1.0.0
15+
*
16+
*/
17+
public class JsResourceElement {
18+
private String jsContent;
19+
private Map<String, Object> variables;
20+
public JsResourceElement() {
21+
this(new HashMap<>());
22+
}
23+
24+
public JsResourceElement(Map<String, Object> variables) {
25+
this.variables = variables;
26+
}
27+
28+
/**
29+
* Adds java content identified by its name.
30+
*
31+
* @param name
32+
* The name of the java content (use ${name} in the js file to
33+
* refer to it.
34+
* @param value
35+
* The java content
36+
* @return the previous value associated with name, or null if there was no
37+
* mapping for name.
38+
*/
39+
public Object put(String name, Object value) {
40+
return variables.put(name, value);
41+
}
42+
43+
/**
44+
* Adds an array of names,values to the resource.
45+
*
46+
* @param keyValues
47+
* Sample new Object[][] { {"id",5},{"name","doe"} }
48+
*/
49+
public void addVariables(Object[][] keyValues) {
50+
for (Object[] array : keyValues) {
51+
if (array.length == 2) {
52+
variables.put(array[0] + "", array[1]);
53+
}
54+
}
55+
}
56+
57+
/**
58+
* Adds names,values of contents to the resource from a json array.
59+
*
60+
* @param jsonString
61+
* sample "{'id':5}"
62+
* @throws IOException
63+
*/
64+
public void addVariables(String jsonString) throws IOException {
65+
ObjectMapper mapper = new ObjectMapper();
66+
Map<String, Object> map = mapper.readValue(jsonString, new TypeReference<Map<String, Object>>() {
67+
});
68+
for (String k : map.keySet()) {
69+
variables.put(k, map.get(k));
70+
}
71+
}
72+
73+
/**
74+
* Adds all members name/value of an object to java contents.
75+
*
76+
* @param o
77+
* @throws IOException
78+
*/
79+
public void addVariables(Object o) throws IOException {
80+
addVariables(new ObjectMapper().writeValueAsString(o));
81+
}
82+
83+
/**
84+
* Returns the javascript content of the file parsed with the java contents.
85+
*
86+
* @return The javascript parsed with the java contents
87+
*/
88+
public String parseContent() {
89+
String res = jsContent;
90+
for (String k : variables.keySet()) {
91+
Object v = variables.get(k);
92+
res = res.replaceAll("\\$\\{" + k + "\\}", v + "");
93+
}
94+
return res;
95+
}
96+
97+
public void setJsContent(String jsContent) {
98+
this.jsContent = jsContent;
99+
}
100+
}

0 commit comments

Comments
 (0)