KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/decorator/CmsDecoratorConfiguration.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.xml.content.CmsXmlContent;
39 import org.opencms.xml.content.CmsXmlContentFactory;
40
41 import java.util.ArrayList JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Locale JavaDoc;
44
45 /**
46  * The CmsDecoratorConfiguration initalizes and stores the text decorations.<p>
47  *
48  * It uses uses the information of one or more <code>{@link CmsDecorationDefintion}</code> to create the
49  * pre- and postfixs for text decorations.
50  *
51  * @author Michael Emmerich
52  *
53  * @version $Revision: 1.2 $
54  *
55  * @since 6.1.3
56  */

57
58 public class CmsDecoratorConfiguration {
59
60     /** The xpath for the decoration configuration. */
61     private static final String JavaDoc XPATH_DECORATION = "decoration";
62
63     /** The xpath for the filename configuration. */
64     private static final String JavaDoc XPATH_FILENAME = "filename";
65
66     /** The xpath for the markfirst configuration. */
67     private static final String JavaDoc XPATH_MARKFIRST = "markfirst";
68
69     /** The xpath for the name configuration. */
70     private static final String JavaDoc XPATH_NAME = "name";
71
72     /** The xpath for the posttext configuration. */
73     private static final String JavaDoc XPATH_POSTTEXT = "posttext";
74
75     /** The xpath for the posttextfirst configuration. */
76     private static final String JavaDoc XPATH_POSTTEXTFIRST = "posttextfirst";
77
78     /** The xpath for the pretext configuration. */
79     private static final String JavaDoc XPATH_PRETEXT = "pretext";
80
81     /** The xpath for the pretextfirst configuration. */
82     private static final String JavaDoc XPATH_PRETEXTFIRST = "pretextfirst";
83
84     /** The xpath for the uselocale configuration. */
85     private static final String JavaDoc XPATH_USELOCALE = "uselocale";
86
87     /** The CmsObject. */
88     private CmsObject m_cms;
89
90     /** The config file. */
91     private String JavaDoc m_configFile;
92
93     /** The locale for extracting the configuration data. */
94     private Locale JavaDoc m_configurationLocale = new Locale JavaDoc("en");
95
96     /** Map of configured decorations. */
97     private CmsDecorationBundle m_decorations;
98
99     /** The locale for to build the configuration for. */
100     private Locale JavaDoc m_locale;
101
102     /** The list of already used decorations. */
103     private List JavaDoc m_usedDecorations;
104
105     /**
106      * Constructor, creates a new, empty CmsDecoratorConfiguration.<p>
107      *
108      * @param cms the CmsObject
109      *
110      */

111     public CmsDecoratorConfiguration(CmsObject cms) {
112
113         m_decorations = new CmsDecorationBundle();
114         m_configFile = null;
115         m_cms = cms;
116         m_locale = m_cms.getRequestContext().getLocale();
117         m_usedDecorations = new ArrayList JavaDoc();
118     }
119
120     /**
121      * Constructor, creates a new, CmsDecoratorConfiguration with a given config file.<p>
122      *
123      * @param cms the CmsObject
124      * @param configFile the configuration file
125      * @throws CmsException if something goes wrong
126      */

127     public CmsDecoratorConfiguration(CmsObject cms, String JavaDoc configFile)
128     throws CmsException {
129
130         m_decorations = new CmsDecorationBundle();
131         m_configFile = configFile;
132         m_cms = cms;
133         m_locale = m_cms.getRequestContext().getLocale();
134         m_usedDecorations = new ArrayList JavaDoc();
135         init();
136     }
137
138     /**
139      * Constructor, creates a new, CmsDecoratorConfiguration with a given config file and locale.<p>
140      *
141      * @param cms the CmsObject
142      * @param configFile the configuration file
143      * @param locale to locale to build this configuration for
144      * @throws CmsException if something goes wrong
145      */

146     public CmsDecoratorConfiguration(CmsObject cms, String JavaDoc configFile, Locale JavaDoc locale)
147     throws CmsException {
148
149         m_decorations = new CmsDecorationBundle(locale);
150         m_configFile = configFile;
151         m_cms = cms;
152         m_locale = locale;
153         m_usedDecorations = new ArrayList JavaDoc();
154         init();
155     }
156
157     /**
158      * Adds decorations defined in a <code>{@link CmsDecorationDefintion}</code> object to the map of all decorations.<p>
159      * @param decorationDefinition the <code>{@link CmsDecorationDefintion}</code> the decorations to be added
160      * @throws CmsException if something goes wrong
161      */

162     public void addDecorations(CmsDecorationDefintion decorationDefinition) throws CmsException {
163
164         m_decorations.putAll(decorationDefinition.createDecorationBundle(m_cms, m_configurationLocale).getAll());
165     }
166
167     /**
168      * Gets the decoration bundle.<p>
169      *@return the decoration bundle to be used
170      */

171     public CmsDecorationBundle getDecorations() {
172
173         return m_decorations;
174     }
175
176     /**
177      * Tests if a decoration key was used before in this configuration.<p>
178      * @param key the key to look for
179      * @return true if this key was already used
180      */

181     public boolean hasUsed(String JavaDoc key) {
182
183         return m_usedDecorations.contains(key);
184     }
185
186     /**
187      * Mark a decoration key as already used.<p>
188      * @param key the key to mark
189      */

190     public void markAsUsed(String JavaDoc key) {
191
192         m_usedDecorations.add(key);
193     }
194
195     /**
196      * Resets the used decoration keys.<p>
197      */

198     public void resetMarkedDecorations() {
199
200         m_usedDecorations = new ArrayList JavaDoc();
201     }
202
203     /**
204      * Sets the decoration bundle, overwriting an exiting one.<p>
205      *
206      * @param decorations new decoration bundle
207      */

208     public void setDecorations(CmsDecorationBundle decorations) {
209
210         m_decorations = decorations;
211     }
212
213     /**
214      * @see java.lang.Object#toString()
215      */

216     public String JavaDoc toString() {
217
218         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
219         buf.append(this.getClass().getName());
220         buf.append(" [configFile = '");
221         buf.append(m_configFile);
222         buf.append("', decorations = '");
223         buf.append(m_decorations);
224         buf.append("', locale = '");
225         buf.append(m_locale);
226         buf.append("']");
227         return buf.toString();
228     }
229
230     /**
231      * Builds a CmsDecorationDefintion from a given configuration file.<p>
232      *
233      * @param configuration the configuration file
234      * @param i the number of the decoration definition to create
235      * @return CmsDecorationDefintion created form configuration file
236      */

237     private CmsDecorationDefintion getDecorationDefinition(CmsXmlContent configuration, int i) {
238
239         CmsDecorationDefintion decDef = new CmsDecorationDefintion();
240         String JavaDoc name = configuration.getValue(XPATH_DECORATION + "[" + i + "]/" + XPATH_NAME, m_configurationLocale).getStringValue(
241             m_cms);
242         String JavaDoc markfirst = configuration.getValue(
243             XPATH_DECORATION + "[" + i + "]/" + XPATH_MARKFIRST,
244             m_configurationLocale).getStringValue(m_cms);
245         String JavaDoc pretext = configuration.getValue(
246             XPATH_DECORATION + "[" + i + "]/" + XPATH_PRETEXT,
247             m_configurationLocale).getStringValue(m_cms);
248         String JavaDoc posttext = configuration.getValue(
249             XPATH_DECORATION + "[" + i + "]/" + XPATH_POSTTEXT,
250             m_configurationLocale).getStringValue(m_cms);
251         String JavaDoc pretextfirst = configuration.getValue(
252             XPATH_DECORATION + "[" + i + "]/" + XPATH_PRETEXTFIRST,
253             m_configurationLocale).getStringValue(m_cms);
254         String JavaDoc posttextfirst = configuration.getValue(
255             XPATH_DECORATION + "[" + i + "]/" + XPATH_POSTTEXTFIRST,
256             m_configurationLocale).getStringValue(m_cms);
257         String JavaDoc filenname = configuration.getValue(
258             XPATH_DECORATION + "[" + i + "]/" + XPATH_FILENAME,
259             m_configurationLocale).getStringValue(m_cms);
260
261         decDef.setName(name);
262         decDef.setMarkFirst(markfirst.equals("true"));
263         decDef.setPreText(pretext);
264         decDef.setPostText(posttext);
265         decDef.setPreTextFirst(pretextfirst);
266         decDef.setPostTextFirst(posttextfirst);
267         decDef.setConfigurationFile(filenname);
268
269         return decDef;
270     }
271
272     /**
273      * Initialises the configuration.<p>
274      * @throws CmsException if something goes wrong
275      */

276     private void init() throws CmsException {
277
278         // get the configuration file
279
CmsResource res = m_cms.readResource(m_configFile);
280         CmsFile file = CmsFile.upgrade(res, m_cms);
281         CmsXmlContent configuration = CmsXmlContentFactory.unmarshal(m_cms, file);
282
283         // get the uselocale flag
284
// if this flag is not set to true, we must build locale independent decoration bundles
285
String JavaDoc uselocale = configuration.getValue(XPATH_USELOCALE, m_configurationLocale).getStringValue(m_cms);
286         if (!uselocale.equals("true")) {
287             m_locale = null;
288         }
289         // get the number of decoration definitions
290
int decorationDefCount = configuration.getIndexCount(XPATH_DECORATION, m_configurationLocale);
291         // get all the decoration definitions
292
for (int i = 1; i <= decorationDefCount; i++) {
293             CmsDecorationDefintion decDef = getDecorationDefinition(configuration, i);
294             CmsDecorationBundle decBundle = decDef.createDecorationBundle(m_cms, m_locale);
295             // merge it to the already existing decorations
296
m_decorations.putAll(decBundle.getAll());
297         }
298     }
299
300 }
301
Popular Tags