1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.james.util;
22
23 import org.apache.oro.text.perl.MalformedPerl5PatternException;
24 import org.apache.oro.text.perl.Perl5Util;
25 import org.w3c.dom.Attr;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.NamedNodeMap;
29 import org.w3c.dom.NodeList;
30
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import java.io.File;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.Map;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public class XMLResources
78 {
79
80
81
82 private Map m_resource = new HashMap();
83
84
85
86
87 static private Map stringTable = java.util.Collections.synchronizedMap(new HashMap());
88
89
90
91
92 private Perl5Util m_perl5Util = new Perl5Util();
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public void init(File xmlFile, String group,
108 String select, Map configParameters)
109 throws Exception
110 {
111
112 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
113 DocumentBuilder builder = factory.newDocumentBuilder();
114 Document doc = builder.parse(xmlFile);
115
116
117 Element matcherElement =
118 (Element)(doc.getElementsByTagName("matchers").item(0));
119 String selectTag = null;
120 if ( matcherElement != null ) {
121 selectTag = match(select, matcherElement);
122 m_perl5Util = null;
123 }
124
125
126 NodeList sections = doc.getElementsByTagName("group");
127 int sectionsCount = sections.getLength();
128 Element sectionElement = null;
129 for (int i = 0; i < sectionsCount; i++ ) {
130 sectionElement = (Element)(sections.item(i));
131 String sectionName = sectionElement.getAttribute("name");
132 if ( sectionName != null && sectionName.equals(group) ) {
133 break;
134 }
135
136 }
137 if ( sectionElement == null ) {
138 StringBuffer exceptionBuffer =
139 new StringBuffer(64)
140 .append("Error loading string definition file. ")
141 .append("The element named \'")
142 .append(group)
143 .append("\' does not exist.");
144 throw new RuntimeException(exceptionBuffer.toString());
145 }
146
147
148
149 Map parameters = new HashMap();
150
151 Element parametersElement =
152 (Element)(sectionElement.getElementsByTagName("parameters").item(0));
153 if ( parametersElement != null ) {
154 NamedNodeMap params = parametersElement.getAttributes();
155 int paramCount = params.getLength();
156 for (int i = 0; i < paramCount; i++ ) {
157 Attr param = (Attr)params.item(i);
158 String paramName = param.getName();
159 String paramValue = param.getValue();
160 parameters.put(paramName, paramValue);
161 }
162 }
163
164 parameters.putAll(configParameters);
165
166
167
168
169 Map defaultStrings = new HashMap();
170 Map selectTagStrings = new HashMap();
171
172
173
174 NodeList resDefs = sectionElement.getElementsByTagName("resource");
175 int resCount = resDefs.getLength();
176 for ( int i = 0; i < resCount; i++ ) {
177
178 Element resElement = (Element)(resDefs.item(i));
179 String resSelect = resElement.getAttribute("for");
180 Map resMap;
181 if ( resSelect.equals("")) {
182
183 resMap = defaultStrings;
184 }
185 else if (resSelect.equals(selectTag) ) {
186
187 resMap = selectTagStrings;
188 }
189 else {
190
191 continue;
192 }
193
194
195 String resKey = resElement.getAttribute("name");
196 if ( resKey == null ) {
197
198 continue;
199 }
200 String resString = resElement.getFirstChild().getNodeValue();
201
202
203 Iterator paramNames = parameters.keySet().iterator();
204 while ( paramNames.hasNext() ) {
205 String paramName = (String)paramNames.next();
206 String paramValue = (String)parameters.get(paramName);
207
208 StringBuffer replaceBuffer =
209 new StringBuffer(64)
210 .append("${")
211 .append(paramName)
212 .append("}");
213 resString = substituteSubString(resString, replaceBuffer.toString(), paramValue);
214 }
215
216
217 String shared = (String) stringTable.get(resString);
218
219 if (shared == null) {
220 stringTable.put(resString, resString);
221 } else {
222 resString = shared;
223 }
224
225
226 resMap.put(resKey, resString);
227 }
228
229
230 m_resource.putAll(defaultStrings);
231 m_resource.putAll(selectTagStrings);
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246 private String match(String select, Element matchersElement)
247 throws MalformedPerl5PatternException
248 {
249 String selectTagName = select;
250
251 NodeList matchers = matchersElement.getElementsByTagName("matcher");
252 for ( int i = 0; i < matchers.getLength(); i++ ) {
253
254 Element matcher = (Element)matchers.item(i);
255 String matchName = matcher.getAttribute("for");
256 StringBuffer selectTagPatternBuffer =
257 new StringBuffer(64)
258 .append("/")
259 .append(matcher.getAttribute("match"))
260 .append("/i");
261
262
263
264 if ( m_perl5Util.match(selectTagPatternBuffer.toString(), selectTagName) ) {
265 return matchName;
266 }
267 }
268 return null;
269 }
270
271
272
273
274
275
276
277
278 static private String substituteSubString( String input,
279 String find,
280 String replace )
281 {
282 int find_length = find.length();
283 int replace_length = replace.length();
284
285 StringBuffer output = new StringBuffer(input);
286 int index = input.indexOf(find);
287 int outputOffset = 0;
288
289 while ( index > -1 ) {
290 output.replace(index + outputOffset, index + outputOffset + find_length, replace);
291 outputOffset = outputOffset + (replace_length - find_length);
292
293 index = input.indexOf(find, index + find_length);
294 }
295
296 String result = output.toString();
297 return result;
298 }
299
300
301
302
303
304
305
306 public String getString(String name)
307 {
308 return (String)m_resource.get(name);
309 }
310
311
312
313
314
315
316
317
318 public String getString(String name, boolean required)
319 {
320 String str = getString(name);
321
322 if (str == null && required) {
323 StringBuffer exceptionBuffer =
324 new StringBuffer(64)
325 .append("Required String resource: '")
326 .append(name)
327 .append("' was not found.");
328 throw new IllegalArgumentException(exceptionBuffer.toString());
329 }
330 return str;
331 }
332
333
334
335
336
337
338
339
340
341 public String getString(String name, Map parameters)
342 {
343 return replaceParameters(getString(name), parameters);
344 }
345
346
347
348
349
350
351
352
353
354 public String getString(String name, Map parameters, boolean required)
355 {
356 return replaceParameters(getString(name, required), parameters);
357 }
358
359
360
361
362
363
364
365
366
367 static public String replaceParameters(String str, Map parameters)
368 {
369 if (str != null && parameters != null) {
370
371 Iterator paramNames = parameters.keySet().iterator();
372 StringBuffer replaceBuffer = new StringBuffer(64);
373 while ( paramNames.hasNext() ) {
374 String paramName = (String)paramNames.next();
375 String paramValue = (String)parameters.get(paramName);
376 replaceBuffer.append("${").append(paramName).append("}");
377 str = substituteSubString(str, replaceBuffer.toString(), paramValue);
378 if (paramNames.hasNext()) replaceBuffer.setLength(0);
379 }
380 }
381
382 return str;
383 }
384 }