KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagContentLoop.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.18 $
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.i18n.CmsMessageContainer;
35 import org.opencms.xml.CmsXmlUtils;
36 import org.opencms.xml.I_CmsXmlDocument;
37
38 import java.util.List JavaDoc;
39 import java.util.Locale JavaDoc;
40
41 import javax.servlet.jsp.JspException JavaDoc;
42 import javax.servlet.jsp.JspTagException JavaDoc;
43 import javax.servlet.jsp.tagext.Tag JavaDoc;
44 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
45
46 /**
47  * Used to loop through the element values of an XML content item.<p>
48  *
49  * @author Alexander Kandzior
50  *
51  * @version $Revision: 1.18 $
52  *
53  * @since 6.0.0
54  */

55 public class CmsJspTagContentLoop extends TagSupport JavaDoc implements I_CmsXmlContentContainer {
56
57     /** Serial version UID required for safe serialization. */
58     private static final long serialVersionUID = 8832749526732064836L;
59
60     /** Reference to the parent 'contentload' tag. */
61     private I_CmsXmlContentContainer m_container;
62
63     /** Reference to the looped content element. */
64     private I_CmsXmlDocument m_content;
65
66     /** Name of the current element (including the index). */
67     private String JavaDoc m_currentElement;
68
69     /** Name of the content node element to show. */
70     private String JavaDoc m_element;
71
72     /** Indicates if this is the first content iteration loop. */
73     private boolean m_firstLoop;
74
75     /** Index of the content node element to show. */
76     private int m_index = -1;
77
78     /** Refenence to the currently selected locale. */
79     private Locale JavaDoc m_locale;
80
81     /**
82      * Empty constructor, required for JSP tags.<p>
83      */

84     public CmsJspTagContentLoop() {
85
86         super();
87     }
88
89     /**
90      * Constructor used when using <code>contentloop</code> from scriptlet code.<p>
91      *
92      * @param container the parent content container that provides the content element to loop
93      * @param element the element to loop in the content
94      */

95     public CmsJspTagContentLoop(I_CmsXmlContentContainer container, String JavaDoc element) {
96
97         m_element = element;
98         init(container);
99     }
100
101     /**
102      * @see javax.servlet.jsp.tagext.TagSupport#doAfterBody()
103      */

104     public int doAfterBody() {
105
106         if (hasMoreContent()) {
107             // one more element with the same name is available, loop again
108
return EVAL_BODY_AGAIN;
109         }
110         // no more elements with this name available, finish the loop
111
return SKIP_BODY;
112     }
113
114     /**
115      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
116      */

117     public int doEndTag() {
118
119         // need to release manually, JSP container may not call release as required (happens with Tomcat)
120
release();
121         return EVAL_PAGE;
122     }
123
124     /**
125      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
126      */

127     public int doStartTag() throws JspException JavaDoc {
128
129         // get a reference to the parent "content container" class
130
Tag JavaDoc ancestor = findAncestorWithClass(this, I_CmsXmlContentContainer.class);
131         if (ancestor == null) {
132             CmsMessageContainer errMsgContainer = Messages.get().container(Messages.ERR_PARENTLESS_TAG_1, "contentloop");
133             String JavaDoc msg = Messages.getLocalizedMessage(errMsgContainer, pageContext);
134             throw new JspTagException JavaDoc(msg);
135         }
136         I_CmsXmlContentContainer container = (I_CmsXmlContentContainer)ancestor;
137
138         // initialize the content
139
init(container);
140
141         if (hasMoreContent()) {
142             // selected element is available at last once in content
143
return EVAL_BODY_INCLUDE;
144         } else {
145             // no value available for the selected element name, so we skip the whole body
146
return SKIP_BODY;
147         }
148     }
149
150     /**
151      * @see org.opencms.jsp.I_CmsXmlContentContainer#getCollectorName()
152      */

153     public String JavaDoc getCollectorName() {
154
155         return m_container.getCollectorName();
156     }
157
158     /**
159      * @see org.opencms.jsp.I_CmsXmlContentContainer#getCollectorParam()
160      */

161     public String JavaDoc getCollectorParam() {
162
163         return m_container.getCollectorParam();
164     }
165
166     /**
167      * @see org.opencms.jsp.I_CmsXmlContentContainer#getCollectorResult()
168      */

169     public List JavaDoc getCollectorResult() {
170
171         return m_container.getCollectorResult();
172     }
173
174     /**
175      * Returns the name of the content node element to show.<p>
176      *
177      * @return the name of the content node element to show
178      */

179     public String JavaDoc getElement() {
180
181         return (m_element != null) ? m_element : "";
182     }
183
184     /**
185      * @see org.opencms.jsp.I_CmsXmlContentContainer#getResourceName()
186      */

187     public String JavaDoc getResourceName() {
188
189         return m_container.getResourceName();
190     }
191
192     /**
193      * @see org.opencms.jsp.I_CmsXmlContentContainer#getXmlDocument()
194      */

195     public I_CmsXmlDocument getXmlDocument() {
196
197         return m_content;
198     }
199
200     /**
201      * @see org.opencms.jsp.I_CmsXmlContentContainer#getXmlDocumentElement()
202      */

203     public String JavaDoc getXmlDocumentElement() {
204
205         return m_currentElement;
206     }
207
208     /**
209      * @see org.opencms.jsp.I_CmsXmlContentContainer#getXmlDocumentLocale()
210      */

211     public Locale JavaDoc getXmlDocumentLocale() {
212
213         return m_locale;
214     }
215
216     /**
217      * @see org.opencms.jsp.I_CmsXmlContentContainer#hasMoreContent()
218      */

219     public boolean hasMoreContent() {
220
221         if (m_firstLoop) {
222             m_firstLoop = false;
223         } else {
224             m_index++;
225         }
226         if (m_content.hasValue(m_element, m_locale, m_index)) {
227             m_currentElement = CmsXmlUtils.createXpath(m_element, m_index + 1);
228             // one more element with the same name is available, loop again
229
return true;
230         } else {
231             // no more elements with this name available, finish the loop
232
return false;
233         }
234     }
235
236     /**
237      * @see org.opencms.jsp.I_CmsXmlContentContainer#isPreloader()
238      */

239     public boolean isPreloader() {
240
241         return m_container.isPreloader();
242     }
243
244     /**
245      * @see javax.servlet.jsp.tagext.Tag#release()
246      */

247     public void release() {
248
249         m_element = null;
250         m_currentElement = null;
251         m_content = null;
252         m_locale = null;
253         m_container = null;
254         m_index = 0;
255         super.release();
256     }
257
258     /**
259      * Sets the name of the content node element to show.<p>
260      *
261      * @param element the name of the content node element to show
262      */

263     public void setElement(String JavaDoc element) {
264
265         m_element = element;
266     }
267
268     /**
269      * Initializes this content loop tag.<p>
270      *
271      * @param container the parent content container that provides the content element to loop
272      */

273     protected void init(I_CmsXmlContentContainer container) {
274
275         m_container = container;
276
277         // append to parent element name (required for nested schemas)
278
m_element = CmsXmlUtils.concatXpath(m_container.getXmlDocumentElement(), m_element);
279
280         // get loaded content from parent <contentload> tag
281
m_content = m_container.getXmlDocument();
282         m_locale = m_container.getXmlDocumentLocale();
283         m_index = 0;
284         m_currentElement = null;
285
286         // the next loop is the first loop
287
m_firstLoop = true;
288     }
289 }
Popular Tags