KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagParse.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.file.CmsObject;
35 import org.opencms.file.CmsRequestContext;
36 import org.opencms.flex.CmsFlexController;
37 import org.opencms.jsp.parse.A_CmsConfiguredHtmlParser;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.CmsLog;
40 import org.opencms.util.CmsStringUtil;
41
42 import java.io.PrintWriter JavaDoc;
43 import java.io.StringWriter JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46
47 import javax.servlet.ServletRequest JavaDoc;
48 import javax.servlet.jsp.JspException JavaDoc;
49 import javax.servlet.jsp.PageContext JavaDoc;
50 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
51
52 import org.apache.commons.logging.Log;
53
54 import org.htmlparser.util.ParserException;
55
56 /**
57  * Implements the <code>&lt;cms:parse&gt;&lt;/cms:parse&gt;</code> tag to allow parsing of nested
58  * HTML with the {@link org.opencms.jsp.parse.A_CmsConfiguredHtmlParser}} implementation specified by the "parserClass" attribute.
59  * <p>
60  *
61  * @author Achim Westermann
62  *
63  * @version $Revision: 1.3 $
64  *
65  * @since 6.1.3
66  */

67 public class CmsJspTagParse extends BodyTagSupport JavaDoc {
68
69     /**
70      * The name of the mandatory Tag attribute for the visitor class an instance of will be guided
71      * throught the body content.
72      */

73     public static final String JavaDoc ATT_VISITOR_CLASS = "parserClass";
74
75     /** The log object for this class. */
76     private static final Log LOG = CmsLog.getLog(CmsJspTagParse.class);
77
78     /** Serial version UID required for safe serialization. */
79     private static final long serialVersionUID = -6541745426202242240L;
80
81     /** Tag name constant for log output. */
82     public static final String JavaDoc TAG_NAME = "parse";
83
84     /** The visitor / parser classname to use. */
85     private String JavaDoc m_configuredParserClassname;
86
87     /** List of upper case tag name strings of tags that should not be auto-corrected if closing divs are missing. */
88     private List JavaDoc m_noAutoCloseTags;
89
90     /** The attribute value of the param attribute. */
91     private String JavaDoc m_param = "";
92
93     /**
94      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
95      * @return EVAL_PAGE
96      * @throws JspException in case soemthing goes wrong
97      */

98     public int doEndTag() throws JspException JavaDoc {
99
100         ServletRequest JavaDoc req = pageContext.getRequest();
101         A_CmsConfiguredHtmlParser parser;
102
103         // This will always be true if the page is called through OpenCms
104
if (CmsFlexController.isCmsRequest(req)) {
105             String JavaDoc content = "";
106             try {
107                 if (CmsStringUtil.isEmpty(m_configuredParserClassname)) {
108                     if (LOG.isErrorEnabled()) {
109                         LOG.error(Messages.get().getBundle().key(
110                             Messages.GUI_ERR_TAG_ATTRIBUTE_MISSING_2,
111                             new Object JavaDoc[] {TAG_NAME, ATT_VISITOR_CLASS}));
112                     }
113
114                 }
115                 // wrong attribute visitorClass -> content will remain empty, but no exception is
116
// thrown
117
try {
118                     // load
119
Class JavaDoc cl = Class.forName(m_configuredParserClassname);
120                     // instanciate
121
Object JavaDoc instance = cl.newInstance();
122                     // cast
123
parser = (A_CmsConfiguredHtmlParser)instance;
124                     parser.setParam(m_param);
125                     // cms object:
126
CmsFlexController controller = CmsFlexController.getController(req);
127                     CmsObject cms = controller.getCmsObject();
128                     parser.setCmsObject(cms);
129                     content = parseTagAction(getBodyContent().getString(), pageContext, parser);
130
131                 } catch (Exception JavaDoc e) {
132                     if (LOG.isErrorEnabled()) {
133                         LOG.error(Messages.get().getBundle().key(
134                             Messages.GUI_ERR_TAG_ATTRIBUTE_INVALID_3,
135                             new Object JavaDoc[] {TAG_NAME, ATT_VISITOR_CLASS, A_CmsConfiguredHtmlParser.class.getName()}), e);
136                     }
137                     e.printStackTrace(System.err);
138                 }
139
140             } finally {
141                 try {
142                     getBodyContent().clear();
143                     getBodyContent().print(content);
144                     getBodyContent().writeOut(pageContext.getOut());
145                     // need to release manually, JSP container may not call release as required
146
// (happens with Tomcat)
147
release();
148
149                 } catch (Exception JavaDoc ex) {
150                     release();
151                     if (LOG.isErrorEnabled()) {
152                         LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, TAG_NAME), ex);
153                     }
154                     // this is severe
155
throw new JspException JavaDoc(ex);
156                 }
157
158             }
159
160         }
161         return EVAL_PAGE;
162     }
163
164     /**
165      * Getter for the attribute "noAutoCloseTags" of the &lt;cms:parse&gt; tag.<p>
166      *
167      * Returns a <code>String</code> that consists of the comma-separated upper case tag names for which this
168      * tag will not correct missing closing tags. <p>
169      *
170      *
171      * @return a String that consists of the comma-separated upper case tag names for which this
172      * tag will not correct missing closing tags.
173      */

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

