KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsWorkplaceEditorConfiguration.java,v $
3  * Date : $Date: 2006/03/27 14:52:49 $
4  * Version: $Revision: 1.14 $
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.main.CmsLog;
35 import org.opencms.util.CmsStringUtil;
36 import org.opencms.xml.CmsXmlException;
37 import org.opencms.xml.CmsXmlUtils;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.regex.Pattern JavaDoc;
45 import java.util.regex.PatternSyntaxException JavaDoc;
46
47 import org.apache.commons.logging.Log;
48
49 import org.dom4j.Document;
50 import org.dom4j.Element;
51
52 /**
53  * Single editor configuration object.<p>
54  *
55  * Holds all necessary information about an OpenCms editor which is stored in the
56  * "editor_configuration.xml" file in each editor folder.<p>
57  *
58  * Provides methods to get the editor information for the editor manager.<p>
59  *
60  * @author Andreas Zahner
61  *
62  * @version $Revision: 1.14 $
63  *
64  * @since 6.0.0
65  */

66 public class CmsWorkplaceEditorConfiguration {
67
68     /** Name of the root document node. */
69     public static final String JavaDoc DOCUMENT_NODE = "editor";
70
71     /** Name of the single user agent node. */
72     protected static final String JavaDoc N_AGENT = "agent";
73
74     /** Name of the resource type class node. */
75     protected static final String JavaDoc N_CLASS = "class";
76
77     /** Name of the editor label node. */
78     protected static final String JavaDoc N_LABEL = "label";
79
80     /** Name of the resource type subnode mapto. */
81     protected static final String JavaDoc N_MAPTO = "mapto";
82
83     /** Name of the resource type subnode name. */
84     protected static final String JavaDoc N_NAME = "name";
85
86     /** Name of the resource type subnode ranking. */
87     protected static final String JavaDoc N_RANKING = "ranking";
88
89     /** Name of the resourcetypes node. */
90     protected static final String JavaDoc N_RESOURCETYPES = "resourcetypes";
91
92     /** Name of the resource type node. */
93     protected static final String JavaDoc N_TYPE = "type";
94
95     /** Name of the useragents node. */
96     protected static final String JavaDoc N_USERAGENTS = "useragents";
97
98     /** Name of the widgeteditor node. */
99     protected static final String JavaDoc N_WIDGETEDITOR = "widgeteditor";
100
101     /** The log object for this class. */
102     private static final Log LOG = CmsLog.getLog(CmsWorkplaceEditorConfiguration.class);
103
104     private List JavaDoc m_browserPattern;
105     private String JavaDoc m_editorLabel;
106     private String JavaDoc m_editorUri;
107     private Map JavaDoc m_resTypes;
108     private List JavaDoc m_userAgentsRegEx;
109     private boolean m_validConfiguration;
110     private String JavaDoc m_widgetEditor;
111
112     /**
113      * Constructor with xml data String.<p>
114      *
115      * @param xmlData the XML data String containing the information about the editor
116      * @param editorUri the editor workplace URI
117      */

118     public CmsWorkplaceEditorConfiguration(byte[] xmlData, String JavaDoc editorUri) {
119
120         setValidConfiguration(true);
121         try {
122             initialize(CmsXmlUtils.unmarshalHelper(xmlData, null), editorUri);
123         } catch (CmsXmlException e) {
124             // xml String could not be parsed
125
logConfigurationError(Messages.get().getBundle().key(Messages.ERR_XML_PARSE_0), e);
126         }
127     }
128
129     /**
130      * Returns the list of compiled browser patterns.<p>
131      *
132      * @return the list of compiled browser patterns
133      */

134     public List JavaDoc getBrowserPattern() {
135
136         return m_browserPattern;
137     }
138
139     /**
140      * Returns the editor label key used for the localized nice name.<p>
141      *
142      * @return the editor label key used for the localized nice name
143      */

144     public String JavaDoc getEditorLabel() {
145
146         return m_editorLabel;
147     }
148
149     /**
150      * Returns the editor workplace URI.<p>
151      *
152      * @return the editor workplace URI
153      */

154     public String JavaDoc getEditorUri() {
155
156         return m_editorUri;
157     }
158
159     /**
160      * Returns the mapping for the given resource type.<p>
161      *
162      * @param resourceType the resource type name to check
163      * @return the mapping or null, if no mapping is specified
164      */

165     public String JavaDoc getMappingForResourceType(String JavaDoc resourceType) {
166
167         String JavaDoc[] resourceTypeParams = (String JavaDoc[])getResourceTypes().get(resourceType);
168         if (resourceTypeParams == null) {
169             return null;
170         } else {
171             return resourceTypeParams[1];
172         }
173     }
174
175     /**
176      * Returns the ranking value for the given resource type.<p>
177      *
178      * @param resourceType the current resource type
179      * @return the ranking (the higher the better)
180      */

181     public float getRankingForResourceType(String JavaDoc resourceType) {
182
183         String JavaDoc[] resourceTypeParams = (String JavaDoc[])getResourceTypes().get(resourceType);
184         if (resourceTypeParams == null) {
185             return -1.0f;
186         } else {
187             return Float.parseFloat(resourceTypeParams[0]);
188         }
189     }
190
191     /**
192      * Returns the valid resource types of the editor.<p>
193      *
194      * A single map item has the resource type name as key,
195      * the value is a String array with two entries:
196      * <ul>
197      * <li>Entry 0: the ranking for the resource type</li>
198      * <li>Entry 1: the mapping to another resource type or null</li>
199      * </ul><p>
200      *
201      * @return the valid resource types of the editor
202      */

203     public Map JavaDoc getResourceTypes() {
204
205         return m_resTypes;
206     }
207
208     /**
209      * Returns the valid user agents regular expressions of the editor.<p>
210      *
211      * @return the valid user agents regular expressions of the editor
212      */

213     public List JavaDoc getUserAgentsRegEx() {
214
215         return m_userAgentsRegEx;
216     }
217
218     /**
219      * Returns the widget editor class for rich text editing.<p>
220      *
221      * @return the widget editor class for rich text editing
222      */

223     public String JavaDoc getWidgetEditor() {
224
225         return m_widgetEditor;
226     }
227
228     /**
229      * Returns if the current configuration is valid.<p>
230      *
231      * @return true if no configuration errors were found, otherwise false
232      */

233     public boolean isValidConfiguration() {
234
235         return m_validConfiguration;
236     }
237
238     /**
239      * Returns if the editor is usable as a widget editor for rich text editing.<p>
240      *
241      * @return true if the editor is usable as a widget editor for rich text editing, otherwise false
242      */

243     public boolean isWidgetEditor() {
244
245         return CmsStringUtil.isNotEmpty(m_widgetEditor);
246     }
247
248     /**
249      * Tests if the current browser is matching the configuration.<p>
250      *
251      * @param currentBrowser the users browser String to test
252      * @return true if the browser matches the configuration, otherwise false
253      */

254     public boolean matchesBrowser(String JavaDoc currentBrowser) {
255
256         if (currentBrowser == null) {
257             return false;
258         }
259         for (int i = 0; i < getBrowserPattern().size(); i++) {
260             boolean matches = ((Pattern JavaDoc)getBrowserPattern().get(i)).matcher(currentBrowser.trim()).matches();
261             if (matches) {
262                 if (LOG.isDebugEnabled()) {
263                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_BROWSER_MATCHES_CONFIG_1, currentBrowser));
264                 }
265                 return true;
266             }
267         }
268         return false;
269     }
270
271     /**
272      * Returns if the configuration is suitable for the given resource type.<p>
273      *
274      * @param resourceType the resource type to check
275      * @return true if the configuration matches the resource type
276      */

