Skip to content

Package: LocalizationServiceHelper

LocalizationServiceHelper

nameinstructionbranchcomplexitylinemethod
LocalizationServiceHelper()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getInstance()
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getString(Bundle, String)
M: 0 C: 5
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: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
innerGetString(Bundle, String)
M: 2 C: 25
93%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 7
88%
M: 0 C: 1
100%
innerGetString(Class, String)
M: 2 C: 25
93%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 7
88%
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.spi.localization;
15:
16: import org.osgi.framework.Bundle;
17: import org.osgi.framework.BundleContext;
18: import org.osgi.framework.FrameworkUtil;
19: import org.osgi.framework.ServiceReference;
20:
21: /**
22: * Helper class for retrieving translated strings.
23: *
24: * @author Eugen Neufeld
25: */
26: public final class LocalizationServiceHelper {
27:
28:         private static LocalizationServiceHelper instance;
29:
30:         private static LocalizationServiceHelper getInstance() {
31:•                if (instance == null) {
32:                         instance = new LocalizationServiceHelper();
33:                 }
34:                 return instance;
35:         }
36:
37:         private final BundleContext bundleContext;
38:
39:         private LocalizationServiceHelper() {
40:                 bundleContext = FrameworkUtil.getBundle(LocalizationServiceHelper.class)
41:                         .getBundleContext();
42:         }
43:
44:         private String innerGetString(Bundle bundle, String key) {
45:                 final ServiceReference<EMFFormsLocalizationService> serviceReference = bundleContext
46:                         .getServiceReference(EMFFormsLocalizationService.class);
47:•                if (serviceReference == null) {
48:                         return null;
49:                 }
50:                 final EMFFormsLocalizationService localeProvider = bundleContext.getService(serviceReference);
51:                 final String result = localeProvider.getString(bundle, key);
52:                 bundleContext.ungetService(serviceReference);
53:                 return result;
54:         }
55:
56:         private String innerGetString(Class<?> clazz, String key) {
57:                 final ServiceReference<EMFFormsLocalizationService> serviceReference = bundleContext
58:                         .getServiceReference(EMFFormsLocalizationService.class);
59:•                if (serviceReference == null) {
60:                         return null;
61:                 }
62:                 final EMFFormsLocalizationService localeProvider = bundleContext.getService(serviceReference);
63:                 final String result = localeProvider.getString(clazz, key);
64:                 bundleContext.ungetService(serviceReference);
65:                 return result;
66:         }
67:
68:         /**
69:          * Return the String for the provided key.
70:          *
71:          * @param bundle The bundle which provides the translated strings
72:          * @param key The key of the string
73:          * @return The translated key
74:          */
75:         public static String getString(Bundle bundle, String key) {
76:                 return getInstance().innerGetString(bundle, key);
77:         }
78:
79:         /**
80:          * Return the String for the provided key.
81:          *
82:          * @param clazz The class which needs a translated string
83:          * @param key The key of the string
84:          * @return The translated key
85:          */
86:         public static String getString(Class<?> clazz, String key) {
87:                 return getInstance().innerGetString(clazz, key);
88:         }
89: }