001
002package org.opencms.i18n;
003
004import java.util.LinkedHashMap;
005import java.util.ListResourceBundle;
006import java.util.Locale;
007import java.util.Map;
008import java.util.ResourceBundle;
009
010/**
011 * A list based resource bundle that with increased visibility of some key methods.<p>
012 *
013 * @since 8.0.1
014 *
015 * @see org.opencms.i18n.CmsResourceBundleLoader
016 */
017public class CmsListResourceBundle extends ListResourceBundle implements I_CmsResourceBundle {
018
019    /** The locale to use. */
020    protected Locale m_locale;
021
022    /** The configured resource key / value pairs in a Map. */
023    private Map<String, String> m_bundleMap;
024
025    /** The configured resource key / value pairs as Objects. */
026    private Object[][] m_bundleObjects;
027
028    /**
029     * Create a new list resource bundle for the XML.<p>
030     */
031    public CmsListResourceBundle() {
032
033        m_bundleMap = new LinkedHashMap<String, String>();
034    }
035
036    /**
037     * Create a new list resource bundle as copy of an existing one.<p>
038     *
039     * @param bundleMap the resource bundle map
040     * @param bundleObjects the resource bundle object to copy
041     */
042    private CmsListResourceBundle(Map<String, String> bundleMap, Object[][] bundleObjects) {
043
044        m_bundleMap = bundleMap;
045        m_bundleObjects = bundleObjects;
046    }
047
048    /**
049     * Adds a message to this list bundle.<p>
050     *
051     * Please note:
052     * All additions after the initial call to {@link #getContents()} are ignored.<p>
053     *
054     * @param key the message key
055     * @param value the message itself
056     */
057    public void addMessage(String key, String value) {
058
059        if (m_bundleMap != null) {
060            m_bundleMap.put(key, value);
061        }
062    }
063
064    /**
065     * Returns a typed clone of this resource bundle.<p>
066     *
067     * This is required in order to make sure the objects in the permanent cache of the
068     * list based resource bundles which are usually read from the XML are never changed.<p>
069     *
070     * @return a typed clone of this resource bundle
071     */
072    public CmsListResourceBundle getClone() {
073
074        return new CmsListResourceBundle(m_bundleMap, m_bundleObjects);
075    }
076
077    /**
078     * @see java.util.ListResourceBundle#getContents()
079     */
080    @Override
081    public Object[][] getContents() {
082
083        if ((m_bundleObjects == null) && (m_bundleMap != null)) {
084            // fill object array based on map
085            m_bundleObjects = new String[m_bundleMap.size()][2];
086            int i = 0;
087            for (Map.Entry<String, String> entry : m_bundleMap.entrySet()) {
088                m_bundleObjects[i][0] = entry.getKey();
089                m_bundleObjects[i][1] = entry.getValue();
090                i++;
091            }
092            // remove the map, we don't need it anymore
093            m_bundleMap = null;
094        }
095        return m_bundleObjects;
096    }
097
098    /**
099     * @see java.util.ResourceBundle#getLocale()
100     */
101    @Override
102    public Locale getLocale() {
103
104        return m_locale;
105    }
106
107    /**
108     * Sets the locale used for this resource bundle.<p>
109     *
110     * @param l the locale to set
111     */
112    public void setLocale(Locale l) {
113
114        m_locale = l;
115    }
116
117    /**
118     * @see java.util.ResourceBundle#setParent(java.util.ResourceBundle)
119     */
120    @Override
121    public void setParent(ResourceBundle p) {
122
123        super.setParent(p);
124    }
125}