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;
029
030import org.opencms.file.CmsObject;
031import org.opencms.flex.CmsFlexController;
032import org.opencms.i18n.CmsEncoder;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.xml.containerpage.CmsContainerElementBean;
037
038import java.util.Collections;
039import java.util.Map;
040
041import javax.servlet.ServletRequest;
042import javax.servlet.jsp.JspException;
043import javax.servlet.jsp.tagext.TagSupport;
044
045import org.apache.commons.logging.Log;
046
047/**
048 * Provides access to the settings of an ADE container element.<p>
049 *
050 * @since 8.0
051 */
052public class CmsJspTagElementSetting extends TagSupport {
053
054    /** The log object for this class. */
055    private static final Log LOG = CmsLog.getLog(CmsJspTagElementSetting.class);
056
057    /** Serial version UID required for safe serialization. */
058    private static final long serialVersionUID = -7847101480288189549L;
059
060    /** The default value. */
061    private String m_defaultValue;
062
063    /** The name of the element setting to read. */
064    private String m_elementSetting;
065
066    /** Indicates if HTML should be escaped. */
067    private boolean m_escapeHtml;
068
069    /**
070     * Internal action method.<p>
071     *
072     * @param req the current request
073     *
074     * @return a map that contains the element settings
075     */
076    public static Map<String, String> elementSettingTagAction(ServletRequest req) {
077
078        CmsFlexController controller = CmsFlexController.getController(req);
079
080        CmsObject cms = controller.getCmsObject();
081        // try to find element setting on the container element
082        try {
083            CmsContainerElementBean currentElement = OpenCms.getADEManager().getCurrentElement(req);
084            currentElement.initResource(cms);
085            return currentElement.getSettings();
086        } catch (CmsException e) {
087            // most likely we are not in a container page
088            LOG.debug(e.getLocalizedMessage(), e);
089            return Collections.emptyMap();
090        }
091    }
092
093    /**
094     * Internal action method.<p>
095     *
096     * @param setting the setting to look up
097     * @param defaultValue the default value
098     * @param escape if the result String should be HTML escaped or not
099     * @param req the current request
100     *
101     * @return the value of the element setting or <code>null</code> if not found
102     */
103    public static String elementSettingTagAction(
104        String setting,
105        String defaultValue,
106        boolean escape,
107        ServletRequest req) {
108
109        String value = elementSettingTagAction(req).get(setting);
110        if (value == null) {
111            value = defaultValue;
112        }
113        if (escape) {
114            // HTML escape the value
115            value = CmsEncoder.escapeHtml(value);
116        }
117        return value;
118    }
119
120    /**
121     * @return SKIP_BODY
122     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
123     */
124    @Override
125    public int doStartTag() throws JspException {
126
127        ServletRequest req = pageContext.getRequest();
128
129        // This will always be true if the page is called through OpenCms
130        if (CmsFlexController.isCmsRequest(req)) {
131
132            try {
133                String setting = elementSettingTagAction(getName(), m_defaultValue, m_escapeHtml, req);
134                // Make sure that no null String is returned
135                if (setting == null) {
136                    setting = "";
137                }
138                pageContext.getOut().print(setting);
139
140            } catch (Exception ex) {
141                if (LOG.isErrorEnabled()) {
142                    LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "elementSetting"), ex);
143                }
144                throw new javax.servlet.jsp.JspException(ex);
145            }
146        }
147        return SKIP_BODY;
148    }
149
150    /**
151     * Returns the default value.<p>
152     *
153     * @return the default value
154     */
155    public String getDefault() {
156
157        return m_defaultValue != null ? m_defaultValue : "";
158    }
159
160    /**
161     * The value of the escape HTML flag.<p>
162     *
163     * @return the value of the escape HTML flag
164     */
165    public String getEscapeHtml() {
166
167        return String.valueOf(m_escapeHtml);
168    }
169
170    /**
171     * Returns the selected element setting name.<p>
172     *
173     * @return the selected element setting name
174     */
175    public String getName() {
176
177        return m_elementSetting != null ? m_elementSetting : "";
178    }
179
180    /**
181     * @see javax.servlet.jsp.tagext.Tag#release()
182     */
183    @Override
184    public void release() {
185
186        super.release();
187        m_elementSetting = null;
188        m_defaultValue = null;
189        m_escapeHtml = false;
190    }
191
192    /**
193     * Sets the default value.<p>
194     *
195     * This is used if a selected element setting is not found.<p>
196     *
197     * @param def the default value
198     */
199    public void setDefault(String def) {
200
201        if (def != null) {
202            m_defaultValue = def;
203        }
204    }
205
206    /**
207     * Set the escape HTML flag.<p>
208     *
209     * @param value must be <code>"true"</code> or <code>"false"</code> (all values other then <code>"true"</code> are
210     * considered to be false)
211     */
212    public void setEscapeHtml(String value) {
213
214        if (value != null) {
215            m_escapeHtml = Boolean.valueOf(value.trim()).booleanValue();
216        } else {
217            m_escapeHtml = false;
218        }
219    }
220
221    /**
222     * Sets the element setting name.<p>
223     *
224     * @param name the element setting name to set
225     */
226    public void setName(String name) {
227
228        if (name != null) {
229            m_elementSetting = name;
230        }
231    }
232}