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