1 /*
2 * $Id: InsertAttributeTag.java 527536 2007-04-11 15:44:51Z apetrelli $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.tiles.jsp.taglib;
23
24 import org.apache.tiles.Attribute;
25 import org.apache.tiles.AttributeContext;
26 import org.apache.tiles.TilesException;
27
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.PageContext;
30
31 import java.io.IOException;
32
33 /**
34 * This is the tag handler for <tiles:attribute>, which defines an
35 * attribute. If the attribute value is a template or a definition, its
36 * attributes and its template can be overridden.
37 *
38 * @version $Rev: 527536 $ $Date: 2007-04-11 17:44:51 +0200 (Wed, 11 Apr 2007) $
39 */
40 public class InsertAttributeTag extends RenderTagSupport {
41
42 /**
43 * Name to insert.
44 */
45 protected String name;
46
47 /**
48 * The value of the attribute.
49 */
50 protected Object value = null;
51
52 /**
53 * The context used to evaluate the attribute.
54 */
55 protected AttributeContext evaluatingContext;
56
57 /**
58 * Sets the name of the attribute.
59 *
60 * @param value The name of the attribute.
61 */
62 public void setName(String value) {
63 this.name = value;
64 }
65
66 /**
67 * Returns the name of the attribute.
68 *
69 * @return The name of the attribute.
70 */
71 public String getName() {
72 return name;
73 }
74
75 /**
76 * Get the value.
77 *
78 * @return The value.
79 */
80 public Object getValue() {
81 return value;
82 }
83
84 /**
85 * Set the value.
86 *
87 * @param value The new value
88 */
89 public void setValue(Object value) {
90 this.value = value;
91 }
92
93 /** {@inheritDoc} */
94 public void release() {
95 super.release();
96 this.name = null;
97 this.value = null;
98 }
99
100 /** {@inheritDoc} */
101 protected void render() throws JspException, TilesException, IOException {
102 Attribute attr = (Attribute) value;
103 if (attr == null && evaluatingContext != null) {
104 attr = evaluatingContext.getAttribute(name);
105 }
106 if (attr == null && ignore) {
107 return;
108 }
109
110 if (attr == null) {
111 if (name != null) {
112 throw new TilesException("Attribute '" + name + "' not found.");
113 } else {
114 throw new TilesException("No attribute name or value has been provided.");
115 }
116 }
117 render(attr);
118 }
119
120 /** {@inheritDoc} */
121 @Override
122 protected void startContext(PageContext context) {
123
124 if (container != null) {
125 evaluatingContext = container.getAttributeContext(context);
126 }
127 super.startContext(context);
128 }
129
130 /**
131 * Renders an attribute for real.
132 *
133 * @param attr The attribute to render.
134 * @throws TilesException If something goes wrong during rendering.
135 * @throws IOException If something goes wrong during the reading of
136 * definition files.
137 */
138 protected void render(Attribute attr)
139 throws TilesException, IOException {
140 container.render(attr, pageContext.getOut(), pageContext);
141 }
142 }