1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package org.apache.tiles.velocity.template; |
23 | |
|
24 | |
import java.io.IOException; |
25 | |
import java.io.Writer; |
26 | |
|
27 | |
import javax.servlet.ServletContext; |
28 | |
|
29 | |
import org.apache.tiles.Attribute; |
30 | |
import org.apache.tiles.AttributeContext; |
31 | |
import org.apache.tiles.TilesContainer; |
32 | |
import org.apache.tiles.access.TilesAccess; |
33 | |
import org.apache.tiles.request.Request; |
34 | |
import org.apache.tiles.request.servlet.ServletUtil; |
35 | |
import org.apache.tiles.request.velocity.VelocityRequest; |
36 | |
import org.apache.velocity.context.InternalContextAdapter; |
37 | |
import org.apache.velocity.runtime.Renderable; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 12 | public class VelocityStyleTilesTool extends ContextHolder { |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public Attribute get(String key) { |
55 | 1 | Request velocityRequest = createVelocityRequest(getServletContext(), |
56 | |
null); |
57 | 1 | TilesContainer container = TilesAccess.getCurrentContainer(velocityRequest); |
58 | 1 | AttributeContext attributeContext = container |
59 | |
.getAttributeContext(velocityRequest); |
60 | 1 | Attribute attribute = attributeContext.getAttribute(key); |
61 | 1 | return attribute; |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public Attribute createAttribute() { |
71 | 1 | return new Attribute(); |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public Attribute clone(Attribute attribute) { |
82 | 1 | return new Attribute(attribute); |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public Attribute createTemplateAttribute(String template) { |
93 | 1 | return Attribute.createTemplateAttribute(template); |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public Renderable render(final Attribute attribute) { |
104 | 1 | return new AbstractDefaultToStringRenderable(getVelocityContext(), |
105 | 1 | null, getResponse(), getRequest()) { |
106 | |
|
107 | |
public boolean render(InternalContextAdapter context, Writer writer) |
108 | |
throws IOException { |
109 | 1 | Request velocityRequest = createVelocityRequest( |
110 | |
getServletContext(), writer); |
111 | 1 | TilesContainer container = TilesAccess |
112 | |
.getCurrentContainer(velocityRequest); |
113 | 1 | container.render(attribute, velocityRequest); |
114 | 1 | return true; |
115 | |
} |
116 | |
|
117 | |
}; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public Renderable renderDefinition(final String definitionName) { |
130 | 1 | return new AbstractDefaultToStringRenderable(getVelocityContext(), |
131 | 1 | null, getResponse(), getRequest()) { |
132 | |
|
133 | |
public boolean render(InternalContextAdapter context, Writer writer) { |
134 | 1 | Request velocityRequest = createVelocityRequest( |
135 | |
getServletContext(), writer); |
136 | 1 | TilesContainer container = TilesAccess |
137 | |
.getCurrentContainer(velocityRequest); |
138 | 1 | container.render(definitionName, velocityRequest); |
139 | 1 | return true; |
140 | |
} |
141 | |
|
142 | |
}; |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public Renderable renderAttributeContext() { |
154 | 1 | return new AbstractDefaultToStringRenderable(getVelocityContext(), |
155 | 1 | null, getResponse(), getRequest()) { |
156 | |
|
157 | |
public boolean render(InternalContextAdapter context, Writer writer) { |
158 | 1 | Request velocityRequest = createVelocityRequest( |
159 | |
getServletContext(), writer); |
160 | 1 | TilesContainer container = TilesAccess |
161 | |
.getCurrentContainer(velocityRequest); |
162 | 1 | container.renderContext(velocityRequest); |
163 | 1 | return true; |
164 | |
} |
165 | |
|
166 | |
}; |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public AttributeContext startAttributeContext() { |
177 | 1 | Request velocityRequest = createVelocityRequest( |
178 | |
getServletContext(), null); |
179 | 1 | TilesContainer container = TilesAccess |
180 | |
.getCurrentContainer(velocityRequest); |
181 | 1 | return container.startContext(velocityRequest); |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
public VelocityStyleTilesTool endAttributeContext() { |
192 | 1 | Request velocityRequest = createVelocityRequest(getServletContext(), |
193 | |
null); |
194 | 1 | TilesContainer container = TilesAccess |
195 | |
.getCurrentContainer(velocityRequest); |
196 | 1 | container.endContext(velocityRequest); |
197 | 1 | return this; |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
public AttributeContext getAttributeContext() { |
207 | 1 | Request velocityRequest = createVelocityRequest( |
208 | |
getServletContext(), null); |
209 | 1 | TilesContainer container = TilesAccess |
210 | |
.getCurrentContainer(velocityRequest); |
211 | 1 | return container.getAttributeContext(velocityRequest); |
212 | |
} |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
public VelocityStyleTilesTool setCurrentContainer(String containerKey) { |
222 | 1 | Request velocityRequest = createVelocityRequest( |
223 | |
getServletContext(), null); |
224 | 1 | TilesAccess.setCurrentContainer(velocityRequest, containerKey); |
225 | 1 | return this; |
226 | |
} |
227 | |
|
228 | |
|
229 | |
@Override |
230 | |
public String toString() { |
231 | 1 | return ""; |
232 | |
} |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
protected Request createVelocityRequest( |
242 | |
ServletContext servletContext, Writer writer) { |
243 | 8 | return VelocityRequest.createVelocityRequest(ServletUtil |
244 | |
.getApplicationContext(servletContext), getRequest(), |
245 | |
getResponse(), getVelocityContext(), writer); |
246 | |
} |
247 | |
} |