KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagContentShow.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.26 $
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.flex.CmsFlexController;
36 import org.opencms.i18n.CmsLocaleManager;
37 import org.opencms.i18n.CmsMessageContainer;
38 import org.opencms.i18n.CmsMessages;
39 import org.opencms.main.CmsLog;
40 import org.opencms.util.CmsMacroResolver;
41 import org.opencms.util.CmsStringUtil;
42 import org.opencms.xml.CmsXmlUtils;
43 import org.opencms.xml.I_CmsXmlDocument;
44
45 import java.io.IOException JavaDoc;
46 import java.util.Locale JavaDoc;
47
48 import javax.servlet.jsp.JspException JavaDoc;
49 import javax.servlet.jsp.JspTagException JavaDoc;
50 import javax.servlet.jsp.PageContext JavaDoc;
51 import javax.servlet.jsp.tagext.Tag JavaDoc;
52 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
53
54 import org.apache.commons.logging.Log;
55
56 /**
57  * Used to access and display XML content item information from the VFS.<p>
58  *
59  * @author Alexander Kandzior
60  *
61  * @version $Revision: 1.26 $
62  *
63  * @since 6.0.0
64  */

65 public class CmsJspTagContentShow extends TagSupport JavaDoc {
66
67     /** The log object for this class. */
68     private static final Log LOG = CmsLog.getLog(CmsJspTagContentShow.class);
69
70     /** Serial version UID required for safe serialization. */
71     private static final long serialVersionUID = -6776067180965738432L;
72
73     /** Name of the content node element to show. */
74     private String JavaDoc m_element;
75
76     /** Locale of the content node elemen to show. */
77     private Locale JavaDoc m_locale;
78
79     /**
80      * Internal action method to show an element from a XML content document.<p>
81      *
82      * @param container the content container to read the XML content from
83      * @param context the current JSP page context
84      * @param element the node name of the element to show
85      * @param locale the locale of the element to show
86      *
87      * @return the value of the selected content element
88      */

89     public static String JavaDoc contentShowTagAction(
90         I_CmsXmlContentContainer container,
91         PageContext JavaDoc context,
92         String JavaDoc element,
93         Locale JavaDoc locale) {
94
95         // get the current users OpenCms context
96
CmsObject cms = CmsFlexController.getCmsObject(context.getRequest());
97
98         // get loaded content from content container
99
I_CmsXmlDocument xmlContent = container.getXmlDocument();
100
101         if (CmsStringUtil.isEmpty(element)) {
102             element = container.getXmlDocumentElement();
103         } else {
104             element = CmsXmlUtils.concatXpath(container.getXmlDocumentElement(), element);
105         }
106
107         String JavaDoc content;
108         if (CmsMacroResolver.isMacro(element)) {
109             // this is a macro, initialize a macro resolver
110
String JavaDoc resourcename = CmsJspTagContentLoad.getResourceName(cms, container);
111             CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(cms).setJspPageContext(context).setResourceName(
112                 resourcename).setKeepEmptyMacros(true);
113             // resolve the macro
114
content = resolver.resolveMacros(element);
115         } else if (xmlContent == null) {
116             // no XML content- no output
117
content = null;
118         } else {
119
120             // determine the locale to display
121
if (locale == null) {
122                 // no locale was set, use default from parent tag (usually "contentload")
123
locale = container.getXmlDocumentLocale();
124             }
125             // now get the content element value to display
126

127             if (xmlContent.hasValue(element, locale)) {
128                 try {
129                     // read the element from the content
130
content = xmlContent.getStringValue(cms, element, locale);
131                 } catch (Exception JavaDoc e) {
132                     LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_CONTENT_SHOW_1, element), e);
133                     content = null;
134                 }
135             } else {
136                 content = null;
137             }
138
139             // make sure that no null String is returned
140
if (content == null) {
141                 content = CmsMessages.formatUnknownKey(element);
142             }
143         }
144
145         return content;
146     }
147
148     /**
149      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
150      */

151     public int doEndTag() {
152
153         // need to release manually, JSP container may not call release as required (happens with Tomcat)
154
release();
155         return EVAL_PAGE;
156     }
157
158     /**
159      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
160      */

161     public int doStartTag() throws JspException JavaDoc {
162
163         // get a reference to the parent "content container" class
164
Tag JavaDoc ancestor = findAncestorWithClass(this, I_CmsXmlContentContainer.class);
165         if (ancestor == null) {
166             CmsMessageContainer errMsgContainer = Messages.get().container(Messages.ERR_PARENTLESS_TAG_1, "contentshow");
167             String JavaDoc msg = Messages.getLocalizedMessage(errMsgContainer, pageContext);
168             throw new JspTagException JavaDoc(msg);
169         }
170         I_CmsXmlContentContainer contentContainer = (I_CmsXmlContentContainer)ancestor;
171
172         // now get the content element value to display
173
String JavaDoc content = contentShowTagAction(contentContainer, pageContext, getElement(), m_locale);
174
175         try {
176             if (content != null) {
177                 pageContext.getOut().print(content);
178             }
179         } catch (IOException JavaDoc e) {
180             if (LOG.isErrorEnabled()) {
181                 LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_JSP_BEAN_0), e);
182             }
183             throw new JspException JavaDoc(e);
184         }
185
186         return SKIP_BODY;
187     }
188
189     /**
190      * Returns the name of the content node element to show.<p>
191      *
192      * @return the name of the content node element to show
193      */

194     public String JavaDoc getElement() {
195
196         return (m_element != null) ? m_element : "";
197     }
198
199     /**
200      * Returns the locale.<p>
201      *
202      * @return the locale
203      */

204     public String JavaDoc getLocale() {
205
206         return (m_locale != null) ? m_locale.toString() : "";
207     }
208
209     /**
210      * @see javax.servlet.jsp.tagext.Tag#release()
211      */

212     public void release() {
213
214         m_element = null;
215         m_locale = null;
216         super.release();
217     }
218
219     /**
220      * Sets the name of the content node element to show.<p>
221      *
222      * @param element the name of the content node element to show
223      */

224     public void setElement(String JavaDoc element) {
225
226         m_element = element;
227     }
228
229     /**
230      * Sets the locale.<p>
231      *
232      * @param locale the locale to set
233      */

234     public void setLocale(String JavaDoc locale) {
235
236         if (CmsStringUtil.isEmpty(locale)) {
237             m_locale = null;
238         } else {
239             m_locale = CmsLocaleManager.getLocale(locale);
240         }
241     }
242 }
Popular Tags