KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > workplace > CmsEditor


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsEditor.java,v $
3 * Date : $Date: 2005/06/27 23:22:07 $
4 * Version: $Revision: 1.6 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
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 OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

28
29
30 package com.opencms.workplace;
31
32 import org.opencms.file.CmsFile;
33 import org.opencms.file.CmsObject;
34 import org.opencms.file.CmsPropertyDefinition;
35 import org.opencms.file.CmsResource;
36 import org.opencms.file.CmsResourceFilter;
37 import org.opencms.i18n.CmsEncoder;
38 import org.opencms.lock.CmsLock;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.CmsLog;
41 import org.opencms.main.OpenCms;
42 import org.opencms.workplace.CmsFrameset;
43
44 import com.opencms.core.I_CmsSession;
45 import com.opencms.legacy.CmsLegacyException;
46 import com.opencms.legacy.CmsXmlTemplateLoader;
47 import com.opencms.template.A_CmsXmlContent;
48 import com.opencms.template.CmsXmlTemplateFile;
49
50 import java.io.IOException JavaDoc;
51 import java.io.UnsupportedEncodingException JavaDoc;
52 import java.util.Hashtable JavaDoc;
53
54 import javax.servlet.http.HttpServletRequest JavaDoc;
55
56 /**
57  * Template class for displaying the text editor of the OpenCms workplace.<P>
58  * Reads the edirtor layout from a editor template file of the content type
59  * <code>CmsXmlWpTemplateFile</code>.
60  *
61  * @author Alexander Lucas
62  * @version $Revision: 1.6 $ $Date: 2005/06/27 23:22:07 $
63  * @see com.opencms.workplace.CmsXmlWpTemplateFile
64  *
65  * @deprecated Will not be supported past the OpenCms 6 release.
66  */

