KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > i18n > CmsMessageContainer


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/i18n/CmsMessageContainer.java,v $
3  * Date : $Date: 2006/03/27 14:53:01 $
4  * Version: $Revision: 1.9 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.i18n;
33
34 import java.util.Locale JavaDoc;
35
36 /**
37  * Contains a localized message key, it's arguments and a <code>{@link I_CmsMessageBundle}</code>.<p>
38  *
39  * Used for delaying the actual message lookup from the bundle to the time the message is displayed,
40  * not generated. This is used for localizing internal OpenCms messages. If a message is generated internally by OpenCms,
41  * at the time no information about the context of the current user may be available. The message is therefore
42  * passed to the class generating the output, where a user context usually exists. Finally, the message is rendered
43  * with the locale of the available user, or the OpenCms default locale if no user is available.<p>
44  *
45  * @author Alexander Kandzior
46  *
47  * @version $Revision: 1.9 $
48  *
49  * @since 6.0.0
50  *
51  * @see org.opencms.i18n.I_CmsMessageBundle
52  */

53 public class CmsMessageContainer {
54
55     /** The message arguments to use. */
56     protected Object JavaDoc[] m_args;
57
58     /** The OpenCms message bundle to read the message from. */
59     protected I_CmsMessageBundle m_bundle;
60
61     /** The message key to use. */
62     protected String JavaDoc m_key;
63
64     /**
65      * Creates a new message container for a key without arguments.<p>
66      *
67      * @param bundle the OpenCms message bundle to read the message from
68      * @param key the message key to use
69      */

70     public CmsMessageContainer(I_CmsMessageBundle bundle, String JavaDoc key) {
71
72         m_bundle = bundle;
73         m_key = key;
74     }
75
76     /**
77      * Creates a new message container.<p>
78      *
79      * @param bundle the OpenCms message bundle to read the message from
80      * @param key the message key to use
81      * @param args the message arguments to use
82      */

83     public CmsMessageContainer(I_CmsMessageBundle bundle, String JavaDoc key, Object JavaDoc[] args) {
84
85         m_bundle = bundle;
86         m_key = key;
87         m_args = args;
88     }
89
90     /**
91      * Returns the message arguments to use.<p>
92      *
93      * @return the message arguments to use
94      */

95     public Object JavaDoc[] getArgs() {
96
97         return m_args;
98     }
99
100     /**
101      * Returns the message bundle used by this container.<p>
102      *
103      * @return the message bundle used by this container
104      */

105     public I_CmsMessageBundle getBundle() {
106
107         return m_bundle;
108     }
109
110     /**
111      * Returns the message key to use.<p>
112      *
113      * @return the message key to use
114      */

115     public String JavaDoc getKey() {
116
117         return m_key;
118     }
119
120     /**
121      * Returns the localized message described by this container for the OpenCms default locale.<p>
122      *
123      * @return the localized message described by this container for the OpenCms default locale
124      */

125     public String JavaDoc key() {
126
127         if (getBundle() == null) {
128             return getKey();
129         }
130         return getBundle().getBundle().key(getKey(), getArgs());
131     }
132
133     /**
134      * Returns the localized message described by this container for the given locale.<p>
135      *
136      * @param locale the locale to use
137      * @return the localized message described by this container for the given locale
138      */

139     public String JavaDoc key(Locale JavaDoc locale) {
140
141         if (getBundle() == null) {
142             return getKey();
143         }
144         return getBundle().getBundle(locale).key(getKey(), getArgs());
145     }
146
147     /**
148      * @see java.lang.Object#toString()
149      */

150     public String JavaDoc toString() {
151
152         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
153
154         result.append('[');
155         result.append(this.getClass().getName());
156         result.append(", bundle: ");
157         result.append(getBundle().getBundleName());
158         result.append(", key: ");
159         result.append(getKey());
160         Object JavaDoc[] args = getArgs();
161         if (args != null) {
162             for (int i = 0; i < args.length; i++) {
163                 result.append(", arg");
164                 result.append(i + 1);
165                 result.append(": ");
166                 result.append(args[i]);
167             }
168         }
169         result.append(']');
170
171         return result.toString();
172     }
173 }
174
Popular Tags