1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.localization.internal; |
21 |
|
|
22 |
|
import java.io.FileNotFoundException; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.net.MalformedURLException; |
26 |
|
import java.net.URL; |
27 |
|
import java.util.Locale; |
28 |
|
import java.util.Map; |
29 |
|
import java.util.Properties; |
30 |
|
|
31 |
|
import javax.inject.Inject; |
32 |
|
|
33 |
|
import org.slf4j.LoggerFactory; |
34 |
|
import org.xwiki.component.manager.ComponentLookupException; |
35 |
|
import org.xwiki.component.manager.ComponentManager; |
36 |
|
import org.xwiki.localization.TranslationBundleContext; |
37 |
|
import org.xwiki.localization.message.TranslationMessage; |
38 |
|
import org.xwiki.localization.message.TranslationMessageParser; |
39 |
|
|
40 |
|
|
41 |
|
@link |
42 |
|
|
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
|
|
| 86% |
Uncovered Elements: 7 (50) |
Complexity: 15 |
Complexity Density: 0.41 |
|
47 |
|
public abstract class AbstractURLResourceTranslationBundle extends AbstractCachedTranslationBundle |
48 |
|
{ |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
public static final String ID_PREFIX = "resource:"; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private static final String PROPERTIES_EXT = ".properties"; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@Inject |
63 |
|
protected TranslationBundleContext bundleContext; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
protected TranslationMessageParser translationMessageParser; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
protected URL baseURL; |
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
@param |
78 |
|
@param |
79 |
|
@throws |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
81 |
8 |
public AbstractURLResourceTranslationBundle(URL baseURL, ComponentManager componentManager,... |
82 |
|
TranslationMessageParser translationMessageParser) throws ComponentLookupException |
83 |
|
{ |
84 |
8 |
this.bundleContext = componentManager.getInstance(TranslationBundleContext.class); |
85 |
|
|
86 |
8 |
this.translationMessageParser = translationMessageParser; |
87 |
|
|
88 |
8 |
this.logger = LoggerFactory.getLogger(getClass()); |
89 |
|
|
90 |
8 |
this.baseURL = baseURL; |
91 |
|
|
92 |
8 |
setId(ID_PREFIX + baseURL); |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
@param |
97 |
|
@return@link |
98 |
|
|
|
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
99 |
17 |
protected URL getLocaleURL(Locale locale)... |
100 |
|
{ |
101 |
17 |
String urlString = this.baseURL.toString(); |
102 |
|
|
103 |
17 |
String localeURL = urlString; |
104 |
|
|
105 |
17 |
if (!locale.equals(Locale.ROOT)) { |
106 |
10 |
if (urlString.endsWith(PROPERTIES_EXT)) { |
107 |
10 |
int index = urlString.lastIndexOf('.'); |
108 |
|
|
109 |
10 |
localeURL = urlString.substring(0, index); |
110 |
10 |
localeURL += "_" + locale.toString(); |
111 |
10 |
localeURL += PROPERTIES_EXT; |
112 |
|
} else { |
113 |
|
|
114 |
0 |
return null; |
115 |
|
} |
116 |
|
} |
117 |
|
|
118 |
17 |
try { |
119 |
17 |
return new URL(localeURL); |
120 |
|
} catch (MalformedURLException e) { |
121 |
|
|
122 |
0 |
return null; |
123 |
|
} |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
@param |
128 |
|
@return@link@link |
129 |
|
|
130 |
|
|
|
|
| 81.8% |
Uncovered Elements: 4 (22) |
Complexity: 8 |
Complexity Density: 0.44 |
|
131 |
17 |
protected LocalizedTranslationBundle loadResourceLocaleBundle(Locale locale)... |
132 |
|
{ |
133 |
|
|
134 |
17 |
URL localeURL = getLocaleURL(locale); |
135 |
|
|
136 |
17 |
if (localeURL == null) { |
137 |
0 |
return LocalizedTranslationBundle.EMPTY; |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
17 |
Properties properties = new Properties(); |
142 |
|
|
143 |
17 |
try (InputStream componentListStream = localeURL.openStream()) { |
144 |
11 |
properties.load(componentListStream); |
145 |
|
} catch (FileNotFoundException e) { |
146 |
|
|
147 |
6 |
return LocalizedTranslationBundle.EMPTY; |
148 |
|
} catch (IOException e) { |
149 |
0 |
this.logger.error("Failed to parse resource [{}] as translation bundle", localeURL, e); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
11 |
DefaultLocalizedTranslationBundle localeBundle = new DefaultLocalizedTranslationBundle(this, locale); |
154 |
|
|
155 |
11 |
TranslationMessageParser parser = getTranslationMessageParser(); |
156 |
|
|
157 |
11 |
for (Map.Entry<Object, Object> entry : properties.entrySet()) { |
158 |
11 |
if (entry.getKey() instanceof String && entry.getValue() instanceof String) { |
159 |
11 |
String key = (String) entry.getKey(); |
160 |
11 |
String message = (String) entry.getValue(); |
161 |
|
|
162 |
11 |
TranslationMessage translationMessage = parser.parse(message); |
163 |
|
|
164 |
11 |
localeBundle.addTranslation(new DefaultTranslation(this.bundleContext, localeBundle, key, |
165 |
|
translationMessage)); |
166 |
|
} |
167 |
|
} |
168 |
|
|
169 |
11 |
return localeBundle; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
@return |
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
175 |
11 |
protected TranslationMessageParser getTranslationMessageParser()... |
176 |
|
{ |
177 |
11 |
return this.translationMessageParser; |
178 |
|
} |
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
180 |
17 |
@Override... |
181 |
|
protected LocalizedTranslationBundle createBundle(Locale locale) |
182 |
|
{ |
183 |
17 |
return loadResourceLocaleBundle(locale); |
184 |
|
} |
185 |
|
} |