KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > CmsTemplateStyleSheet


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/CmsTemplateStyleSheet.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.21 $
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.frontend.templateone;
33
34 import org.opencms.main.CmsLog;
35 import org.opencms.util.CmsStringUtil;
36 import org.opencms.xml.content.CmsXmlContent;
37 import org.opencms.xml.types.I_CmsXmlContentValue;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Locale JavaDoc;
42 import java.util.StringTokenizer JavaDoc;
43
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.http.HttpServletResponse JavaDoc;
46 import javax.servlet.jsp.PageContext JavaDoc;
47
48 import org.apache.commons.logging.Log;
49
50 /**
51  * Provides methods to build the dynamic CSS style sheet of template one.<p>
52  *
53  * @author Andreas Zahner
54  *
55  * @version $Revision: 1.21 $
56  *
57  * @since 6.0.0
58  */

59 public class CmsTemplateStyleSheet extends CmsTemplateBase {
60
61     /** Default file name of the CSS configuration file. */
62     public static final String JavaDoc FILENAME_CONFIGFILE = "configuration_css";
63
64     /** Node name of the optional configuration nodes. */
65     public static final String JavaDoc NODE_OPTIONALCONFIG = "StyleOptional";
66
67     /** Request parameter name providing the configuration file URI. */
68     public static final String JavaDoc PARAM_CONFIGFILE = "config";
69
70     /** Name of the property key to set the path to the configuration file. */
71     public static final String JavaDoc PROPERTY_CONFIGFILE = "properties_style";
72
73     /** The log object for this class. */
74     private static final Log LOG = CmsLog.getLog(CmsTemplateStyleSheet.class);
75
76     /** Stores the style sheet configuration. */
77     private CmsXmlContent m_configuration;
78
79     /** Stores the sizes of the headlines. */
80     private List JavaDoc m_headlineSizes;
81
82     /** Stores the substituted path to the modules resources. */
83     private String JavaDoc m_resPath;
84
85     /** Stores the calculated width of the template. */
86     private String JavaDoc m_templateWidth;
87
88     /**
89      * Empty constructor, required for every JavaBean.<p>
90      */

91     public CmsTemplateStyleSheet() {
92
93         super();
94     }
95
96     /**
97      * Constructor, with parameters.<p>
98      *
99      * Use this constructor for the template.<p>
100      *
101      * @param context the JSP page context object
102      * @param req the JSP request
103      * @param res the JSP response
104      */

105     public CmsTemplateStyleSheet(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
106
107         super();
108         init(context, req, res);
109     }
110
111     /**
112      * Calculates the height of an element.<p>
113      *
114      * @param value the old height value
115      * @param delta the delta to use
116      * @return the new height value
117      */

118     public static String JavaDoc calculateHeight(String JavaDoc value, int delta) {
119
120         String JavaDoc newHeight = value;
121         try {
122             int val = Integer.parseInt(value);
123             newHeight = "" + (val + delta);
124         } catch (Exception JavaDoc e) {
125             // error parsing the value
126
}
127         return newHeight;
128     }
129
130     /**
131      * Returns the configuration value for the specified key from the configuration.<p>
132      *
133      * @param key the key name to look up
134      * @param defaultValue the default value used when no value was found for the key
135      * @return the configuration value for the specified key
136      */

137     public String JavaDoc getConfigValue(String JavaDoc key, String JavaDoc defaultValue) {
138
139         String JavaDoc value = null;
140         try {
141             value = m_configuration.getStringValue(getCmsObject(), key, getRequestContext().getLocale());
142             if (CmsStringUtil.isEmpty(value)) {
143                 // value not found for current Locale, try to get it from first found Locale
144
value = m_configuration.getStringValue(getCmsObject(), key, (Locale JavaDoc)m_configuration.getLocales().get(0));
145             }
146         } catch (Exception JavaDoc e) {
147             // log error in debug mode
148
if (LOG.isDebugEnabled()) {
149                 LOG.debug(e.getMessage(), e);
150             }
151         }
152         if (CmsStringUtil.isEmpty(value)) {
153             // no configuration value found, use the default value
154
value = defaultValue;
155         }
156         return value;
157     }
158
159     /**
160      * Returns the CSS formatting String for underlining an element.<p>
161      *
162      * Checks the value in the configuration file, if the value is <code>"true"</code>,
163      * the text decoration will be underlined, otherwise none.<p>
164      *
165      * @param key the key name to look up in the optional configuration
166      * @param defaultValue the default value used when no value was found for the key
167      * @return the CSS formatting String for underlining an element
168      */

169     public String JavaDoc getFontUnderline(String JavaDoc key, String JavaDoc defaultValue) {
170
171         String JavaDoc value = getOptionalConfigValue(key, defaultValue);
172         if (Boolean.valueOf(value).booleanValue()) {
173             value = "underline;";
174         } else {
175             value = "none;";
176         }
177         return "text-decoration: " + value;
178     }
179
180     /**
181      * Returns the CSS formatting String for bold output of an element.<p>
182      *
183      * Checks the value in the configuration file, if the value is <code>"true"</code>,
184      * the font weight will be bold, otherwise normal.<p>
185      *
186      * @param key the key name to look up in the optional configuration
187      * @param defaultValue the default value used when no value was found for the key
188      * @return the CSS formatting String for bold output of an element
189      */

190     public String JavaDoc getFontWeight(String JavaDoc key, String JavaDoc defaultValue) {
191
192         String JavaDoc value = getOptionalConfigValue(key, defaultValue);
193         if (Boolean.valueOf(value).booleanValue()) {
194             value = "bold;";
195         } else {
196             value = "normal;";
197         }
198         return "font-weight: " + value;
199     }
200
201     /**
202      * Returns the size of the headline with the given number.<p>
203      *
204      * @param headline the number of the heading element (1 to 6)
205      * @return the size of the headline with the given number
206      */

207     public String JavaDoc getHeadlineSize(int headline) {
208
209         String JavaDoc size = (String JavaDoc)getHeadlineSizes().get(headline - 1);
210         if (CmsStringUtil.isEmpty(size)) {
211             size = "13";
212         }
213         return size;
214     }
215
216     /**
217      * Returns an optional configuration value for the specified key from the configuration.<p>
218      *
219      * @param key the key name to look up
220      * @param defaultValue the default value used when no value was found for the key
221      * @return the optional configuration value for the specified key
222      */

223     public String JavaDoc getOptionalConfigValue(String JavaDoc key, String JavaDoc defaultValue) {
224
225         return getConfigValue(NODE_OPTIONALCONFIG + "/" + key, defaultValue);
226     }
227
228     /**
229      * Returns the substituted path to the modules resource folder.<p>
230      *
231      * @return the substituted path to the modules resource folder
232      */

233     public String JavaDoc getResourcePath() {
234
235         return m_resPath;
236     }
237
238     /**
239      * Returns the width of the template to display depending on the configuration.<p>
240      *
241      * @return the width of the template
242      */

243     public String JavaDoc getTemplateWidth() {
244
245         if (m_templateWidth == null) {
246             String JavaDoc templateType = getConfigValue("main.template.type", "normal");
247             if ("small".equals(templateType)) {
248                 m_templateWidth = "800";
249             } else {
250                 m_templateWidth = "950";
251             }
252         }
253         return m_templateWidth;
254     }
255
256     /**
257      * Initialize this bean with the current page context, request and response.<p>
258      *
259      * It is required to call one of the init() methods before you can use the
260      * instance of this bean.
261      *
262      * @param context the JSP page context object
263      * @param req the JSP request
264      * @param res the JSP response
265      */

266     public void init(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
267
268         // call initialization of super class
269
super.init(context, req, res);
270         // set site root to get correct configuration files
271
String JavaDoc siteRoot = req.getParameter(CmsTemplateBean.PARAM_SITE);
272         if (CmsStringUtil.isNotEmpty(siteRoot)) {
273             getRequestContext().setSiteRoot(siteRoot);
274         }
275         // set resource path
276
m_resPath = req.getParameter(CmsTemplateNavigation.PARAM_RESPATH);
277
278         // collect the configuration information
279
try {
280             String JavaDoc configUri = req.getParameter(PARAM_CONFIGFILE);
281             if (CmsStringUtil.isNotEmpty(configUri)) {
282                 m_configuration = CmsTemplateBean.getConfigurationFile(configUri, getCmsObject());
283             }
284         } catch (Exception JavaDoc e) {
285             // problem getting properties, log error
286
if (LOG.isDebugEnabled()) {
287                 LOG.debug(e.getMessage(), e);
288             }
289         }
290     }
291
292     /**
293      * Calculates the size of the heading elements from the configuration.<p>
294      *
295      * The list holds the sizes of the heading elements and contains 6 String objects.<p>
296      *
297      * @return the size of the heading elements from the configuration
298      */

299     private List JavaDoc getHeadlineSizes() {
300
301         if (m_headlineSizes == null) {
302             List JavaDoc sizes = new ArrayList JavaDoc(6);
303             // get values from configuration file
304
String JavaDoc selectedValues = getOptionalConfigValue("headlines.set", "");
305             if (CmsStringUtil.isEmpty(selectedValues)) {
306                 // sizes are not configured, determine default values
307
try {
308                     CmsXmlContent conf = m_configuration;
309                     if (conf != null) {
310                         // create optional configuration node
311
conf.addValue(getCmsObject(), NODE_OPTIONALCONFIG, getRequestContext().getLocale(), 0);
312                         I_CmsXmlContentValue value = conf.getValue(
313                             NODE_OPTIONALCONFIG + "/headlines.set",
314                             getRequestContext().getLocale());
315                         // get default value String from XSD
316
selectedValues = value.getContentDefinition().getContentHandler().getConfiguration(value);
317                         // get default size sequence from beginning of String
318
selectedValues = selectedValues.substring(0, selectedValues.indexOf('*'));
319                     } else {
320                         selectedValues = "13-12-11-10-9-9";
321                     }
322                 } catch (Exception JavaDoc e) {
323                     selectedValues = "13-12-11-10-9-9";
324                 }
325             }
326             // split values in parts to get individual size values
327
StringTokenizer JavaDoc T = new StringTokenizer JavaDoc(selectedValues, "-");
328             while (T.hasMoreTokens()) {
329                 sizes.add(T.nextToken());
330             }
331             m_headlineSizes = sizes;
332         }
333         return m_headlineSizes;
334     }
335 }
Popular Tags