KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > layoutpage > CmsLayoutPageBean


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/layoutpage/CmsLayoutPageBean.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
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.frontend.layoutpage;
33
34 import com.alkacon.simapi.Simapi;
35
36 import org.opencms.file.CmsFile;
37 import org.opencms.jsp.CmsJspActionElement;
38 import org.opencms.loader.CmsImageScaler;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.CmsLog;
41 import org.opencms.util.CmsMacroResolver;
42 import org.opencms.util.CmsStringUtil;
43 import org.opencms.workplace.CmsWorkplace;
44 import org.opencms.xml.content.CmsXmlContent;
45 import org.opencms.xml.content.CmsXmlContentFactory;
46 import org.opencms.xml.types.I_CmsXmlContentValue;
47
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.Locale JavaDoc;
51 import java.util.Map JavaDoc;
52
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletResponse JavaDoc;
55 import javax.servlet.jsp.PageContext JavaDoc;
56
57 import org.apache.commons.logging.Log;
58
59 /**
60  * Provides special methods to build the predefined layout page output HTML.<p>
61  *
62  * Please read the package information for details about modifying layouts or adding new layouts.<p>
63  *
64  * @author Andreas Zahner
65  *
66  * @version $Revision: 1.2 $
67  *
68  * @since 6.1.9
69  */

