Coverage details for org.chwf.i18n.Message

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 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 met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.i18n;
34  
35 import java.io.Serializable;
36  
37 import org.chwf.config.Config;
38 import org.chwf.config.ConfigFactory;
39  
40 /**
41  * <p>A class for encapsulating an internationalizable message. It is
42  * designed to defer computation of the message, to handle the situation
43  * when the precise locale is not available at the time when the message
44  * is generated. All the information necessary to generate the message is
45  * stored in the <code>Message</code> object. It can be printed out using
46  * the message's <code>toString()</code> method when the message reaches
47  * a point where it is possible to determine the locale.</p>
48  *
49  * <p>Every message has a <b>context class</b>. This class will typically
50  * be the class that created the message. The context class is used to
51  * determine which configuration file should be used to load internationalized
52  * messages from.</p>
53  *
54  * <p>When the message is rendered, the precise locale is determined using
55  * {@link UserLocale#getLocale()}.</p>
56  *
57  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
58  */
59 public class Message implements Serializable {
60  
61   /** An empty array for constructing messages with no arguments. */
621  public static final Object[] NO_ARGUMENTS = new Object[0];
63  
64   /** The message pattern. */
65   private final String key;
66  
67   /** The message arguments. */
68   private final Object[] arguments;
69  
70   /** The context in which the message was generated. */
71   private final String context;
72  
73   /**
74    * Create a message object.<p>
75    *
76    * @param key The message key. If the key lookup fails, the key is used
77    * as the pattern instead.
78    * @param arguments The arguments for the pattern.
79    * @param context The context class.
80    * @return The message object.
81    */
82   public static Message getMessage(
83     String key,
84     Object[] arguments,
85     Class context) {
86  
87117    return new Message(key, arguments, context.getName());
88   }
89  
90   /**
91    * Create a message object.<p>
92    *
93    * @param key The message key. If the key lookup fails, the key is used
94    * as the pattern instead.
95    * @param arguments The arguments for the pattern.
96    * @param context The context resource.
97    * @return The message object.
98    */
99   public static Message getMessage(
100     String key,
101     Object[] arguments,
102     String context) {
103  
1043    return new Message(key, arguments, context);
105   }
106  
107   /**
108    * Constructor.
109    *
110    * @param key The message key. If the key lookup fails, the key is used
111    * as the pattern instead.
112    * @param arguments The message arguments.
113    * @param context The context in which the message was generated.
114    */
115120  private Message(String key, Object[] arguments, String context) {
116120    this.key = key;
117120    this.arguments = arguments;
118120    this.context = context;
119120  }
120  
121   /**
122    * Returns the formatted message using the user's locale.<p>
123    *
124    * @return The formatted message.
125    */
126   public String toString() {
12758    return Localizer.format(getPattern(), arguments);
128   }
129  
130   /**
131    * Gets the key.
132    *
133    * @return The key.
134    */
135   private String getKey() {
136116    return this.key;
137   }
138  
139   /**
140    * Get the pattern. First looks up in the configuration using the key. If
141    * this fails, uses the key as the pattern.
142    *
143    * @return The pattern.
144    */
145   private String getPattern() {
14658    Config config =
147       ConfigFactory.getConfig(this.context, UserLocale.getLocale());
14858    return config.get(getKey(), getKey());
149   }
150 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.