1 /*
2 * Copyright 2007-2011 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.exceptions.i18n.message;
17
18 import java.io.Serializable;
19 import java.util.Locale;
20 import java.util.MissingResourceException;
21
22 import de.smartics.exceptions.core.Code;
23 import de.smartics.exceptions.i18n.I18nExceptionContext;
24 import de.smartics.exceptions.i18n.I18nExceptionContextManager;
25
26 /**
27 * Controls the localized information for a throwable.
28 *
29 * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
30 * @version $Revision:591 $
31 */
32 public class LocalizedInfo implements Serializable
33 {
34 // ********************************* Fields *********************************
35
36 // --- constants ------------------------------------------------------------
37
38 /**
39 * The class version identifier.
40 */
41 private static final long serialVersionUID = 1L;
42
43 /**
44 * The suffix for the auto generated bundle name.
45 * <p>
46 * The value of this constant is {@value}.
47 */
48 public static final String BUNDLE_NAME_SUFFIX = "Bundle";
49
50 /**
51 * The localization key to fetch messages from message bundles. If this value
52 * is not set, the {@link String} representation of the provided code instance
53 * is used.
54 *
55 * @serial
56 */
57 protected final String resourceKey;
58
59 /**
60 * The fully qualified base name of the bundle to use.
61 * <p>
62 * This bundle base name must never be <code>null</code>.
63 *
64 * @serial
65 */
66 protected final String bundleBaseName;
67
68 // --- members --------------------------------------------------------------
69
70 // ****************************** Initializer *******************************
71
72 // ****************************** Constructors ******************************
73
74 /**
75 * Default constructor.
76 *
77 * @param code the error or exception code of the exception.
78 * @param bundleBaseName the fully qualified name of the bundle to use.
79 * @param resourceKey the localization key to fetch messages from message
80 * bundles.
81 */
82 public LocalizedInfo(final Code code, final String bundleBaseName,
83 final String resourceKey)
84 {
85 this.resourceKey = resourceKey != null ? resourceKey : code.getCode();
86 this.bundleBaseName =
87 bundleBaseName != null ? bundleBaseName : createDefaultBundleName(code);
88 }
89
90 // ****************************** Inner Classes *****************************
91
92 // ********************************* Methods ********************************
93
94 // --- init -----------------------------------------------------------------
95
96 /**
97 * Created the default bundle name for the given code if no bundle name is
98 * provided. The name is build by using the class name of the code
99 * implementation and the {@link LocalizedInfo#BUNDLE_NAME_SUFFIX}.
100 *
101 * @param code the code that provides the bundle name prefix.
102 * @return the default bundle name derived from the code's class.
103 */
104 private String createDefaultBundleName(final Code code)
105 {
106 final String defaultBundleBaseName =
107 code.getClass().getName() + BUNDLE_NAME_SUFFIX;
108 return defaultBundleBaseName;
109 }
110
111 // --- get&set --------------------------------------------------------------
112
113 /**
114 * Returns the localization key to fetch messages from message bundles. If
115 * this value is not set, the {@link String} representation of the code
116 * instance is used.
117 *
118 * @return the localization key to fetch messages from message bundles.
119 */
120 public String getResourceKey()
121 {
122 return this.resourceKey;
123 }
124
125 /**
126 * Returns the fully qualified base name of the bundle to use.
127 *
128 * @return the fully qualified base name of the bundle to use.
129 */
130 public String getBundleBaseName()
131 {
132 return this.bundleBaseName;
133 }
134
135 // --- business -------------------------------------------------------------
136
137 /**
138 * Returns the localized message for the given locale.
139 *
140 * @param throwable the throwable to construct the localized message.
141 * @param locale the locale for which the message is requested.
142 * @param loader the loader to read the message bundle.
143 * @return returns the localized message of this exception.
144 */
145 public String getLocalizedMessage(final Throwable throwable,
146 final Locale locale, final ClassLoader loader)
147 {
148 final I18nExceptionContext context =
149 I18nExceptionContextManager.getInstance().getContext(loader);
150 final MessageTemplate template = context.getMessageTemplateId();
151 if (MessageTemplate.SINGLE_LINE_STANDARD == template)
152 {
153 return getLocalizedSingleLineMessage(throwable, locale, loader, true);
154 }
155 if (MessageTemplate.SINGLE_LINE_SUMMARY_ONLY == template)
156 {
157 return getLocalizedSingleLineMessage(throwable, locale, loader, false);
158 }
159 else // multi-line all
160 {
161 return getLocalizedTextMessage(throwable, locale, loader);
162 }
163 }
164
165 /**
166 * Returns the localized message for the given locale.
167 *
168 * @param throwable the throwable to construct the localized message.
169 * @param locale the locale for which the message is requested.
170 * @param loader the loader to read the message bundle.
171 * @return returns the localized message of this exception.
172 */
173 public String getLocalizedSingleLineMessage(final Throwable throwable,
174 final Locale locale, final ClassLoader loader, final boolean standard)
175 {
176 final SingleLineTextHandler handler =
177 new SingleLineTextHandler(bundleBaseName, resourceKey, standard);
178 return handler.getMessage(throwable, locale, loader);
179 }
180
181 /**
182 * Returns the structured and localized message for the given locale,
183 * returning each part of information that is provided.
184 *
185 * @param throwable the throwable to construct the localized message.
186 * @param locale the locale for which the message is requested.
187 * @param loader the loader to read the message bundle.
188 * @return returns the localized message of this exception.
189 */
190 public String getLocalizedTextMessage(final Throwable throwable,
191 final Locale locale, final ClassLoader loader)
192 {
193 final StructuredTextHandler handler =
194 new StructuredTextHandler(bundleBaseName, resourceKey);
195 return handler.getMessage(throwable, locale, loader);
196 }
197
198 /**
199 * Returns the localized message for the given locale.
200 *
201 * @param throwable the throwable to construct the localized message.
202 * @param keyPrefix the prefix of the key to load from the bundle.
203 * @param locale the locale for which the message is requested.
204 * @param messageType the type of message to localize.
205 * @param loader the loader to read the message bundle.
206 * @return returns the localized message of this exception.
207 */
208 public String getLocalizedMessage(final Throwable throwable,
209 final String keyPrefix, final Locale locale,
210 final MessageType messageType, final ClassLoader loader)
211 {
212 final SingleLineTextHandler handler =
213 new SingleLineTextHandler(bundleBaseName, resourceKey, true);
214 try
215 {
216 return handler.fetchLocalizedMessage(throwable, keyPrefix, locale,
217 messageType, loader);
218 }
219 catch (final MissingResourceException e)
220 {
221 return keyPrefix + '.' + messageType;
222 }
223 }
224
225 // --- object basics --------------------------------------------------------
226
227 }