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.widgets;
029
030import org.opencms.file.CmsObject;
031import org.opencms.i18n.CmsEncoder;
032import org.opencms.main.CmsLog;
033import org.opencms.util.CmsHtmlExtractor;
034import org.opencms.util.CmsStringUtil;
035
036import java.io.UnsupportedEncodingException;
037import java.util.Map;
038
039import org.apache.commons.logging.Log;
040
041import org.htmlparser.util.ParserException;
042
043/**
044 * {@link org.opencms.widgets.CmsTextareaWidget} that strips HTML Tags from the input before storing values.<p>
045 *
046 * @since 6.3.0
047 */
048public final class CmsTextareaWidgetPlaintext extends CmsTextareaWidget {
049
050    /** The log object for this class. */
051    private static final Log LOG = CmsLog.getLog(CmsTextareaWidgetPlaintext.class);
052
053    /**
054     * Defcon.<p>
055     */
056    public CmsTextareaWidgetPlaintext() {
057
058        super();
059    }
060
061    /**
062     * @see org.opencms.widgets.CmsTextareaWidget#newInstance()
063     */
064    @Override
065    public I_CmsWidget newInstance() {
066
067        return new CmsTextareaWidgetPlaintext();
068    }
069
070    /**
071     * @see org.opencms.widgets.A_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
072     */
073    @Override
074    public void setEditorValue(
075        CmsObject cms,
076        Map<String, String[]> formParameters,
077        I_CmsWidgetDialog widgetDialog,
078        I_CmsWidgetParameter param) {
079
080        String[] values = formParameters.get(param.getId());
081        if ((values != null) && (values.length > 0)) {
082            String value = values[0];
083            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(value)) {
084                try {
085                    value = CmsHtmlExtractor.extractText(value, CmsEncoder.ENCODING_UTF_8);
086                } catch (ParserException e) {
087                    if (LOG.isErrorEnabled()) {
088                        LOG.error(
089                            Messages.get().getBundle().key(Messages.LOG_ERR_WIDGET_PLAINTEXT_EXTRACT_HTML_1, value));
090                    }
091                } catch (UnsupportedEncodingException e) {
092                    if (LOG.isErrorEnabled()) {
093                        LOG.error(
094                            Messages.get().getBundle().key(Messages.LOG_ERR_WIDGET_PLAINTEXT_EXTRACT_HTML_1, value));
095                    }
096                }
097            } else {
098                value = "";
099            }
100            param.setStringValue(cms, value);
101        }
102    }
103}