KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagTemplate.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.37 $
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.loader.I_CmsResourceLoader;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.CmsLog;
38 import org.opencms.main.OpenCms;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.xml.I_CmsXmlDocument;
41 import org.opencms.xml.page.CmsXmlPageFactory;
42
43 import java.util.List JavaDoc;
44 import java.util.Locale JavaDoc;
45
46 import javax.servlet.ServletRequest JavaDoc;
47 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
48
49 import org.apache.commons.logging.Log;
50
51 /**
52  * Used to select various template elements form a JSP template that
53  * is included in another file.<p>
54  *
55  * @version $Revision: 1.37 $
56  *
57  * @since 6.0.0
58  */

59 public class CmsJspTagTemplate extends BodyTagSupport JavaDoc {
60
61     /** Serial version UID required for safe serialization. */
62     private static final long serialVersionUID = -3773247710025810438L;
63
64     /** The log object for this class. */
65     private static final Log LOG = CmsLog.getLog(CmsJspTagTemplate.class);
66
67     /** Condition for element check. */
68     private boolean m_checkall;
69
70     /** Condition for negative element check. */
71     private boolean m_checknone;
72
73     /** Name of element. */
74     private String JavaDoc m_element;
75
76     /** List of elements for element check. */
77     private String JavaDoc m_elementlist;
78
79     /**
80      * Internal action method.<p>
81      *
82      * @param element the selected element
83      * @param elementlist list the list of elements to check
84      * @param checkall flag to indicate that all elements should be checked
85      * @param checknone flag to indicate that the check is done for nonexisting elements
86      * @param req the current request
87      * @return boolean <code>true</code> if this element should be inclued, <code>false</code>
88      * otherwise
89      */

90     public static boolean templateTagAction(
91         String JavaDoc element,
92         String JavaDoc elementlist,
93         boolean checkall,
94         boolean checknone,
95         ServletRequest JavaDoc req) {
96
97         if (elementlist != null) {
98
99             CmsFlexController controller = CmsFlexController.getController(req);
100             String JavaDoc filename = controller.getCmsObject().getRequestContext().getUri();
101
102             I_CmsXmlDocument content = null;
103             try {
104                 content = CmsXmlPageFactory.unmarshal(controller.getCmsObject(), filename, req);
105             } catch (CmsException e) {
106                 LOG.error(Messages.get().getBundle().key(Messages.ERR_XML_DOCUMENT_UNMARSHAL_1, filename), e);
107             }
108
109             if (content != null) {
110                 String JavaDoc absolutePath = controller.getCmsObject().getSitePath(content.getFile());
111                 // check the elements in the elementlist, if the check fails don't render the element
112
String JavaDoc[] elements = CmsStringUtil.splitAsArray(elementlist, ',');
113                 boolean found = false;
114                 for (int i = 0; i < elements.length; i++) {
115                     String JavaDoc el = elements[i].trim();
116                     List JavaDoc locales = content.getLocales(el);
117                     Locale JavaDoc locale = null;
118                     if ((locales != null) && (locales.size() != 0)) {
119                         locale = OpenCms.getLocaleManager().getBestMatchingLocale(
120                             controller.getCmsObject().getRequestContext().getLocale(),
121                             OpenCms.getLocaleManager().getDefaultLocales(controller.getCmsObject(), absolutePath),
122                             locales);
123                     }
124                     if ((locale != null) && content.hasValue(el, locale) && content.isEnabled(el, locale)) {
125
126                         found = true;
127                         if (!checkall) {
128                             // found at least an element that is available
129
break;
130                         }
131                     } else {
132                         if (checkall) {
133                             // found at least an element that is not available
134
return false;
135                         }
136                     }
137                 }
138
139                 if (!found && !checknone) {
140                     // no element found while checking for existing elements
141
return false;
142                 } else if (found && checknone) {
143                     // element found while checking for nonexisting elements
144
return false;
145                 }
146             }
147         }
148
149         // otherwise, check if an element was defined and if its equal to the desired element
150
String JavaDoc param = req.getParameter(I_CmsResourceLoader.PARAMETER_ELEMENT);
151         return ((element == null) || (param == null) || (param.equals(element)));
152     }
153
154     /**
155      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
156      */

157     public int doStartTag() {
158
159         if (templateTagAction(m_element, m_elementlist, m_checkall, m_checknone, pageContext.getRequest())) {
160             return EVAL_BODY_INCLUDE;
161         } else {
162             return SKIP_BODY;
163         }
164     }
165
166     /**
167      * Returns the selected element.<p>
168      *
169      * @return the selected element
170      */

171     public String JavaDoc getElement() {
172
173         return m_element != null ? m_element : "";
174     }
175
176     /**
177      * Returns the list of elements to check.<p>
178      *
179      * @return the list of elements
180      */

181     public String JavaDoc getIfexists() {
182
183         return m_elementlist != null ? m_elementlist : "";
184     }
185
186     /**
187      * Returns the list of elements to check.<p>
188      *
189      * @return the list of elements
190      */

191     public String JavaDoc getIfexistsall() {
192
193         return m_elementlist != null ? m_elementlist : "";
194     }
195
196     /**
197      * Returns the list of elements to check.<p>
198      *
199      * @return the list of elements
200      */

201     public String JavaDoc getIfexistsnone() {
202
203         return m_elementlist != null ? m_elementlist : "";
204     }
205
206     /**
207      * Returns the list of elements to check.<p>
208      *
209      * @return the list of elements
210      */

211     public String JavaDoc getIfexistsone() {
212
213         return m_elementlist != null ? m_elementlist : "";
214     }
215
216     /**
217      * @see javax.servlet.jsp.tagext.Tag#release()
218      */

219     public void release() {
220
221         super.release();
222         m_element = null;
223     }
224
225     /**
226      * Sets the element target.<p>
227      *
228      * @param element the target to set
229      */

230     public void setElement(String JavaDoc element) {
231
232         if (element != null) {
233             m_element = element.toLowerCase();
234         }
235     }
236
237     /**
238      * Sets the list of elements to check.<p>
239      *
240      * @param elements the list of elements
241      */

242     public void setIfexists(String JavaDoc elements) {
243
244         if (elements != null) {
245             m_elementlist = elements;
246             m_checkall = false;
247             m_checknone = false;
248         }
249     }
250
251     /**
252      * Sets the list of elements to check.<p>
253      *
254      * @param elements the list of elements
255      */

256     public void setIfexistsall(String JavaDoc elements) {
257
258         if (elements != null) {
259             m_elementlist = elements;
260             m_checkall = true;
261             m_checknone = false;
262         }
263     }
264
265     /**
266      * Sets the list of elements to check.<p>
267      *
268      * @param elements the list of elements
269      */

270     public void setIfexistsnone(String JavaDoc elements) {
271
272         if (elements != null) {
273             m_elementlist = elements;
274             m_checkall = false;
275             m_checknone = true;
276         }
277     }
278
279     /**
280      * Sets the list of elements to check.<p>
281      *
282      * @param elements the list of elements
283      */

284     public void setIfexistsone(String JavaDoc elements) {
285
286         if (elements != null) {
287             m_elementlist = elements;
288             m_checkall = false;
289             m_checknone = false;
290         }
291     }
292 }
Popular Tags