KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > IfEmpty


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.NodeData;
17 import info.magnolia.cms.util.Resource;
18
19 import javax.jcr.RepositoryException;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.jstl.core.ConditionalTagSupport;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29 /**
30  * @author Marcel Salathe
31  * @author Fabrizio Giustina
32  * @version $Revision $ ($Author $)
33  */

34 public class IfEmpty extends ConditionalTagSupport {
35
36     /**
37      * Stable serialVersionUID.
38      */

39     private static final long serialVersionUID = 222L;
40
41     /**
42      * Logger.
43      */

44     private static Logger log = LoggerFactory.getLogger(IfEmpty.class);
45
46     private String JavaDoc nodeDataName = StringUtils.EMPTY;
47
48     private String JavaDoc contentNodeName = StringUtils.EMPTY;
49
50     private String JavaDoc contentNodeCollectionName = StringUtils.EMPTY;
51
52     private transient Content contentNodeCollection;
53
54     private transient Content contentNode;
55
56     private transient NodeData nodeData;
57
58     private boolean actpage;
59
60     /**
61      * @deprecated
62      */

63     public void setAtomName(String JavaDoc name) {
64         this.setNodeDataName(name);
65     }
66
67     /**
68      * @param name , antom name to evaluate
69      */

70     public void setNodeDataName(String JavaDoc name) {
71         this.nodeDataName = name;
72     }
73
74     /**
75      * @deprecated
76      */

77     public void setContainerName(String JavaDoc name) {
78         this.setContentNodeName(name);
79     }
80
81     /**
82      * @param contentNodeName , contentNodeName to check
83      */

84     public void setContentNodeName(String JavaDoc contentNodeName) {
85         this.contentNodeName = contentNodeName;
86     }
87
88     /**
89      * @param name , contentNode collection name
90      * @deprecated
91      */

92     public void setContainerListName(String JavaDoc name) {
93         this.setContentNodeCollectionName(name);
94     }
95
96     /**
97      * @param name contentNodeCollectionName to check
98      */

99     public void setContentNodeCollectionName(String JavaDoc name) {
100         this.contentNodeCollectionName = name;
101     }
102
103     /**
104      * Set the actpage.
105      * @param set
106      */

107     public void setActpage(boolean set) {
108         this.actpage = set;
109     }
110
111     /**
112      * @see javax.servlet.jsp.jstl.core.ConditionalTagSupport#condition()
113      */

114     protected boolean condition() {
115         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
116         // in the case where a contentNodeCollectionName is provided
117
if (StringUtils.isNotEmpty(this.contentNodeCollectionName)) {
118             try {
119                 this.contentNodeCollection = Resource.getCurrentActivePage(req).getContent(
120                     this.contentNodeCollectionName);
121             }
122             catch (RepositoryException e) {
123                 if (log.isDebugEnabled()) {
124                     log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
125
}
126             }
127             if (this.contentNodeCollection == null) {
128                 return true;
129             }
130             if (!this.contentNodeCollection.hasChildren()) {
131                 return true;
132             }
133             return false;
134         }
135         // if only contentNodeName is provided, it checks if this contentNode exists
136
if (StringUtils.isNotEmpty(this.contentNodeName) && StringUtils.isEmpty(this.nodeDataName)) {
137             try {
138                 this.contentNode = Resource.getCurrentActivePage(req).getContent(this.contentNodeName);
139             }
140             catch (RepositoryException re) {
141                 log.error(re.getMessage());
142             }
143             if (this.contentNode == null) {
144                 // contentNode doesn't exist, evaluate body
145
return true;
146             }
147         }
148         // if both contentNodeName and nodeDataName are set, it checks if that nodeData of that contentNode exitsts
149
// and is not empty
150
else if (StringUtils.isNotEmpty(this.contentNodeName) && StringUtils.isNotEmpty(this.nodeDataName)) {
151             try {
152                 this.contentNode = Resource.getCurrentActivePage(req).getContent(this.contentNodeName);
153             }
154             catch (RepositoryException re) {
155                 if (log.isDebugEnabled()) {
156                     log.debug("Repository exception while reading " + this.contentNodeName + ": " + re.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
157
}
158             }
159             if (this.contentNode == null) {
160                 return true;
161             }
162             if (this.contentNode != null) {
163
164                 this.nodeData = this.contentNode.getNodeData(this.nodeDataName);
165
166                 if ((this.nodeData == null)
167                     || !this.nodeData.isExist()
168                     || StringUtils.isEmpty(this.nodeData.getString())) {
169                     return true;
170                 }
171             }
172         }
173         // if only nodeDataName is provided, it checks if that nodeData of the current contentNode exists and is not
174
// empty
175
else if (StringUtils.isEmpty(this.contentNodeName) && StringUtils.isNotEmpty(this.nodeDataName)) {
176             if (this.actpage) {
177                 this.contentNode = Resource.getCurrentActivePage((HttpServletRequest JavaDoc) pageContext.getRequest());
178             }
179             else {
180                 this.contentNode = Resource.getLocalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
181                 if (this.contentNode == null) {
182                     this.contentNode = Resource.getGlobalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
183                 }
184             }
185             if (this.contentNode == null) {
186                 return true;
187             }
188             if (this.contentNode != null) {
189
190                 this.nodeData = this.contentNode.getNodeData(this.nodeDataName);
191
192                 if ((this.nodeData == null)
193                     || !this.nodeData.isExist()
194                     || StringUtils.isEmpty(this.nodeData.getString())) {
195                     return true;
196                 }
197             }
198         }
199         // if both contentNodeName and nodeDataName are not provided, it checks if the current contentNode exists
200
else {
201             this.contentNode = Resource.getLocalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
202             if (this.contentNode == null) {
203                 this.contentNode = Resource.getGlobalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
204             }
205             if (this.contentNode == null) {
206                 return true;
207             }
208         }
209         return false;
210     }
211
212     /**
213      * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
214      */

215     public int doEndTag() throws JspException JavaDoc {
216         this.contentNodeCollection = null;
217         this.contentNode = null;
218         this.nodeData = null;
219         return super.doEndTag();
220     }
221
222     /**
223      * @see javax.servlet.jsp.tagext.Tag#release()
224      */

225     public void release() {
226         this.nodeDataName = StringUtils.EMPTY;
227         this.contentNodeName = StringUtils.EMPTY;
228         this.contentNodeCollectionName = StringUtils.EMPTY;
229         this.actpage = false;
230     }
231
232 }
233
Popular Tags