1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.localization.legacy.internal.xwikipreferences

File XWikiPreferencesWikiTranslationBundle.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
38
8
1
196
124
12
0.32
4.75
8
1.5

Classes

Class Line # Actions
XWikiPreferencesWikiTranslationBundle 56 38 0% 12 2
0.9615384396.2%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 $Id: b245fe9d7c93d5535ed337278f4c39fea2d484e6 $
54    * @since 4.3M2
55    */
 
56    public class XWikiPreferencesWikiTranslationBundle extends AbstractTranslationBundle implements EventListener,
57    DisposableCacheValue
58    {
59    public static final String ID = XWikiPreferencesTranslationBundle.ID + ".wiki";
60   
61    /**
62    * The name of the property containing the list of global document bundles.
63    */
64    private static final String DOCUMENT_BUNDLE_PROPERTY = "documentBundles";
65   
66    /**
67    * String to use when joining the list of document names.
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   
 
85  73 toggle 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    // Observation
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   
 
114  75 toggle 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   
 
137  75 toggle 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    // EventListeners
151   
 
152  371 toggle @Override
153    public String getName()
154    {
155  371 return "localization.bundle." + getId();
156    }
157   
 
158  73 toggle @Override
159    public List<Event> getEvents()
160    {
161  73 return this.events;
162    }
163   
 
164  2 toggle @Override
165    public void onEvent(Event arg0, Object arg1, Object arg2)
166    {
167  2 intializeBundles();
168    }
169   
170    // Bundle
171   
 
172  250940 toggle @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    // Try parent locale
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   
 
191  0 toggle @Override
192    public void dispose() throws Exception
193    {
194  0 this.observation.removeListener(getName());
195    }
196    }