277     public boolean matchesResourceType(String JavaDoc resourceType) {
278
279         return m_resTypes.containsKey(resourceType);
280     }
281
282     /**
283      * Initializes all member variables.<p>
284      *
285      * @param document the XML configuration document
286      * @param editorUri the editor workplace URI
287      */

288     private void initialize(Document document, String JavaDoc editorUri) {
289
290         // get the root element of the configuration
291
Element rootElement = document.getRootElement();
292
293         // set the label of the editor
294
setEditorLabel(rootElement.elementText(N_LABEL));
295
296         // set the widget editor class if available
297
String JavaDoc widgetClass = rootElement.elementText(N_WIDGETEDITOR);
298         if (CmsStringUtil.isNotEmpty(widgetClass)) {
299             setWidgetEditor(widgetClass);
300         }
301
302         // set the URI of the editor
303
setEditorUri(editorUri);
304
305         // create the map of valid resource types
306
Iterator JavaDoc i = rootElement.element(N_RESOURCETYPES).elementIterator(N_TYPE);
307         Map JavaDoc resTypes = new HashMap JavaDoc();
308         while (i.hasNext()) {
309             Element currentType = (Element)i.next();
310             float ranking;
311             String JavaDoc name = currentType.elementText(N_NAME);
312             if (CmsStringUtil.isEmpty(name)) {
313                 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_INVALID_RESTYPE_NAME_0), null);
314                 continue;
315             }
316             try {
317                 ranking = Float.parseFloat(currentType.elementText(N_RANKING));
318             } catch (Throwable JavaDoc t) {
319                 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_INVALID_RESTYPE_RANKING_1, name), t);
320                 continue;
321             }
322             String JavaDoc mapTo = currentType.elementText(N_MAPTO);
323             if (CmsStringUtil.isEmpty(mapTo)) {
324                 mapTo = null;
325             }
326             resTypes.put(name, new String JavaDoc[] {"" + ranking, mapTo});
327         }
328         // add the additional resource types
329
i = rootElement.element(N_RESOURCETYPES).elementIterator(N_CLASS);
330         while (i.hasNext()) {
331             Element currentClass = (Element)i.next();
332             String JavaDoc name = currentClass.elementText(N_NAME);
333             List JavaDoc assignedTypes = new ArrayList JavaDoc();
334             try {
335                 // get the editor type matcher class
336
I_CmsEditorTypeMatcher matcher = (I_CmsEditorTypeMatcher)Class.forName(name).newInstance();
337                 assignedTypes = matcher.getAdditionalResourceTypes();
338             } catch (Throwable JavaDoc t) {
339                 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_INVALID_RESTYPE_CLASS_1, name), t);
340                 continue;
341             }
342             float ranking;
343             try {
344                 ranking = Float.parseFloat(currentClass.elementText(N_RANKING));
345             } catch (Throwable JavaDoc t) {
346                 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_INVALID_RESTYPE_RANKING_1, name), t);
347                 continue;
348             }
349             String JavaDoc mapTo = currentClass.elementText(N_MAPTO);
350             if ("".equals(mapTo)) {
351                 mapTo = null;
352             }
353             // now loop through all types found and add them
354
Iterator JavaDoc j = assignedTypes.iterator();
355             while (j.hasNext()) {
356                 String JavaDoc typeName = (String JavaDoc)j.next();
357                 resTypes.put(typeName, new String JavaDoc[] {"" + ranking, mapTo});
358             }
359         }
360
361         setResourceTypes(resTypes);
362
363         // create the list of user agents & compiled patterns for editor
364
i = document.getRootElement().element(N_USERAGENTS).elementIterator(N_AGENT);
365         List JavaDoc pattern = new ArrayList JavaDoc();
366         List JavaDoc userAgents = new ArrayList JavaDoc();
367         while (i.hasNext()) {
368             Element currentAgent = (Element)i.next();
369             String JavaDoc agentName = currentAgent.getText();
370             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(agentName)) {
371                 userAgents.add(agentName);
372                 try {
373                     pattern.add(Pattern.compile(agentName));
374                 } catch (PatternSyntaxException JavaDoc e) {
375                     logConfigurationError(
376                         Messages.get().getBundle().key(Messages.ERR_COMPILE_EDITOR_REGEX_1, agentName),
377                         e);
378                 }
379             } else {
380                 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_INVALID_USERAGENT_DEF_0), null);
381             }
382         }
383         setBrowserPattern(pattern);
384         setUserAgentsRegEx(userAgents);
385     }
386
387     /**
388      * Logs configuration errors and invalidates the current configuration.<p>
389      *
390      * @param message the message specifying the configuration error
391      * @param t the Throwable object or null
392      */

