Source code: com/obinary/cms/taglibs/Include.java
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-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11 *
12 * */
13
14
15
16
17
18
19 package com.obinary.cms.taglibs;
20
21
22 import com.obinary.cms.core.Container;
23 import com.obinary.cms.util.Resource;
24
25 import javax.servlet.jsp.tagext.BodyTagSupport;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31
32
33 /**
34 * User: marcelsalathe
35 * Date: Apr 28, 2003
36 * Time: 11:20:59 AM
37 * @author marcel Salathe
38 * @author Sameer Charles
39 * @version 1.0
40 */
41
42
43 public class Include extends BodyTagSupport {
44
45
46 private Container container;
47 private String path;
48
49 private ArrayList attributes;
50
51
52 /**
53 * <p>after the body, this method sets attributes</p>
54 *
55 * @return int
56 */
57 public int doAfterBody() {
58 HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
59 if ((attributes != null) && (attributes.size()>0)) {
60 Iterator i = attributes.iterator();
61 while (i.hasNext()) {
62 String[] s = (String[])i.next();
63 req.setAttribute(s[0],s[1]);
64 }
65 }
66
67 return SKIP_BODY;
68 }
69
70
71 /**
72 * <p>end of includeTemplate tag, includes template and removes attributes</p>
73 *
74 * @return int
75 */
76 public int doEndTag() {
77 this.include();
78 this.removeAttributes();
79 return EVAL_PAGE;
80 }
81
82
83 /**
84 *
85 */
86 private void removeAttributes() {
87 HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
88 if ((attributes != null) && (attributes.size()>0)) {
89 Iterator i = attributes.iterator();
90 while (i.hasNext()) {
91 String[] s = (String[])i.next();
92 req.removeAttribute(s[0]);
93 }
94 }
95 attributes = null;
96 }
97
98
99
100 /**
101 *
102 */
103 private void include() {
104 if (this.container == null)
105 this.includeFromResource();
106 else
107 this.includeFromParam();
108 }
109
110
111
112 /**
113 *
114 */
115 private void includeFromResource() {
116 try {
117 HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
118 if (this.path == null)
119 pageContext.include(Resource.getLocalContainer(req).getTemplate());
120 else
121 pageContext.include(this.path);
122 } catch (ServletException se) {
123 se.printStackTrace();
124 } catch (Exception e) {e.printStackTrace();}
125 }
126
127
128
129 /**
130 *
131 */
132 private void includeFromParam() {
133 Resource.setLocalContainer((HttpServletRequest)pageContext.getRequest(),this.container);
134 try {
135 pageContext.include(this.path);
136 } catch (ServletException se) {
137 se.printStackTrace();
138 } catch (IOException e) {
139 e.printStackTrace();
140 } finally {
141 Resource.removeLocalContainer((HttpServletRequest)pageContext.getRequest());
142 }
143
144 }
145
146
147
148
149 /**
150 * <p>set the content object (type Container)</p>
151 *
152 * @param container
153 */
154 public void setContainer(Container container) {
155 this.container = container;
156 }
157
158
159
160 /**
161 * <p>set the jsp path</p>
162 *
163 * @param path
164 */
165 public void setPath(String path) {
166 this.path = path;
167 }
168
169
170 /**
171 * @param name , name of attribute to pass with the include
172 * @param value , value of attribute to pass with the include
173 */
174 public void setAttribute(String name, String value) {
175 if (attributes == null) attributes = new ArrayList();
176 String [] attributesArray = new String[] {name,value};
177 attributes.add(attributesArray);
178
179 }
180
181
182 }