67
68 public class CmsEditor extends CmsWorkplaceDefault {
69
70     /**
71      * Get the name of the section that should be loaded from the editor's
72      * template file for displaying the editor.
73      * MS IE and Netscape Navigator use different ways to display
74      * the text editor, so we must distinguish here.
75      * @param cms cms object for accessing the original HTTP request
76      * @param templateFile the editor's template file containing different sections
77      * @return name of the browser specific section in <code>templateFile</code>
78      */

79     protected String JavaDoc getBrowserSpecificSection(CmsObject cms, CmsXmlTemplateFile templateFile, Hashtable JavaDoc parameters) {
80         HttpServletRequest JavaDoc orgReq = (HttpServletRequest JavaDoc)CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest();
81         String JavaDoc browser = orgReq.getHeader("user-agent");
82         String JavaDoc result = null;
83         // if the following parameter is given and MSIE is used,
84
// the template "ns" must be selected because there might be no ActiveX control
85
String JavaDoc noactivex = (String JavaDoc)parameters.get("noactivex");
86
87         // check browser and return a valid template selector
88
if(browser.indexOf("MSIE") > -1) {
89             if(templateFile.hasSection("ie")) {
90                 if(!"true".equalsIgnoreCase(noactivex)){
91                     result = "ie";
92                 } else {
93                     // there is no ActiveX Control so use the textarea
94
if(templateFile.hasSection("ns")) {
95                         result = "ns";
96                     }
97                 }
98             }
99         }
100         else {
101             if(templateFile.hasSection("ns")) {
102                 result = "ns";
103             }
104         }
105         return result;
106     }
107
108     /**
109      * Displays the editor described by the template file <code>templateFile</code>.
110      * This can be either the HTML editor or the text editor.
111      * <p>
112      * The given template file will be scanned for special section "ie" and "ns"
113      * that can be used to generate browser specific versions of the editors
114      * (MS IE or Netscape Navigator). If no such section exists, the default
115      * section will be displayed.
116      *
117      * @see #getContent(CmsObject, String, String, Hashtable, String)
118      * @param cms CmsObject Object for accessing system resources.
119      * @param templateFile Filename of the template file.
120      * @param elementName Element name of this template in our parent template.
121      * @param parameters Hashtable with all template class parameters.
122      * @param templateSelector template section that should be processed.
123      */

124     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName,
125             Hashtable JavaDoc parameters, String JavaDoc templateSelector) throws CmsException {
126
127         I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
128         String JavaDoc saveerror = "";
129         // Get all editor parameters
130
String JavaDoc file = (String JavaDoc)parameters.get(CmsWorkplaceDefault.C_PARA_RESOURCE);
131         // try to get the value from the session because we might come from the error page
132
if((file == null) || ("".equals(file))){
133             file = (String JavaDoc)session.getValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
134             session.removeValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
135         }
136         if((file != null) && (! "".equals(file))) {
137             session.putValue("te_file", file);
138         }
139         String JavaDoc content = (String JavaDoc)parameters.get(CmsWorkplaceDefault.C_PARA_CONTENT);
140         // try to get the value from the session because we might come from the error page
141
if((content == null) || ("".equals(content))){
142             content = (String JavaDoc)session.getValue(CmsWorkplaceDefault.C_PARA_CONTENT);
143             if(content != null){
144                 parameters.put(CmsWorkplaceDefault.C_PARA_CONTENT, content);
145             }
146             session.removeValue(CmsWorkplaceDefault.C_PARA_CONTENT);
147         }
148         String JavaDoc action = (String JavaDoc)parameters.get(CmsWorkplaceDefault.C_PARA_ACTION);
149         String JavaDoc jsfile = (String JavaDoc)parameters.get(com.opencms.core.I_CmsConstants.C_ROOT_TEMPLATE_NAME + "." + CmsWorkplaceDefault.C_PARA_JSFILE);
150         // try to get the value from the session because we might come from the error page
151
if((jsfile == null) || ("".equals(jsfile))){
152             jsfile = (String JavaDoc)session.getValue(CmsWorkplaceDefault.C_PARA_JSFILE);
153             session.removeValue(CmsWorkplaceDefault.C_PARA_JSFILE);
154         }
155         String JavaDoc editorframe = (String JavaDoc)parameters.get("root.editorframe");
156         if((editorframe == null) || ("".equals(editorframe))){
157             editorframe = (String JavaDoc)session.getValue("editorframe");
158             session.removeValue("editorframe");
159         }
160
161         boolean checkit = false;
162         boolean existsFileParam = ((file != null) && (!"".equals(file)));
163         boolean saveRequested = ((action != null) && (CmsWorkplaceDefault.C_EDIT_ACTION_SAVE.equals(action)
164                 || CmsWorkplaceDefault.C_EDIT_ACTION_SAVEEXIT.equals(action)));
165         boolean exitRequested = ((action != null) && (CmsWorkplaceDefault.C_EDIT_ACTION_EXIT.equals(action)
166                 || CmsWorkplaceDefault.C_EDIT_ACTION_SAVEEXIT.equals(action)));
167
168         // CmsFile object of the file to be edited
169
CmsFile editFile = null;
170         CmsLock lock = null;
171
172         // If there is a file parameter and no content, try to read the file.
173
// If the user requested a "save file", also load the file.
174
if (existsFileParam && (content == null || saveRequested)) {
175             editFile = readFile(cms, file);
176             lock = cms.getLock(file);
177             
178             // block any editing immediately in case of insuffient locks
179
if (lock.isNullLock()) {
180                 // check the autolock resource setting and lock the resource if necessary
181
if (OpenCms.getWorkplaceManager().autoLockResources()) {
182                     // resource is not locked, lock it automatically
183
cms.lockResource(file);
184                     lock = cms.getLock(file);
185                 } else {
186                     throw new CmsLegacyException("Resource is not locked", CmsLegacyException.C_RESOURCE_UNLOCKED);
187                 }
188             }
189             if (lock.getType() == CmsLock.TYPE_INHERITED) {
190                 cms.lockResource(file);
191                 lock = cms.getLock(file);
192             }
193             if (lock.getType() != CmsLock.TYPE_EXCLUSIVE) {
194                 throw new CmsLegacyException("Insufficient lock to edit content of resource", CmsLegacyException.C_RESOURCE_LOCKED_NON_EXCLUSIVE);
195             }
196             if (!lock.getUserId().equals(cms.getRequestContext().currentUser().getId())) {
197                 throw new CmsLegacyException("Resource locked by another user", CmsLegacyException.C_RESOURCE_LOCKED_BY_OTHER_USER);
198             }
199             
200             checkit = true;
201
202             // Read file encoding from the property of the file
203
String JavaDoc encoding = cms.getRequestContext().getEncoding();
204             encoding = cms.readProperty(file, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true, encoding);
205
206             // If there is no content set, this is the first request of the editor.
207
// So load the file content and set the "content" parameter.
208
if(content == null) {
209                 try {
210                     content = new String JavaDoc(editFile.getContents(), encoding);
211                 } catch (UnsupportedEncodingException JavaDoc e) {
212                     content = new String JavaDoc(editFile.getContents());
213                 }
214                 content = CmsEncoder.escapeWBlanks(content, CmsEncoder.ENCODING_UTF_8);
215                 parameters.put(CmsWorkplaceDefault.C_PARA_CONTENT, content);
216             }
217
218             // If the user requested a file save, write the file content
219
// back to the database.
220
if(saveRequested) {
221                 try{
222                     String JavaDoc decodedContent = CmsEncoder.unescape(content, CmsEncoder.ENCODING_UTF_8);
223                     try {
224                         editFile.setContents(decodedContent.getBytes(encoding));
225                     } catch (UnsupportedEncodingException JavaDoc e) {
226                         editFile.setContents(decodedContent.getBytes());
227                     }
228                     cms.writeFile(editFile);
229                 } catch (CmsException e){
230                     saveerror = e.getMessage();
231                 }
232             }
233         }
234
235         // Check if we should leave th editor instead of start processing
236
if(exitRequested && ((saveerror == null) || ("".equals(saveerror)))) {
237             try {
238                 CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect(CmsWorkplaceAction.getWorkplaceUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()));
239             } catch(IOException JavaDoc e) {
240                 throwException("Could not send redirect to workplace main screen.", e);
241             }
242
243             //return "".getBytes();
244
return null;
245         }
246
247         // Load the template file and get the browser specific section name
248
CmsXmlWpTemplateFile xmlTemplateDocument = (CmsXmlWpTemplateFile)getOwnTemplateFile(cms,
249                 templateFile, elementName, parameters, templateSelector);
250         String JavaDoc sectionName = getBrowserSpecificSection(cms, xmlTemplateDocument, parameters);
251         
252         // Put the "file" datablock for processing in the template file.
253
// It will be inserted in a hidden input field and given back when submitting.
254
xmlTemplateDocument.setData(CmsWorkplaceDefault.C_PARA_RESOURCE, file);
255         xmlTemplateDocument.setData(CmsWorkplaceDefault.C_PARA_JSFILE, jsfile);
256         xmlTemplateDocument.setData("editorframe", editorframe);
257         xmlTemplateDocument.setData("OpenCmsContext", OpenCms.getSystemInfo().getOpenCmsContext());
258         // Announcement of path and file name in the header of the browser.
259
if(checkit==true){
260             xmlTemplateDocument.setData("fileName", editFile.getName());
261             String JavaDoc parent = CmsResource.getParentFolder(cms.getSitePath(editFile));
262             xmlTemplateDocument.setData("pathName", parent);
263         }
264         String JavaDoc lasturlname = null;
265         if(!"".equals(saveerror)){
266             if(file != null){
267                 session.putValue(CmsWorkplaceDefault.C_PARA_RESOURCE, file);
268             }
269             if(content != null){
270                 session.putValue(CmsWorkplaceDefault.C_PARA_CONTENT, content);
271             }
272             if(jsfile != null){
273                 session.putValue(CmsWorkplaceDefault.C_PARA_JSFILE, jsfile);
274             }
275             if(editorframe != null){
276                 session.putValue("editorframe", editorframe);
277             }
278             sectionName = "errorsave";
279             xmlTemplateDocument.setData("errordetail", saveerror);
280             lasturlname=(String JavaDoc)parameters.get("editor._TEMPLATE_");
281             if (lasturlname != null){
282                 lasturlname=lasturlname.substring(lasturlname.lastIndexOf("/")+1, lasturlname.length());
283             }
284             xmlTemplateDocument.setData("errorlasturl", lasturlname+".html");
285         }
286         
287         // test if the "help"- button has to be displayed for the user's current language
288
String JavaDoc userLanguage = CmsXmlLanguageFile.getCurrentUserLanguage(cms);
289         xmlTemplateDocument.setData("LOCALE", "" + userLanguage);
290
291         try {
292             cms.readFolder(CmsWorkplaceDefault.C_VFS_PATH_HELP + userLanguage);
293             // the localized help- folder exists
294
xmlTemplateDocument.setData("HELP", xmlTemplateDocument.getProcessedDataValue("HELP_ENABLED", this));
295         }
296         catch (CmsException e) {
297             // the localized help- folder does not exist
298
try {
299                 xmlTemplateDocument.setData("HELP", xmlTemplateDocument.getProcessedDataValue("HELP_DISABLED", this));
300             }
301             catch (Exception JavaDoc ex) {
302                 // ignore this error so the workplace can still be used
303
}
304         }
305                 
306         return startProcessing(cms, xmlTemplateDocument, elementName, parameters, sectionName);
307     }
308
309     /**
310      * Indicates if the results of this class are cacheable.
311      *
312      * @param cms CmsObject Object for accessing system resources
313      * @param templateFile Filename of the template file
314      * @param elementName Element name of this template in our parent template.
315      * @param parameters Hashtable with all template class parameters.
316      * @param templateSelector template section that should be processed.
317      * @return <EM>true</EM> if cacheable, <EM>false</EM> otherwise.
318      */

