KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.config.Paragraph;
16 import info.magnolia.cms.beans.config.ParagraphManager;
17 import info.magnolia.cms.core.Content;
18 import info.magnolia.cms.util.Resource;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
27
28 import org.apache.commons.lang.exception.NestableRuntimeException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33 /**
34  * @author marcel Salathe
35  * @author Sameer Charles
36  * @author Fabrizio Giustina
37  * @version $Revision: 6400 $ ($Author: philipp $)
38  */

39 public class Include extends BodyTagSupport JavaDoc {
40
41     /**
42      * Stable serialVersionUID.
43      */

44     private static final long serialVersionUID = 222L;
45
46     /**
47      * Logger.
48      */

49     private static Logger log = LoggerFactory.getLogger(Include.class);
50
51     /**
52      * file to be included (e.g. /templates/jsp/x.jsp).
53      */

54     private String JavaDoc path;
55
56     /**
57      * Attributes to be passed to the included template (set by nested Attribute tags)
58      */

59     private transient List JavaDoc attributes;
60
61     /**
62      * the instance contentNode (i.e. paragraph) you wish to show.
63      */

64     private transient Content contentNode;
65
66     /**
67      * the name of the contentNode (i.e. paragraph) you wish to show.
68      */

69     private String JavaDoc contentNodeName;
70
71     /**
72      * @deprecated
73      * @see #setContentNode(Content)
74      */

75     public void setContainer(Content contentNode) {
76         this.setContentNode(contentNode);
77     }
78
79     /**
80      * Set the content object.
81      * @param contentNode the instance contentNode (i.e. paragraph) you wish to show
82      */

83     public void setContentNode(Content contentNode) {
84         this.contentNode = contentNode;
85     }
86
87     /**
88      * Set the file to be included.
89      * @param path file to be included (e.g. /templates/jsp/x.jsp)
90      */

91     public void setPath(String JavaDoc path) {
92         this.path = path;
93     }
94
95     /**
96      * If this parameter is passed the include tag uses the defined node of the page
97      * @param contentNodeName the name of the contentNode (i.e. paragraph) you wish to show
98      */

99     public void setContentNodeName(String JavaDoc contentNodeName) {
100         this.contentNodeName = contentNodeName;
101     }
102
103     /**
104      * @param name name of attribute to pass with the include
105      * @param value value of attribute to pass with the include
106      */

107     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
108         if (attributes == null) {
109             attributes = new ArrayList JavaDoc();
110         }
111         Object JavaDoc[] attributesArray = new Object JavaDoc[]{name, value};
112         attributes.add(attributesArray);
113     }
114
115     /**
116      * @see javax.servlet.jsp.tagext.IterationTag#doAfterBody()
117      */

118     public int doAfterBody() {
119         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
120         if ((attributes != null) && (attributes.size() > 0)) {
121             Iterator JavaDoc i = attributes.iterator();
122             while (i.hasNext()) {
123                 Object JavaDoc[] s = (Object JavaDoc[]) i.next();
124                 req.setAttribute((String JavaDoc) s[0], s[1]);
125             }
126         }
127         return SKIP_BODY;
128     }
129
130     /**
131      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
132      */

133     public int doEndTag() {
134         boolean localContentNodeSet = false;
135         try {
136             HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
137             
138             // get content
139
Content content = this.contentNode;
140             if (content == null) {
141                 // was there a node name passed
142
if (this.contentNodeName != null) {
143                     content = Resource.getCurrentActivePage(req).getContent(this.contentNodeName);
144                     if (content != null) {
145                         Resource.setLocalContentNode(req, content);
146                         localContentNodeSet = true;
147                     }
148                 }
149                 // use current (first local then global)
150
else {
151                     content = Resource.getLocalContentNode(req);
152                     if (content == null) {
153                         content = Resource.getGlobalContentNode(req);
154                         if (content != null) {
155                             Resource.setLocalContentNode(req, content);
156                             localContentNodeSet = true;
157                         }
158                     }
159                 }
160                 if (content == null) {
161                     throw new Exception JavaDoc("no content node found"); //$NON-NLS-1$
162
}
163             }
164
165             String JavaDoc jspPage = this.path;
166
167             if (jspPage == null) {
168                 String JavaDoc paragraphName = content.getMetaData().getTemplate();
169                 Paragraph paragraph = ParagraphManager.getInstance().getInfo(paragraphName);
170
171                 if (paragraph == null) {
172                     log.error("Paragraph {} not found for page {}", //$NON-NLS-1$
173
paragraphName,
174                         content.getHandle());
175                 }
176                 else {
177                     jspPage = paragraph.getTemplatePath();
178                 }
179
180                 if (jspPage == null) {
181                     log.error("Unable to render paragraph {} in page {}: templatePath not set.", //$NON-NLS-1$
182
paragraphName,
183                         content.getHandle());
184                 }
185             }
186             if (jspPage != null) {
187                 pageContext.include(jspPage);
188             }
189         }
190         catch (IOException JavaDoc e) {
191             // should never happen
192
throw new NestableRuntimeException(e);
193         }
194         catch (Exception JavaDoc e) {
195             log.error(e.getMessage(), e);
196         }
197
198         finally {
199             // if we set the local content node we have to reset it again else we keep the node
200
if(localContentNodeSet){
201                 Resource.removeLocalContentNode((HttpServletRequest JavaDoc) pageContext.getRequest());
202                 
203             }
204         }
205
206         this.removeAttributes();
207         return EVAL_PAGE;
208     }
209
210     /**
211      *
212      */

213     private void removeAttributes() {
214         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
215         if ((attributes != null) && (attributes.size() > 0)) {
216             Iterator JavaDoc i = attributes.iterator();
217             while (i.hasNext()) {
218                 Object JavaDoc[] s = (Object JavaDoc[]) i.next();
219                 req.removeAttribute((String JavaDoc) s[0]);
220             }
221         }
222         attributes = null;
223     }
224
225     /**
226      * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
227      */

228     public void release() {
229         this.path = null;
230         this.attributes = null;
231         this.contentNode = null;
232         super.release();
233     }
234
235 }
Popular Tags