KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > decorator > CmsDecorationDefintion


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/decorator/CmsDecorationDefintion.java,v $
3  * Date : $Date: 2006/03/27 14:52:31 $
4  * Version: $Revision: 1.2 $
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.jsp.decorator;
33
34 import org.opencms.cache.CmsVfsMemoryObjectCache;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsResourceFilter;
38 import org.opencms.file.types.CmsResourceTypePlain;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.CmsLog;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.Collections JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Locale JavaDoc;
47
48 import org.apache.commons.logging.Log;
49
50 /**
51  * This class defines text decoration to be made by the postprocessor.<p>
52  *
53  * @author Michael Emmerich
54  *
55  * @version $Revision: 1.2 $
56  *
57  * @since 6.1.3
58  */

59 public class CmsDecorationDefintion {
60
61     /** The log object for this class. */
62     private static final Log LOG = CmsLog.getLog(CmsDecorationDefintion.class);
63
64     /** The name of the configuration file holding all word substitutions. */
65     private String JavaDoc m_configurationFile;
66
67     /** Flag, signaling if the first occurance of a word must be marked differntly. */
68     private boolean m_markFirst;
69
70     /** The name of the substitution. */
71     private String JavaDoc m_name;
72
73     /** The post to be added after the occurance of a word. */
74     private String JavaDoc m_postText;
75
76     /** The post to be added after the occurance of a word on its first occurance. */
77     private String JavaDoc m_postTextFirst;
78
79     /** The prefix to be added in front of the occurance of a word. */
80     private String JavaDoc m_preText;
81
82     /** The prefix to be added in front of the occurance of a word on its first occurance. */
83     private String JavaDoc m_preTextFirst;
84
85     /**
86      * Constructor, creates a new empty CmsDecorationDefintion.<p>
87      */

88     public CmsDecorationDefintion() {
89
90         m_configurationFile = null;
91         m_markFirst = false;
92         m_name = null;
93         m_postText = null;
94         m_postTextFirst = null;
95         m_preText = null;
96         m_preTextFirst = null;
97     }
98
99     /**
100      * Constructor, creates a new CmsDecorationDefintion with given values.<p>
101      *
102      * @param name the name of the decoration defintinion
103      * @param preText the preText to be used
104      * @param postText the postText to be used
105      * @param preTextFirst the preTextFirst to be used
106      * @param postTextFirst the postTextFirst to be used
107      * @param markFrist the flag to use different decorations for the first occurance
108      * @param configurationFile the name of the configuration file
109      */

110     public CmsDecorationDefintion(
111         String JavaDoc name,
112         String JavaDoc preText,
113         String JavaDoc postText,
114         String JavaDoc preTextFirst,
115         String JavaDoc postTextFirst,
116         boolean markFrist,
117         String JavaDoc configurationFile) {
118
119         m_configurationFile = configurationFile;
120         m_markFirst = markFrist;
121         m_name = name;
122         m_postText = postText;
123         m_postTextFirst = postTextFirst;
124         m_preText = preText;
125         m_preTextFirst = preTextFirst;
126     }
127
128     /**
129      * Creates a CmsDecorationBundle of text decoration to be used by the decorator.<p>
130      *
131      * @param cms the CmsObject
132      * @param locale the locale to build the decoration bundle for. If no locale is given, a bundle of all locales is build
133      * @return CmsDecorationBundle including all decoration lists that match the locale
134      * @throws CmsException if something goes wrong
135      */

136     public CmsDecorationBundle createDecorationBundle(CmsObject cms, Locale JavaDoc locale) throws CmsException {
137
138         // get configfile basename and the list of all decoration map files
139
List JavaDoc decorationMapFiles = getDecorationMapFiles(cms);
140         if (LOG.isDebugEnabled()) {
141             LOG.debug(Messages.get().getBundle().key(
142                 Messages.LOG_DECORATION_DEFINITION_MAP_FILES_2,
143                 decorationMapFiles,
144                 locale));
145         }
146
147         // create decoration maps
148
List JavaDoc decorationMaps = getDecorationMaps(cms, decorationMapFiles);
149         if (LOG.isDebugEnabled()) {
150             LOG.debug(Messages.get().getBundle().key(Messages.LOG_DECORATION_DEFINITION_MAPS_2, decorationMaps, locale));
151         }
152
153         // now that we have all decoration maps we can build the decoration bundle
154
// the bundele is depending on the locale, if a locale is given, only those decoration maps that contain the
155
// locale (or no locale at all) must be used. If no locale is given, all decoration maps are
156
// put into the decoration bundle
157
return createDecorationBundle(decorationMaps, locale);
158     }
159
160     /**
161      * Returns the configurationFile.<p>
162      *
163      *
164      * @return the configurationFile
165      */

166     public String JavaDoc getConfigurationFile() {
167
168         return m_configurationFile;
169     }
170
171     /**
172      * Returns the name.<p>
173      *
174      * @return the name
175      */

176     public String JavaDoc getName() {
177
178         return m_name;
179     }
180
181     /**
182      * Returns the postText.<p>
183      *
184      * @return the postText
185      */

186     public String JavaDoc getPostText() {
187
188         return m_postText;
189     }
190
191     /**
192      * Returns the postTextFirst.<p>
193      *
194      * @return the postTextFirst
195      */

196     public String JavaDoc getPostTextFirst() {
197
198         return m_postTextFirst;
199     }
200
201     /**
202      * Returns the preText.<p>
203      *
204      * @return the preText
205      */

206     public String JavaDoc getPreText() {
207
208         return m_preText;
209     }
210
211     /**
212      * Returns the preTextFirst.<p>
213      *
214      * @return the preTextFirst
215      */

216     public String JavaDoc getPreTextFirst() {
217
218         return m_preTextFirst;
219     }
220
221     /**
222      * Returns the markFirst flag.<p>
223      *
224      * @return the markFirst flag
225      */

226     public boolean isMarkFirst() {
227
228         return m_markFirst;
229     }
230
231     /**
232      * Sets the configurationFile.<p>
233      *
234      * @param configurationFile the configurationFile to set
235      */

236     public void setConfigurationFile(String JavaDoc configurationFile) {
237
238         m_configurationFile = configurationFile;
239     }
240
241     /**
242      * Sets the markFirst flag.<p>
243      *
244      * @param markFirst the markFirst flag to set
245      */

246     public void setMarkFirst(boolean markFirst) {
247
248         m_markFirst = markFirst;
249     }
250
251     /**
252      * Sets the name.<p>
253      *
254      * @param name the name to set
255      */

256     public void setName(String JavaDoc name) {
257
258         m_name = name;
259     }
260
261     /**
262      * Sets the postText.<p>
263      *
264      * @param postText the postText to set
265      */

266     public void setPostText(String JavaDoc postText) {
267
268         m_postText = postText;
269     }
270
271     /**
272      * Sets the postTextFirst.<p>
273      *
274      * @param postTextFirst the postTextFirst to set
275      */

276     public void setPostTextFirst(String JavaDoc postTextFirst) {
277
278         m_postTextFirst = postTextFirst;
279     }
280
281     /**
282      * Sets the preText.<p>
283      *
284      * @param preText the preText to set
285      */

286     public void setPreText(String JavaDoc preText) {
287
288         m_preText = preText;
289     }
290
291     /**
292      * Sets the preTextFirst.<p>
293      *
294      * @param preTextFirst the preTextFirst to set
295      */

296     public void setPreTextFirst(String JavaDoc preTextFirst) {
297
298         m_preTextFirst = preTextFirst;
299     }
300
301     /**
302      * @see java.lang.Object#toString()
303      */

304     public String JavaDoc toString() {
305
306         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
307         buf.append(this.getClass().getName());
308         buf.append(" [name = '");
309         buf.append(m_name);
310         buf.append("', markFirst = '");
311         buf.append(m_markFirst);
312         buf.append("', preText = '");
313         buf.append(m_preText);
314         buf.append("', postText = '");
315         buf.append(m_postText);
316         buf.append("', preTextFirst = '");
317         buf.append(m_preTextFirst);
318         buf.append("', postTextFirst = '");
319         buf.append(m_postTextFirst);
320         buf.append("', configFile = ");
321         buf.append(m_configurationFile);
322         buf.append("]");
323         return buf.toString();
324     }
325
326     /**
327      * Creates a CmsDecorationBundle of text decoration to be used by the decorator based on a list of decoration maps.<p>
328      *
329      * @param decorationMaps the decoration maps to build the bundle from
330      * @param locale the locale to build the decoration bundle for. If no locale is given, a bundle of all locales is build
331      * @return CmsDecorationBundle including all decoration lists that match the locale
332      */

333     private CmsDecorationBundle createDecorationBundle(List JavaDoc decorationMaps, Locale JavaDoc locale) {
334
335         CmsDecorationBundle decorationBundle = new CmsDecorationBundle(locale);
336         // sort the bundles
337
Collections.sort(decorationMaps);
338         // now process the decoration maps to see which of those must be added to the bundle
339
Iterator JavaDoc i = decorationMaps.iterator();
340         while (i.hasNext()) {
341             CmsDecorationMap decMap = (CmsDecorationMap)i.next();
342             // a decoration map must be added to the bundle if one of the following conditions match:
343
// 1) the bundle has no locale
344
// 2) the bundle has a locale and the locale of the map is equal or a sublocale
345
// 3) the bundle has a locale and the map has no locale
346
if ((locale == null)
347                 || (locale != null && decMap.getLocale() != null && locale.getDisplayLanguage().equals(
348                     decMap.getLocale().getDisplayLanguage()))
349                 || (locale != null && decMap.getLocale() == null)) {
350                 decorationBundle.putAll(decMap.getDecorationMap());
351                 if (LOG.isDebugEnabled()) {
352                     LOG.debug(Messages.get().getBundle().key(
353                         Messages.LOG_DECORATION_DEFINITION_CREATE_BUNDLE_2,
354                         decMap.getName(),
355                         locale));
356                 }
357             }
358         }
359         return decorationBundle;
360     }
361
362     /**
363      * Gets the list of all decoartion map files that match to the current basename.<p>
364      *
365      * @param cms the CmsObject
366      * @return list of CmsResources of the decoration map files
367      * @throws CmsException if something goes wrong.
368      */

369     private List JavaDoc getDecorationMapFiles(CmsObject cms) throws CmsException {
370
371         List JavaDoc files = new ArrayList JavaDoc();
372
373         // calcualte the basename for the decoration map files
374
// the basename is the filename without the fileextension and any "_locale" postfixes
375
// e.g. decoration_en.csv will generate "decoration" as basename
376
StringBuffer JavaDoc baseFilename = new StringBuffer JavaDoc();
377         baseFilename.append(CmsResource.getParentFolder(m_configurationFile));
378         String JavaDoc filename = cms.readResource(m_configurationFile).getName();
379         // get rid of the fileextension if there is one
380
if (filename.indexOf(".") > -1) {
381             filename = filename.substring(0, filename.indexOf("."));
382         }
383         // extract the basename
384
if (filename.indexOf("_") > -1) {
385             filename = filename.substring(0, filename.indexOf("_"));
386         }
387         baseFilename.append(filename);
388         String JavaDoc basename = baseFilename.toString();
389
390         // get all config files which belong to this basename
391

392         List JavaDoc resources = cms.readResources(CmsResource.getParentFolder(m_configurationFile), CmsResourceFilter.DEFAULT);
393         Iterator JavaDoc i = resources.iterator();
394         while (i.hasNext()) {
395             CmsResource res = (CmsResource)i.next();
396             if (cms.getSitePath(res).startsWith(basename) && res.getTypeId() == CmsResourceTypePlain.getStaticTypeId()) {
397                 files.add(res);
398             }
399         }
400
401         return files;
402     }
403
404     /**
405      * Creates a list of decoration map objects from a given list of decoration files.<p>
406      *
407      * @param cms the CmsObject
408      * @param decorationListFiles the list of decoration files
409      * @return list of decoration map objects
410      */

411     private List JavaDoc getDecorationMaps(CmsObject cms, List JavaDoc decorationListFiles) {
412
413         List JavaDoc decorationMaps = new ArrayList JavaDoc();
414         Iterator JavaDoc i = decorationListFiles.iterator();
415         while (i.hasNext()) {
416             CmsResource res = (CmsResource)i.next();
417             try {
418                 CmsDecorationMap decMap = (CmsDecorationMap)CmsVfsMemoryObjectCache.getVfsMemoryObjectCache().getCachedObject(
419                     cms,
420                     res.getRootPath());
421                 if (decMap == null) {
422                     decMap = new CmsDecorationMap(cms, res, this);
423                     CmsVfsMemoryObjectCache.getVfsMemoryObjectCache().putCachedObject(cms, res.getRootPath(), decMap);
424                 }
425
426                 decorationMaps.add(decMap);
427             } catch (CmsException e) {
428                 if (LOG.isErrorEnabled()) {
429                     LOG.error(Messages.get().getBundle().key(
430                         Messages.LOG_DECORATION_DEFINITION_CREATE_MAP_2,
431                         res.getName(),
432                         e));
433                 }
434             }
435         }
436         return decorationMaps;
437     }
438
439 }
440
Popular Tags