View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons-sandbox//i18n/src/java/org/apache/commons/i18n/LocalizedBundle.java,v 1.3 2004/12/29 17:03:55 dflorey Exp $
3    * $Revision: 480489 $
4    * $Date: 2006-11-29 09:00:46 +0000 (Wed, 29 Nov 2006) $
5    *
6    * ====================================================================
7    *
8    * Licensed to the Apache Software Foundation (ASF) under one or more
9    * contributor license agreements.  See the NOTICE file distributed with
10   * this work for additional information regarding copyright ownership.
11   * The ASF licenses this file to You under the Apache License, Version 2.0
12   * (the "License"); you may not use this file except in compliance with
13   * the License.  You may obtain a copy of the License at
14   *
15   *     http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the License for the specific language governing permissions and
21   * limitations under the License.
22   *
23   */
24  
25  package org.apache.commons.i18n;
26  
27  import java.io.Serializable;
28  import java.util.Locale;
29  
30  /**
31   * 
32   * <p>The <code>LocalizedBundle</code> class represents a bundle of localized messages that
33   * belong together.</p>
34   * <p> The <code>LocalizedBundle</code> class itself contains the message id and the arguments
35   * that might be used to include dynamic values into the message text.
36   * The message id specifies the base name of the message bundle.
37   * Single entries of the message bundle can be retrieved using the <code>getText()</code> method by passing
38   * the key of the desired message entry.</p>  
39   * This class should not be used directly in order to retrieve entries of a message bundle. It is recommended
40   * to subclass the <code>LocalizedBundle</code> class in order to define a specific localized bundle. 
41   * @see org.apache.commons.i18n.bundles.TextBundle, MessageBundle, ErrorBundle
42   */
43  public class LocalizedBundle implements Serializable {
44      public final static String ID = "id";
45  
46      protected String providerId;
47      protected String id;
48      protected Object[] arguments;
49  
50    /**
51     * @param providerId The name of the message provider (i.e. source) to use for the message
52     * @param messageId The messageId refers the corresponding bundle in the file containing
53     * the localized messages.
54     */
55    public LocalizedBundle(String providerId, String messageId) {
56        this(providerId, messageId, new Object[0]);
57    }
58  
59      /**
60       * @param messageId The messageId refers the corresponding bundle in the file containing
61       * the localized messages.
62       */
63      public LocalizedBundle(String messageId) {
64          this(messageId, new Object[0]);
65      }
66  
67      /**
68       * @param messageId The messageId refers the corresponding bundle in the file containing
69       * the localized messages.
70       * @param arguments An array of objects containing arguments for the messages. These arguments
71       * are used to insert dynamic values into the localized messages.
72       */
73      public LocalizedBundle(String messageId, Object[] arguments) {
74          this.id = messageId;
75          this.arguments = arguments;
76      }
77  
78    /**
79     * @param providerId The name of the message provider (i.e. source) to use for the message
80     * @param messageId The messageId refers the corresponding bundle in the file containing
81     * the localized messages.
82     * @param arguments An array of objects containing arguments for the messages. These arguments
83     * are used to insert dynamic values into the localized messages.
84     */
85    public LocalizedBundle(String providerId, String messageId, Object[] arguments) {
86      this.providerId = providerId;
87      this.id = messageId;
88        this.arguments = arguments;
89    }
90  
91      /**
92       * @return returns the id of this bundle
93       */
94      public String getId() {
95          return id;
96      }
97  
98      /**
99       * @return returns the arguments associated with this message bundle
100      */
101     public Object[] getArguments() {
102     	return arguments;
103     }
104 
105     /**
106      * @return The name of the message provider (i.e. source) to use for the message  
107      */
108     public String getProviderId() {
109         return providerId;
110     }
111 
112     /**
113      * @param key the key of the specific message entry in the message bundle
114      * @param locale the locale for that this message should be rendered
115      * @return returns the text of the desired message entry for the given locale  
116      * @throws MessageNotFoundException if an entry with the given key can not be found
117      * in this bundle
118      */
119     public String getEntry(String key, Locale locale) throws MessageNotFoundException {
120         return providerId != null ?
121             MessageManager.getText(providerId, id, key, arguments, locale) :
122             MessageManager.getText(id, key, arguments, locale);
123     }
124 
125     /**
126      * @param key the key of the specific message entry in the message bundle
127      * @param locale the locale for that this message should be rendered
128      * @param defaultText the text to be returned if no entry was found for the given key
129      * @return returns the text of the desired message entry for the given locale  
130      */
131     public String getEntry(String key, Locale locale, String defaultText) {
132         return providerId != null ?
133             MessageManager.getText(providerId, id, key, arguments, locale, defaultText) :
134             MessageManager.getText(id, key, arguments, locale, defaultText);
135     }
136 }