KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/decorator/CmsDecorationMap.java,v $
3  * Date : $Date: 2006/03/27 14:52:30 $
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.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsLog;
39 import org.opencms.util.CmsStringUtil;
40
41 import java.util.HashMap JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Locale JavaDoc;
45 import java.util.Map JavaDoc;
46
47 import org.apache.commons.logging.Log;
48
49 /**
50  * The CmsDecorationMap is the object representation of a single decoartion file.<p>
51  *
52  * The semicolon seperated elements of the decoartion file are stored in a map. <p>
53  * The map uses the decoration as keys and CmsDecorationObjects as values.<p>
54  * Multiple CmsDecorationMaps form a CmsDecorationBundle.
55  *
56  * @author Michael Emmerich
57  *
58  * @version $Revision: 1.2 $
59  *
60  * @since 6.1.3
61  */

62 public class CmsDecorationMap implements Comparable JavaDoc {
63
64     /** The seperator for the CSV file. */
65     public static final String JavaDoc CSV_SEPERATOR = "|";
66
67     /** The log object for this class. */
68     private static final Log LOG = CmsLog.getLog(CmsDecorationMap.class);
69
70     /** The map to store all elements in. */
71     private Map JavaDoc m_decorationMap;
72
73     /** The decorator defintion to be used for this decoration map. */
74     private CmsDecorationDefintion m_decoratorDefinition;
75
76     /** The locale of this decoration map. */
77     private Locale JavaDoc m_locale;
78
79     /** The name of the decoration map. */
80     private String JavaDoc m_name;
81
82     /**
83      * Constructor, creates a new CmsDecorationMap.<p>
84      *
85      * @param cms the CmsObject
86      * @param res the resource to extrace the decorations from
87      * @param decDef the CmsDecorationDefintion to be used in this decoration map
88      * @throws CmsException if something goes wrong
89      */

90     public CmsDecorationMap(CmsObject cms, CmsResource res, CmsDecorationDefintion decDef)
91     throws CmsException {
92
93         m_decoratorDefinition = decDef;
94         m_name = res.getName();
95         m_locale = extractLocale();
96         m_decorationMap = fillMap(cms, res);
97
98     }
99
100     /**
101      * @see java.lang.Comparable#compareTo(java.lang.Object)
102      */

103     public int compareTo(Object JavaDoc o) {
104
105         int compValue = 0;
106         if (o instanceof CmsDecorationMap) {
107             compValue = m_name.compareTo(((CmsDecorationMap)o).getName());
108         }
109         return compValue;
110     }
111
112     /**
113      * Returns the decorationMap.<p>
114      *
115      * @return the decorationMap
116      */

117     public Map JavaDoc getDecorationMap() {
118
119         return m_decorationMap;
120     }
121
122     /**
123      * Returns the locale.<p>
124      *
125      * @return the locale
126      */

127     public Locale JavaDoc getLocale() {
128
129         return m_locale;
130     }
131
132     /**
133      * Returns the name.<p>
134      *
135      * @return the name
136      */

137     public String JavaDoc getName() {
138
139         return m_name;
140     }
141
142     /**
143      * @see java.lang.Object#toString()
144      */

145     public String JavaDoc toString() {
146
147         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
148         buf.append(this.getClass().getName());
149         buf.append(" [name = '");
150         buf.append(m_name);
151         buf.append("' locale=");
152         buf.append(m_locale);
153         buf.append("' mapsize=");
154         buf.append(m_decorationMap.size());
155         buf.append("]");
156         return buf.toString();
157     }
158
159     /**
160      * Extracts the locale from the decoration filename.<p>
161      *
162      *@return locale extraced form filename or null
163      */

164     private Locale JavaDoc extractLocale() {
165
166         Locale JavaDoc loc = null;
167         int underscore = m_name.indexOf("_");
168         if (underscore > -1) {
169             String JavaDoc localeName = m_name.substring(underscore + 1);
170             if (localeName.indexOf(".") > -1) {
171                 localeName = localeName.substring(0, localeName.indexOf("."));
172             }
173             loc = new Locale JavaDoc(localeName);
174         }
175
176         return loc;
177     }
178
179     /**
180      * Fills the decoration map with values from the decoation file.<p>
181      *
182      * @param cms the CmsObject
183      * @param res the decoration file
184      * @return decoration map, using decoration as key and decoration description as value
185      * @throws CmsException if something goes wrong
186      */

187     private Map JavaDoc fillMap(CmsObject cms, CmsResource res) throws CmsException {
188
189         if (LOG.isDebugEnabled()) {
190             LOG.debug(Messages.get().getBundle().key(
191                 Messages.LOG_DECORATION_MAP_FILL_MAP_2,
192                 m_name,
193                 m_decoratorDefinition));
194         }
195
196         Map JavaDoc decMap = new HashMap JavaDoc();
197         // upgrade the resource to get the file content
198
CmsFile file = CmsFile.upgrade(res, cms);
199         // get all the entries
200
String JavaDoc unparsedContent = new String JavaDoc(file.getContents());
201
202         String JavaDoc delimiter = "\r\n";
203         if (unparsedContent.indexOf(delimiter) == -1) {
204             // there was no \r\n delimiter in the csv file, so check if the lines are seperated by
205
// \n only
206
if (unparsedContent.indexOf("\n") > -1) {
207                 delimiter = "\n";
208             }
209         }
210
211         if (LOG.isDebugEnabled()) {
212             LOG.debug(Messages.get().getBundle().key(
213                 Messages.LOG_DECORATION_MAP_FILL_MAP_DELIMITER_2,
214                 res.getName(),
215                 CmsStringUtil.escapeJavaScript(delimiter)));
216         }
217
218         List JavaDoc entries = CmsStringUtil.splitAsList(unparsedContent, delimiter);
219
220         if (LOG.isDebugEnabled()) {
221             LOG.debug(Messages.get().getBundle().key(
222                 Messages.LOG_DECORATION_MAP_FILL_MAP_SPLIT_LIST_2,
223                 res.getName(),
224                 entries));
225         }
226         Iterator JavaDoc i = entries.iterator();
227         while (i.hasNext()) {
228             try {
229                 String JavaDoc entry = (String JavaDoc)i.next();
230                 // extract key and value
231
if (CmsStringUtil.isNotEmpty(entry)) {
232                     int speratator = entry.indexOf(CSV_SEPERATOR);
233                     if (speratator > -1) {
234                         String JavaDoc key = entry.substring(0, speratator).trim();
235                         String JavaDoc value = entry.substring(speratator + 1).trim();
236                         if (CmsStringUtil.isNotEmpty(key) && CmsStringUtil.isNotEmpty(value)) {
237                             CmsDecorationObject decObj = new CmsDecorationObject(
238                                 key,
239                                 value,
240                                 m_decoratorDefinition,
241                                 m_locale);
242                             decMap.put(key, decObj);
243                             if (LOG.isDebugEnabled()) {
244                                 LOG.debug(Messages.get().getBundle().key(
245                                     Messages.LOG_DECORATION_MAP_ADD_DECORATION_OBJECT_2,
246                                     decObj,
247                                     key));
248                             }
249                         }
250                     }
251                 }
252             } catch (Exception JavaDoc e) {
253                 if (LOG.isErrorEnabled()) {
254                     LOG.error(Messages.get().getBundle().key(Messages.LOG_DECORATION_MAP_FILL_2, m_name, e));
255                 }
256             }
257         }
258
259         return decMap;
260     }
261
262 }
263
Popular Tags