192     public String JavaDoc getParam() {
193
194         return m_param;
195     }
196
197     /**
198      * Returns the fully qualified class name of the {@link A_CmsConfiguredHtmlParser} class to use
199      * for parsing.
200      * <p>
201      *
202      * @return the parserrClass
203      */

204     public String JavaDoc getParserClass() {
205
206         return m_configuredParserClassname;
207     }
208
209     /**
210      * Internal action method.
211      * <p>
212      *
213      * Parses (and potentially transforms) a HTMl content block.
214      * <p>
215      *
216      * @param content the content to be parsed / transformed.
217      *
218      * @param context needed for getting the encoding / the locale.
219      *
220      * @param parser the visitor / parser to use.
221      *
222      * @return the transformed content.
223      *
224      */

225     public String JavaDoc parseTagAction(String JavaDoc content, PageContext JavaDoc context, A_CmsConfiguredHtmlParser parser) {
226
227         String JavaDoc result = null;
228         CmsRequestContext cmsContext = CmsFlexController.getCmsObject(context.getRequest()).getRequestContext();
229
230         if (parser == null) {
231             if (LOG.isErrorEnabled()) {
232                 LOG.error(Messages.get().getBundle(cmsContext.getLocale()).key(
233                     Messages.GUI_ERR_TAG_ATTRIBUTE_MISSING_2,
234                     new Object JavaDoc[] {TAG_NAME, ATT_VISITOR_CLASS}));
235             }
236             result = content;
237         } else {
238
239             String JavaDoc encoding = cmsContext.getEncoding();
240             try {
241                 result = parser.doParse(content, encoding, this.m_noAutoCloseTags);
242
243             } catch (ParserException pex) {
244
245                 if (LOG.isErrorEnabled()) {
246                     LOG.error(Messages.get().getBundle(cmsContext.getLocale()).key(
247                         Messages.ERR_PROCESS_TAG_1,
248                         new Object JavaDoc[] {TAG_NAME}), pex);
249                 }
250                 StringWriter JavaDoc stackTrace = new StringWriter JavaDoc();
251                 PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new StringWriter JavaDoc());
252                 StringBuffer JavaDoc msg = new StringBuffer JavaDoc("<!--\n").append(pex.getLocalizedMessage()).append("\n");
253                 pex.printStackTrace(writer);
254                 msg.append(stackTrace.toString()).append("\n-->");
255                 result = msg.toString();
256             } catch (CmsException cmex) {
257                 if (LOG.isErrorEnabled()) {
258                     LOG.error(Messages.get().getBundle(cmsContext.getLocale()).key(
259                         Messages.ERR_PROCESS_TAG_1,
260                         new Object JavaDoc[] {TAG_NAME}), cmex);
261                 }
262                 StringWriter JavaDoc stackTrace = new StringWriter JavaDoc();
263                 PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new StringWriter JavaDoc());
264                 StringBuffer JavaDoc msg = new StringBuffer JavaDoc("<!--\n").append(cmex.getLocalizedMessage()).append("\n");
265                 cmex.printStackTrace(writer);
266                 msg.append(stackTrace.toString()).append("\n-->");
267                 result = msg.toString();
268             }
269
270         }
271         return result;
272     }
273
274     /**
275      * @see javax.servlet.jsp.tagext.Tag#release()
276      */

277     public void release() {
278
279         m_configuredParserClassname = null;
280         m_param = null;
281         super.release();
282     }
283
284     /**
285      * Setter for the attribute "noAutoCloseTags" of the &lt;cms:parse&gt; tag.<p>
286      *
287      * Awaits a <code>String</code> that consists of the comma-separated upper case tag names for which this
288      * tag should not correct missing closing tags.<p>
289      *
290      * @param noAutoCloseTagList a <code>String</code> that consists of the comma-separated upper case tag names for which this
291      * tag should not correct missing closing tags.
292      */

293     public void setNoAutoCloseTags(String JavaDoc noAutoCloseTagList) {
294     
295         m_noAutoCloseTags = CmsStringUtil.splitAsList(noAutoCloseTagList, ',');
296         
297     }
298
299     /**
300      * Sets the param.
301      * <p>
302      *
303      * @param param the param to set
304      */

305     public void setParam(String JavaDoc param) {
306
307         m_param = param;
308     }
309
310     /**
311      * Sets the fully qualified class name of the {@link A_CmsConfiguredHtmlParser} class to use for
312      * parsing.
313      * <p>
314      *
315      * @param parserClass the fully qualified class name of the {@link A_CmsConfiguredHtmlParser}
316      * class to use for parsing.
317      */

318     public void setParserClass(String JavaDoc parserClass) {
319
320         m_configuredParserClassname = parserClass;
321     }
322 }
323
Popular Tags