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;
029
030import org.opencms.json.JSONTokener;
031import org.opencms.main.CmsLog;
032
033import javax.servlet.jsp.JspException;
034
035import org.apache.commons.logging.Log;
036
037/**
038 * Adds a JSON value to the surrounding context and/or stores it as a variable in the page context.
039 */
040public class CmsJspTagJsonValue extends A_CmsJspJsonTag {
041
042    /** Logger instance for this class. */
043    private static final Log LOG = CmsLog.getLog(CmsJspTagJsonValue.class);
044
045    /** Serial version id. */
046    private static final long serialVersionUID = -8383685322762531356L;
047
048    /** The value attribute. */
049    private Object m_value;
050
051    /** Keeps track of whether the value should be parsed as a JSON string. */
052    private boolean m_parse;
053
054    /** Keeps track if a value has been specified or the body should be evaluated. */
055    private boolean m_valueSpecified;
056
057    /** Name of variable to store errors under in the page scope. */
058    private String m_errorVar;
059
060    /**
061     * Default constructor explicitly resetting all variables.
062     */
063    public CmsJspTagJsonValue() {
064
065        init();
066    }
067
068    /**
069     * @see org.opencms.jsp.A_CmsJspJsonTag#doEndTag()
070     */
071    @Override
072    public int doEndTag() throws JspException {
073
074        setError(null);
075        return super.doEndTag();
076    }
077
078    /**
079     * @see org.opencms.jsp.A_CmsJspJsonTag#getJsonValue()
080     */
081    @Override
082    public Object getJsonValue() {
083
084        Object value = null;
085        if (m_valueSpecified) {
086            value = m_value;
087        } else {
088            if ((bodyContent == null) || (bodyContent.getString() == null)) {
089                value = "";
090            } else {
091                value = bodyContent.getString().trim();
092            }
093        }
094        if (m_parse) {
095            String strValue = "" + value;
096            strValue = strValue.trim();
097            try {
098                JSONTokener parser = new JSONTokener(strValue);
099                value = parser.nextValue();
100            } catch (Exception e) {
101                LOG.warn(e.getLocalizedMessage(), e);
102                String errorMessage = e.getLocalizedMessage();
103                setError(errorMessage);
104                return null;
105            }
106        }
107        return value;
108    }
109
110    /**
111     * Variable to store errors under in the page scope.
112     *
113     * @param errorVar the error variable
114     */
115    public void setErrorVar(String errorVar) {
116
117        m_errorVar = errorVar;
118    }
119
120    /**
121     * Sets the parse attribute.
122     *
123     * <p>If set to 'true', the value will be treated as a string and then parsed into JSON.
124     *
125     * @param parse the value being set
126     */
127    public void setParse(String parse) {
128
129        m_parse = Boolean.valueOf(parse).booleanValue();
130
131    }
132
133    /**
134     * Sets the value attribute.
135     *
136     * @param value the JSON value
137     */
138    public void setValue(Object value) {
139
140        m_value = value;
141        m_valueSpecified = true;
142    }
143
144    /**
145     * Initializes / resets the internal values.<p>
146     */
147    @Override
148    protected void init() {
149
150        super.init();
151        m_value = null;
152        m_valueSpecified = false;
153    }
154
155    /**
156     * If an error variable has been specified, store the given error message in that variable.
157     *
158     * @param errorMessage the error message
159     */
160    protected void setError(String errorMessage) {
161
162        try {
163            if (m_errorVar != null) {
164                pageContext.setAttribute(m_errorVar, errorMessage);
165            }
166        } catch (Exception e2) {
167            LOG.error(e2.getLocalizedMessage(), e2);
168        }
169    }
170}