1/*2 * $Id: GetAsStringModel.java 797765 2009-07-25 13:20:26Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.tiles.template;
2324import java.io.IOException;
25import java.io.Writer;
2627import org.apache.tiles.ArrayStack;
28import org.apache.tiles.Attribute;
29import org.apache.tiles.TilesContainer;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
3233/***34 * <p>35 * <strong> Render the value of the specified template attribute to the current36 * Writer</strong>37 * </p>38 *39 * <p>40 * Retrieve the value of the specified template attribute property, and render41 * it to the current Writer as a String. The usual toString() conversions is42 * 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.047 */48publicclassGetAsStringModel {
4950/***51 * The logging object.52 */53private Logger log = LoggerFactory.getLogger(getClass());
5455/***56 * The attribute resolver to use.57 */58privateAttributeResolver attributeResolver;
5960/***61 * Constructor.62 *63 * @param attributeResolver The attribute resolver to use.64 * @since 2.2.065 */66publicGetAsStringModel(AttributeResolver attributeResolver) {
67this.attributeResolver = attributeResolver;
68 }
6970/***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 during76 * 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 attribute79 * 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 if81 * the attribute was not computed.82 * @param defaultValueRole The default comma-separated list of roles. To use83 * only if the attribute was not computed.84 * @param defaultValueType The default type of the attribute. To use only if85 * 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.090 */91publicvoid start(ArrayStack<Object> composeStack, TilesContainer container,
92boolean 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 }
100101/***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 during108 * 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 */112publicvoid 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 }
118119/***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 during125 * 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 attribute128 * 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 if130 * the attribute was not computed.131 * @param defaultValueRole The default comma-separated list of roles. To use132 * only if the attribute was not computed.133 * @param defaultValueType The default type of the attribute. To use only if134 * 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.0140 */141publicvoid execute(TilesContainer container, Writer writer,
142boolean 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 }
150151/***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 during156 * 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 attribute159 * 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 if161 * the attribute was not computed.162 * @param defaultValueRole The default comma-separated list of roles. To use163 * only if the attribute was not computed.164 * @param defaultValueType The default type of the attribute. To use only if165 * 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 */171private Attribute resolveAttribute(TilesContainer container,
172boolean ignore, String preparer, String role, Object defaultValue,
173 String defaultValueRole, String defaultValueType, String name,
174 Attribute value, Object... requestItems) {
175if (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);
182return attribute;
183 }
184185/***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 during192 * 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 */196privatevoid renderAttribute(Attribute attribute, TilesContainer container,
197 Writer writer, boolean ignore, Object... requestItems)
198 throws IOException {
199if (attribute == null && ignore) {
200return;
201 }
202try {
203 writer.write(attribute.getValue().toString());
204 } catch (IOException e) {
205if (!ignore) {
206throw e;
207 } elseif (log.isDebugEnabled()) {
208 log.debug("Ignoring exception", e);
209 }
210 } catch (RuntimeException e) {
211if (!ignore) {
212throw e;
213 } elseif (log.isDebugEnabled()) {
214 log.debug("Ignoring exception", e);
215 }
216 } finally {
217 container.endContext(requestItems);
218 }
219 }
220 }