1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.web; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.StringReader; |
24 |
|
import java.text.MessageFormat; |
25 |
|
import java.util.ArrayList; |
26 |
|
import java.util.Arrays; |
27 |
|
import java.util.Date; |
28 |
|
import java.util.HashMap; |
29 |
|
import java.util.HashSet; |
30 |
|
import java.util.List; |
31 |
|
import java.util.Map; |
32 |
|
import java.util.Properties; |
33 |
|
import java.util.ResourceBundle; |
34 |
|
import java.util.Set; |
35 |
|
|
36 |
|
import org.apache.commons.lang3.ArrayUtils; |
37 |
|
import org.slf4j.Logger; |
38 |
|
import org.slf4j.LoggerFactory; |
39 |
|
import org.xwiki.localization.ContextualLocalizationManager; |
40 |
|
|
41 |
|
import com.xpn.xwiki.XWikiContext; |
42 |
|
import com.xpn.xwiki.XWikiException; |
43 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
@version |
61 |
|
@deprecated@link |
62 |
|
|
63 |
|
@Deprecated |
|
|
| 85.6% |
Uncovered Elements: 20 (139) |
Complexity: 37 |
Complexity Density: 0.41 |
|
64 |
|
public class XWikiMessageTool |
65 |
|
{ |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(XWikiMessageTool.class); |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private static final String KEY = "documentBundles"; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
private static final String LOAD_ERROR_MSG_FMT = "Failed to load internationalization document bundle [ %s ]."; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
protected ResourceBundle bundle; |
86 |
|
|
87 |
|
|
88 |
|
@link |
89 |
|
|
90 |
|
@Deprecated |
91 |
|
protected XWikiContext context; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
private Map<Long, Properties> propsCache = new HashMap<Long, Properties>(); |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
private Map<Long, Date> previousDates = new HashMap<Long, Date>(); |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
private Set<Long> docsToRefresh = new HashSet<Long>(); |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
private ContextualLocalizationManager localization; |
117 |
|
|
118 |
|
|
119 |
|
@param |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
121 |
11354 |
public XWikiMessageTool(ContextualLocalizationManager localization)... |
122 |
|
{ |
123 |
11351 |
this.localization = localization; |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
@param |
128 |
|
@param@link |
129 |
|
|
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
131 |
33 |
public XWikiMessageTool(ResourceBundle bundle, XWikiContext context)... |
132 |
|
{ |
133 |
33 |
this.bundle = bundle; |
134 |
33 |
this.context = context; |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
137 |
47 |
protected XWikiContext getXWikiContext()... |
138 |
|
{ |
139 |
47 |
return this.context; |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
@param |
144 |
|
@return |
145 |
|
@link |
146 |
|
@see |
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
148 |
2020 |
public String get(String key)... |
149 |
|
{ |
150 |
2020 |
String translation; |
151 |
2020 |
if (this.localization != null) { |
152 |
2004 |
translation = get(key, ArrayUtils.EMPTY_OBJECT_ARRAY); |
153 |
|
} else { |
154 |
16 |
translation = getTranslation(key); |
155 |
16 |
if (translation == null) { |
156 |
4 |
try { |
157 |
4 |
translation = this.bundle.getString(key); |
158 |
|
} catch (Exception e) { |
159 |
3 |
translation = key; |
160 |
|
} |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
2020 |
return translation; |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
@link |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
@param |
177 |
|
@param |
178 |
|
@link |
179 |
|
@return |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
181 |
1 |
public String get(String key, List<?> params)... |
182 |
|
{ |
183 |
1 |
return get(key, params.toArray()); |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
@link |
189 |
|
|
190 |
|
@param |
191 |
|
@param |
192 |
|
@link |
193 |
|
@return |
194 |
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 5 |
Complexity Density: 0.56 |
|
195 |
2006 |
public String get(String key, Object... params)... |
196 |
|
{ |
197 |
2006 |
String translation; |
198 |
2006 |
if (this.localization != null) { |
199 |
2005 |
translation = this.localization.getTranslationPlain(key, params); |
200 |
2005 |
if (translation == null) { |
201 |
16 |
translation = key; |
202 |
|
} |
203 |
|
} else { |
204 |
1 |
translation = get(key); |
205 |
|
|
206 |
1 |
if (params != null && translation != null) { |
207 |
1 |
translation = MessageFormat.format(translation, params); |
208 |
|
} |
209 |
|
} |
210 |
|
|
211 |
2006 |
return translation; |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
|
@return |
216 |
|
|
217 |
|
@see |
218 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
219 |
16 |
protected List<String> getDocumentBundleNames()... |
220 |
|
{ |
221 |
16 |
List<String> docNamesList; |
222 |
|
|
223 |
16 |
XWikiContext context = getXWikiContext(); |
224 |
|
|
225 |
16 |
String docNames = context.getWiki().getXWikiPreference(KEY, context); |
226 |
16 |
if (docNames == null || "".equals(docNames)) { |
227 |
4 |
docNames = context.getWiki().Param("xwiki." + KEY); |
228 |
|
} |
229 |
|
|
230 |
16 |
if (docNames == null) { |
231 |
2 |
docNamesList = new ArrayList<String>(); |
232 |
|
} else { |
233 |
14 |
docNamesList = Arrays.asList(docNames.split(",")); |
234 |
|
} |
235 |
|
|
236 |
16 |
return docNamesList; |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
|
@return@link |
241 |
|
@see |
242 |
|
|
|
|
| 95.2% |
Uncovered Elements: 1 (21) |
Complexity: 4 |
Complexity Density: 0.27 |
|
243 |
16 |
public List<XWikiDocument> getDocumentBundles()... |
244 |
|
{ |
245 |
16 |
XWikiContext context = getXWikiContext(); |
246 |
|
|
247 |
16 |
String defaultLanguage = context.getWiki().getDefaultLanguage(context); |
248 |
16 |
List<XWikiDocument> result = new ArrayList<XWikiDocument>(); |
249 |
16 |
for (String docName : getDocumentBundleNames()) { |
250 |
15 |
for (XWikiDocument docBundle : getDocumentBundles(docName.trim(), defaultLanguage)) { |
251 |
17 |
if (docBundle != null) { |
252 |
17 |
if (!docBundle.isNew()) { |
253 |
|
|
254 |
16 |
Long docId = new Long(docBundle.getId()); |
255 |
16 |
Date docDate = docBundle.getDate(); |
256 |
|
|
257 |
16 |
if (!docDate.equals(this.previousDates.get(docId))) { |
258 |
10 |
this.docsToRefresh.add(docId); |
259 |
10 |
this.previousDates.put(docId, docDate); |
260 |
|
} |
261 |
16 |
result.add(docBundle); |
262 |
|
} else { |
263 |
|
|
264 |
|
|
265 |
1 |
LOGGER.warn("The document [" + docBundle.getFullName() + "] is listed " |
266 |
|
+ "as an internationalization document bundle but it does not " + "exist."); |
267 |
|
} |
268 |
|
} |
269 |
|
} |
270 |
|
} |
271 |
16 |
return result; |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
@param |
279 |
|
@return |
280 |
|
|
281 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
282 |
0 |
public XWikiDocument getDocumentBundle(String documentName)... |
283 |
|
{ |
284 |
0 |
XWikiDocument docBundle; |
285 |
|
|
286 |
0 |
if (documentName.length() == 0) { |
287 |
0 |
docBundle = null; |
288 |
|
} else { |
289 |
0 |
try { |
290 |
0 |
XWikiContext context = getXWikiContext(); |
291 |
|
|
292 |
|
|
293 |
0 |
docBundle = context.getWiki().getDocument(documentName, context); |
294 |
0 |
docBundle = docBundle.getTranslatedDocument(context); |
295 |
|
} catch (XWikiException e) { |
296 |
|
|
297 |
|
|
298 |
|
|
299 |
0 |
LOGGER.error(String.format(LOAD_ERROR_MSG_FMT, documentName), e); |
300 |
0 |
docBundle = null; |
301 |
|
} |
302 |
|
} |
303 |
|
|
304 |
0 |
return docBundle; |
305 |
|
} |
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
@param |
312 |
|
@param |
313 |
|
@return |
314 |
|
|
315 |
|
|
|
|
| 84.2% |
Uncovered Elements: 3 (19) |
Complexity: 5 |
Complexity Density: 0.38 |
|
316 |
15 |
public List<XWikiDocument> getDocumentBundles(String documentName, String defaultLanguage)... |
317 |
|
{ |
318 |
15 |
List<XWikiDocument> list = new ArrayList<XWikiDocument>(); |
319 |
|
|
320 |
15 |
if (documentName.length() != 0) { |
321 |
15 |
try { |
322 |
15 |
XWikiContext context = getXWikiContext(); |
323 |
|
|
324 |
|
|
325 |
15 |
XWikiDocument docBundle = context.getWiki().getDocument(documentName, context); |
326 |
15 |
XWikiDocument tdocBundle = docBundle.getTranslatedDocument(context); |
327 |
15 |
list.add(tdocBundle); |
328 |
15 |
if (!tdocBundle.getRealLanguage().equals(defaultLanguage)) { |
329 |
2 |
XWikiDocument defdocBundle = docBundle.getTranslatedDocument(defaultLanguage, context); |
330 |
2 |
if (tdocBundle != defdocBundle) { |
331 |
2 |
list.add(defdocBundle); |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
|
} catch (XWikiException e) { |
336 |
|
|
337 |
|
|
338 |
|
|
339 |
0 |
LOGGER.error(String.format(LOAD_ERROR_MSG_FMT, documentName), e); |
340 |
|
} |
341 |
|
} |
342 |
|
|
343 |
15 |
return list; |
344 |
|
} |
345 |
|
|
346 |
|
|
347 |
|
@param |
348 |
|
@return |
349 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
350 |
10 |
public Properties getDocumentBundleProperties(XWikiDocument docBundle)... |
351 |
|
{ |
352 |
10 |
Properties props = new Properties(); |
353 |
10 |
String content = docBundle.getContent(); |
354 |
10 |
try { |
355 |
10 |
props.load(new StringReader(content)); |
356 |
|
} catch (IOException e) { |
357 |
0 |
LOGGER.error("Failed to parse content of document [" + docBundle + "] as translation content", e); |
358 |
|
} |
359 |
|
|
360 |
10 |
return props; |
361 |
|
} |
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
@param |
368 |
|
@return |
369 |
|
|
|
|
| 95.7% |
Uncovered Elements: 1 (23) |
Complexity: 6 |
Complexity Density: 0.4 |
|
370 |
16 |
protected String getTranslation(String key)... |
371 |
|
{ |
372 |
16 |
String returnValue = null; |
373 |
|
|
374 |
16 |
if (key != null) { |
375 |
15 |
for (XWikiDocument docBundle : getDocumentBundles()) { |
376 |
15 |
if (docBundle != null) { |
377 |
15 |
Long docId = new Long(docBundle.getId()); |
378 |
15 |
Properties props = null; |
379 |
15 |
if (this.docsToRefresh.contains(docId) || !this.propsCache.containsKey(docId)) { |
380 |
|
|
381 |
10 |
props = getDocumentBundleProperties(docBundle); |
382 |
|
|
383 |
10 |
this.propsCache.put(docId, props); |
384 |
10 |
this.docsToRefresh.remove(docId); |
385 |
|
} else { |
386 |
|
|
387 |
5 |
props = this.propsCache.get(docId); |
388 |
|
} |
389 |
|
|
390 |
15 |
returnValue = props.getProperty(key); |
391 |
15 |
if (returnValue != null) { |
392 |
12 |
break; |
393 |
|
} |
394 |
|
} |
395 |
|
} |
396 |
|
} |
397 |
|
|
398 |
16 |
return returnValue; |
399 |
|
} |
400 |
|
} |