KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > CmsJspTagDecorate


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagDecorate.java,v $
3  * Date : $Date: 2006/09/19 14:29:08 $
4  * Version: $Revision: 1.3 $
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;
33
34 import org.opencms.flex.CmsFlexController;
35 import org.opencms.jsp.decorator.CmsDecoratorConfiguration;
36 import org.opencms.jsp.decorator.CmsHtmlDecorator;
37 import org.opencms.main.CmsLog;
38 import org.opencms.util.CmsStringUtil;
39
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Locale JavaDoc;
43
44 import javax.servlet.ServletRequest JavaDoc;
45 import javax.servlet.jsp.JspException JavaDoc;
46 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
47
48 import org.apache.commons.logging.Log;
49
50 /**
51  * Implements the <code>&lt;cms:decorate&gt;&lt;/cms:decorate&gt;</code>
52  * tag to decorate HTML content with configurated decoration maps.<p>
53  *
54  * @author Michael Emmerich
55  *
56  * @version $Revision: 1.3 $
57  *
58  * @since 6.1.3
59  */

60 public class CmsJspTagDecorate extends BodyTagSupport JavaDoc {
61
62     /** The log object for this class. */
63     private static final Log LOG = CmsLog.getLog(CmsJspTagDecorate.class);
64
65     /** Serial version UID required for safe serialization. */
66     private static final long serialVersionUID = 3072561342127379294L;
67
68     /** The configuration. */
69     private String JavaDoc m_file;
70
71     /** The decoration locale. */
72     private String JavaDoc m_locale;
73
74     /** List of upper case tag name strings of tags that should not be auto-corrected if closing divs are missing. */
75     private List JavaDoc m_noAutoCloseTags;
76
77     /**
78      * Internal action method.<p>
79      *
80      * DEcorates a HTMl content block.<p>
81      *
82      * @param content the content to be decorated
83      * @param configFile the config file
84      * @param locale the locale to use for decoration or NOLOCALE if not locale should be used
85      * @param req the current request
86      * @return the decorated content
87      *
88      * @see org.opencms.staticexport.CmsLinkManager#substituteLink(org.opencms.file.CmsObject, String)
89      */

90     public String JavaDoc decorateTagAction(String JavaDoc content, String JavaDoc configFile, String JavaDoc locale, ServletRequest JavaDoc req) {
91
92         try {
93             Locale JavaDoc loc = null;
94             CmsFlexController controller = CmsFlexController.getController(req);
95             if (CmsStringUtil.isEmpty(locale)) {
96                 loc = controller.getCmsObject().getRequestContext().getLocale();
97             } else {
98                 loc = new Locale JavaDoc(locale);
99             }
100
101             String JavaDoc encoding = controller.getCmsObject().getRequestContext().getEncoding();
102             CmsDecoratorConfiguration config = new CmsDecoratorConfiguration(controller.getCmsObject(), configFile, loc);
103             CmsHtmlDecorator decorator = new CmsHtmlDecorator(config);
104             decorator.setNoAutoCloseTags(m_noAutoCloseTags);
105             return decorator.doDecoration(content, encoding);
106         } catch (Exception JavaDoc e) {
107             if (LOG.isErrorEnabled()) {
108                 LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "decoration"), e);
109             }
110             return content;
111         }
112     }
113
114     /**
115      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
116      * @return EVAL_PAGE
117      * @throws JspException in case soemthing goes wrong
118      */

119     public int doEndTag() throws JspException JavaDoc {
120
121         ServletRequest JavaDoc req = pageContext.getRequest();
122
123         // This will always be true if the page is called through OpenCms
124
if (CmsFlexController.isCmsRequest(req)) {
125             try {
126                 String JavaDoc content = decorateTagAction(getBodyContent().getString(), getFile(), getLocale(), req);
127                 getBodyContent().clear();
128                 getBodyContent().print(content);
129                 getBodyContent().writeOut(pageContext.getOut());
130
131             } catch (Exception JavaDoc ex) {
132                 if (LOG.isErrorEnabled()) {
133                     LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "decoration"), ex);
134                 }
135                 throw new JspException JavaDoc(ex);
136             }
137         }
138         return EVAL_PAGE;
139     }
140
141     /**
142      * Returns the file name.<p>
143      *
144      * @return the file name
145      */

146     public String JavaDoc getFile() {
147
148         return m_file;
149     }
150
151     /**
152      * Returns the locale name.<p>
153      *
154      * @return the locale name
155      */

156     public String JavaDoc getLocale() {
157
158         return m_locale;
159     }
160
161     /**
162      * Getter for the attribute "noAutoCloseTags" of the &lt;cms:parse&gt; tag.<p>
163      *
164      * Returns a <code>String</code> that consists of the comma-separated upper case tag names for which this
165      * tag will not correct missing closing tags. <p>
166      *
167      *
168      * @return a String that consists of the comma-separated upper case tag names for which this
169      * tag will not correct missing closing tags.
170      */

171     public String JavaDoc getNoAutoCloseTags() {
172
173         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
174         if (m_noAutoCloseTags != null & m_noAutoCloseTags.size() > 0) {
175             Iterator JavaDoc it = m_noAutoCloseTags.iterator();
176             while (it.hasNext()) {
177                 result.append(it.next()).append(',');
178             }
179         }
180         return result.toString();
181     }
182
183     /**
184      * Sets the file name.<p>
185      *
186      * @param file the file name
187      */

188     public void setFile(String JavaDoc file) {
189
190         if (file != null) {
191             m_file = file.toLowerCase();
192         }
193     }
194
195     /**
196      * Sets the locale name.<p>
197      *
198      * @param locale the locale name
199      */

200     public void setLocale(String JavaDoc locale) {
201
202         m_locale = locale;
203     }
204
205     /**
206      * Setter for the attribute "noAutoCloseTags" of the &lt;cms:parse&gt; tag.<p>
207      *
208      * Awaits a <code>String</code> that consists of the comma-separated upper case tag names for which this
209      * tag should not correct missing closing tags.<p>
210      *
211      * @param noAutoCloseTagList a <code>String</code> that consists of the comma-separated upper case tag names for which this
212      * tag should not correct missing closing tags.
213      */

214     public void setNoAutoCloseTags(String JavaDoc noAutoCloseTagList) {
215
216         m_noAutoCloseTags = CmsStringUtil.splitAsList(noAutoCloseTagList, ',');
217
218     }
219 }
220
Popular Tags