1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.localization.jar.internal; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.InputStream; |
24 |
|
import java.net.URL; |
25 |
|
import java.util.Enumeration; |
26 |
|
import java.util.List; |
27 |
|
import java.util.ListIterator; |
28 |
|
import java.util.Locale; |
29 |
|
import java.util.Map; |
30 |
|
import java.util.Properties; |
31 |
|
|
32 |
|
import javax.inject.Inject; |
33 |
|
import javax.inject.Named; |
34 |
|
import javax.inject.Singleton; |
35 |
|
|
36 |
|
import org.apache.commons.collections.EnumerationUtils; |
37 |
|
import org.xwiki.component.annotation.Component; |
38 |
|
import org.xwiki.localization.TranslationBundleContext; |
39 |
|
import org.xwiki.localization.internal.AbstractCachedTranslationBundle; |
40 |
|
import org.xwiki.localization.internal.DefaultLocalizedTranslationBundle; |
41 |
|
import org.xwiki.localization.internal.DefaultTranslation; |
42 |
|
import org.xwiki.localization.internal.LocalizedTranslationBundle; |
43 |
|
import org.xwiki.localization.message.TranslationMessage; |
44 |
|
import org.xwiki.localization.message.TranslationMessageParser; |
45 |
|
|
46 |
|
|
47 |
|
@link |
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Named("rootclassloader") |
54 |
|
@Singleton |
|
|
| 91.1% |
Uncovered Elements: 4 (45) |
Complexity: 10 |
Complexity Density: 0.3 |
|
55 |
|
public class RootClassLoaderTranslationBundle extends AbstractCachedTranslationBundle |
56 |
|
{ |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
@Inject |
61 |
|
@Named("messagetool/1.0") |
62 |
|
private TranslationMessageParser parser; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
@Inject |
68 |
|
private TranslationBundleContext bundleContext; |
69 |
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 4 |
Complexity Density: 0.36 |
|
70 |
145 |
@Override... |
71 |
|
protected LocalizedTranslationBundle createBundle(Locale locale) |
72 |
|
{ |
73 |
145 |
Properties properties = getResourceProperties(locale); |
74 |
|
|
75 |
145 |
if (properties == null) { |
76 |
2 |
return LocalizedTranslationBundle.EMPTY; |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
143 |
DefaultLocalizedTranslationBundle localeBundle = new DefaultLocalizedTranslationBundle(this, locale); |
81 |
|
|
82 |
143 |
for (Map.Entry<Object, Object> entry : properties.entrySet()) { |
83 |
292716 |
if (entry.getKey() instanceof String && entry.getValue() instanceof String) { |
84 |
292716 |
String key = (String) entry.getKey(); |
85 |
292716 |
String message = (String) entry.getValue(); |
86 |
|
|
87 |
292716 |
TranslationMessage translationMessage = this.parser.parse(message); |
88 |
|
|
89 |
292716 |
localeBundle.addTranslation(new DefaultTranslation(this.bundleContext, localeBundle, key, |
90 |
|
translationMessage)); |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
143 |
return localeBundle; |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
@param |
99 |
|
@return |
100 |
|
|
|
|
| 89.3% |
Uncovered Elements: 3 (28) |
Complexity: 6 |
Complexity Density: 0.27 |
|
101 |
145 |
private Properties getResourceProperties(Locale locale)... |
102 |
|
{ |
103 |
145 |
String resourceName = "ApplicationResources"; |
104 |
145 |
if (!locale.equals(Locale.ROOT)) { |
105 |
75 |
resourceName += "_" + locale; |
106 |
|
} |
107 |
145 |
resourceName += ".properties"; |
108 |
|
|
109 |
145 |
Enumeration<URL> urls; |
110 |
145 |
try { |
111 |
145 |
urls = getClass().getClassLoader().getResources(resourceName); |
112 |
|
} catch (IOException e) { |
113 |
0 |
this.logger.error("Failed to get resource URLs from class loader for name [{}]", resourceName, e); |
114 |
|
|
115 |
0 |
return null; |
116 |
|
} |
117 |
|
|
118 |
145 |
if (!urls.hasMoreElements()) { |
119 |
2 |
return null; |
120 |
|
} |
121 |
|
|
122 |
143 |
List<URL> urlList = EnumerationUtils.toList(urls); |
123 |
|
|
124 |
143 |
Properties properties = new Properties(); |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
704 |
for (ListIterator<URL> it = urlList.listIterator(urlList.size()); it.hasPrevious();) { |
129 |
561 |
URL url = it.previous(); |
130 |
|
|
131 |
561 |
try { |
132 |
561 |
InputStream componentListStream = url.openStream(); |
133 |
|
|
134 |
561 |
try { |
135 |
561 |
properties.load(componentListStream); |
136 |
|
} finally { |
137 |
561 |
componentListStream.close(); |
138 |
|
} |
139 |
|
} catch (IOException e) { |
140 |
0 |
this.logger.error("Failed to parse resource [{}] as translation budle", url, e); |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
143 |
return properties; |
145 |
|
} |
146 |
|
} |