KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > AbstractMessages


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import java.text.MessageFormat JavaDoc;
18 import java.util.Locale JavaDoc;
19
20 import org.apache.hivemind.HiveMind;
21 import org.apache.hivemind.Messages;
22 import org.apache.hivemind.util.Defense;
23
24 /**
25  * Abstract base class for implementations of {@link org.apache.hivemind.Messages}. Subclasses must
26  * provide {@link #getLocale()}and {@link #findMessage(String)} implementations.
27  *
28  * @author Howard M. Lewis Ship
29  * @since 1.1
30  */

31 public abstract class AbstractMessages implements Messages
32 {
33     public final String JavaDoc format(String JavaDoc key, Object JavaDoc[] args)
34     {
35         String JavaDoc pattern = getMessage(key);
36
37         for (int i = 0; i < args.length; i++)
38         {
39             Object JavaDoc arg = args[i];
40
41             if (arg != null && arg instanceof Throwable JavaDoc)
42                 args[i] = extractMessage((Throwable JavaDoc) arg);
43         }
44
45         // This ugliness is mandated for JDK 1.3 compatibility, which has a bug
46
// in MessageFormat ... the
47
// pattern is applied in the constructor, using the system default Locale,
48
// regardless of what locale is later specified!
49
// It appears that the problem does not exist in JDK 1.4.
50

51         MessageFormat JavaDoc messageFormat = new MessageFormat JavaDoc("");
52         messageFormat.setLocale(getLocale());
53         messageFormat.applyPattern(pattern);
54
55         return messageFormat.format(args);
56     }
57
58     private final String JavaDoc extractMessage(Throwable JavaDoc t)
59     {
60         String JavaDoc message = t.getMessage();
61
62         return HiveMind.isNonBlank(message) ? message : t.getClass().getName();
63     }
64
65     public final String JavaDoc format(String JavaDoc key, Object JavaDoc arg0)
66     {
67         return format(key, new Object JavaDoc[]
68         { arg0 });
69     }
70
71     public final String JavaDoc format(String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1)
72     {
73         return format(key, new Object JavaDoc[]
74         { arg0, arg1 });
75     }
76
77     public final String JavaDoc format(String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2)
78     {
79         return format(key, new Object JavaDoc[]
80         { arg0, arg1, arg2 });
81     }
82
83     /** @since 1.2 */
84     public final boolean containsMessage(String JavaDoc key)
85     {
86         return findMessage(key) != null;
87     }
88
89     public final String JavaDoc getMessage(String JavaDoc key)
90     {
91         Defense.notNull(key, "key");
92
93         String JavaDoc result = findMessage(key);
94
95         if (result == null)
96             result = "[" + key.toUpperCase() + "]";
97
98         return result;
99     }
100
101     /**
102      * Concrete implementations must provide a non-null Locale.
103      */

104
105     protected abstract Locale JavaDoc getLocale();
106
107     /**
108      * Concrete implementations must implement this method.
109      * <p>
110      * Note: starting with release 1.2, it is no longer considered an error if the key does not
111      * match a known message (i.e., due to {@link #containsMessage(String)}). Prior to 1.2, some
112      * implementations would log an error in that situation.
113      *
114      * @param key
115      * @return the localized message for the key, or null if no such message exists.
116      */

117
118     protected abstract String JavaDoc findMessage(String JavaDoc key);
119
120 }
Popular Tags