1
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 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 website = new URL(url);
42
43 URLConnection connection = website.openConnection();
44 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
45
46 StringBuilder response = new StringBuilder();
47 String inputLine;
48
49 while ((inputLine = in.readLine()) != null)
50 response.append(inputLine);
51
52 in.close();
53
54 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 String result = null;
69
70 if (language == null)
71 {
72 try
73 {
74 return getRemoteTemplate(url);
75 }
76 catch (Exception e)
77 {
78 DIFLogger.getLogger().debug(e);
79 return null;
80 }
81 }
82
83
84 try
85 {
86 result = getRemoteTemplate(url + "." + language);
87 }
88 catch (Exception e)
89 {
90 DIFLogger.getLogger().debug(e);
91 }
92
93 if (result != null)
94 {
95 return result;
96 }
97 else
98 {
99
100 try
101 {
102 result = getRemoteTemplate(url + "."
103 + DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage());
104 }
105 catch (Exception e)
106 {
107 DIFLogger.getLogger().debug(e);
108 }
109 }
110
111 if (result != null)
112 {
113 return result;
114 }
115 else
116 {
117
118 try
119 {
120 result = getRemoteTemplate(url);
121 }
122 catch (Exception e)
123 {
124 DIFLogger.getLogger().debug(e);
125 }
126 }
127
128 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 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
146
147 StringBuffer buffer = new StringBuffer();
148 String line = parseTemplateLine(bufferedReader.readLine(), params);
149 while (line != null)
150 {
151 buffer.append(line);
152 line = parseTemplateLine(bufferedReader.readLine(), params);
153 }
154
155 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 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 if (language != null)
192 {
193 InputStream fileInput = Thread.currentThread().getContextClassLoader()
194 .getResourceAsStream(resource + "." + language);
195
196 if (fileInput == null)
197 fileInput = Thread
198 .currentThread()
199 .getContextClassLoader()
200 .getResourceAsStream(
201 resource + "." + DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage());
202
203 if (fileInput == null)
204 fileInput = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
205
206 return getTemplateContent(fileInput, params);
207 }
208 else
209 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 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 if (line != null && !"".equals(line) && params != null)
239 {
240 for (Entry<String, String> param: params.entrySet())
241 line = line.replaceAll("\\$\\{" + param.getKey() + "\\}", param.getValue());
242 }
243
244 return line;
245 }
246 }