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, 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.util;
029
030import org.opencms.file.CmsObject;
031import org.opencms.util.CmsStringUtil;
032
033/**
034 * Element setting access wrapper.<p>
035 */
036public class CmsJspElementSettingValueWrapper extends A_CmsJspValueWrapper {
037
038    /** The instance of the enclosing context bean. */
039    private final CmsJspStandardContextBean m_contextBean;
040
041    /** Flag indicating the setting has been configured. */
042    private boolean m_exists;
043
044    /** Calculated hash code. */
045    private int m_hashCode;
046
047    /** The wrapped setting value, which always is a String. */
048    private String m_value;
049
050    /**
051     * Constructor.<p>
052     *
053     * @param contextBean the standard context bean this setting was wrapped from
054     * @param value the wrapped value
055     * @param exists flag indicating the setting has been configured
056     */
057    CmsJspElementSettingValueWrapper(CmsJspStandardContextBean contextBean, String value, boolean exists) {
058
059        m_contextBean = contextBean;
060        m_value = value;
061        m_exists = exists;
062    }
063
064    /**
065     * @see org.opencms.jsp.util.A_CmsJspValueWrapper#getCmsObject()
066     */
067    @Override
068    public CmsObject getCmsObject() {
069
070        return m_contextBean.m_cms;
071    }
072
073    /**
074     * Returns if the setting has been configured.<p>
075     *
076     * @return <code>true</code> if the setting has been configured
077     */
078    @Override
079    public boolean getExists() {
080
081        return m_exists;
082    }
083
084    /**
085     * Returns if the setting value is null or empty.<p>
086     *
087     * @return <code>true</code> if the setting value is null or empty
088     */
089    @Override
090    public boolean getIsEmpty() {
091
092        return !m_exists || CmsStringUtil.isEmpty(m_value);
093    }
094
095    /**
096     * Returns if the setting value is null or white space only.<p>
097     *
098     * @return <code>true</code> if the setting value is null or white space only
099     */
100    @Override
101    public boolean getIsEmptyOrWhitespaceOnly() {
102
103        return !m_exists || CmsStringUtil.isEmptyOrWhitespaceOnly(m_value);
104    }
105
106    /**
107     * @see org.opencms.jsp.util.A_CmsJspValueWrapper#getObjectValue()
108     */
109    @Override
110    public Object getObjectValue() {
111
112        return m_value;
113    }
114
115    /**
116     * Returns the raw value.<p>
117     *
118     * This may return <code>null</code>.
119     *
120     * @return the value
121     *
122     * @deprecated use {@link #getObjectValue()} instead
123     */
124    @Deprecated
125    public String getValue() {
126
127        return m_value;
128    }
129
130    /**
131     * The hash code is based in the String value of the element setting.<p>
132     *
133     * @see org.opencms.jsp.util.A_CmsJspValueWrapper#hashCode()
134     */
135    @Override
136    public int hashCode() {
137
138        if (m_value == null) {
139            return 0;
140        }
141        if (m_hashCode == 0) {
142            m_hashCode = m_value.hashCode();
143        }
144        return m_hashCode;
145    }
146
147    /**
148     * Returns the string value.<p>
149     *
150     * This will always be at least an empty String <code>""</code>, never <code>null</code>.
151     *
152     * @return the string value
153     */
154    @Override
155    public String toString() {
156
157        return m_value != null ? m_value : "";
158    }
159}