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.workplace.editors;
029
030import org.opencms.file.CmsResourceFilter;
031import org.opencms.file.CmsVfsResourceNotFoundException;
032import org.opencms.i18n.CmsEncoder;
033import org.opencms.jsp.CmsJspActionElement;
034import org.opencms.lock.CmsLockType;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.workplace.CmsWorkplaceSettings;
038import org.opencms.xml.page.CmsXmlPageFactory;
039
040import javax.servlet.http.HttpServletRequest;
041import javax.servlet.jsp.JspException;
042
043import org.apache.commons.logging.Log;
044
045/**
046 * Creates the output for editing a CmsDefaultPage with the simple textarea editor.<p>
047 *
048 * The following editor uses this class:
049 * <ul>
050 * <li>/editors/simplehtml/editor.jsp
051 * </ul>
052 * <p>
053 *
054 * @since 6.0.0
055 */
056public class CmsSimplePageEditor extends CmsDefaultPageEditor {
057
058    /** Constant for the editor type, must be the same as the editors subfolder name in the VFS. */
059    private static final String EDITOR_TYPE = "simplehtml";
060
061    /** The log object for this class. */
062    private static final Log LOG = CmsLog.getLog(CmsSimplePageEditor.class);
063
064    /**
065     * Public constructor.<p>
066     *
067     * @param jsp an initialized JSP action element
068     */
069    public CmsSimplePageEditor(CmsJspActionElement jsp) {
070
071        super(jsp);
072    }
073
074    /**
075     * @see org.opencms.workplace.editors.CmsEditor#getEditorResourceUri()
076     */
077    @Override
078    public String getEditorResourceUri() {
079
080        return getSkinUri() + "editors/" + EDITOR_TYPE + "/";
081    }
082
083    /**
084     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
085     */
086    @Override
087    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
088
089        // fill the parameter values in the get/set methods
090        fillParamValues(request);
091
092        // set the dialog type
093        setParamDialogtype(EDITOR_TYPE);
094
095        // Initialize a page object from the temporary file
096        if ((getParamTempfile() != null) && !"null".equals(getParamTempfile())) {
097            try {
098                m_file = getCms().readFile(getParamTempfile(), CmsResourceFilter.ALL);
099                m_page = CmsXmlPageFactory.unmarshal(getCms(), m_file);
100            } catch (CmsException e) {
101                if (e instanceof CmsVfsResourceNotFoundException) {
102                    // the tempfile is missing, maybe someone else has deleted it
103                    // try to create a new one and redo the initialization
104                    try {
105                        setParamTempfile(createTempFile());
106                        m_file = getCms().readFile(getParamTempfile(), CmsResourceFilter.ALL);
107                        m_page = CmsXmlPageFactory.unmarshal(getCms(), m_file);
108                    } catch (CmsException e1) {
109                        // error during initialization
110                        try {
111                            showErrorPage(this, e1);
112                        } catch (JspException exc) {
113                            // should usually never happen
114                            if (LOG.isInfoEnabled()) {
115                                LOG.info(exc);
116                            }
117                        }
118                    }
119                } else {
120                    // error during initialization
121                    try {
122                        showErrorPage(this, e);
123                    } catch (JspException exc) {
124                        // should usually never happen
125                        if (LOG.isInfoEnabled()) {
126                            LOG.info(exc);
127                        }
128                    }
129                }
130            }
131        }
132
133        // set the action for the JSP switch
134        if (EDITOR_SAVE.equals(getParamAction())) {
135            setAction(ACTION_SAVE);
136        } else if (EDITOR_SAVEEXIT.equals(getParamAction())) {
137            setAction(ACTION_SAVEEXIT);
138        } else if (EDITOR_SAVEACTION.equals(getParamAction())) {
139            setAction(ACTION_SAVEACTION);
140            try {
141                actionDirectEdit();
142            } catch (Exception e) {
143                // should usually never happen
144                if (LOG.isInfoEnabled()) {
145                    LOG.info(e.getLocalizedMessage(), e);
146                }
147            }
148            setAction(ACTION_EXIT);
149        } else if (EDITOR_EXIT.equals(getParamAction())) {
150            setAction(ACTION_EXIT);
151        } else if (EDITOR_CLOSEBROWSER.equals(getParamAction())) {
152            // closed browser window accidentally, unlock resource and delete temporary file
153            actionClear(true);
154            return;
155        } else if (EDITOR_DELETELOCALE.equals(getParamAction())) {
156            setAction(ACTION_DELETELOCALE);
157        } else if (EDITOR_CHANGE_ELEMENT.equals(getParamAction())) {
158            setAction(ACTION_SHOW);
159            actionChangeBodyElement();
160            // prepare the content String for the editor
161            prepareContent(false);
162        } else if (EDITOR_CLEANUP.equals(getParamAction())) {
163            setAction(ACTION_SHOW);
164            actionCleanupBodyElement();
165            // prepare the content String for the editor
166            prepareContent(false);
167        } else if (EDITOR_SHOW.equals(getParamAction())) {
168            setAction(ACTION_SHOW);
169            // prepare the content String for the editor
170            prepareContent(false);
171        } else if (EDITOR_PREVIEW.equals(getParamAction())) {
172            setAction(ACTION_PREVIEW);
173        } else {
174            // initial call of editor, initialize page and page parameters
175            setAction(ACTION_DEFAULT);
176            try {
177                // lock resource if auto-lock is enabled in configuration
178                if (Boolean.valueOf(getParamDirectedit()).booleanValue()) {
179                    // set a temporary lock in direct edit mode
180                    checkLock(getParamResource(), CmsLockType.TEMPORARY);
181                } else {
182                    // set common lock
183                    checkLock(getParamResource());
184                }
185                // create the temporary file
186                setParamTempfile(createTempFile());
187                // initialize a page object from the created temporary file
188                m_file = getCms().readFile(getParamTempfile(), CmsResourceFilter.ALL);
189                m_page = CmsXmlPageFactory.unmarshal(getCms(), m_file);
190            } catch (CmsException e) {
191                // error during initialization
192                try {
193                    showErrorPage(this, e);
194                    return;
195                } catch (JspException exc) {
196                    // should usually never happen
197                    if (LOG.isInfoEnabled()) {
198                        LOG.info(exc);
199                    }
200                }
201            }
202            // set the initial body language & name if not given in request parameters
203            if (getParamElementlanguage() == null) {
204                initBodyElementLanguage();
205            }
206            if (getParamElementname() == null) {
207                initBodyElementName(null);
208            }
209            // initialize the editor content
210            initContent();
211            // prepare the content String for the editor
212            prepareContent(false);
213        }
214    }
215
216    /**
217     * Manipulates the content String and removes leading and trailing white spaces.<p>
218     *
219     * @param save if set to true, the content parameter is not updated
220     * @return the prepared content String
221     */
222    @Override
223    protected String prepareContent(boolean save) {
224
225        String content = getParamContent().trim();
226        // ensure all chars in the content are valid for the selected encoding
227        content = CmsEncoder.adjustHtmlEncoding(content, getFileEncoding());
228        if (!save) {
229            setParamContent(content);
230        }
231        return content;
232    }
233
234}