1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.localization.legacy.internal.xwikipreferences; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.LinkedHashMap; |
25 |
|
import java.util.LinkedHashSet; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Locale; |
28 |
|
import java.util.Map; |
29 |
|
import java.util.Set; |
30 |
|
import java.util.regex.Pattern; |
31 |
|
|
32 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
33 |
|
import org.xwiki.cache.DisposableCacheValue; |
34 |
|
import org.xwiki.component.manager.ComponentLookupException; |
35 |
|
import org.xwiki.component.manager.ComponentManager; |
36 |
|
import org.xwiki.localization.LocaleUtils; |
37 |
|
import org.xwiki.localization.Translation; |
38 |
|
import org.xwiki.localization.TranslationBundle; |
39 |
|
import org.xwiki.localization.internal.AbstractTranslationBundle; |
40 |
|
import org.xwiki.model.EntityType; |
41 |
|
import org.xwiki.model.reference.DocumentReference; |
42 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
43 |
|
import org.xwiki.model.reference.EntityReference; |
44 |
|
import org.xwiki.model.reference.RegexEntityReference; |
45 |
|
import org.xwiki.observation.EventListener; |
46 |
|
import org.xwiki.observation.ObservationManager; |
47 |
|
import org.xwiki.observation.event.Event; |
48 |
|
|
49 |
|
import com.xpn.xwiki.internal.event.XObjectPropertyAddedEvent; |
50 |
|
import com.xpn.xwiki.internal.event.XObjectPropertyUpdatedEvent; |
51 |
|
|
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
|
|
| 96.2% |
Uncovered Elements: 2 (52) |
Complexity: 12 |
Complexity Density: 0.32 |
|
56 |
|
public class XWikiPreferencesWikiTranslationBundle extends AbstractTranslationBundle implements EventListener, |
57 |
|
DisposableCacheValue |
58 |
|
{ |
59 |
|
public static final String ID = XWikiPreferencesTranslationBundle.ID + ".wiki"; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private static final String DOCUMENT_BUNDLE_PROPERTY = "documentBundles"; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private static final String JOIN_SEPARATOR = ","; |
70 |
|
|
71 |
|
private ObservationManager observation; |
72 |
|
|
73 |
|
private DocumentAccessBridge documentAccessBridge; |
74 |
|
|
75 |
|
private DocumentReferenceResolver<String> resolver; |
76 |
|
|
77 |
|
private final List<Event> events; |
78 |
|
|
79 |
|
private final String wiki; |
80 |
|
|
81 |
|
private XWikiPreferencesTranslationBundle parent; |
82 |
|
|
83 |
|
private Map<DocumentReference, XWikiPreferencesDocumentTranslationBundle> bundles; |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
85 |
73 |
public XWikiPreferencesWikiTranslationBundle(String wiki, XWikiPreferencesTranslationBundle parent,... |
86 |
|
ComponentManager componentManager) throws ComponentLookupException |
87 |
|
{ |
88 |
73 |
super(XWikiPreferencesWikiTranslationBundle.ID + '.' + wiki); |
89 |
|
|
90 |
73 |
this.wiki = wiki; |
91 |
73 |
this.parent = parent; |
92 |
|
|
93 |
73 |
this.observation = componentManager.getInstance(ObservationManager.class); |
94 |
73 |
this.documentAccessBridge = componentManager.getInstance(DocumentAccessBridge.class); |
95 |
73 |
this.resolver = componentManager.getInstance(DocumentReferenceResolver.TYPE_STRING); |
96 |
|
|
97 |
73 |
intializeBundles(); |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
73 |
DocumentReference preferences = new DocumentReference(this.wiki, "XWiki", "XWikiPreferences"); |
102 |
|
|
103 |
73 |
EntityReference documentBundlesProperty = |
104 |
|
new EntityReference(DOCUMENT_BUNDLE_PROPERTY, EntityType.OBJECT_PROPERTY, new RegexEntityReference( |
105 |
|
Pattern.compile(this.wiki + ":XWiki.XWikiPreferences\\[\\d*\\]"), EntityType.OBJECT, preferences)); |
106 |
|
|
107 |
73 |
this.events = |
108 |
|
Arrays.<Event>asList(new XObjectPropertyAddedEvent(documentBundlesProperty), |
109 |
|
new XObjectPropertyUpdatedEvent(documentBundlesProperty)); |
110 |
|
|
111 |
73 |
this.observation.addListener(this); |
112 |
|
} |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
114 |
75 |
private Set<DocumentReference> getDocuments()... |
115 |
|
{ |
116 |
75 |
DocumentReference preferencesReference = new DocumentReference(this.wiki, "XWiki", "XWikiPreferences"); |
117 |
|
|
118 |
75 |
String documentNameListString = |
119 |
|
(String) this.documentAccessBridge.getProperty(preferencesReference, preferencesReference, |
120 |
|
DOCUMENT_BUNDLE_PROPERTY); |
121 |
|
|
122 |
75 |
Set<DocumentReference> documents; |
123 |
75 |
if (documentNameListString != null) { |
124 |
9 |
String[] documentNameList = documentNameListString.split(JOIN_SEPARATOR); |
125 |
|
|
126 |
9 |
documents = new LinkedHashSet<DocumentReference>(documentNameList.length); |
127 |
9 |
for (String documentName : documentNameList) { |
128 |
9 |
documents.add(this.resolver.resolve(documentName.trim(), preferencesReference)); |
129 |
|
} |
130 |
|
} else { |
131 |
66 |
documents = Collections.emptySet(); |
132 |
|
} |
133 |
|
|
134 |
75 |
return documents; |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
137 |
75 |
private void intializeBundles()... |
138 |
|
{ |
139 |
75 |
Set<DocumentReference> documents = getDocuments(); |
140 |
|
|
141 |
75 |
Map<DocumentReference, XWikiPreferencesDocumentTranslationBundle> newBundles = |
142 |
|
new LinkedHashMap<DocumentReference, XWikiPreferencesDocumentTranslationBundle>(documents.size()); |
143 |
75 |
for (DocumentReference document : documents) { |
144 |
9 |
newBundles.put(document, this.parent.getDocumentTranslationBundle(document)); |
145 |
|
} |
146 |
|
|
147 |
75 |
this.bundles = newBundles; |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
152 |
371 |
@Override... |
153 |
|
public String getName() |
154 |
|
{ |
155 |
371 |
return "localization.bundle." + getId(); |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
158 |
73 |
@Override... |
159 |
|
public List<Event> getEvents() |
160 |
|
{ |
161 |
73 |
return this.events; |
162 |
|
} |
163 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
164 |
2 |
@Override... |
165 |
|
public void onEvent(Event arg0, Object arg1, Object arg2) |
166 |
|
{ |
167 |
2 |
intializeBundles(); |
168 |
|
} |
169 |
|
|
170 |
|
|
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
172 |
250940 |
@Override... |
173 |
|
public Translation getTranslation(String key, Locale locale) |
174 |
|
{ |
175 |
250940 |
for (TranslationBundle bundle : this.bundles.values()) { |
176 |
67 |
Translation translation = bundle.getTranslation(key, locale); |
177 |
67 |
if (translation != null && translation.getLocale().equals(locale)) { |
178 |
36 |
return translation; |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
250899 |
Locale parentLocale = LocaleUtils.getParentLocale(locale); |
184 |
250903 |
if (parentLocale != null) { |
185 |
84996 |
return getTranslation(key, parentLocale); |
186 |
|
} |
187 |
|
|
188 |
165906 |
return null; |
189 |
|
} |
190 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
191 |
0 |
@Override... |
192 |
|
public void dispose() throws Exception |
193 |
|
{ |
194 |
0 |
this.observation.removeListener(getName()); |
195 |
|
} |
196 |
|
} |