KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsWorkplaceEditorManager.java,v $
3  * Date : $Date: 2006/03/27 14:52:49 $
4  * Version: $Revision: 1.10 $
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.db.CmsUserSettings;
35 import org.opencms.file.CmsFile;
36 import org.opencms.file.CmsFolder;
37 import org.opencms.file.CmsObject;
38 import org.opencms.file.CmsRequestContext;
39 import org.opencms.file.CmsResourceFilter;
40 import org.opencms.file.types.CmsResourceTypeXmlPage;
41 import org.opencms.main.CmsException;
42 import org.opencms.main.CmsLog;
43 import org.opencms.main.OpenCms;
44 import org.opencms.util.CmsStringUtil;
45 import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52 import java.util.SortedMap JavaDoc;
53 import java.util.TreeMap JavaDoc;
54
55 import org.apache.commons.logging.Log;
56
57 /**
58  * The editor manager stores information about all available configured editors in OpenCms.<p>
59  *
60  * This class provides methods and constants to select the right editor according to:
61  * <ul>
62  * <li>the user preferences</li>
63  * <li>the users current browser</li>
64  * <li>the resource type</li>
65  * <li>the editor rankings</li>
66  * </ul>
67  * <p>
68  *
69  * @author Andreas Zahner
70  *
71  * @version $Revision: 1.10 $
72  *
73  * @since 6.0.0
74  */

