KickJava   Java API By Example, From Geeks To Geeks.

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


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.tagext.TagSupport JavaDoc;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 /**
29  * @author fgiust
30  * @version $Revision: 6341 $ ($Author: philipp $)
31  */

32 public class BaseContentTag extends TagSupport JavaDoc {
33
34     /**
35      * Stable serialVersionUID.
36      */

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

42     protected Logger log = LoggerFactory.getLogger(getClass());
43
44     protected String JavaDoc nodeDataName;
45
46     protected String JavaDoc contentNodeName;
47
48     protected String JavaDoc contentNodeCollectionName;
49
50     protected boolean inherit;
51
52     /**
53      * Set the node data name, e.g. "mainText".
54      * @param name
55      */

56     public void setNodeDataName(String JavaDoc name) {
57         this.nodeDataName = name;
58     }
59
60     /**
61      * Set the content node name name, e.g. "01".
62      * @param name
63      */

64     public void setContentNodeName(String JavaDoc name) {
65         this.contentNodeName = name;
66     }
67
68     /**
69      * Set the content node collection name name, e.g. "mainColumnParagraphs".
70      * @param name
71      */

72     public void setContentNodeCollectionName(String JavaDoc name) {
73         this.contentNodeCollectionName = name;
74     }
75
76     /**
77      * Setter for <code>inherit</code>.
78      * @param inherit <code>true</code> to inherit from parent pages if value is not set.
79      */

80     public void setInherit(boolean inherit) {
81         this.inherit = inherit;
82     }
83
84     /**
85      * Get the first matching node containing a NodeData named <code>nodeDataName</code>
86      * @return the active node, or the first matching one if nodedata is null and inherit is set.
87      */

88     protected Content getFirtMatchingNode() {
89
90         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
91         Content currentPage = Resource.getCurrentActivePage(request);
92         Content contentNode = resolveNode(currentPage);
93         if (contentNode == null) {
94             return null;
95         }
96
97         NodeData nodeData = contentNode.getNodeData(this.nodeDataName);
98
99         try {
100             while (inherit && currentPage.getLevel() > 0 && !nodeData.isExist()) {
101                 currentPage = currentPage.getParent();
102                 contentNode = resolveNode(currentPage);
103                 nodeData = contentNode.getNodeData(this.nodeDataName);
104             }
105         }
106         catch (RepositoryException e) {
107             log.error(e.getMessage(), e);
108         }
109
110         return contentNode;
111     }
112
113     protected Content resolveNode(Content currentPage) {
114         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
115
116         Content currentParagraph = Resource.getLocalContentNode(request);
117
118         if (StringUtils.isNotEmpty(contentNodeName)) {
119             // contentNodeName is defined
120
try {
121                 if (StringUtils.isEmpty(contentNodeCollectionName)) {
122                     // e.g. <cms:out nodeDataName="title" contentNodeName="footer"/>
123
return currentPage.getContent(contentNodeName);
124                 }
125
126                 // e.g. <cms:out nodeDataName="title" contentNodeName="01" contentNodeCollectionName="mainPars"/>
127
// e.g. <cms:out nodeDataName="title" contentNodeName="footer" contentNodeCollectionName=""/>
128
return currentPage.getContent(contentNodeCollectionName).getContent(contentNodeName);
129
130             }
131             catch (RepositoryException re) {
132                 if (log.isDebugEnabled()) {
133                     log.debug(re.getMessage());
134                 }
135             }
136         }
137         else {
138             if (currentParagraph == null) {
139                 // outside collection iterator
140
if (StringUtils.isEmpty(contentNodeCollectionName)) {
141                     // e.g. <cms:out nodeDataName="title"/>
142
// e.g. <cms:out nodeDataName="title" contentNodeName=""/>
143
// e.g. <cms:out nodeDataName="title" contentNodeCollectionName=""/>
144
return currentPage;
145                 }
146                 // ERROR: no content node assignable because contentNodeName is empty
147
// e.g. <cms:out nodeDataName="title" contentNodeCollectionName="mainPars"/>
148
}
149             else {
150                 // inside collection iterator
151
if (contentNodeName == null && contentNodeCollectionName == null) {
152                     // e.g. <cms:out nodeDataName="title"/>
153
return currentParagraph;
154                 }
155                 else if ((contentNodeName != null && StringUtils.isEmpty(contentNodeName))
156                     || (contentNodeCollectionName != null && StringUtils.isEmpty(contentNodeCollectionName))) {
157                     // empty collection name -> use actpage
158
// e.g. <cms:out nodeDataName="title" contentNodeCollectionName=""/>
159
return currentPage;
160                 }
161             }
162         }
163         return null;
164     }
165
166     /**
167      * @see javax.servlet.jsp.tagext.TagSupport#release()
168      */

169     public void release() {
170         super.release();
171
172         this.nodeDataName = null;
173         this.contentNodeName = null;
174         this.contentNodeCollectionName = null;
175         this.inherit = false;
176     }
177
178 }
179
Popular Tags