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