KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagContentCheck.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.CmsLocaleManager;
35 import org.opencms.i18n.CmsMessageContainer;
36 import org.opencms.util.CmsStringUtil;
37 import org.opencms.xml.CmsXmlUtils;
38 import org.opencms.xml.I_CmsXmlDocument;
39
40 import java.util.Locale JavaDoc;
41
42 import javax.servlet.jsp.JspException JavaDoc;
43 import javax.servlet.jsp.JspTagException JavaDoc;
44 import javax.servlet.jsp.tagext.Tag JavaDoc;
45 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
46
47 /**
48  * Used to check the availablity of an XML content item for conditional display.<p>
49  *
50  * @author Alexander Kandzior
51  *
52  * @version $Revision: 1.18 $
53  *
54  * @since 6.0.0
55  */

56 public class CmsJspTagContentCheck extends TagSupport JavaDoc {
57
58     /** Serial version UID required for safe serialization. */
59     private static final long serialVersionUID = -5996702196453747182L;
60
61     /** Condition for element check. */
62     private boolean m_checkall;
63
64     /** Condition for negative element check. */
65     private boolean m_checknone;
66
67     /** The list of element to check. */
68     private String JavaDoc m_elementList;
69
70     /** The locale to check for. */
71     private Locale JavaDoc m_locale;
72
73     /**
74      * Internal action method to check the elements from the provided XML content item.<p>
75      *
76      * @param elementList the list of elements to check for
77      * @param prefix the Xpath prefix to append the elements to (in case of nested schemas)
78      * @param checkall flag to indicate that all elements should be checked
79      * @param checknone flag to indicate that the check is done for nonexisting elements
80      * @param content the XML content document to check the elements from
81      * @param locale the locale to check the element for
82      *
83      * @return true if the test succeeds, false if the test fails
84      */

85     public static boolean contentCheckTagAction(
86         String JavaDoc elementList,
87         String JavaDoc prefix,
88         boolean checkall,
89         boolean checknone,
90         I_CmsXmlDocument content,
91         Locale JavaDoc locale) {
92
93         boolean found = false;
94         String JavaDoc[] elements = CmsStringUtil.splitAsArray(elementList, ',');
95         for (int i = (elements.length - 1); i >= 0; i--) {
96
97             String JavaDoc element = CmsXmlUtils.concatXpath(prefix, elements[i].trim());
98             found = found || content.hasValue(element, locale);
99
100             if (found && checknone) {
101                 // found an item that must not exist
102
return false;
103             }
104             if (found && !checkall && !checknone) {
105                 // we need to find only one item
106
return true;
107             }
108         }
109
110         if (!found && checknone) {
111             // no item found as expected
112
return true;
113         }
114
115         return found;
116     }
117
118     /**
119      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
120      */

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

131     public int doStartTag() throws JspException JavaDoc {
132
133         // get a reference to the parent "content load" class
134
Tag JavaDoc ancestor = findAncestorWithClass(this, I_CmsXmlContentContainer.class);
135         if (ancestor == null) {
136             CmsMessageContainer errMsgContainer = Messages.get().container(Messages.ERR_TAG_CONTENTCHECK_WRONG_PARENT_0);
137             String JavaDoc msg = Messages.getLocalizedMessage(errMsgContainer, pageContext);
138             throw new JspTagException JavaDoc(msg);
139         }
140         I_CmsXmlContentContainer contentContainer = (I_CmsXmlContentContainer)ancestor;
141         String JavaDoc prefix = contentContainer.getXmlDocumentElement();
142
143         // get loaded content from parent <contentload> tag
144
I_CmsXmlDocument content = contentContainer.getXmlDocument();
145
146         if (m_locale == null) {
147             m_locale = contentContainer.getXmlDocumentLocale();
148         }
149
150         // calculate the result
151
boolean result = contentCheckTagAction(m_elementList, prefix, m_checkall, m_checknone, content, m_locale);
152
153         if (result) {
154             return EVAL_BODY_INCLUDE;
155         } else {
156             return SKIP_BODY;
157         }
158     }
159
160     /**
161      * Returns the list of elements to check.<p>
162      *
163      * @return the list of elements to check
164      */

165     public String JavaDoc getIfexists() {
166
167         return m_elementList != null ? m_elementList : "";
168     }
169
170     /**
171      * Returns the list of elements to check.<p>
172      *
173      * @return the list of elements to check
174      */

175     public String JavaDoc getIfexistsall() {
176
177         return m_elementList != null ? m_elementList : "";
178     }
179
180     /**
181      * Returns the list of elements to check.<p>
182      *
183      * @return the list of elements to check
184      */

185     public String JavaDoc getIfexistsnone() {
186
187         return m_elementList != null ? m_elementList : "";
188     }
189
190     /**
191      * Returns the list of elements to check.<p>
192      *
193      * @return the list of elements to check
194      */

195     public String JavaDoc getIfexistsone() {
196
197         return m_elementList != null ? m_elementList : "";
198     }
199
200     /**
201      * Returns the locale used for checking.<p>
202      *
203      * @return the locale used for checking
204      */

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

213     public void release() {
214
215         m_checkall = false;
216         m_checknone = false;
217         m_elementList = null;
218         m_locale = null;
219         super.release();
220     }
221
222     /**
223      * Sets the list of elements to check for.<p>
224      *
225      * @param elementList the list of elements to check for
226      */

227     public void setIfexists(String JavaDoc elementList) {
228
229         if (elementList != null) {
230             m_elementList = elementList;
231             m_checkall = false;
232             m_checknone = false;
233         }
234     }
235
236     /**
237      * Sets the list of elements to check for.<p>
238      *
239      * @param elementList the list of elements to check for
240      */

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

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

269     public void setIfexistsone(String JavaDoc elementList) {
270
271         if (elementList != null) {
272             m_elementList = elementList;
273             m_checkall = false;
274             m_checknone = false;
275         }
276     }
277
278     /**
279      * Sets the locale used for checking.<p>
280      *
281      * @param locale the locale to set
282      */

283     public void setLocale(String JavaDoc locale) {
284
285         if (CmsStringUtil.isEmpty(locale)) {
286             m_locale = null;
287         } else {
288             m_locale = CmsLocaleManager.getLocale(locale);
289         }
290     }
291 }
Popular Tags