1 /*
2 * Copyright 2006-2012 smartics, Kronseder & Reiner GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package de.smartics.maven.util.report;
17
18 import java.util.Locale;
19 import java.util.MissingResourceException;
20 import java.util.ResourceBundle;
21
22 /**
23 * Helps to fetch labels to render reports. Labels are taken from two resource
24 * bundles: a standard and a default one. If a key is not found in the standard
25 * bundle, it is fetched from the default bundle. If it is not found there
26 * either the key is returned verbatim.
27 *
28 * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
29 * @version $Revision:591 $
30 */
31 public class MessageHelper
32 {
33 // ********************************* Fields *********************************
34
35 // --- constants ------------------------------------------------------------
36
37 // --- members --------------------------------------------------------------
38
39 /**
40 * The report title ID.
41 */
42 private final String reportTitleId;
43
44 /**
45 * The report description ID.
46 */
47 private final String reportDescriptionId;
48
49 /**
50 * The primary message bundle to use. May be <code>null</code> so that all
51 * labels are provided by the {@link #defaultMessages}.
52 */
53 private final ResourceBundle messages;
54
55 /**
56 * The default messages that are used if the {@link #messages} contain no
57 * value for a given key.
58 */
59 private final ResourceBundle defaultMessages;
60
61 // ****************************** Initializer *******************************
62
63 // ****************************** Constructors ******************************
64
65 /**
66 * Default constructor.
67 *
68 * @param reportTitleId the localized report title.
69 * @param reportDescriptionId the localized report description.
70 * @param messages the primary message bundle to use.
71 * @param defaultMessages the default messages that are used if the
72 * {@link #messages} contain no value for a given key.
73 * @throws NullPointerException if <code>defaultMessages</code> is
74 * <code>null</code>.
75 */
76 public MessageHelper(final String reportTitleId,
77 final String reportDescriptionId, final ResourceBundle messages,
78 final ResourceBundle defaultMessages) throws NullPointerException
79 {
80 checkArguments(defaultMessages);
81 this.reportTitleId = reportTitleId;
82 this.reportDescriptionId = reportDescriptionId;
83 this.messages = messages;
84 this.defaultMessages = defaultMessages;
85 }
86
87 // ****************************** Inner Classes *****************************
88
89 // ********************************* Methods ********************************
90
91 // --- init -----------------------------------------------------------------
92
93 private static void checkArguments(final ResourceBundle defaultMessages)
94 throws NullPointerException
95 {
96 if (defaultMessages == null)
97 {
98 throw new NullPointerException(
99 "The default resource bundle must not be 'null'.");
100 }
101 }
102
103 // --- get&set --------------------------------------------------------------
104
105 /**
106 * Returns the localized report title.
107 *
108 * @return the localized report title.
109 */
110 public String getReportTitleId()
111 {
112 return reportTitleId;
113 }
114
115 /**
116 * Returns the localized report description.
117 *
118 * @return the localized report description.
119 */
120 public String getReportDescriptionId()
121 {
122 return reportDescriptionId;
123 }
124
125 /**
126 * Returns the locale the message helper provides messages for.
127 *
128 * @return the locale of the resource bundles.
129 */
130 public Locale getLocale()
131 {
132 return defaultMessages.getLocale();
133 }
134
135 // --- business -------------------------------------------------------------
136
137 /**
138 * Returns the label for the given <code>key</code>.
139 *
140 * @param key the key to the requested label.
141 * @return the label to the <code>key</code> or the <code>key</code>, if no
142 * label is found for that <code>key</code>.
143 */
144 public String getLabel(final String key)
145 {
146 return getLabel(key, key);
147 }
148
149 /**
150 * Returns the label for the given <code>key</code>.
151 *
152 * @param key the key to the requested label.
153 * @param defaultLabel the label to return if no label is found by the
154 * <code>key</code>.
155 * @return the label to the <code>key</code> or the <code>defaultLabel</code>,
156 * if no label is found for that <code>key</code>.
157 */
158 public String getLabel(final String key, final String defaultLabel)
159 {
160 String label = getStringFromMessages(key);
161 if (label == null)
162 {
163 label = getStringFromDefaultMessages(key, defaultLabel);
164 }
165 return label;
166 }
167
168 private String getStringFromDefaultMessages(final String key,
169 final String defaultLabel)
170 {
171 try
172 {
173 return defaultMessages.getString(key);
174 }
175 catch (final MissingResourceException e)
176 {
177 return defaultLabel;
178 }
179 }
180
181 private String getStringFromMessages(final String key)
182 {
183 if (messages != null)
184 {
185 try
186 {
187 return messages.getString(key);
188 }
189 catch (final MissingResourceException e)
190 {
191 // Return null at the end
192 }
193 }
194 return null;
195 }
196
197 // --- object basics --------------------------------------------------------
198
199 }