70 public class CmsLayoutPageBean extends CmsJspActionElement {
71
72     /** Default align type: paragraph with image on bottom, text above. */
73     public static final String JavaDoc ALIGN_TYPE_BOTTOM = "imagebottom";
74
75     /** Default align type: paragraph with image to the left. */
76     public static final String JavaDoc ALIGN_TYPE_IMAGE_LEFT = "imageleft";
77
78     /** Default align type: paragraph with image left, text to the right. */
79     public static final String JavaDoc ALIGN_TYPE_IMAGE_LEFT_TEXT_RIGHT = "imageleft_textright";
80
81     /** Default align type: paragraph with image to the right. */
82     public static final String JavaDoc ALIGN_TYPE_IMAGE_RIGHT = "imageright";
83
84     /** Default align type: paragraph with image right, text to the left. */
85     public static final String JavaDoc ALIGN_TYPE_IMAGE_RIGHT_TEXT_LEFT = "imageright_textleft";
86
87     /** Default align type: paragraph without image. */
88     public static final String JavaDoc ALIGN_TYPE_TEXT_ONLY = "textonly";
89
90     /** Align type: paragraph with image on top, text below. */
91     public static final String JavaDoc ALIGN_TYPE_TOP = "imagetop";
92
93     /** Default width for large images. */
94     public static final int IMG_WIDTH_LARGE = 737;
95
96     /** Default width for medium images. */
97     public static final int IMG_WIDTH_MEDIUM = 200;
98
99     /** Default width for small images. */
100     public static final int IMG_WIDTH_SMALL = 100;
101
102     /** Macro name for the image description macro. */
103     public static final String JavaDoc MACRO_DESCRIPTION = "layout.description";
104
105     /** Macro name for the headline macro. */
106     public static final String JavaDoc MACRO_HEADLINE = "layout.headline";
107
108     /** Macro name for the image macro. */
109     public static final String JavaDoc MACRO_IMAGE = "layout.image";
110
111     /** Macro name for the text macro. */
112     public static final String JavaDoc MACRO_TEXT = "layout.text";
113
114     /** Name of the frontend module in OpenCms. */
115     public static final String JavaDoc MODULE_NAME = "org.opencms.frontend.layoutpage";
116
117     /** Name of the align node. */
118     public static final String JavaDoc NODE_ALIGN = "Align";
119
120     /** Name of the description node. */
121     public static final String JavaDoc NODE_DESCRIPTION = "Description";
122
123     /** Name of the headline node. */
124     public static final String JavaDoc NODE_HEADLINE = "Headline";
125
126     /** Name of the Image node. */
127     public static final String JavaDoc NODE_IMAGE = "Image";
128
129     /** Name of the paragraph node. */
130     public static final String JavaDoc NODE_PARAGRAPH = "Paragraph";
131
132     /** Name of the text node. */
133     public static final String JavaDoc NODE_TEXT = "Text";
134
135     /** Default VFS path to the html snippet files to include to render the layout paragraphs. */
136     public static final String JavaDoc VFS_PATH_LAYOUTELEMENTS = CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/layouts/";
137
138     /** The log object for this class. */
139     private static final Log LOG = CmsLog.getLog(CmsLayoutPageBean.class);
140
141     /** The XML content that stores the layout. */
142     private CmsXmlContent m_content;
143
144     /** Stores the possible layout patterns. */
145     private Map JavaDoc m_layoutPatterns;
146
147     /** The VFS path to the html snippet files. */
148     private String JavaDoc m_pathLayoutElements;
149
150     /**
151      * Empty constructor, required for every JavaBean.<p>
152      */

153     public CmsLayoutPageBean() {
154
155         super();
156     }
157
158     /**
159      * Constructor, with parameters.<p>
160      *
161      * Use this constructor for the template.<p>
162      *
163      * @param context the JSP page context object
164      * @param req the JSP request
165      * @param res the JSP response
166      */

167     public CmsLayoutPageBean(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
168
169         super();
170         init(context, req, res);
171     }
172
173     /**
174      * Adds a pattern with the provided name and image width to the possible patterns to show.<p>
175      *
176      * @param patternName the name of the pattern layout
177      * @param imgWidth the image width to use
178      */

179     public void addLayoutPattern(String JavaDoc patternName, int imgWidth) {
180
181         m_layoutPatterns.put(patternName, new Integer JavaDoc(imgWidth));
182     }
183
184     /**
185      * Returns the HTML for the paragraphs to display.<p>
186      *
187      * @return the HTML for the paragraphs to display
188      */

189     public String JavaDoc buildHtmlParagraphs() {
190
191         StringBuffer JavaDoc result = new StringBuffer JavaDoc(16384);
192         Locale JavaDoc locale = getRequestContext().getLocale();
193         Iterator JavaDoc i = m_content.getValues(NODE_PARAGRAPH, locale).iterator();
194         while (i.hasNext()) {
195             // loop all paragraph nodes
196
I_CmsXmlContentValue value = (I_CmsXmlContentValue)i.next();
197             String JavaDoc xPath = value.getPath() + "/";
198
199             // get the optional headline
200
String JavaDoc headline = "";
201             if (m_content.hasValue(xPath + NODE_HEADLINE, locale)) {
202                 headline = m_content.getStringValue(getCmsObject(), xPath + NODE_HEADLINE, locale);
203                 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(headline)) {
204                     headline = "<h2 class=\"lp_headline\">" + headline + "</h2>\n";
205                 }
206             }
207
208             // get the text value
209
String JavaDoc textValue = m_content.getStringValue(getCmsObject(), xPath + NODE_TEXT, locale);
210
211             // process optional image
212
xPath += NODE_IMAGE + "/";
213             String JavaDoc imgDesc = "";
214             String JavaDoc imgAlign = "";
215             String JavaDoc imgTag = "";
216             if (m_content.hasValue(xPath, locale)) {
217                 // image node found, check VFS presence
218
String JavaDoc imgUri = m_content.getStringValue(getCmsObject(), xPath + NODE_IMAGE, locale);
219                 if (getCmsObject().existsResource(imgUri)) {
220                     // image exists, create image tag to show
221
if (m_content.hasValue(xPath + NODE_DESCRIPTION, locale)) {
222                         // get image description
223
imgDesc = m_content.getStringValue(getCmsObject(), xPath + NODE_DESCRIPTION, locale);
224                     }
225                     imgAlign = m_content.getStringValue(getCmsObject(), xPath + NODE_ALIGN, locale);
226                     CmsImageScaler scaler = getImageScaler(imgAlign);
227                     // create image tag with additional "alt" and "title" attributes
228
Map JavaDoc attrs = new HashMap JavaDoc(5);
229                     attrs.put("alt", imgDesc);
230                     attrs.put("title", imgDesc);
231                     imgTag = img(imgUri, scaler, attrs);
232                 }
233             }
234
235             if (CmsStringUtil.isEmpty(imgAlign)) {
236                 // set default display type (in case no image was found)
237
imgAlign = ALIGN_TYPE_TEXT_ONLY;
238             }
239
240             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(imgDesc)) {
241                 // format image description string
242
imgDesc = "<p class=\"lp_imgdesc\">" + imgDesc + "</p>";
243             }
244
245             // get the HTML layout containing macros to use for this paragraph
246
String JavaDoc elementContent = getContent(m_pathLayoutElements + imgAlign);
247             CmsMacroResolver resolver = CmsMacroResolver.newInstance();
248             // fill in macro values
249
resolver.addMacro(MACRO_HEADLINE, headline);
250             resolver.addMacro(MACRO_TEXT, textValue);
251             resolver.addMacro(MACRO_IMAGE, imgTag);
252             resolver.addMacro(MACRO_DESCRIPTION, imgDesc);
253
254             // add resolved layout element to result
255
result.append(resolver.resolveMacros(elementContent));
256         }
257
258         return result.toString();
259     }
260
261     /**
262      * Fills the default layout patterns to show.<p>
263      */

