KickJava   Java API By Example, From Geeks To Geeks.

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


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-2005 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.util.Resource;
17
18 import javax.jcr.RepositoryException;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.jstl.core.ConditionalTagSupport;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.log4j.Logger;
25
26
27 /**
28  * @author Michael Aemisegger
29  * @version $Revision $ ($Author $)
30  */

31 public class IfExisting extends ConditionalTagSupport {
32
33     /**
34      * Stable serialVersionUID.
35      */

36     private static final long serialVersionUID = 222L;
37
38     /**
39      * Logger.
40      */

41     private static Logger log = Logger.getLogger(IfExisting.class);
42
43     private String JavaDoc nodeDataName = StringUtils.EMPTY;
44
45     private String JavaDoc contentNodeName = StringUtils.EMPTY;
46
47     private String JavaDoc contentNodeCollectionName = StringUtils.EMPTY;
48
49     private transient Content contentNode;
50
51     private boolean actpage;
52
53     /**
54      * @param name , antom name to evaluate
55      */

56     public void setNodeDataName(String JavaDoc name) {
57         this.nodeDataName = name;
58     }
59
60     /**
61      * @param contentNodeName , contentNodeName to check
62      */

63     public void setContentNodeName(String JavaDoc contentNodeName) {
64         this.contentNodeName = contentNodeName;
65     }
66
67     /**
68      * @param name contentNodeCollectionName to check
69      */

70     public void setContentNodeCollectionName(String JavaDoc name) {
71         this.contentNodeCollectionName = name;
72     }
73
74     /**
75      * Set the actpage.
76      * @param set
77      */

78     public void setActpage(boolean set) {
79         this.actpage = set;
80     }
81
82     /**
83      * @see javax.servlet.jsp.jstl.core.ConditionalTagSupport#condition()
84      */

85     protected boolean condition() {
86         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
87         // in the case where a contentNodeCollectionName is provided
88
if (StringUtils.isNotEmpty(this.contentNodeCollectionName)) {
89             try {
90                 if (Resource.getCurrentActivePage(req).hasContent(this.contentNodeCollectionName)) {
91                     return true;
92                 }
93             }
94             catch (RepositoryException re) {
95                 return false;
96             }
97             return false;
98         }
99         // if only contentNodeName is provided, it checks if this contentNode exists
100
if (StringUtils.isNotEmpty(this.contentNodeName) && StringUtils.isEmpty(this.nodeDataName)) {
101             try {
102                 if (Resource.getCurrentActivePage(req).hasContent(this.contentNodeName)) {
103                     return true;
104                 }
105             }
106             catch (RepositoryException re) {
107                 return false;
108             }
109             return false;
110         }
111         // if both contentNodeName and nodeDataName are set, it checks if that nodeData of that contentNode exitsts
112
else if (StringUtils.isNotEmpty(this.contentNodeName) && StringUtils.isNotEmpty(this.nodeDataName)) {
113             try {
114                 this.contentNode = Resource.getCurrentActivePage(req).getContent(this.contentNodeName);
115             }
116             catch (RepositoryException re) {
117                 log.debug("Repository exception while reading " + this.contentNodeName + ": " + re.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
118
}
119             if (this.contentNode == null) {
120                 return false;
121             }
122             if (this.contentNode != null) {
123
124                 try {
125                     if (this.contentNode.hasNodeData(this.nodeDataName)) {
126                         return true;
127                     }
128                 }
129                 catch (RepositoryException e) {
130                     return false;
131                 }
132             }
133         }
134         // if only nodeDataName is provided, it checks if that nodeData of the current contentNode exists
135
else if (StringUtils.isEmpty(this.contentNodeName) && StringUtils.isNotEmpty(this.nodeDataName)) {
136             if (this.actpage) {
137                 this.contentNode = Resource.getCurrentActivePage((HttpServletRequest JavaDoc) pageContext.getRequest());
138             }
139             else {
140                 this.contentNode = Resource.getLocalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
141                 if (this.contentNode == null) {
142                     this.contentNode = Resource.getGlobalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
143                 }
144             }
145             if (this.contentNode == null) {
146                 return false;
147             }
148             if (this.contentNode != null) {
149
150                 try {
151                     if (this.contentNode.hasNodeData(this.nodeDataName)) {
152                         return true;
153                     }
154                 }
155                 catch (RepositoryException e) {
156                     return false;
157                 }
158             }
159         }
160         // if both contentNodeName and nodeDataName are not provided, it checks if the current contentNode exists
161
else {
162             this.contentNode = Resource.getLocalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
163             if (this.contentNode == null) {
164                 this.contentNode = Resource.getGlobalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
165             }
166             if (this.contentNode == null) {
167                 return false;
168             }
169         }
170         return false;
171     }
172
173     /**
174      * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
175      */

176     public int doEndTag() throws JspException JavaDoc {
177         this.contentNode = null;
178         return super.doEndTag();
179     }
180
181     /**
182      * @see javax.servlet.jsp.tagext.Tag#release()
183      */

184     public void release() {
185         this.nodeDataName = StringUtils.EMPTY;
186         this.contentNodeName = StringUtils.EMPTY;
187         this.contentNodeCollectionName = StringUtils.EMPTY;
188         this.actpage = false;
189     }
190
191 }
192
Popular Tags