KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagContentInfo.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.17 $
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 02111R-1307 USA
30  */

31
32 package org.opencms.jsp;
33
34 import org.opencms.i18n.CmsMessageContainer;
35 import org.opencms.main.CmsLog;
36 import org.opencms.util.CmsMacroResolver;
37 import org.opencms.util.CmsStringUtil;
38 import org.opencms.util.I_CmsMacroResolver;
39
40 import java.io.IOException JavaDoc;
41 import java.util.Arrays JavaDoc;
42 import java.util.Collections JavaDoc;
43 import java.util.List JavaDoc;
44
45 import javax.servlet.jsp.JspException JavaDoc;
46 import javax.servlet.jsp.JspTagException JavaDoc;
47 import javax.servlet.jsp.PageContext JavaDoc;
48 import javax.servlet.jsp.tagext.Tag JavaDoc;
49 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
50
51 import org.apache.commons.logging.Log;
52
53 /**
54  * Used to access and display XML content item information from the VFS.<p>
55  *
56  * @author Thomas Weckert
57  *
58  * @version $Revision: 1.17 $
59  *
60  * @since 6.0.0
61  */

62 public class CmsJspTagContentInfo extends TagSupport JavaDoc implements I_CmsMacroResolver {
63
64     /** The keys of the supported content info values. */
65     private static final String JavaDoc[] KEYS = {
66         "resultSize",
67         "resultIndex",
68         "pageCount",
69         "pageIndex",
70         "pageSize",
71         "pageNavStartIndex",
72         "pageNavEndIndex",
73         "pageNavLength"};
74
75     /** The keys of the supported content info values as a list. */
76     private static final List JavaDoc KEYS_LIST = Collections.unmodifiableList(Arrays.asList(KEYS));
77
78     /** The log object for this class. */
79     private static final Log LOG = CmsLog.getLog(CmsJspTagContentInfo.class);
80
81     /** The scopes supported by the page context. */
82     private static final String JavaDoc[] SCOPES = {"application", "session", "request", "page"};
83
84     /** The scopes supported by the page context as a list. */
85     private static final List JavaDoc SCOPES_LIST = Collections.unmodifiableList(Arrays.asList(SCOPES));
86
87     /** Serial version UID required for safe serialization. */
88     private static final long serialVersionUID = -1955531050687258685L;
89
90     /** The scope under which the content info is saved in the page context. */
91     private String JavaDoc m_scope;
92
93     /** The name of the content info's value that should be printed out. */
94     private String JavaDoc m_value;
95
96     /** The name of the variable under which the content info bean should be saved in the page context. */
97     private String JavaDoc m_variable;
98
99     /**
100      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
101      */

102     public int doEndTag() {
103
104         // need to release manually, JSP container may not call release as required (happens with Tomcat)
105
release();
106         return EVAL_PAGE;
107     }
108
109     /**
110      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
111      */

112     public int doStartTag() throws JspException JavaDoc {
113
114         // get a reference to the parent "content container" class
115
Tag JavaDoc ancestor = findAncestorWithClass(this, I_CmsXmlContentContainer.class);
116         if (ancestor == null) {
117             // build a container
118
CmsMessageContainer container = Messages.get().container(Messages.ERR_PARENTLESS_TAG_1, "contentinfo");
119             String JavaDoc msg = Messages.getLocalizedMessage(container, pageContext);
120             throw new JspTagException JavaDoc(msg);
121         }
122
123         I_CmsXmlContentContainer contentContainer = (I_CmsXmlContentContainer)ancestor;
124
125         String JavaDoc tagContent = "";
126
127         if (CmsStringUtil.isNotEmpty(m_variable)) {
128             int scope = getScopeAsInt(m_scope);
129             storeContentInfoBean((CmsJspTagContentLoad)contentContainer, m_variable, scope);
130         }
131
132         if (CmsStringUtil.isNotEmpty(m_value)) {
133             // value is provided - resolve macros
134
tagContent = resolveMacros(m_value);
135         }
136
137         try {
138             pageContext.getOut().print(tagContent);
139         } catch (IOException JavaDoc e) {
140             CmsMessageContainer message = Messages.get().container(Messages.ERR_PROCESS_TAG_1, "contentinfo");
141             LOG.error(message.key(), e);
142             throw new JspException JavaDoc(message.key(pageContext.getRequest().getLocale()));
143         }
144
145         return SKIP_BODY;
146     }
147
148     /**
149      * @see org.opencms.util.I_CmsMacroResolver#getMacroValue(java.lang.String)
150      */

151     public String JavaDoc getMacroValue(String JavaDoc macro) {
152
153         int dotIndex = macro.indexOf('.');
154         String JavaDoc beanName = null;
155
156         if ((dotIndex > 1) && (dotIndex < (macro.length() - 1))) {
157             beanName = macro.substring(0, dotIndex);
158         } else {
159             return null;
160         }
161
162         String JavaDoc variableName = macro.substring(dotIndex + 1, macro.length());
163
164         if (CmsStringUtil.isEmpty(beanName) || CmsStringUtil.isEmpty(variableName)) {
165             return null;
166         }
167
168         // extract bean from page context
169
CmsContentInfoBean bean;
170         int scope = pageContext.getAttributesScope(beanName);
171         try {
172             bean = (CmsContentInfoBean)pageContext.getAttribute(beanName, scope);
173         } catch (ClassCastException JavaDoc e) {
174             // attribute exists but is not of required class
175
return null;
176         }
177
178         if (bean == null) {
179             return null;
180         }
181
182         switch (KEYS_LIST.indexOf(variableName)) {
183             case 0:
184                 // "resultSize"
185
return Integer.toString(bean.getResultSize());
186             case 1:
187                 // "resultIndex"
188
return Integer.toString(bean.getResultIndex());
189             case 2:
190                 // "pageCount"
191
return Integer.toString(bean.getPageCount());
192             case 3:
193                 // "pageIndex"
194
return Integer.toString(bean.getPageIndex());
195             case 4:
196                 // "pageSize"
197
return Integer.toString(bean.getPageSize());
198             case 5:
199                 // pageNavStartIndex
200
return Integer.toString(bean.getPageNavStartIndex());
201             case 6:
202                 // pageNavEndIndex
203
return Integer.toString(bean.getPageNavEndIndex());
204             case 7:
205                 // pageNavLength
206
return Integer.toString(bean.getPageNavLength());
207             default:
208                 // unknown value
209
return null;
210         }
211     }
212
213     /**
214      * Returns the scope under which the content info is saved in the page context.<p>
215      *
216      * @return the scope under which the content info is saved in the page context
217      */

218     public String JavaDoc getScope() {
219
220         return m_scope;
221     }
222
223     /**
224      * Returns the name of the content info's value that should be printed out.<p>
225      *
226      * @return the name of the content info's value that should be printed out
227      */

228     public String JavaDoc getValue() {
229
230         return m_value;
231     }
232
233     /**
234      * Returns the name of the variable under which the content info bean should be saved in the page context.<p>
235      *
236      * @return the name of the variable under which the content info bean should be saved in the page context
237      */

238     public String JavaDoc getVar() {
239
240         return m_variable;
241     }
242
243     /**
244      * @see org.opencms.util.I_CmsMacroResolver#isKeepEmptyMacros()
245      */

246     public boolean isKeepEmptyMacros() {
247
248         return true;
249     }
250
251     /**
252      * @see javax.servlet.jsp.tagext.Tag#release()
253      */

254     public void release() {
255
256         m_scope = null;
257         m_value = null;
258         m_variable = null;
259         super.release();
260     }
261
262     /**
263      * @see org.opencms.util.I_CmsMacroResolver#resolveMacros(java.lang.String)
264      */

265     public String JavaDoc resolveMacros(String JavaDoc input) {
266
267         return CmsMacroResolver.resolveMacros(input, this);
268     }
269
270     /**
271      * Sets the scope under which the content info is saved in the page context.<p>
272      *
273      * @param scope the scope under which the content info is saved in the page context
274      */

275     public void setScope(String JavaDoc scope) {
276
277         m_scope = scope;
278     }
279
280     /**
281      * Sets the name of the content info's value that should be printed out.<p>
282      *
283      * @param value the name of the content info's value that should be printed out
284      */

285     public void setValue(String JavaDoc value) {
286
287         m_value = value;
288     }
289
290     /**
291      * Sets the name of the variable under which the content info bean should be saved in the page context.<p>
292      *
293      * @param var the name of the variable under which the content info bean should be saved in the page context
294      */

295     public void setVar(String JavaDoc var) {
296
297         m_variable = var;
298     }
299
300     /**
301      * Returns the int value of the specified scope string.<p>
302      *
303      * The default value is {@link PageContext#PAGE_SCOPE}.<p>
304      *
305      * @param scope the string name of the desired scope, e.g. "application", "request"
306      * @return the int value of the specified scope string
307      */

308     protected int getScopeAsInt(String JavaDoc scope) {
309
310         int scopeValue;
311         switch (SCOPES_LIST.indexOf(scope)) {
312             case 0:
313                 // application
314
scopeValue = PageContext.APPLICATION_SCOPE;
315                 break;
316             case 1:
317                 // session
318
scopeValue = PageContext.SESSION_SCOPE;
319                 break;
320             case 2:
321                 // request
322
scopeValue = PageContext.REQUEST_SCOPE;
323                 break;
324             default:
325                 // page
326
scopeValue = PageContext.PAGE_SCOPE;
327                 break;
328         }
329
330         return scopeValue;
331
332     }
333
334     /**
335      * Stores the container's content info bean under the specified scope in the page context.<p>
336      *
337      * @param container the parent container
338      * @param variable the variable under which the content info bean is saved
339      * @param scope the scope under which the content info bean is saved
340      */

341     protected void storeContentInfoBean(CmsJspTagContentLoad container, String JavaDoc variable, int scope) {
342
343         CmsContentInfoBean contentInfoBean = container.getContentInfoBean();
344
345         contentInfoBean.setPageSize(container.getContentInfoBean().getPageSize());
346         contentInfoBean.setPageIndex(container.getContentInfoBean().getPageIndex());
347         contentInfoBean.setResultSize(container.getContentInfoBean().getResultSize());
348
349         pageContext.setAttribute(variable, contentInfoBean, scope);
350     }
351 }
Popular Tags