KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.NodeData;
17 import info.magnolia.cms.gui.misc.FileProperties;
18 import info.magnolia.cms.util.Resource;
19
20 import java.util.Collection JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.jcr.PropertyType;
25 import javax.jcr.RepositoryException;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.log4j.Logger;
32
33
34 /**
35  * Exposes a content node to the pagecontext as a Map of nodeData, in order to access the exposed object using jstl.
36  * Since JSTL doesn't allow calling a method like <code>Content.getNodeData(String)</code> the <code>Content</code>
37  * is wrapped into a <code>NodeMapWrapper</code> which exposes NodeData using a map interface. This tag can be useful
38  * in similar situations:
39  *
40  * <pre>
41  * &lt;cms:setNode var="currentNode"/>
42  * &lt;c:if test="${!empty currentNode.title}">
43  * &lt;c:out value="${currentNode.title}"/>
44  * &lt;/c:if>
45  * </pre>
46  *
47  * @author Fabrizio Giustina
48  * @version $Revision: 1261 $ ($Author: fgiust $)
49  */

50 public class SetNode extends TagSupport JavaDoc {
51
52     /**
53      * Logger.
54      */

55     protected static final Logger log = Logger.getLogger(SetNode.class);
56
57     /**
58      * Stable serialVersionUID.
59      */

60     private static final long serialVersionUID = 222L;
61
62     /**
63      * Tag attribute. Name of the content node which will be saved in pagecontext.
64      */

65     private String JavaDoc contentNodeName;
66
67     /**
68      * Tag attribute. Name of the collection holding the content node.
69      */

70     private String JavaDoc contentNodeCollectionName;
71
72     /**
73      * Tag attribute. Variable name: the content node will be added to the pagecontext with this name.
74      */

75     private String JavaDoc var;
76
77     /**
78      * Tag attribute. Scope for the declared variable. Can be <code>page</code>, <code>request</code>,
79      * <code>session</code> or <code>application</code><code></code>.
80      */

81     private int scope = PageContext.PAGE_SCOPE;
82
83     /**
84      * set the content node name name, e.g. "01"
85      * @param name content node name
86      */

87     public void setContentNodeName(String JavaDoc name) {
88         this.contentNodeName = name;
89     }
90
91     /**
92      * set the name of the collection holding the content node, e.g. "mainColumnParagraphs"
93      * @param name content node collection name
94      */

95     public void setContentNodeCollectionName(String JavaDoc name) {
96         this.contentNodeCollectionName = name;
97     }
98
99     /**
100      * Setter fot the <code>var</code> tag attribute.
101      * @param var variable name: the content node will be added to the pagecontext with this name
102      */

103     public void setVar(String JavaDoc var) {
104         this.var = var;
105     }
106
107     /**
108      * Scope for the declared variable.
109      * @param scope Can be <code>page</code>, <code>request</code>, <code>session</code> or
110      * <code>application</code><code></code>.
111      */

112     public void setScope(String JavaDoc scope) {
113         if ("request".equalsIgnoreCase(scope)) { //$NON-NLS-1$
114
this.scope = PageContext.REQUEST_SCOPE;
115         }
116         else if ("session".equalsIgnoreCase(scope)) { //$NON-NLS-1$
117
this.scope = PageContext.SESSION_SCOPE;
118         }
119         else if ("application".equalsIgnoreCase(scope)) { //$NON-NLS-1$
120
this.scope = PageContext.APPLICATION_SCOPE;
121         }
122         else {
123             // default
124
this.scope = PageContext.PAGE_SCOPE;
125         }
126     }
127
128     /**
129      * Set contentNode in pagecontext and continue evaluating jsp.
130      * @return int
131      */

132     public int doEndTag() {
133
134         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
135         Content local = Resource.getLocalContentNode(request);
136         Content actpage = Resource.getCurrentActivePage(request);
137
138         // Evaluated content node.
139
Content contentNode = null;
140
141         if (StringUtils.isNotEmpty(contentNodeName)) {
142             // contentNodeName is defined
143
try {
144                 if (StringUtils.isEmpty(contentNodeCollectionName)) {
145                     // e.g. <cms:setNode contentNodeName="footer"/>
146
contentNode = actpage.getContent(contentNodeName);
147                 }
148                 else {
149                     // e.g. <cms:setNode contentNodeName="01" contentNodeCollectionName="mainPars"/>
150
// e.g. <cms:setNode contentNodeName="footer" contentNodeCollectionName=""/>
151
contentNode = actpage.getContent(contentNodeCollectionName).getContent(contentNodeName);
152                 }
153             }
154             catch (RepositoryException re) {
155                 log.debug(re.getMessage());
156             }
157         }
158         else {
159             if (local == null) {
160                 // outside collection iterator
161
if (StringUtils.isEmpty(contentNodeCollectionName)) {
162                     // e.g. <cms:setNode contentNodeName=""/>
163
// e.g. <cms:setNode contentNodeCollectionName=""/>
164
contentNode = actpage;
165                 }
166                 // else:
167
// ERROR: no content node assignable because contentNodeName is empty
168
// e.g. <cms:setNode contentNodeCollectionName="mainPars"/>
169
}
170             else {
171                 // inside collection iterator
172
if (contentNodeName == null && contentNodeCollectionName == null) {
173                     // e.g. <cms:setNode />
174
contentNode = local;
175                 }
176                 else if ((contentNodeName != null && StringUtils.isEmpty(contentNodeName))
177                     || (contentNodeCollectionName != null && StringUtils.isEmpty(contentNodeCollectionName))) {
178                     // empty collection name -> use actpage
179
// e.g. <cms:setNode contentNodeCollectionName=""/>
180
contentNode = actpage;
181                 }
182             }
183         }
184
185         // set attribute
186
if (contentNode != null) {
187             pageContext.setAttribute(this.var, new NodeMapWrapper(contentNode), this.scope);
188         }
189         else {
190             pageContext.removeAttribute(this.var);
191         }
192
193         return EVAL_PAGE;
194     }
195
196     /**
197      * @see javax.servlet.jsp.tagext.Tag#release()
198      */

199     public void release() {
200         this.contentNodeCollectionName = null;
201         this.contentNodeName = null;
202         this.var = null;
203         this.scope = PageContext.PAGE_SCOPE;
204         super.release();
205     }
206
207     /**
208      * Wrapper for a content Node which exposes a Map interface, used to access its content using jstl.
209      * @author fgiust
210      * @version $Revision: 1261 $ ($Author: fgiust $)
211      */

212     public static class NodeMapWrapper implements Map JavaDoc {
213
214         /**
215          * The wrapped Content.
216          */

217         private Content wrappedNode;
218
219         /**
220          * Instantiates a new NodeMapWrapper for the given node.
221          * @param node Content node
222          */

223         public NodeMapWrapper(Content node) {
224             wrappedNode = node;
225         }
226
227         /**
228          * @see java.util.Map#size()
229          */

230         public int size() {
231             // not implemented, only get() is needed
232
return 0;
233         }
234
235         /**
236          * @see java.util.Map#isEmpty()
237          */

238         public boolean isEmpty() {
239             // not implemented, only get() is needed
240
return false;
241         }
242
243         /**
244          * @see java.util.Map#containsKey(java.lang.Object)
245          */

246         public boolean containsKey(Object JavaDoc key) {
247             // not implemented, only get() is needed
248
return false;
249         }
250
251         /**
252          * @see java.util.Map#containsValue(java.lang.Object)
253          */

254         public boolean containsValue(Object JavaDoc value) {
255             // not implemented, only get() is needed
256
return false;
257         }
258
259         /**
260          * Shortcut for Content.getNodeData(name).getString() or Content.getNodeData(name).getName().
261          * @see java.util.Map#get(java.lang.Object)
262          */

263         public Object JavaDoc get(Object JavaDoc key) {
264             NodeData nodeData;
265
266             nodeData = this.wrappedNode.getNodeData((String JavaDoc) key);
267             Object JavaDoc value;
268             int type = nodeData.getType();
269             if (type == PropertyType.DATE) {
270                 value = nodeData.getDate();
271             }
272             else if (type == PropertyType.BINARY) {
273                 // only file path is supported
274
FileProperties props = new FileProperties(this.wrappedNode, (String JavaDoc) key);
275                 value = props.getProperty(StringUtils.EMPTY);
276             }
277             else {
278                 value = nodeData.getString();
279             }
280             return value;
281         }
282
283         /**
284          * @see java.util.Map#put(java.lang.Object, java.lang.Object)
285          */

286         public Object JavaDoc put(Object JavaDoc arg0, Object JavaDoc arg1) {
287             // not implemented, only get() is needed
288
return null;
289         }
290
291         /**
292          * @see java.util.Map#remove(java.lang.Object)
293          */

294         public Object JavaDoc remove(Object JavaDoc key) {
295             // not implemented, only get() is needed
296
return null;
297         }
298
299         /**
300          * @see java.util.Map#putAll(java.util.Map)
301          */

302         public void putAll(Map JavaDoc t) {
303             // not implemented, only get() is needed
304
}
305
306         /**
307          * @see java.util.Map#clear()
308          */

309         public void clear() {
310             // not implemented, only get() is needed
311
}
312
313         /**
314          * @see java.util.Map#keySet()
315          */

316         public Set keySet() {
317             // not implemented, only get() is needed
318
return null;
319         }
320
321         /**
322          * @see java.util.Map#values()
323          */

324         public Collection JavaDoc values() {
325             // not implemented, only get() is needed
326
return null;
327         }
328
329         /**
330          * @see java.util.Map#entrySet()
331          */

332         public Set entrySet() {
333             // not implemented, only get() is needed
334
return null;
335         }
336     }
337
338 }
339
Popular Tags