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