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

File RootClassLoaderTranslationBundle.java

 

Coverage histogram

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

Code metrics

10
33
2
1
146
86
10
0.3
16.5
2
5

Classes

Class Line # Actions
RootClassLoaderTranslationBundle 55 33 0% 10 4
0.911111191.1%
 

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.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    * Provide translations coming from the root {@link ClassLoader}.
48    *
49    * @version $Id: ceecec30802ef07676521733b4576d26d9836d95 $
50    * @since 4.5M1
51    */
52    @Component
53    @Named("rootclassloader")
54    @Singleton
 
55    public class RootClassLoaderTranslationBundle extends AbstractCachedTranslationBundle
56    {
57    /**
58    * The parser to use for each message.
59    */
60    @Inject
61    @Named("messagetool/1.0")
62    private TranslationMessageParser parser;
63   
64    /**
65    * Used to access the current bundles.
66    */
67    @Inject
68    private TranslationBundleContext bundleContext;
69   
 
70  145 toggle @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    // Convert to LocalBundle
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 locale the locale to search for
99    * @return the content of all the resources files associated to the provided locale
100    */
 
101  145 toggle 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    // Load resources in reverse order to give priority to first found resources (follow ClassLoader#getResource
127    // behavior)
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    }