1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
package pt.digitalis.dif.utils.templates; |
7 | |
|
8 | |
import java.io.BufferedReader; |
9 | |
import java.io.IOException; |
10 | |
import java.io.InputStream; |
11 | |
import java.io.InputStreamReader; |
12 | |
import java.net.URL; |
13 | |
import java.net.URLConnection; |
14 | |
import java.util.Map; |
15 | |
import java.util.Map.Entry; |
16 | |
|
17 | |
import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters; |
18 | |
import pt.digitalis.dif.utils.logging.DIFLogger; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class TemplateUtils { |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
static public String getRemoteTemplate(String url) throws Exception |
38 | |
{ |
39 | |
URL website; |
40 | |
|
41 | 0 | website = new URL(url); |
42 | |
|
43 | 0 | URLConnection connection = website.openConnection(); |
44 | 0 | BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
45 | |
|
46 | 0 | StringBuilder response = new StringBuilder(); |
47 | |
String inputLine; |
48 | |
|
49 | 0 | while ((inputLine = in.readLine()) != null) |
50 | 0 | response.append(inputLine); |
51 | |
|
52 | 0 | in.close(); |
53 | |
|
54 | 0 | return response.toString(); |
55 | |
} |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
static public String getRemoteTemplate(String url, String language) |
67 | |
{ |
68 | 0 | String result = null; |
69 | |
|
70 | 0 | if (language == null) |
71 | |
{ |
72 | |
try |
73 | |
{ |
74 | 0 | return getRemoteTemplate(url); |
75 | |
} |
76 | 0 | catch (Exception e) |
77 | |
{ |
78 | 0 | DIFLogger.getLogger().debug(e); |
79 | 0 | return null; |
80 | |
} |
81 | |
} |
82 | |
|
83 | |
|
84 | |
try |
85 | |
{ |
86 | 0 | result = getRemoteTemplate(url + "." + language); |
87 | |
} |
88 | 0 | catch (Exception e) |
89 | |
{ |
90 | 0 | DIFLogger.getLogger().debug(e); |
91 | |
} |
92 | |
|
93 | 0 | if (result != null) |
94 | |
{ |
95 | 0 | return result; |
96 | |
} |
97 | |
else |
98 | |
{ |
99 | |
|
100 | |
try |
101 | |
{ |
102 | 0 | result = getRemoteTemplate(url + "." |
103 | 0 | + DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage()); |
104 | |
} |
105 | 0 | catch (Exception e) |
106 | |
{ |
107 | 0 | DIFLogger.getLogger().debug(e); |
108 | |
} |
109 | |
} |
110 | |
|
111 | 0 | if (result != null) |
112 | |
{ |
113 | 0 | return result; |
114 | |
} |
115 | |
else |
116 | |
{ |
117 | |
|
118 | |
try |
119 | |
{ |
120 | 0 | result = getRemoteTemplate(url); |
121 | |
} |
122 | 0 | catch (Exception e) |
123 | |
{ |
124 | 0 | DIFLogger.getLogger().debug(e); |
125 | |
} |
126 | |
} |
127 | |
|
128 | 0 | return result; |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
static private StringBuffer getTemplateContent(InputStream inputStream, Map<String, String> params) |
143 | |
throws IOException |
144 | |
{ |
145 | 0 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); |
146 | |
|
147 | 0 | StringBuffer buffer = new StringBuffer(); |
148 | 0 | String line = parseTemplateLine(bufferedReader.readLine(), params); |
149 | 0 | while (line != null) |
150 | |
{ |
151 | 0 | buffer.append(line); |
152 | 0 | line = parseTemplateLine(bufferedReader.readLine(), params); |
153 | |
} |
154 | |
|
155 | 0 | return buffer; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
static public StringBuffer getTemplateContent(String resource, Map<String, String> params) throws IOException |
170 | |
{ |
171 | 0 | return getTemplateContent(getTemplateStream(resource), null); |
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
static public StringBuffer getTemplateContent(String resource, String language, Map<String, String> params) |
188 | |
throws IOException |
189 | |
{ |
190 | |
|
191 | 0 | if (language != null) |
192 | |
{ |
193 | 0 | InputStream fileInput = Thread.currentThread().getContextClassLoader() |
194 | 0 | .getResourceAsStream(resource + "." + language); |
195 | |
|
196 | 0 | if (fileInput == null) |
197 | 0 | fileInput = Thread |
198 | 0 | .currentThread() |
199 | 0 | .getContextClassLoader() |
200 | 0 | .getResourceAsStream( |
201 | 0 | resource + "." + DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage()); |
202 | |
|
203 | 0 | if (fileInput == null) |
204 | 0 | fileInput = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); |
205 | |
|
206 | 0 | return getTemplateContent(fileInput, params); |
207 | |
} |
208 | |
else |
209 | 0 | return getTemplateContent(resource, params); |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
static public InputStream getTemplateStream(String resource) throws IOException |
222 | |
{ |
223 | |
|
224 | 0 | return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); |
225 | |
} |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
static public String parseTemplateLine(String line, Map<String, String> params) |
237 | |
{ |
238 | 0 | if (line != null && !"".equals(line) && params != null) |
239 | |
{ |
240 | 0 | for (Entry<String, String> param: params.entrySet()) |
241 | 0 | line = line.replaceAll("\\$\\{" + param.getKey() + "\\}", param.getValue()); |
242 | |
} |
243 | |
|
244 | 0 | return line; |
245 | |
} |
246 | |
} |