393     private void logConfigurationError(String JavaDoc message, Throwable JavaDoc t) {
394
395         setValidConfiguration(false);
396         if (LOG.isErrorEnabled()) {
397             if (t == null) {
398                 LOG.error(Messages.get().getBundle().key(Messages.LOG_EDITOR_CONFIG_ERROR_1, message));
399             } else {
400                 LOG.error(Messages.get().getBundle().key(Messages.LOG_EDITOR_CONFIG_ERROR_1, message), t);
401             }
402         }
403     }
404
405     /**
406      * Sets the list of compiled browser patterns.<p>
407      *
408      * @param pattern the list of compiled browser patterns
409      */

410     private void setBrowserPattern(List JavaDoc pattern) {
411
412         if (pattern == null || pattern.size() == 0) {
413             setValidConfiguration(false);
414             LOG.error(Messages.get().getBundle().key(Messages.LOG_EDITOR_CONFIG_NO_PATTERN_0));
415         }
416         m_browserPattern = pattern;
417     }
418
419     /**
420      * Sets the editor label key used for the localized nice name.<p>
421      *
422      * @param label the editor label key used for the localized nice name
423      */

424     private void setEditorLabel(String JavaDoc label) {
425
426         if (CmsStringUtil.isEmptyOrWhitespaceOnly(label)) {
427             setValidConfiguration(false);
428             LOG.error(Messages.get().getBundle().key(Messages.LOG_EDITOR_CONFIG_NO_LABEL_0));
429         }
430         m_editorLabel = label;
431     }
432
433     /**
434      * Sets the editor workplace URI.<p>
435      * @param uri the editor workplace URI
436      */

