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