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.jsp.decorator;
029
030import java.util.HashMap;
031import java.util.Locale;
032import java.util.Map;
033import java.util.Set;
034
035/**
036 * CmsDecorationBundle, contains a map of merged CmsDEcorationMaps.<p>
037 *
038 * The map inside the decoration bundle uses the decoration as keys and CmsDecorationObjects as values.<p>
039 *
040 * A decoration bundle contains either all decoarions for one locale (similar to a resource bundle), or
041 * is locale independend. If its a locale independend bundle, the included locale is set to null.
042 *
043 *
044 * @since 6.1.3
045 */
046public class CmsDecorationBundle {
047
048    /** The bundle map. */
049    private Map<String, CmsDecorationObject> m_bundle;
050
051    /** The locale of this bundle. */
052    private Locale m_locale;
053
054    /**
055     * Constructor, creates a new, empty CmsDecorationBundle.<p>
056     */
057    public CmsDecorationBundle() {
058
059        m_bundle = new HashMap<String, CmsDecorationObject>();
060        m_locale = null;
061    }
062
063    /**
064     * Constructor, creates a new CmsDecorationBundle for a given locale.<p>
065     *
066     * @param locale the locale of this bundle or null
067     */
068    public CmsDecorationBundle(Locale locale) {
069
070        m_bundle = new HashMap<String, CmsDecorationObject>();
071        m_locale = locale;
072
073    }
074
075    /**
076     * Gets an object from the decoration bundle.<p>
077     * @param key the key of the object ot get
078     * @return the value matching the key or null.
079     */
080    public Object get(Object key) {
081
082        return m_bundle.get(adjustKey(key.toString()));
083    }
084
085    /**
086     * Gets the map of all decoarion bundle entries.<p>
087     * @return map of all decoarion bundle entries
088     */
089    public Map<String, CmsDecorationObject> getAll() {
090
091        return m_bundle;
092    }
093
094    /**
095     * Gets the locale of this decoration bundle.<p>
096     * @return locale of the decoration bundle
097     */
098    public Locale getLocale() {
099
100        return m_locale;
101    }
102
103    /**
104     * Gets the keyset of the decoration bundle map.<p>
105     * @return keyset of the decoration bundle map
106     */
107    public Set<String> keySet() {
108
109        return m_bundle.keySet();
110    }
111
112    /**
113     * Stores an obiect in the decoration bundle.<p>
114     * @param key the key of the object to store
115     * @param value the value of the object to store
116     */
117    public void put(String key, CmsDecorationObject value) {
118
119        m_bundle.put(key, value);
120    }
121
122    /**
123     * Puts a complete map of objects into bundle.<p>
124     * @param map the map to put into the bundle
125     */
126    public void putAll(Map<String, CmsDecorationObject> map) {
127
128        m_bundle.putAll(map);
129    }
130
131    /**
132     * Sets the locale of the decoration bundle.<p>
133     * @param locale the locale to set
134     */
135    public void setLocale(Locale locale) {
136
137        m_locale = locale;
138    }
139
140    /**
141     * Adjusts the key for the decoration.<p>
142     * The following adjustments are made:
143     * <ul>
144     * <li>&nbsp; is replaced with space</li>
145     * <li>multiple spaces are replaced with a single space</li>
146     * </ul>
147     * @param key the key to adjust
148     * @return the adjusted key
149     */
150    private String adjustKey(String key) {
151
152        // replace the &nbsp; with spaces
153        key = key.replaceAll("&nbsp;", " ");
154        // now eleiminate all double spaces
155        int keyLen;
156        do {
157            keyLen = key.length();
158            key = key.replaceAll("  ", " ");
159        } while (key.length() != keyLen);
160        return key;
161    }
162}