75 public class CmsWorkplaceEditorManager {
76
77     /** The filename of the editor configuration XML file. */
78     public static final String JavaDoc EDITOR_CONFIGURATION_FILENAME = "editor_configuration.xml";
79
80     /** The filename of the editor JSP. */
81     public static final String JavaDoc EDITOR_FILENAME = "editor.jsp";
82
83     /** The log object for this class. */
84     private static final Log LOG = CmsLog.getLog(CmsWorkplaceEditorManager.class);
85
86     private List JavaDoc m_editorConfigurations;
87     private Map JavaDoc m_preferredEditors;
88
89     /**
90      * Creates a new editor manager.<p>
91      *
92      * @param cms an OpenCms context object that must have been initialized with "Admin" permissions
93      */

94     public CmsWorkplaceEditorManager(CmsObject cms) {
95
96         // get all subfolders of the workplace editor folder
97
List JavaDoc editorFolders = new ArrayList JavaDoc();
98         try {
99             editorFolders = cms.getSubFolders(CmsEditor.PATH_EDITORS);
100         } catch (CmsException e) {
101             LOG.error(Messages.get().getBundle().key(Messages.LOG_READ_EDITIR_FOLDER_FAILED_1, CmsEditor.PATH_EDITORS));
102             // can not throw exception here since then OpenCms would not even start in shell mode (runlevel 2)
103
editorFolders = new ArrayList JavaDoc();
104         }
105
106         m_editorConfigurations = new ArrayList JavaDoc(editorFolders.size());
107
108         // try to read the configuration files and create configuration objects for valid configurations
109
Iterator JavaDoc i = editorFolders.iterator();
110         while (i.hasNext()) {
111             CmsFolder currentFolder = (CmsFolder)i.next();
112             String JavaDoc folderName = CmsEditor.PATH_EDITORS + currentFolder.getName();
113             if (!folderName.endsWith("/")) {
114                 folderName += "/";
115             }
116             CmsFile configFile = null;
117             try {
118                 configFile = cms.readFile(
119                     folderName + EDITOR_CONFIGURATION_FILENAME,
120                     CmsResourceFilter.IGNORE_EXPIRATION);
121             } catch (CmsException e) {
122                 // no configuration file present, ignore this folder
123
if (LOG.isInfoEnabled()) {
124                     LOG.info(e);
125                 }
126                 continue;
127             }
128             // get the file contents
129
byte[] xmlData = configFile.getContents();
130             CmsWorkplaceEditorConfiguration editorConfig = new CmsWorkplaceEditorConfiguration(xmlData, folderName
131                 + EDITOR_FILENAME);
132             if (editorConfig.isValidConfiguration()) {
133                 m_editorConfigurations.add(editorConfig);
134             }
135         }
136         m_preferredEditors = new HashMap JavaDoc(m_editorConfigurations.size());
137     }
138
139     /**
140      * Returns a map of configurable editors for the workplace preferences dialog.<p>
141      *
142      * This map has the resource type name as key, the value is a sorted map with
143      * the ranking as key and a CmsWorkplaceEditorConfiguration object as value.<p>
144      *
145      * @return configurable editors for the workplace preferences dialog
146      */

147     public Map JavaDoc getConfigurableEditors() {
148
149         Map JavaDoc configurableEditors = new HashMap JavaDoc();
150         Iterator JavaDoc i = m_editorConfigurations.iterator();
151         while (i.hasNext()) {
152             CmsWorkplaceEditorConfiguration currentConfig = (CmsWorkplaceEditorConfiguration)i.next();
153             // get all resource types specified for the current editor configuration
154
Iterator JavaDoc k = currentConfig.getResourceTypes().keySet().iterator();
155             while (k.hasNext()) {
156                 // key is the current resource type of the configuration
157
String JavaDoc key = (String JavaDoc)k.next();
158
159                 // check if the current resource type is only a reference to another resource type
160
CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(key);
161                 if (CmsStringUtil.isNotEmpty(settings.getReference())) {
162                     // skip this resource type
163
continue;
164                 }
165
166                 if (currentConfig.getMappingForResourceType(key) == null) {
167                     // editor is configurable for specified resource type
168
SortedMap JavaDoc editorConfigs = (SortedMap JavaDoc)configurableEditors.get(key);
169                     if (editorConfigs == null) {
170                         // no configuration map present for resource type, create one
171
editorConfigs = new TreeMap JavaDoc();
172                     }
173                     // put the current editor configuration to the resource map with ranking value as key
174
editorConfigs.put(new Float JavaDoc(currentConfig.getRankingForResourceType(key)), currentConfig);
175                     // put the resource map to the result map with resource type as key
176
configurableEditors.put(key, editorConfigs);
177                 }
178             }
179         }
180         return configurableEditors;
181     }
182
183     /**
184      * Returns the editor URI for the current resource type.<p>
185      *
186      * @param context the request context
187      * @param userAgent the user agent String that identifies the browser
188      * @return a valid editor URI for the resource type or null, if no editor matches
189      */

190     public String JavaDoc getWidgetEditor(CmsRequestContext context, String JavaDoc userAgent) {
191
192         // step 1: check if the user specified a preferred editor for the resource type xmlpage
193
CmsUserSettings settings = new CmsUserSettings(context.currentUser());
194         String JavaDoc resourceType = CmsResourceTypeXmlPage.getStaticTypeName();
195         String JavaDoc preferredEditorSetting = settings.getPreferredEditor(resourceType);
196         if (preferredEditorSetting == null) {
197             // no preferred editor setting found for this resource type, look for mapped resource type preferred editor
198
Iterator JavaDoc i = m_editorConfigurations.iterator();
199             while (i.hasNext()) {
200                 CmsWorkplaceEditorConfiguration currentConfig = (CmsWorkplaceEditorConfiguration)i.next();
201                 String JavaDoc mapping = currentConfig.getMappingForResourceType(resourceType);
202                 if (mapping != null) {
203                     preferredEditorSetting = settings.getPreferredEditor(mapping);
204                 }
205                 if (preferredEditorSetting != null) {
206                     break;
207                 }
208             }
209         }
210         if (preferredEditorSetting != null) {
211             CmsWorkplaceEditorConfiguration preferredConf = filterPreferredEditor(preferredEditorSetting);
212             if (preferredConf != null && preferredConf.isWidgetEditor() && preferredConf.matchesBrowser(userAgent)) {
213                 // return preferred editor only if it matches the current users browser
214
return preferredConf.getWidgetEditor();
215             }
216         }
217
218         // step 2: filter editors for the given resoure type
219
SortedMap JavaDoc filteredEditors = filterEditorsForResourceType(resourceType);
220
221         // step 3: check if one of the editors matches the current users browser
222
while (filteredEditors.size() > 0) {
223             // check editor configuration with highest ranking
224
Float JavaDoc key = (Float JavaDoc)filteredEditors.lastKey();
225             CmsWorkplaceEditorConfiguration conf = (CmsWorkplaceEditorConfiguration)filteredEditors.get(key);
226             if (conf.isWidgetEditor() && conf.matchesBrowser(userAgent)) {
227                 return conf.getWidgetEditor();
228             }
229             filteredEditors.remove(key);
230         }
231
232         // no valid editor found
233
return null;
234     }
235
236     /**
237      * Returns the default editor URI for the current resource type.<p>
238      *
239      * @param context the request context
240      * @param resourceType the current resource type
241      * @param userAgent the user agent String that identifies the browser
242      * @return a valid default editor URI for the resource type or null, if no editor matches
243      */

244     protected String JavaDoc getDefaultEditorUri(CmsRequestContext context, String JavaDoc resourceType, String JavaDoc userAgent) {
245
246         SortedMap JavaDoc filteredEditors = filterEditorsForResourceType(resourceType);
247         while (filteredEditors.size() > 0) {
248             // get the configuration with the lowest key value from the map
249
Float JavaDoc key = (Float JavaDoc)filteredEditors.firstKey();
250             CmsWorkplaceEditorConfiguration conf = (CmsWorkplaceEditorConfiguration)filteredEditors.get(key);
251             // match the found configuration with the current users browser
252
if (conf.matchesBrowser(userAgent)) {
253                 return conf.getEditorUri();
254             }
255             filteredEditors.remove(key);
256         }
257         if (context == null) {
258             // this is just so that all parameters are used, signature should be identical to getEditorUri(...)
259
return null;
260         }
261         // no valid default editor found
262
return null;
263     }
264
265     /**
266      * Returns the editor configuration objects.<p>
267      *
268      * @return the editor configuration objects
269      */

270     protected List JavaDoc getEditorConfigurations() {
271
272         return m_editorConfigurations;
273     }
274
275     /**
276      * Returns the editor URI for the current resource type.<p>
277      *
278      * @param context the request context
279      * @param resourceType the current resource type
280      * @param userAgent the user agent String that identifies the browser
281      * @return a valid editor URI for the resource type or null, if no editor matches
282      */

283     protected String JavaDoc getEditorUri(CmsRequestContext context, String JavaDoc resourceType, String JavaDoc userAgent) {
284
285         // step 1: check if the user specified a preferred editor for the given resource type
286
CmsUserSettings settings = new CmsUserSettings(context.currentUser());
287         String JavaDoc preferredEditorSetting = settings.getPreferredEditor(resourceType);
288         if (preferredEditorSetting == null) {
289             // no preferred editor setting found for this resource type, look for mapped resource type preferred editor
290
Iterator JavaDoc i = m_editorConfigurations.iterator();
291             while (i.hasNext()) {
292                 CmsWorkplaceEditorConfiguration currentConfig = (CmsWorkplaceEditorConfiguration)i.next();
293                 String JavaDoc mapping = currentConfig.getMappingForResourceType(resourceType);
294                 if (mapping != null) {
295                     preferredEditorSetting = settings.getPreferredEditor(mapping);
296                 }
297                 if (preferredEditorSetting != null) {
298                     break;
299                 }
300             }
301         }
302         if (preferredEditorSetting != null) {
303             CmsWorkplaceEditorConfiguration preferredConf = filterPreferredEditor(preferredEditorSetting);
304             if (preferredConf != null && preferredConf.matchesBrowser(userAgent)) {
305                 // return preferred editor only if it matches the current users browser
306
return preferredConf.getEditorUri();
307             }
308         }
309
310         // step 2: filter editors for the given resoure type
311
SortedMap JavaDoc filteredEditors = filterEditorsForResourceType(resourceType);
312
313         // step 3: check if one of the editors matches the current users browser
314
while (filteredEditors.size() > 0) {
315             // check editor configuration with highest ranking
316
Float JavaDoc key = (Float JavaDoc)filteredEditors.lastKey();
317             CmsWorkplaceEditorConfiguration conf = (CmsWorkplaceEditorConfiguration)filteredEditors.get(key);
318             if (conf.matchesBrowser(userAgent)) {
319                 return conf.getEditorUri();
320             }
321             filteredEditors.remove(key);
322         }
323
324         // no valid editor found
325
return null;
326     }
327
328     /**
329      * Filters the matching editors for the given resource type from the list of all available editors.<p>
330      *
331      * @param resourceType the resource type to filter
332      * @return a map of filtered editor configurations sorted asceding by the ranking for the current resource type, with the (Float) ranking as key
333      */

334     private SortedMap JavaDoc filterEditorsForResourceType(String JavaDoc resourceType) {
335
336         SortedMap JavaDoc filteredEditors = new TreeMap JavaDoc();
337         Iterator JavaDoc i = m_editorConfigurations.iterator();
338         while (i.hasNext()) {
339             CmsWorkplaceEditorConfiguration currentConfig = (CmsWorkplaceEditorConfiguration)i.next();
340             if (currentConfig.matchesResourceType(resourceType)) {
341                 float key = currentConfig.getRankingForResourceType(resourceType);
342                 if (key >= 0) {
343                     filteredEditors.put(new Float JavaDoc(key), currentConfig);
344                 }
345             }
346         }
347         return filteredEditors;
348     }
349
350     /**
351      * Filters the preferred editor from the list of all available editors.<p>
352      *
353      * @param preferredEditor the preferred editor identification String
354      * @return the preferred editor configuration object or null, if none is found
355      */

356     private CmsWorkplaceEditorConfiguration filterPreferredEditor(String JavaDoc preferredEditor) {
357
358         if (m_preferredEditors.size() == 0) {
359             Iterator JavaDoc i = m_editorConfigurations.iterator();
360             while (i.hasNext()) {
361                 CmsWorkplaceEditorConfiguration currentConfig = (CmsWorkplaceEditorConfiguration)i.next();
362                 m_preferredEditors.put(currentConfig.getEditorUri(), currentConfig);
363             }
364         }
365         return (CmsWorkplaceEditorConfiguration)m_preferredEditors.get(preferredEditor);
366     }
367
368 }
369
Popular Tags