1- package io .github .jeemv .springboot .vuejs .utilities ;
1+ package io .github .jeemv .springboot .vuejs .utilities . resources ;
22
3- import java .io .ByteArrayOutputStream ;
43import java .io .IOException ;
5- import java .io .InputStream ;
64import java .util .HashMap ;
75import 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 */
2418public 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 */
0 commit comments