001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/i18n/CmsMessageContainer.java,v $
003: * Date : $Date: 2008-02-27 12:05:47 $
004: * Version: $Revision: 1.12 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.i18n;
033:
034: import java.io.Serializable;
035: import java.util.Locale;
036:
037: /**
038: * Contains a localized message key, it's arguments and a <code>{@link I_CmsMessageBundle}</code>.<p>
039: *
040: * Used for delaying the actual message lookup from the bundle to the time the message is displayed,
041: * not generated. This is used for localizing internal OpenCms messages. If a message is generated internally by OpenCms,
042: * at the time no information about the context of the current user may be available. The message is therefore
043: * passed to the class generating the output, where a user context usually exists. Finally, the message is rendered
044: * with the locale of the available user, or the OpenCms default locale if no user is available.<p>
045: *
046: * @author Alexander Kandzior
047: *
048: * @version $Revision: 1.12 $
049: *
050: * @since 6.0.0
051: *
052: * @see org.opencms.i18n.I_CmsMessageBundle
053: */
054: public class CmsMessageContainer implements Serializable {
055:
056: /** Serial version UID required for safe serialization. */
057: private static final long serialVersionUID = 2844402574674092147L;
058:
059: /** The message arguments to use. */
060: protected Object[] m_args;
061:
062: /** The OpenCms message bundle to read the message from. */
063: protected I_CmsMessageBundle m_bundle;
064:
065: /** The message key to use. */
066: protected String m_key;
067:
068: /**
069: * Creates a new message container for a key without arguments.<p>
070: *
071: * @param bundle the OpenCms message bundle to read the message from
072: * @param key the message key to use
073: */
074: public CmsMessageContainer(I_CmsMessageBundle bundle, String key) {
075:
076: m_bundle = bundle;
077: m_key = key;
078: }
079:
080: /**
081: * Creates a new message container.<p>
082: *
083: * @param bundle the OpenCms message bundle to read the message from
084: * @param key the message key to use
085: * @param args the message arguments to use
086: */
087: public CmsMessageContainer(I_CmsMessageBundle bundle, String key,
088: Object[] args) {
089:
090: m_bundle = bundle;
091: m_key = key;
092: m_args = args;
093: }
094:
095: /**
096: * Returns the message arguments to use.<p>
097: *
098: * @return the message arguments to use
099: */
100: public Object[] getArgs() {
101:
102: return m_args;
103: }
104:
105: /**
106: * Returns the message bundle used by this container.<p>
107: *
108: * @return the message bundle used by this container
109: */
110: public I_CmsMessageBundle getBundle() {
111:
112: return m_bundle;
113: }
114:
115: /**
116: * Returns the message key to use.<p>
117: *
118: * @return the message key to use
119: */
120: public String getKey() {
121:
122: return m_key;
123: }
124:
125: /**
126: * Returns the localized message described by this container for the OpenCms default locale.<p>
127: *
128: * @return the localized message described by this container for the OpenCms default locale
129: */
130: public String key() {
131:
132: if (getBundle() == null) {
133: return getKey();
134: }
135: return getBundle().getBundle().key(getKey(), getArgs());
136: }
137:
138: /**
139: * Returns the localized message described by this container for the given locale.<p>
140: *
141: * @param locale the locale to use
142: * @return the localized message described by this container for the given locale
143: */
144: public String key(Locale locale) {
145:
146: if (getBundle() == null) {
147: return getKey();
148: }
149: return getBundle().getBundle(locale).key(getKey(), getArgs());
150: }
151:
152: /**
153: * @see java.lang.Object#toString()
154: */
155: public String toString() {
156:
157: StringBuffer result = new StringBuffer();
158:
159: result.append('[');
160: result.append(this .getClass().getName());
161: result.append(", bundle: ");
162: result.append(getBundle().getBundleName());
163: result.append(", key: ");
164: result.append(getKey());
165: Object[] args = getArgs();
166: if (args != null) {
167: for (int i = 0; i < args.length; i++) {
168: result.append(", arg");
169: result.append(i + 1);
170: result.append(": ");
171: result.append(args[i]);
172: }
173: }
174: result.append(']');
175:
176: return result.toString();
177: }
178: }
|