View Javadoc

1   /*
2    * Copyright 2007-2013 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.util.Locale;
19  
20  /**
21   * Helper to render the messages of an exception in a structured multi-line
22   * text.
23   */
24  class SingleLineTextHandler extends AbstractTextHandler
25  {
26    // ********************************* Fields *********************************
27  
28    // --- constants ------------------------------------------------------------
29  
30    // --- members --------------------------------------------------------------
31  
32    /**
33     * The flag that adds all information to the exception text (
34     * <code>true</code>) or only the summary (<code>false</code>).
35     */
36    private final boolean standard;
37  
38    // ****************************** Initializer *******************************
39  
40    // ****************************** Constructors ******************************
41  
42    /**
43     * Default constructor.
44     *
45     * @param bundleBaseName the fully qualified base name of the bundle to use.
46     * @param resourceKey the localization key to fetch messages from message
47     *          bundles.
48     * @param standard the flag that adds all information to the exception text (
49     *          <code>true</code>) or only the summary (<code>false</code>).
50     */
51    public SingleLineTextHandler(final String bundleBaseName,
52        final String resourceKey, final boolean standard)
53    {
54      super(bundleBaseName, resourceKey);
55      this.standard = standard;
56    }
57  
58    // ****************************** Inner Classes *****************************
59  
60    // ********************************* Methods ********************************
61  
62    // --- init -----------------------------------------------------------------
63  
64    // --- get&set --------------------------------------------------------------
65  
66    // --- business -------------------------------------------------------------
67  
68    /**
69     * Returns the structured and localized message for the given locale,
70     * returning each part of information that is provided.
71     *
72     * @param bean the throwable to construct the localized message.
73     * @param locale the locale for which the message is requested.
74     * @param loader the loader to read the message bundle.
75     * @return returns the localized message of this exception.
76     */
77    public String getMessage(final Object bean, final Locale locale,
78        final ClassLoader loader)
79    {
80      final StringBuilder buffer = new StringBuilder();
81      if (standard)
82      {
83        final String message =
84            getLocalizedMessage(bean, resourceKey, locale,
85                MessageType.TITLE, loader);
86        buffer.append(message).append(' ');
87      }
88  
89      final String summaryMessage =
90          getLocalizedMessage(bean, resourceKey, locale,
91              MessageType.SUMMARY, loader);
92      buffer.append(summaryMessage).append(' ');
93  
94      if (standard)
95      {
96        final String message =
97            getLocalizedMessage(bean, resourceKey, locale,
98                MessageType.DETAILS, loader);
99        buffer.append(message);
100     }
101 
102     return buffer.toString();
103   }
104 
105   // --- object basics --------------------------------------------------------
106 
107 }