1 /*
2 * The MIT License
3 * Copyright © 2004-2014 Fabrizio Giustina
4 * Copyright © 2022-2022 Web-Legacy
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package net.sf.maventaglib;
25
26 import java.util.Locale;
27 import java.util.MissingResourceException;
28 import java.util.ResourceBundle;
29 import java.util.concurrent.ConcurrentHashMap;
30
31 /**
32 * @author Fabrizio Giustina
33 * @version $Id: Messages.java 217 2014-08-15 20:50:32Z fgiust $
34 */
35 public class Messages
36 {
37
38 private static final String BUNDLE_NAME = "m2-taglib"; //$NON-NLS-1$
39
40 /**
41 * the resource-bundle with all locals.
42 */
43 private static final ConcurrentHashMap<String, ResourceBundle> RESOURCE_BUNDLES = new ConcurrentHashMap<>();
44
45 /**
46 * the resource-bundle with default-locale.
47 */
48 private static final ResourceBundle DFLT_RESOURCE_BUNDLE;
49
50 static
51 {
52 DFLT_RESOURCE_BUNDLE = ResourceBundle.getBundle( BUNDLE_NAME );
53 RESOURCE_BUNDLES.put( DFLT_RESOURCE_BUNDLE.getLocale().toString(),
54 DFLT_RESOURCE_BUNDLE );
55 }
56
57 private Messages()
58 {
59 }
60
61 /**
62 * Gets a string for the given key from the resource bundle with the default-locale.
63 *
64 * @param key the key for the desired string
65 *
66 * @exception NullPointerException if {@code key} is {@code null}
67 * @exception ClassCastException if the object found for the given key is not a string
68 *
69 * @return the string for the given key with the default-locale
70 */
71 public static String getString( String key )
72 {
73 return getString( DFLT_RESOURCE_BUNDLE, key );
74 }
75
76 /**
77 * Gets a string for the given key from the resource bundle with the given locale.
78 * If the locale is {@code null}, then the default-locale is used.
79 *
80 * @param locale the resource-bundle with the wanted locale
81 * @param key the key for the desired string
82 *
83 * @exception NullPointerException if {@code key} is {@code null}
84 * @exception ClassCastException if the object found for the given key is not a string
85 *
86 * @return the string for the given key with the given locale
87 */
88 public static String getString( Locale locale, String key )
89 {
90 final ResourceBundle resourceBundle = locale == null
91 ? DFLT_RESOURCE_BUNDLE
92 : RESOURCE_BUNDLES.computeIfAbsent( locale.toString(),
93 (k) -> ResourceBundle.getBundle( BUNDLE_NAME, locale ) );
94 return getString( resourceBundle, key );
95 }
96
97 /**
98 * Gets a string for the given key from the given resource bundle.
99 *
100 * @param resourceBundle the resource-bundle with the wanted locale
101 * @param key the key for the desired string
102 *
103 * @exception NullPointerException if {@code key} is {@code null}
104 * @exception ClassCastException if the object found for the given key is not a string
105 *
106 * @return the string for the given key
107 */
108 private static String getString( ResourceBundle resourceBundle, String key )
109 {
110 try
111 {
112 return resourceBundle == null
113 ? '!' + key + '!'
114 : resourceBundle.getString( key );
115 }
116 catch ( MissingResourceException e )
117 {
118 return '!' + key + '!';
119 }
120 }
121 }