This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
MessageExtractor.java
82 lines (69 loc) · 3.11 KB
/
MessageExtractor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*******************************************************************************
* Copyright (c) 2012 IBM Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
*
* Russell Boykin - initial API and implementation
* Alberto Giammaria - initial API and implementation
* Chris Peters - initial API and implementation
* Gianluca Bernardini - initial API and implementation
*******************************************************************************/
package org.eclipse.lyo.client.exception.exception;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MessageExtractor {
private static final String CLASS = MessageExtractor.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
private static final String BUNDLE_NAME = "oslc4jclient";
private static ResourceBundle bundle = null;
private MessageExtractor(){
}
private static synchronized ResourceBundle getMessageBundle(final Locale locale){
if(bundle == null) {
bundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
}
return bundle;
}
private static String getString(final Locale locale, final String key, final Object[] args ) {
final ResourceBundle messages = getMessageBundle(locale);
if (messages != null) {
try {
final String message = messages.getString( key );
return formatMessage(locale, message, args);
} catch ( final MissingResourceException missingResourceException ) {
logger.log(Level.SEVERE, missingResourceException.getMessage(), missingResourceException );
return "???" + key + "???";
}
}
logger.log(Level.SEVERE, "missing resource: " + key);
return "???" + key + "???";
}
private static String formatMessage(final Locale locale, final String message, final Object[] args) {
final String fixedMessage = FixMessageFormat.fixPattern(message);
return new MessageFormat(fixedMessage, locale).format(args);
}
public static String getMessage(final Locale locale, final String key, final Object[] params){
return getString(locale, key, params);
}
public static String getMessage(final Locale locale, final String key){
return getMessage(locale, key, null);
}
public static String getMessage(final String key){
return getMessage(key, null);
}
public static String getMessage(final String key, final Object[] params){
return getMessage(Locale.getDefault(), key, params);
}
}