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.template;
23
24 import java.io.IOException;
25 import java.io.Writer;
26
27 import org.apache.tiles.ArrayStack;
28 import org.apache.tiles.Attribute;
29 import org.apache.tiles.TilesContainer;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /***
34 * <p>
35 * <strong> Render the value of the specified template attribute to the current
36 * Writer</strong>
37 * </p>
38 *
39 * <p>
40 * Retrieve the value of the specified template attribute property, and render
41 * it to the current Writer as a String. The usual toString() conversions is
42 * applied on found value.
43 * </p>
44 *
45 * @version $Rev: 797765 $ $Date: 2009-07-25 15:20:26 +0200 (sab, 25 lug 2009) $
46 * @since 2.2.0
47 */
48 public class GetAsStringModel {
49
50 /***
51 * The logging object.
52 */
53 private Logger log = LoggerFactory.getLogger(getClass());
54
55 /***
56 * The attribute resolver to use.
57 */
58 private AttributeResolver attributeResolver;
59
60 /***
61 * Constructor.
62 *
63 * @param attributeResolver The attribute resolver to use.
64 * @since 2.2.0
65 */
66 public GetAsStringModel(AttributeResolver attributeResolver) {
67 this.attributeResolver = attributeResolver;
68 }
69
70 /***
71 * Starts the operation.
72 *
73 * @param composeStack The compose stack,
74 * @param container The Tiles container to use.
75 * @param ignore If <code>true</code>, if an exception happens during
76 * rendering, of if the attribute is null, the problem will be ignored.
77 * @param preparer The preparer to invoke before rendering the attribute.
78 * @param role A comma-separated list of roles. If present, the attribute
79 * will be rendered only if the current user belongs to one of the roles.
80 * @param defaultValue The default value of the attribute. To use only if
81 * the attribute was not computed.
82 * @param defaultValueRole The default comma-separated list of roles. To use
83 * only if the attribute was not computed.
84 * @param defaultValueType The default type of the attribute. To use only if
85 * the attribute was not computed.
86 * @param name The name of the attribute.
87 * @param value The attribute to use immediately, if not null.
88 * @param requestItems The request objects.
89 * @since 2.2.0
90 */
91 public void start(ArrayStack<Object> composeStack, TilesContainer container,
92 boolean ignore, String preparer, String role, Object defaultValue,
93 String defaultValueRole, String defaultValueType, String name,
94 Attribute value, Object... requestItems) {
95 Attribute attribute = resolveAttribute(container, ignore, preparer,
96 role, defaultValue, defaultValueRole, defaultValueType, name,
97 value, requestItems);
98 composeStack.push(attribute);
99 }
100
101 /***
102 * Ends the operation.
103 *
104 * @param composeStack The compose stack,
105 * @param container The Tiles container to use.
106 * @param writer The writer into which the attribute will be written.
107 * @param ignore If <code>true</code>, if an exception happens during
108 * rendering, of if the attribute is null, the problem will be ignored.
109 * @param requestItems The request objects.
110 * @throws IOException If an I/O error happens during rendering.
111 */
112 public void end(ArrayStack<Object> composeStack, TilesContainer container,
113 Writer writer, boolean ignore, Object... requestItems)
114 throws IOException {
115 Attribute attribute = (Attribute) composeStack.pop();
116 renderAttribute(attribute, container, writer, ignore, requestItems);
117 }
118
119 /***
120 * Executes the operation.
121 *
122 * @param container The Tiles container to use.
123 * @param writer The writer into which the attribute will be written.
124 * @param ignore If <code>true</code>, if an exception happens during
125 * rendering, of if the attribute is null, the problem will be ignored.
126 * @param preparer The preparer to invoke before rendering the attribute.
127 * @param role A comma-separated list of roles. If present, the attribute
128 * will be rendered only if the current user belongs to one of the roles.
129 * @param defaultValue The default value of the attribute. To use only if
130 * the attribute was not computed.
131 * @param defaultValueRole The default comma-separated list of roles. To use
132 * only if the attribute was not computed.
133 * @param defaultValueType The default type of the attribute. To use only if
134 * the attribute was not computed.
135 * @param name The name of the attribute.
136 * @param value The attribute to use immediately, if not null.
137 * @param requestItems The request objects.
138 * @throws IOException If an I/O error happens during rendering.
139 * @since 2.2.0
140 */
141 public void execute(TilesContainer container, Writer writer,
142 boolean ignore, String preparer, String role, Object defaultValue,
143 String defaultValueRole, String defaultValueType, String name,
144 Attribute value, Object... requestItems) throws IOException {
145 Attribute attribute = resolveAttribute(container, ignore, preparer,
146 role, defaultValue, defaultValueRole, defaultValueType, name,
147 value, requestItems);
148 renderAttribute(attribute, container, writer, ignore, requestItems);
149 }
150
151 /***
152 * Resolves the attribute. and starts the context.
153 *
154 * @param container The Tiles container to use.
155 * @param ignore If <code>true</code>, if an exception happens during
156 * rendering, of if the attribute is null, the problem will be ignored.
157 * @param preparer The preparer to invoke before rendering the attribute.
158 * @param role A comma-separated list of roles. If present, the attribute
159 * will be rendered only if the current user belongs to one of the roles.
160 * @param defaultValue The default value of the attribute. To use only if
161 * the attribute was not computed.
162 * @param defaultValueRole The default comma-separated list of roles. To use
163 * only if the attribute was not computed.
164 * @param defaultValueType The default type of the attribute. To use only if
165 * the attribute was not computed.
166 * @param name The name of the attribute.
167 * @param value The attribute to use immediately, if not null.
168 * @param requestItems The request objects.
169 * @return The resolved attribute.
170 */
171 private Attribute resolveAttribute(TilesContainer container,
172 boolean ignore, String preparer, String role, Object defaultValue,
173 String defaultValueRole, String defaultValueType, String name,
174 Attribute value, Object... requestItems) {
175 if (preparer != null) {
176 container.prepare(preparer, requestItems);
177 }
178 Attribute attribute = attributeResolver.computeAttribute(container,
179 value, name, role, ignore, defaultValue, defaultValueRole,
180 defaultValueType, requestItems);
181 container.startContext(requestItems);
182 return attribute;
183 }
184
185 /***
186 * Renders the attribute as a string.
187 *
188 * @param attribute The attribute to use, previously resolved.
189 * @param container The Tiles container to use.
190 * @param writer The writer into which the attribute will be written.
191 * @param ignore If <code>true</code>, if an exception happens during
192 * rendering, of if the attribute is null, the problem will be ignored.
193 * @param requestItems The request objects.
194 * @throws IOException If an I/O error happens during rendering.
195 */
196 private void renderAttribute(Attribute attribute, TilesContainer container,
197 Writer writer, boolean ignore, Object... requestItems)
198 throws IOException {
199 if (attribute == null && ignore) {
200 return;
201 }
202 try {
203 writer.write(attribute.getValue().toString());
204 } catch (IOException e) {
205 if (!ignore) {
206 throw e;
207 } else if (log.isDebugEnabled()) {
208 log.debug("Ignoring exception", e);
209 }
210 } catch (RuntimeException e) {
211 if (!ignore) {
212 throw e;
213 } else if (log.isDebugEnabled()) {
214 log.debug("Ignoring exception", e);
215 }
216 } finally {
217 container.endContext(requestItems);
218 }
219 }
220 }