View Javadoc

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.util.Locale;
19  import java.util.ResourceBundle;
20  
21  /**
22   * Helper to render the messages of an exception in a structured multi-line
23   * text.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision:591 $
27   */
28  class StructuredTextHandler extends AbstractTextHandler
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    // --- members --------------------------------------------------------------
35  
36    // ****************************** Initializer *******************************
37  
38    // ****************************** Constructors ******************************
39  
40    /**
41     * Default constructor.
42     *
43     * @param bundleBaseName the fully qualified base name of the bundle to use.
44     * @param resourceKey the localization key to fetch messages from message
45     *          bundles.
46     */
47    public StructuredTextHandler(final String bundleBaseName,
48        final String resourceKey)
49    {
50      super(bundleBaseName, resourceKey);
51    }
52  
53    // ****************************** Inner Classes *****************************
54  
55    // ********************************* Methods ********************************
56  
57    // --- init -----------------------------------------------------------------
58  
59    // --- get&set --------------------------------------------------------------
60  
61    // --- business -------------------------------------------------------------
62  
63    /**
64     * Returns the structured and localized message for the given locale,
65     * returning each part of information that is provided.
66     *
67     * @param throwable the throwable to construct the localized message.
68     * @param locale the locale for which the message is requested.
69     * @param loader the loader to read the message bundle.
70     * @return returns the localized message of this exception.
71     */
72    @Override
73    public String getMessage(final Throwable throwable, final Locale locale,
74        final ClassLoader loader)
75    {
76      final StringBuilder buffer = new StringBuilder();
77      final ResourceBundle messageBundle =
78          ResourceBundle.getBundle(
79              "de.smartics.exceptions.i18n.message.StructuredTextBundle", locale,
80              loader);
81  
82      appendMessage(throwable, locale, loader, buffer, MessageType.TITLE, null);
83  
84      appendWhatHasHappendMessage(throwable, locale, loader, buffer,
85          messageBundle);
86  
87      appendMessage(throwable, locale, loader, buffer,
88          MessageType.IMPLICATIONS_ON_CURRENT_TASK,
89          messageBundle.getString("header.implications"));
90  
91      appendMessage(throwable, locale, loader, buffer,
92          MessageType.WHAT_TO_DO_NOW,
93          messageBundle.getString("header.whatToDoNow"));
94  
95      appendMessage(throwable, locale, loader, buffer, MessageType.URL,
96          messageBundle.getString("header.url"));
97  
98      return buffer.toString();
99    }
100 
101   private void appendWhatHasHappendMessage(final Throwable throwable,
102       final Locale locale, final ClassLoader loader,
103       final StringBuilder buffer, final ResourceBundle messageBundle)
104   {
105     final String summaryMessage =
106         internalGetLocalizedMessage(throwable, resourceKey, locale,
107             MessageType.SUMMARY, loader);
108     final String detailsMessage =
109         internalGetLocalizedMessage(throwable, resourceKey, locale,
110             MessageType.DETAILS, loader);
111     if (summaryMessage != null || detailsMessage != null)
112     {
113       buffer.append(' ')
114           .append(messageBundle.getString("header.whatHasHappened"))
115           .append('\n');
116       if (summaryMessage != null)
117       {
118         buffer.append("  ");
119         if (detailsMessage != null)
120         {
121           buffer.append(
122               messageBundle.getString("header.whatHasHappened.summary"))
123               .append(": ");
124         }
125 
126         buffer.append(summaryMessage).append('\n');
127       }
128       if (detailsMessage != null)
129       {
130         buffer.append("  ");
131         if (summaryMessage != null)
132         {
133           buffer.append(
134               messageBundle.getString("header.whatHasHappened.details"))
135               .append(": ");
136         }
137         buffer.append(detailsMessage).append('\n');
138       }
139     }
140   }
141 
142   private void appendMessage(final Throwable throwable, final Locale locale,
143       final ClassLoader loader, final StringBuilder buffer,
144       final MessageType type, final String headline)
145   {
146     final String message =
147         internalGetLocalizedMessage(throwable, resourceKey, locale, type,
148             loader);
149     if (message != null)
150     {
151       if (headline != null)
152       {
153         buffer.append(' ').append(headline).append("\n  ");
154       }
155 
156       buffer.append(message).append('\n');
157     }
158   }
159 
160   // --- object basics --------------------------------------------------------
161 
162 }