1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.localization.wiki.internal; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Locale; |
26 |
|
import java.util.Map; |
27 |
|
import java.util.Properties; |
28 |
|
|
29 |
|
import javax.inject.Provider; |
30 |
|
|
31 |
|
import org.slf4j.LoggerFactory; |
32 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
33 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
34 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
35 |
|
import org.xwiki.bridge.event.WikiDeletedEvent; |
36 |
|
import org.xwiki.cache.DisposableCacheValue; |
37 |
|
import org.xwiki.component.manager.ComponentLookupException; |
38 |
|
import org.xwiki.component.manager.ComponentManager; |
39 |
|
import org.xwiki.component.phase.Disposable; |
40 |
|
import org.xwiki.localization.Translation; |
41 |
|
import org.xwiki.localization.TranslationBundle; |
42 |
|
import org.xwiki.localization.TranslationBundleContext; |
43 |
|
import org.xwiki.localization.internal.AbstractCachedTranslationBundle; |
44 |
|
import org.xwiki.localization.internal.DefaultLocalizedTranslationBundle; |
45 |
|
import org.xwiki.localization.internal.DefaultTranslation; |
46 |
|
import org.xwiki.localization.internal.LocalizedTranslationBundle; |
47 |
|
import org.xwiki.localization.message.TranslationMessage; |
48 |
|
import org.xwiki.localization.message.TranslationMessageParser; |
49 |
|
import org.xwiki.model.reference.DocumentReference; |
50 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
51 |
|
import org.xwiki.observation.EventListener; |
52 |
|
import org.xwiki.observation.ObservationManager; |
53 |
|
import org.xwiki.observation.event.Event; |
54 |
|
|
55 |
|
import com.xpn.xwiki.XWiki; |
56 |
|
import com.xpn.xwiki.XWikiContext; |
57 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
58 |
|
|
59 |
|
|
60 |
|
@link |
61 |
|
|
62 |
|
|
63 |
|
@version |
64 |
|
@since |
65 |
|
|
|
|
| 91.5% |
Uncovered Elements: 7 (82) |
Complexity: 22 |
Complexity Density: 0.4 |
|
66 |
|
public abstract class AbstractDocumentTranslationBundle extends AbstractCachedTranslationBundle implements |
67 |
|
DisposableCacheValue, Disposable, EventListener |
68 |
|
{ |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
public static final int DEFAULTPRIORITY_WIKI = DEFAULTPRIORITY - 100; |
73 |
|
|
74 |
|
protected ComponentManager componentManager; |
75 |
|
|
76 |
|
protected TranslationBundleContext bundleContext; |
77 |
|
|
78 |
|
protected EntityReferenceSerializer<String> serializer; |
79 |
|
|
80 |
|
protected Provider<XWikiContext> contextProvider; |
81 |
|
|
82 |
|
protected ObservationManager observation; |
83 |
|
|
84 |
|
protected TranslationMessageParser translationMessageParser; |
85 |
|
|
86 |
|
protected List<Event> events; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
protected boolean disposed; |
93 |
|
|
94 |
|
protected DocumentReference documentReference; |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
protected String idPrefix; |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
101 |
137 |
public AbstractDocumentTranslationBundle(String idPrefix, DocumentReference reference,... |
102 |
|
ComponentManager componentManager, TranslationMessageParser translationMessageParser) |
103 |
|
throws ComponentLookupException |
104 |
|
{ |
105 |
137 |
this.idPrefix = idPrefix; |
106 |
|
|
107 |
137 |
this.componentManager = componentManager; |
108 |
137 |
this.bundleContext = componentManager.getInstance(TranslationBundleContext.class); |
109 |
137 |
this.serializer = componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING); |
110 |
137 |
this.contextProvider = componentManager.getInstance(XWikiContext.TYPE_PROVIDER); |
111 |
137 |
this.observation = componentManager.getInstance(ObservationManager.class); |
112 |
|
|
113 |
137 |
this.translationMessageParser = translationMessageParser; |
114 |
|
|
115 |
137 |
this.logger = LoggerFactory.getLogger(getClass()); |
116 |
|
|
117 |
137 |
setPriority(DEFAULTPRIORITY_WIKI); |
118 |
|
|
119 |
137 |
setReference(reference); |
120 |
|
|
121 |
137 |
initialize(); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
124 |
137 |
private void initialize()... |
125 |
|
{ |
126 |
137 |
this.events = |
127 |
|
Arrays.<Event>asList(new DocumentUpdatedEvent(this.documentReference), new DocumentCreatedEvent( |
128 |
|
this.documentReference), new DocumentDeletedEvent(this.documentReference), new WikiDeletedEvent( |
129 |
|
this.documentReference.getWikiReference().getName())); |
130 |
|
|
131 |
137 |
this.observation.addListener(this); |
132 |
|
} |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
134 |
137 |
protected void setReference(DocumentReference reference)... |
135 |
|
{ |
136 |
137 |
this.documentReference = reference; |
137 |
|
|
138 |
137 |
setId(this.idPrefix + this.serializer.serialize(reference)); |
139 |
|
} |
140 |
|
|
|
|
| 84.8% |
Uncovered Elements: 5 (33) |
Complexity: 9 |
Complexity Density: 0.39 |
|
141 |
278 |
protected LocalizedTranslationBundle loadDocumentLocaleBundle(Locale locale) throws Exception... |
142 |
|
{ |
143 |
278 |
XWikiContext context = this.contextProvider.get(); |
144 |
|
|
145 |
278 |
if (context == null) { |
146 |
|
|
147 |
0 |
return null; |
148 |
|
} |
149 |
|
|
150 |
278 |
XWiki xwiki = context.getWiki(); |
151 |
|
|
152 |
278 |
if (xwiki == null) { |
153 |
|
|
154 |
0 |
return null; |
155 |
|
} |
156 |
|
|
157 |
278 |
XWikiDocument document = xwiki.getDocument(this.documentReference, context); |
158 |
|
|
159 |
278 |
if (locale != null && !locale.equals(Locale.ROOT) && !locale.equals(document.getDefaultLocale())) { |
160 |
14 |
document = xwiki.getDocument(new DocumentReference(document.getDocumentReference(), locale), context); |
161 |
|
|
162 |
14 |
if (document.isNew()) { |
163 |
|
|
164 |
11 |
return LocalizedTranslationBundle.EMPTY; |
165 |
|
} |
166 |
|
} |
167 |
|
|
168 |
267 |
String content = document.getContent(); |
169 |
|
|
170 |
267 |
Properties properties = new Properties(); |
171 |
267 |
properties.load(new StringReader(content)); |
172 |
|
|
173 |
|
|
174 |
267 |
DefaultLocalizedTranslationBundle localeBundle = new DefaultLocalizedTranslationBundle(this, locale); |
175 |
|
|
176 |
267 |
TranslationMessageParser parser = getTranslationMessageParser(); |
177 |
|
|
178 |
267 |
for (Map.Entry<Object, Object> entry : properties.entrySet()) { |
179 |
5136 |
if (entry.getKey() instanceof String && entry.getValue() instanceof String) { |
180 |
5136 |
String key = (String) entry.getKey(); |
181 |
5136 |
String message = (String) entry.getValue(); |
182 |
|
|
183 |
5136 |
TranslationMessage translationMessage = parser.parse(message); |
184 |
|
|
185 |
5136 |
localeBundle.addTranslation(new DefaultTranslation(this.bundleContext, localeBundle, key, |
186 |
|
translationMessage)); |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
267 |
return localeBundle; |
191 |
|
} |
192 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
193 |
267 |
protected TranslationMessageParser getTranslationMessageParser()... |
194 |
|
{ |
195 |
267 |
return this.translationMessageParser; |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
198 |
278 |
@Override... |
199 |
|
protected LocalizedTranslationBundle createBundle(Locale locale) throws Exception |
200 |
|
{ |
201 |
278 |
return loadDocumentLocaleBundle(locale); |
202 |
|
} |
203 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
204 |
1140333 |
@Override... |
205 |
|
public Translation getTranslation(String key, Locale locale) |
206 |
|
{ |
207 |
1140332 |
if (this.disposed) { |
208 |
0 |
return null; |
209 |
|
} |
210 |
|
|
211 |
1140333 |
return super.getTranslation(key, locale); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
216 |
9 |
@Override... |
217 |
|
public void dispose() |
218 |
|
{ |
219 |
9 |
this.disposed = true; |
220 |
9 |
this.bundleCache.clear(); |
221 |
9 |
this.observation.removeListener(getName()); |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
226 |
45 |
@Override... |
227 |
|
public void onEvent(Event event, Object source, Object data) |
228 |
|
{ |
229 |
45 |
if (event instanceof WikiDeletedEvent) { |
230 |
5 |
this.bundleCache.clear(); |
231 |
|
|
232 |
5 |
this.disposed = true; |
233 |
|
} else { |
234 |
40 |
XWikiDocument document = (XWikiDocument) source; |
235 |
|
|
236 |
40 |
this.bundleCache.remove(document.getLocale()); |
237 |
|
|
238 |
40 |
if (document.getLocale().equals(Locale.ROOT)) { |
239 |
8 |
this.bundleCache.remove(document.getDefaultLocale()); |
240 |
|
} |
241 |
|
} |
242 |
|
} |
243 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
244 |
1379 |
@Override... |
245 |
|
public String getName() |
246 |
|
{ |
247 |
1379 |
return "localization.bundle." + getId(); |
248 |
|
} |
249 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
250 |
137 |
@Override... |
251 |
|
public List<Event> getEvents() |
252 |
|
{ |
253 |
137 |
return events; |
254 |
|
} |
255 |
|
} |