437     private void setEditorUri(String JavaDoc uri) {
438
439         if (CmsStringUtil.isEmptyOrWhitespaceOnly(uri)) {
440             setValidConfiguration(false);
441             LOG.error(Messages.get().getBundle().key(Messages.LOG_EDITOR_CONFIG_NO_URI_0));
442         }
443         m_editorUri = uri;
444     }
445
446     /**
447      * Sets the valid resource types of the editor.<p>
448      *
449      * @param types the valid resource types of the editor
450      */

451     private void setResourceTypes(Map JavaDoc types) {
452
453         if (types == null || types.size() == 0) {
454             setValidConfiguration(false);
455             LOG.error(Messages.get().getBundle().key(Messages.LOG_NO_RESOURCE_TYPES_0));
456         }
457         m_resTypes = types;
458     }
459
460     /**
461      * Sets the valid user agents regular expressions of the editor.<p>
462      *
463      * @param agents the valid user agents regular expressions of the editor
464      */

465     private void setUserAgentsRegEx(List JavaDoc agents) {
466
467         if (agents == null || agents.size() == 0) {
468             setValidConfiguration(false);
469             LOG.error(Messages.get().getBundle().key(Messages.LOG_NO_USER_AGENTS_0));
470         }
471         m_userAgentsRegEx = agents;
472     }
473
474     /**
475      * Sets if the current configuration is valid.<p>
476      *
477      * @param isValid true if no configuration errors were found, otherwise false
478      */

479     private void setValidConfiguration(boolean isValid) {
480
481         m_validConfiguration = isValid;
482     }
483
484     /**
485      * Sets the widget editor class for rich text editing.<p>
486      *
487      * @param widgetEditor the widget editor class for rich text editing
488      */

489     private void setWidgetEditor(String JavaDoc widgetEditor) {
490
491         m_widgetEditor = widgetEditor;
492     }
493
494 }
495
Popular Tags