KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > editors > CmsSimpleEditor


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsSimpleEditor.java,v $
3  * Date : $Date: 2005/07/06 12:45:07 $
4  * Version: $Revision: 1.12 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.editors;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsResourceFilter;
36 import org.opencms.i18n.CmsEncoder;
37 import org.opencms.jsp.CmsJspActionElement;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.CmsLog;
40 import org.opencms.util.CmsStringUtil;
41 import org.opencms.workplace.CmsWorkplaceSettings;
42
43 import java.io.IOException JavaDoc;
44 import java.io.UnsupportedEncodingException JavaDoc;
45
46 import javax.servlet.ServletException JavaDoc;
47 import javax.servlet.http.HttpServletRequest JavaDoc;
48 import javax.servlet.jsp.JspException JavaDoc;
49
50 import org.apache.commons.logging.Log;
51
52 /**
53  * Creates the output for editing a resource (text or JSP files).<p>
54  *
55  * This class is extended by the LEdit class, so be careful when changing anything.<p>
56  *
57  * The following files use this class:
58  * <ul>
59  * <li>/editors/simple/editor.jsp</li>
60  * </ul>
61  * <p>
62  *
63  * @author Andreas Zahner
64  *
65  * @version $Revision: 1.12 $
66  *
67  * @since 6.0.0
68  */

69 public class CmsSimpleEditor extends CmsEditor {
70
71     /** Constant for the editor type, must be the same as the editors subfolder name in the VFS. */
72     private static final String JavaDoc EDITOR_TYPE = "simple";
73
74     /** The log object for this class. */
75     private static final Log LOG = CmsLog.getLog(CmsSimpleEditor.class);
76
77     /**
78      * Public constructor.<p>
79      *
80      * @param jsp an initialized JSP action element
81      */

82     public CmsSimpleEditor(CmsJspActionElement jsp) {
83
84         super(jsp);
85     }
86
87     /**
88      * @see org.opencms.workplace.editors.CmsEditor#actionClear(boolean)
89      */

90     public void actionClear(boolean forceUnlock) {
91
92         boolean modified = Boolean.valueOf(getParamModified()).booleanValue();
93         if (forceUnlock || !modified) {
94             // unlock the resource when force unlock is true or resource was not modified
95
try {
96                 getCms().unlockResource(getParamResource());
97             } catch (CmsException e) {
98                 // should usually never happen
99
if (LOG.isInfoEnabled()) {
100                     LOG.info(e);
101                 }
102             }
103         }
104     }
105
106     /**
107      * Performs the exit editor action.<p>
108      *
109      * @see org.opencms.workplace.editors.CmsEditor#actionExit()
110      */

111     public void actionExit() throws IOException JavaDoc, JspException JavaDoc, ServletException JavaDoc {
112
113         if (getAction() == ACTION_CANCEL) {
114             // save and exit was canceled
115
return;
116         }
117
118         // unlock resource, if no modified
119
actionClear(false);
120
121         // close the editor
122
actionClose();
123     }
124
125     /**
126      * Performs the save content ation.<p>
127      *
128      * @see org.opencms.workplace.editors.CmsEditor#actionSave()
129      */

130     public void actionSave() throws JspException JavaDoc {
131
132         CmsFile editFile = null;
133         try {
134             editFile = getCms().readFile(getParamResource(), CmsResourceFilter.ALL);
135             // ensure all chars in the content are valid for the selected encoding
136
String JavaDoc decodedContent = CmsEncoder.adjustHtmlEncoding(decodeContent(getParamContent()), getFileEncoding());
137
138             try {
139                 editFile.setContents(decodedContent.getBytes(getFileEncoding()));
140             } catch (UnsupportedEncodingException JavaDoc e) {
141                 throw new CmsException(
142                     Messages.get().container(Messages.ERR_INVALID_CONTENT_ENC_1, getParamResource()),
143                     e);
144             }
145             // the file content might have been modified during the write operation
146
CmsFile writtenFile = getCms().writeFile(editFile);
147             try {
148                 decodedContent = new String JavaDoc(writtenFile.getContents(), getFileEncoding());
149             } catch (UnsupportedEncodingException JavaDoc e) {
150                 throw new CmsException(
151                     Messages.get().container(Messages.ERR_INVALID_CONTENT_ENC_1, getParamResource()),
152                     e);
153             }
154             setParamContent(encodeContent(decodedContent));
155             // set the modified parameter
156
setParamModified(Boolean.TRUE.toString());
157         } catch (CmsException e) {
158             showErrorPage(e);
159         }
160
161         if (getAction() != ACTION_CANCEL) {
162             // save successful, set save action
163
setAction(ACTION_SAVE);
164         }
165     }
166
167     /**
168      * @see org.opencms.workplace.editors.CmsEditor#getEditorResourceUri()
169      */

170     public String JavaDoc getEditorResourceUri() {
171
172         return getSkinUri() + "editors/" + EDITOR_TYPE + "/";
173     }
174
175     /**
176      * Initializes the editor content when openening the editor for the first time.<p>
177      */

178     protected void initContent() {
179
180         // save initialized instance of this class in request attribute for included sub-elements
181
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
182         // get the default encoding
183
String JavaDoc content = getParamContent();
184         if (CmsStringUtil.isNotEmpty(content)) {
185             // content already read, must be decoded
186
setParamContent(decodeContent(content));
187             return;
188         } else {
189             content = "";
190         }
191
192         try {
193             // lock resource if autolock is enabled
194
checkLock(getParamResource());
195             CmsFile editFile = getCms().readFile(getParamResource(), CmsResourceFilter.ALL);
196             try {
197                 content = new String JavaDoc(editFile.getContents(), getFileEncoding());
198             } catch (UnsupportedEncodingException JavaDoc e) {
199                 throw new CmsException(
200                     Messages.get().container(Messages.ERR_INVALID_CONTENT_ENC_1, getParamResource()),
201                     e);
202             }
203         } catch (CmsException e) {
204             // reading of file contents failed, show error dialog
205
try {
206                 showErrorPage(this, e);
207             } catch (JspException JavaDoc exc) {
208                 // should usually never happen
209
if (LOG.isInfoEnabled()) {
210                     LOG.info(exc);
211                 }
212             }
213         }
214         setParamContent(content);
215     }
216
217     /**
218      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
219      */

220     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
221
222         // fill the parameter values in the get/set methods
223
fillParamValues(request);
224         // set the dialog type
225
setParamDialogtype(EDITOR_TYPE);
226
227         // set the action for the JSP switch
228
if (EDITOR_SAVE.equals(getParamAction())) {
229             setAction(ACTION_SAVE);
230         } else if (EDITOR_SAVEEXIT.equals(getParamAction())) {
231             setAction(ACTION_SAVEEXIT);
232         } else if (EDITOR_EXIT.equals(getParamAction())) {
233             setAction(ACTION_EXIT);
234         } else if (EDITOR_SHOW.equals(getParamAction())) {
235             setAction(ACTION_SHOW);
236         } else if (EDITOR_SHOW_ERRORMESSAGE.equals(getParamAction())) {
237             setAction(ACTION_SHOW_ERRORMESSAGE);
238         } else {
239             // initial call of editor
240
setAction(ACTION_DEFAULT);
241             initContent();
242         }
243
244         setParamContent(encodeContent(getParamContent()));
245     }
246 }
247
Popular Tags