1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.exceptions.i18n.message;
17
18 import java.util.Locale;
19 import java.util.ResourceBundle;
20
21
22
23
24
25
26
27
28 class StructuredTextHandler extends AbstractTextHandler
29 {
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public StructuredTextHandler(final String bundleBaseName,
48 final String resourceKey)
49 {
50 super(bundleBaseName, resourceKey);
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
161
162 }