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 org.opencms.util.CmsMacroResolver;
031
032import java.util.Locale;
033
034/**
035 * The CmsDecorationObject defines a single text decoration.<p>
036 *
037 * It uses the information of a <code>{@link CmsDecorationDefintion}</code> to create the
038 * pre- and postfix for a text decoration.
039
040 *
041 * @since 6.1.3
042 */
043public class CmsDecorationObject {
044
045    /** Macro for the decoration. */
046    public static final String MACRO_DECORATION = "decoration";
047
048    /** Macro for the decoration key. */
049    public static final String MACRO_DECORATIONKEY = "decorationkey";
050
051    /** Macro for the language. */
052    public static final String MACRO_LANGUAGE = "language";
053
054    /** Macro for the locale. */
055    public static final String MACRO_LOCALE = "locale";
056
057    /** The decoration. */
058    private String m_decoration;
059
060    /** The CmsDecorationDefintion to be used by this decoration object. */
061    private CmsDecorationDefintion m_decorationDefinition;
062
063    /** The key for this decoration. */
064    private String m_decorationKey;
065
066    /** The locale of this decoration. */
067    private Locale m_locale;
068
069    /**
070     * Constructor, creates a new, empty decoration object.<p>
071     */
072    public CmsDecorationObject() {
073
074        m_decorationDefinition = new CmsDecorationDefintion();
075    }
076
077    /**
078     * Constructor, creates a new decoration object with given values.<p>
079     *
080     * @param decorationKey the decoration key
081     * @param decoration the decoration for this decoration key
082     * @param decDef the decoration defintion to be used
083     * @param locale the locale of this decoration object
084     */
085    public CmsDecorationObject(String decorationKey, String decoration, CmsDecorationDefintion decDef, Locale locale) {
086
087        m_decorationKey = decorationKey;
088        m_decoration = decoration;
089        m_decorationDefinition = decDef;
090        m_locale = locale;
091    }
092
093    /**
094     * Gets the decorated content for this decoration object.<p>
095     *
096     * @param config the configuration used
097     * @param text the text to be decorated
098     * @param contentLocale the locale of the content to be decorated
099     * @return decorated content
100     */
101    public String getContentDecoration(I_CmsDecoratorConfiguration config, String text, String contentLocale) {
102
103        StringBuffer content = new StringBuffer();
104        // TODO: we have to handle with word phrases, too
105
106        // add the pretext
107        if (!config.hasUsed(m_decorationKey) && m_decorationDefinition.isMarkFirst()) {
108            content.append(m_decorationDefinition.getPreTextFirst());
109        } else {
110            content.append(m_decorationDefinition.getPreText());
111        }
112        // now add the original word
113        content.append(text);
114
115        // add the posttext
116        if (!config.hasUsed(m_decorationKey) && m_decorationDefinition.isMarkFirst()) {
117            content.append(m_decorationDefinition.getPostTextFirst());
118            config.markAsUsed(m_decorationKey);
119        } else {
120            content.append(m_decorationDefinition.getPostText());
121        }
122
123        // replace the occurance of the ${decoration} makro in the decorated text
124        return replaceMacros(content.toString(), contentLocale);
125    }
126
127    /**
128     * Returns the decoration.<p>
129     *
130     * @return the decoration
131     */
132    public String getDecoration() {
133
134        return m_decoration;
135    }
136
137    /**
138     * Returns the decorationDefinition.<p>
139     *
140     * @return the decorationDefinition
141     */
142    public CmsDecorationDefintion getDecorationDefinition() {
143
144        return m_decorationDefinition;
145    }
146
147    /**
148     * Returns the decorationKey.<p>
149     *
150     * @return the decorationKey
151     */
152    public String getDecorationKey() {
153
154        return m_decorationKey;
155    }
156
157    /**
158     * @see java.lang.Object#toString()
159     */
160    @Override
161    public String toString() {
162
163        StringBuffer buf = new StringBuffer();
164        buf.append(this.getClass().getName());
165        buf.append(" [name = '");
166        buf.append(m_decorationKey);
167        buf.append("', decoration = '");
168        buf.append(m_decoration);
169        buf.append("', locale = '");
170        buf.append(m_locale);
171        buf.append("' decorationDefinition ='");
172        buf.append(m_decorationDefinition);
173        buf.append("']");
174        return buf.toString();
175    }
176
177    /**
178     * Replaces the macros in the given message.<p>
179     *
180     * @param msg the message in which the macros are replaced
181     * @param contentLocale the locale of the content that is currently decorated
182     *
183     * @return the message with the macros replaced
184     */
185    private String replaceMacros(String msg, String contentLocale) {
186
187        CmsMacroResolver resolver = CmsMacroResolver.newInstance();
188        resolver.addMacro(MACRO_DECORATION, m_decoration);
189        resolver.addMacro(MACRO_DECORATIONKEY, m_decorationKey);
190        if (m_locale != null) {
191            resolver.addMacro(MACRO_LOCALE, m_locale.toString());
192            if (!contentLocale.equals(m_locale.toString())) {
193                resolver.addMacro(MACRO_LANGUAGE, "lang=\"" + m_locale.toString() + "\"");
194            }
195        }
196
197        return resolver.resolveMacros(msg);
198    }
199
200}