264     public void createDefaultLayoutPatterns() {
265
266         m_layoutPatterns.put(ALIGN_TYPE_BOTTOM, new Integer JavaDoc(IMG_WIDTH_LARGE));
267         m_layoutPatterns.put(ALIGN_TYPE_TOP, new Integer JavaDoc(IMG_WIDTH_LARGE));
268         m_layoutPatterns.put(ALIGN_TYPE_IMAGE_LEFT, new Integer JavaDoc(IMG_WIDTH_MEDIUM));
269         m_layoutPatterns.put(ALIGN_TYPE_IMAGE_RIGHT, new Integer JavaDoc(IMG_WIDTH_MEDIUM));
270         m_layoutPatterns.put(ALIGN_TYPE_IMAGE_LEFT_TEXT_RIGHT, new Integer JavaDoc(IMG_WIDTH_SMALL));
271         m_layoutPatterns.put(ALIGN_TYPE_IMAGE_RIGHT_TEXT_LEFT, new Integer JavaDoc(IMG_WIDTH_SMALL));
272     }
273
274     /**
275      * Returns the layout patterns with the layout name as key, the value is the corresponding image width.<p>
276      *
277      * @return the layout patterns with the layout name as key, the value is the corresponding image width
278      */

279     public Map JavaDoc getLayoutPatterns() {
280
281         return m_layoutPatterns;
282     }
283
284     /**
285      * Returns the VFS path to the html snippet files to include to render the layout paragraphs.<p>
286      *
287      * @return the VFS path to the html snippet files to include to render the layout paragraphs
288      */

289     public String JavaDoc getPathLayoutElements() {
290
291         return m_pathLayoutElements;
292     }
293
294     /**
295      * Initialize this bean with the current page context, request and response.<p>
296      *
297      * It is required to call one of the init() methods before you can use the
298      * instance of this bean.
299      *
300      * @param context the JSP page context object
301      * @param req the JSP request
302      * @param res the JSP response
303      */

304     public void init(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
305
306         super.init(context, req, res);
307         // set default path to html snippet files to use
308
m_pathLayoutElements = VFS_PATH_LAYOUTELEMENTS;
309         // read layout configuration XML content file
310
try {
311             CmsFile file = getCmsObject().readFile(getRequestContext().getUri());
312             m_content = CmsXmlContentFactory.unmarshal(getCmsObject(), file);
313         } catch (CmsException e) {
314             // log error if reading resource fails
315
if (LOG.isErrorEnabled()) {
316                 LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_VFS_RESOURCE_1, getRequestContext().getUri()));
317             }
318         }
319         // initialize pattern Map
320
m_layoutPatterns = new HashMap JavaDoc(10);
321     }
322
323     /**
324      * Sets the layout patterns with the layout name as key, the value is the corresponding image width.<p>
325      *
326      * @param layoutPatterns the layout patterns with the layout name as key, the value is the corresponding image width
327      */

328     public void setLayoutPatterns(Map JavaDoc layoutPatterns) {
329
330         m_layoutPatterns = layoutPatterns;
331     }
332
333     /**
334      * Sets the VFS path to the html snippet files to include to render the layout paragraphs.<p>
335      *
336      * @param pathLayoutElements the VFS path to the html snippet files to include to render the layout paragraphs
337      */

338     public void setPathLayoutElements(String JavaDoc pathLayoutElements) {
339
340         m_pathLayoutElements = pathLayoutElements;
341     }
342
343     /**
344      * Returns an initialized image scaler depending on the image align to use.<p>
345      *
346      * @param imgAlign the image align to use for a paragraph
347      * @return an initialized image scaler depending on the image align to use
348      */

349     protected CmsImageScaler getImageScaler(String JavaDoc imgAlign) {
350
351         // create scaler instance
352
CmsImageScaler scaler = new CmsImageScaler();
353         scaler.setType(1);
354         scaler.setPosition(Simapi.POS_DOWN_LEFT);
355
356         // get the image width from layout pattern Map
357
Integer JavaDoc imgWidth = (Integer JavaDoc)getLayoutPatterns().get(imgAlign);
358         if (imgWidth != null) {
359             // found a width value
360
scaler.setWidth(imgWidth.intValue());
361         } else {
362             // did not find a value, provide a default width
363
scaler.setWidth(IMG_WIDTH_MEDIUM);
364         }
365
366         return scaler;
367     }
368
369 }
370
Popular Tags