KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > tools > localization > L10NMessage


1 package org.tigris.scarab.tools.localization;
2
3
4 /* ================================================================
5  * Copyright (c) 2000 CollabNet. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement: "This product includes
20  * software developed by CollabNet (http://www.collab.net/)."
21  * Alternately, this acknowlegement may appear in the software itself, if
22  * and wherever such third-party acknowlegements normally appear.
23  *
24  * 4. The hosted project names must not be used to endorse or promote
25  * products derived from this software without prior written
26  * permission. For written permission, please contact info@collab.net.
27  *
28  * 5. Products derived from this software may not use the "Tigris" name
29  * nor may "Tigris" appear in their names without prior written
30  * permission of CollabNet.
31  *
32  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
36  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
40  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  * ====================================================================
45  *
46  * This software consists of voluntary contributions made by many
47  * individuals on behalf of CollabNet.
48  */

49
50
51 import org.tigris.scarab.tools.ScarabLocalizationTool;
52
53 /**
54  * This class defines localizable messages. Each Resource
55  * may contain dynamic parameters ({$1}, {$2}, etc.) These
56  * parameters get resolved, when the ResourceBundle is called
57  * for the String representation of a specific Resource key.
58  *
59  * L10NMessage always contains an L10N key and may contain
60  * an arbitrary set of parameters, which will be used as values
61  * for the {$n} variables in the Resource.
62  *
63  * Note: We check for two special cases:
64  * <ol>
65  * <li>If a paramater is itself an L10NInstance, it will be resolved,
66  * before it's value is used.</li>
67  * <li>If a parameter is a scarabException, it is resolved, before
68  * it's value is used.</li>
69  * <li>If a parameter is an ordinary Exception, it's stored message is
70  * retrieved via Exception.getMessage() before it's value is used.
71  * </li>
72  * </ol>
73  * @author <a HREF="mailto:dabbous@saxess.com">Hussayn Dabbous</a>
74  */

75 public class L10NMessage implements Localizable
76 {
77     /**
78      * The Localization key to be used.
79      */

80     LocalizationKey l10nKey;
81
82     /**
83      * The list of localization parameters. may contain L10NInstnaces and
84      * Exceptions. May be null (no parameters)
85      */

86     private Object JavaDoc[] parameters;
87
88     /**
89      * Constructor always needs the L10N key for creation.
90      * @param theKey
91      */

92     public L10NMessage(LocalizationKey theKey)
93     {
94         l10nKey = theKey;
95         this.parameters = null;
96     }
97
98     /**
99      * Constructor with parameters. theParameters is an array of objects
100      * and may contain Exceptions and
101      * L10NInstances.
102      * @param theKey
103      * @param theParameters
104      */

105     public L10NMessage(LocalizationKey theKey, Object JavaDoc[] theParameters)
106     {
107         l10nKey = theKey;
108         this.parameters = theParameters;
109     }
110
111     /**
112      * Convenience constructor with one extra parameter.
113      * @param theKey
114      * @param p1
115      */

116     public L10NMessage(LocalizationKey theKey, Object JavaDoc p1)
117     {
118         this(theKey, new Object JavaDoc[]{p1});
119     }
120
121     /**
122      * Convenience constructor with two extra parameters.
123      * @param theKey
124      * @param p1
125      * @param p2
126      */

127     public L10NMessage(LocalizationKey theKey, Object JavaDoc p1, Object JavaDoc p2)
128     {
129         this(theKey, new Object JavaDoc[]{p1, p2});
130     }
131
132     /**
133      * Convenience constructor with three extra parameters.
134      * @param theKey
135      * @param p1
136      * @param p2
137      * @param p3
138      */

139     public L10NMessage(LocalizationKey theKey, Object JavaDoc p1, Object JavaDoc p2, Object JavaDoc p3)
140     {
141         this(theKey, new Object JavaDoc[]{p1, p2, p3});
142     }
143
144     /**
145      * resolve the instance to the ScarabLocalizationTool.DEFAULT_LOCALE
146      * Note: This method returns english messages independent of
147      * any l10n settings. it is preferreable to use
148      * {@link resolve(ScarabLocalizationTool) }
149      * @return the resolved String
150      */

151     public String JavaDoc getMessage()
152     {
153         ScarabLocalizationTool l10n = new ScarabLocalizationTool();
154         l10n.init(ScarabLocalizationTool.DEFAULT_LOCALE);
155         return getMessage(l10n);
156     }
157
158     /**
159      * Format the message using the specified ScarabLocalizationTool instance.
160      * The parameters are resolved recursively if necessary.
161      * @return a localized <code>String</code> representation of this message.
162      */

163     public String JavaDoc getMessage(final ScarabLocalizationTool l10n)
164     {
165         final int nbParameters = (parameters == null ? 0 : parameters.length);
166         final Object JavaDoc[] formatedParameters = new Object JavaDoc[nbParameters];
167         for (int index = 0; index < nbParameters; index++)
168         {
169             Object JavaDoc param = parameters[index];
170             if (param instanceof Localizable)
171             {
172                 formatedParameters[index] =
173                     ((Localizable) param).getMessage(l10n);
174             }
175             else if (param instanceof Throwable JavaDoc)
176             {
177                 // Note: We can not simply keep the parameter
178
// as is, because MessageFormat internally uses
179
// toString() which in turn should call Throwable.getLocalizedMessage()
180
// but:
181
// 1- Throwable.toString() is declared to use getMessage() even,
182
// when it also implements getLocalizedMessage()
183
// 2- Throwable.toString() also appends the class name, which we
184
// don't want.
185
//
186
// so we force the localize message ourselves:
187
Throwable JavaDoc t = (Throwable JavaDoc) param;
188                 formatedParameters[index] = t.getLocalizedMessage();
189             }
190             else
191             {
192                 formatedParameters[index] = param;
193             }
194         }
195         return l10n.format(l10nKey.toString(), formatedParameters);
196     }
197
198 }
199
Popular Tags