319     public boolean isCacheable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName,
320             Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
321         return false;
322     }
323
324     /**
325      * Reads in the requested file to be edited by calling the corresponding
326      * method in the cms object.
327      * @param cms Cms object for accessing system resources
328      * @param filename Name of the file to be loaded
329      * @return CmsFile object of the loaded file
330      */

331     protected CmsFile readFile(CmsObject cms, String JavaDoc filename) throws CmsException {
332         CmsFile result = null;
333         try {
334             result = cms.readFile(filename, CmsResourceFilter.IGNORE_EXPIRATION);
335         }
336         catch(Exception JavaDoc e) {
337
338             // Anything is wrong. Perhaps a wrong file name ???
339
String JavaDoc errorMessage = "Error while reading file " + filename + ": " + e;
340             if(CmsLog.getLog(this).isErrorEnabled()) {
341                 CmsLog.getLog(this).error(errorMessage, e);
342             }
343
344             // throw this exception again, so it can be displayed in the servlet.
345
if(e instanceof CmsException) {
346                 throw (CmsException)e;
347             }
348             else {
349                 throw new CmsLegacyException(errorMessage);
350             }
351         }
352         return result;
353     }
354
355     /**
356      * User method for setting the editable text in the editor window.
357      * <P>
358      * This method can be called in the editor's template file using
359      * <code>&lt;METHOD name="setText"/&gt></code>. This call will be replaced
360      * by the content of the file that should be edited.
361      *
362      * @param cms CmsObject Object for accessing system resources.
363      * @param tagcontent Unused in this special case of a user method. Can be ignored.
364      * @param doc Reference to the A_CmsXmlContent object of the initiating XLM document.
365      * @param userObj Hashtable with parameters.
366      * @return String or byte[] with the content of the file that should be edited.
367      */

368     public Object JavaDoc setText(CmsObject cms, String JavaDoc tagcontent, A_CmsXmlContent doc,
369             Object JavaDoc userObj) {
370         Hashtable JavaDoc parameters = (Hashtable JavaDoc)userObj;
371         String JavaDoc content = (String JavaDoc)parameters.get(CmsWorkplaceDefault.C_PARA_CONTENT);
372         if(content == null) {
373             content = "";
374         }
375         return content;
376     }
377 }
378
Popular Tags