1
- package io .github .jeemv .springboot .vuejs .utilities ;
1
+ package io .github .jeemv .springboot .vuejs .utilities . resources ;
2
2
3
- import java .io .ByteArrayOutputStream ;
4
3
import java .io .IOException ;
5
- import java .io .InputStream ;
6
4
import java .util .HashMap ;
7
5
import java .util .Map ;
8
6
9
- import org .springframework .core .io .ClassPathResource ;
10
- import org .springframework .core .io .Resource ;
11
7
12
- import com .fasterxml .jackson .core .type .TypeReference ;
13
- import com .fasterxml .jackson .databind .ObjectMapper ;
14
8
15
9
/**
16
10
* JavascriptResource This class is part of springBoot-VueJS Allows to
17
11
* Externalize a js script (from resources/static/js) and to use java variables
18
12
* in it.
19
13
*
20
14
* @author jcheron myaddressmail@gmail.com
21
- * @version 1.0 .0
15
+ * @version 1.1 .0
22
16
*
23
17
*/
24
18
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 ;
29
20
public JavascriptResource () {
30
21
this (new HashMap <>());
31
22
}
32
23
33
24
public JavascriptResource (Map <String , Object > variables ) {
34
- this .variables = variables ;
25
+ this .jsResourceElement = new JsResourceElement ( variables ) ;
35
26
}
36
27
37
28
/**
38
29
* Adds java content identified by its name.
39
30
*
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
43
36
* @return the previous value associated with name, or null if there was no
44
37
* mapping for name.
45
38
*/
46
39
public Object put (String name , Object value ) {
47
- return variables .put (name , value );
40
+ return jsResourceElement .put (name , value );
48
41
}
49
42
50
43
/**
51
44
* Adds an array of names,values to the resource.
52
45
*
53
- * @param keyValues Sample new Object[][] { {"id",5},{"name","doe"} }
46
+ * @param keyValues
47
+ * Sample new Object[][] { {"id",5},{"name","doe"} }
54
48
*/
55
49
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 );
61
51
}
62
52
63
53
/**
64
54
* Adds names,values of contents to the resource from a json array.
65
55
*
66
- * @param jsonString sample "{'id':5}"
56
+ * @param jsonString
57
+ * sample "{'id':5}"
67
58
* @throws IOException
68
59
*/
69
60
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 );
76
62
}
77
63
78
64
/**
@@ -82,26 +68,19 @@ public void addVariables(String jsonString) throws IOException {
82
68
* @throws IOException
83
69
*/
84
70
public void addVariables (Object o ) throws IOException {
85
- addVariables (new ObjectMapper (). writeValueAsString ( o ) );
71
+ jsResourceElement . addVariables (o );
86
72
}
87
73
88
74
/**
89
75
* Loads a javascript file.
90
76
*
91
- * @param filename The name of the js file
77
+ * @param filename
78
+ * The name of the js file
92
79
* @throws IOException
93
80
*/
94
81
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 ));
105
84
}
106
85
107
86
/**
@@ -110,20 +89,17 @@ public void loadFile(String filename) throws IOException {
110
89
* @return The javascript parsed with the java contents
111
90
*/
112
91
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 ();
119
93
}
120
94
121
95
/**
122
96
* Returns the javascript content of a javascript file parsed with java
123
97
* contents.
124
98
*
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.
127
103
* @return The javascript content of the file parsed with the java contents
128
104
* @throws IOException
129
105
*/
@@ -136,9 +112,11 @@ public static String load(String filename, Map<String, Object> variables) throws
136
112
/**
137
113
* Creates and returns a javascript resource.
138
114
*
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
142
120
* @return A Javascript resource
143
121
* @throws IOException
144
122
*/
0 commit comments