Skip to content

Package: EMFFormsLocalizationServiceImpl

EMFFormsLocalizationServiceImpl

nameinstructionbranchcomplexitylinemethod
EMFFormsLocalizationServiceImpl()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getLocale()
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getResourceBundle(Bundle, Locale)
M: 0 C: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getString(Bundle, Locale, String)
M: 27 C: 46
63%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 6 C: 10
63%
M: 0 C: 1
100%
getString(Bundle, String)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getString(Class, String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hasKey(Bundle, Locale, String)
M: 27 C: 11
29%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 6 C: 3
33%
M: 0 C: 1
100%
hasKey(Bundle, String)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hasKey(Class, String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setBundleLocalization(BundleLocalization)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setEMFFormsLocaleProvider(EMFFormsLocaleProvider)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setReportService(ReportService)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
unsetEMFFormsLocaleProvider(EMFFormsLocaleProvider)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.localization;
15:
16: import java.util.Locale;
17: import java.util.ResourceBundle;
18:
19: import org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleProvider;
20: import org.eclipse.emfforms.spi.common.report.AbstractReport;
21: import org.eclipse.emfforms.spi.common.report.ReportService;
22: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
23: import org.eclipse.osgi.service.localization.BundleLocalization;
24: import org.osgi.framework.Bundle;
25: import org.osgi.framework.FrameworkUtil;
26: import org.osgi.service.component.annotations.Component;
27: import org.osgi.service.component.annotations.Reference;
28: import org.osgi.service.component.annotations.ReferenceCardinality;
29: import org.osgi.service.component.annotations.ReferencePolicy;
30:
31: /**
32: * Service Implementation for retrieving translated Strings.
33: *
34: * @author Eugen Neufeld
35: *
36: */
37: @Component(name = "localizationService")
38: public class EMFFormsLocalizationServiceImpl implements EMFFormsLocalizationService {
39:
40:         private EMFFormsLocaleProvider localeProvider;
41:         private BundleLocalization bundleLocalization;
42:         private ReportService reportService;
43:
44:         /**
45:          * Called by the framework to set the EMFFormsLocaleProvider.
46:          *
47:          * @param localeProvider The {@link EMFFormsLocaleProvider}
48:          */
49:         @Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
50:         protected void setEMFFormsLocaleProvider(EMFFormsLocaleProvider localeProvider) {
51:                 this.localeProvider = localeProvider;
52:         }
53:
54:         /**
55:          * Called by the framework to unset the EMFFormsLocaleProvider.
56:          *
57:          * @param localeProvider The {@link EMFFormsLocaleProvider}
58:          */
59:         protected void unsetEMFFormsLocaleProvider(EMFFormsLocaleProvider localeProvider) {
60:                 this.localeProvider = null;
61:         }
62:
63:         /**
64:          * Called by the framework to set the BundleLocalization.
65:          *
66:          * @param bundleLocalization The {@link BundleLocalization}
67:          */
68:         @Reference(unbind = "-")
69:         protected void setBundleLocalization(BundleLocalization bundleLocalization) {
70:                 this.bundleLocalization = bundleLocalization;
71:         }
72:
73:         /**
74:          * Called by the framework to set the ReportService.
75:          *
76:          * @param reportService The {@link ReportService}
77:          */
78:         @Reference(unbind = "-")
79:         protected void setReportService(ReportService reportService) {
80:                 this.reportService = reportService;
81:         }
82:
83:         /**
84:          * {@inheritDoc}
85:          *
86:          * @see org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService#getString(java.lang.Class,
87:          * java.lang.String)
88:          */
89:         @Override
90:         public String getString(Class<?> clazz, String key) {
91:                 return getString(FrameworkUtil.getBundle(clazz), key);
92:         }
93:
94:         /**
95:          * {@inheritDoc}
96:          *
97:          * @see org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService#getString(org.osgi.framework.Bundle,
98:          * java.lang.String)
99:          */
100:         @Override
101:         public String getString(Bundle bundle, String key) {
102:                 return getString(bundle, getLocale(), key);
103:         }
104:
105:         private Locale getLocale() {
106:•                if (localeProvider == null) {
107:                         return null;
108:                 }
109:                 return localeProvider.getLocale();
110:         }
111:
112:         private String getString(Bundle bundle, Locale locale, String key) {
113:                 final ResourceBundle resourceBundle = getResourceBundle(bundle, locale);
114:•                if (resourceBundle == null) {
115:                         reportService
116:                                 .report(new AbstractReport(
117:                                         String
118:                                                 .format(
119:                                                         "No ResourceBundle found for Locale '%1$s' in Bundle %2$s with Version %3$s.", //$NON-NLS-1$
120:                                                         locale, bundle.getSymbolicName(), bundle.getVersion().toString())));
121:                         return key;
122:                 }
123:•                if (!resourceBundle.containsKey(key)) {
124:                         reportService
125:                                 .report(new AbstractReport(
126:                                         String
127:                                                 .format(
128:                                                         "The ResourceBundle for Locale '%1$s' in Bundle %2$s with Version %3$s doesn't contain the key '%4$s'.", //$NON-NLS-1$
129:                                                         locale, bundle.getSymbolicName(), bundle.getVersion().toString(), key)));
130:                         return key;
131:                 }
132:                 return resourceBundle.getString(key);
133:         }
134:
135:         private ResourceBundle getResourceBundle(Bundle bundle, Locale locale) {
136:•                if (locale == null) {
137:                         return bundleLocalization.getLocalization(bundle, null);
138:                 }
139:                 return bundleLocalization.getLocalization(bundle, locale.toString());
140:         }
141:
142:         /**
143:          * {@inheritDoc}
144:          *
145:          * @see org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService#hasKey(org.osgi.framework.Bundle,
146:          * java.lang.String)
147:          */
148:         @Override
149:         public boolean hasKey(Bundle bundle, String key) {
150:                 return hasKey(bundle, getLocale(), key);
151:         }
152:
153:         /**
154:          * {@inheritDoc}
155:          *
156:          * @see org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService#hasKey(java.lang.Class, java.lang.String)
157:          */
158:         @Override
159:         public boolean hasKey(Class<?> clazz, String key) {
160:                 return hasKey(FrameworkUtil.getBundle(clazz), key);
161:         }
162:
163:         private boolean hasKey(Bundle bundle, Locale locale, String key) {
164:                 final ResourceBundle resourceBundle = getResourceBundle(bundle, locale);
165:•                if (resourceBundle == null) {
166:                         reportService
167:                                 .report(new AbstractReport(
168:                                         String
169:                                                 .format(
170:                                                         "No ResourceBundle found for Locale '%1$s' in Bundle %2$s with Version %3$s.", //$NON-NLS-1$
171:                                                         locale, bundle.getSymbolicName(), bundle.getVersion().toString())));
172:                         return false;
173:                 }
174:                 return resourceBundle.containsKey(key);
175:         }
176: }