1 /*
2 * $Id: UseAttributeTag.java 1360373 2012-07-11 19:52:00Z nlebas $
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 java.io.IOException;
25
26 import javax.servlet.jsp.JspException;
27 import javax.servlet.jsp.tagext.SimpleTagSupport;
28 import javax.servlet.jsp.tagext.TagData;
29 import javax.servlet.jsp.tagext.TagExtraInfo;
30 import javax.servlet.jsp.tagext.VariableInfo;
31
32 import org.apache.tiles.autotag.core.runtime.AutotagRuntime;
33 import org.apache.tiles.request.Request;
34 import org.apache.tiles.template.ImportAttributeModel;
35
36 /**
37 * Exposes am attribute as a scripting variable within the page.
38 *
39 * @since Tiles 1.0
40 * @version $Rev: 1360373 $ $Date: 2012-07-12 05:52:00 +1000 (Thu, 12 Jul 2012) $
41 */
42 public class UseAttributeTag extends SimpleTagSupport {
43
44 /**
45 * The template model.
46 */
47 private ImportAttributeModel model = new ImportAttributeModel();
48
49 /**
50 * The id of the imported scripting variable.
51 */
52 private String id;
53
54 /**
55 * The scope name.
56 */
57 private String scopeName = null;
58
59 /**
60 * The name of the attribute.
61 */
62 private String name = null;
63
64 /**
65 * Flag that, if <code>true</code>, ignores exceptions.
66 */
67 private boolean ignore = false;
68
69 /**
70 * Class name of object.
71 */
72 private String classname = null;
73
74 /**
75 * Returns the id of the imported scripting variable.
76 *
77 * @return The id of the imported scripting variable.
78 * @since 2.2.0
79 */
80 public String getId() {
81 return id;
82 }
83
84 /**
85 * Sets the id of the imported scripting variable.
86 *
87 * @param id
88 * The id of the imported scripting variable.
89 * @since 2.2.0
90 */
91 public void setId(String id) {
92 this.id = id;
93 }
94
95 /**
96 * Set the scope.
97 *
98 * @param scope
99 * Scope.
100 */
101 public void setScope(String scope) {
102 this.scopeName = scope;
103 }
104
105 /**
106 * Get scope.
107 *
108 * @return Scope.
109 */
110 public String getScope() {
111 return scopeName;
112 }
113
114 /**
115 * Get the name.
116 *
117 * @return Name.
118 */
119 public String getName() {
120 return name;
121 }
122
123 /**
124 * Set the name.
125 *
126 * @param name
127 * The new name
128 */
129 public void setName(String name) {
130 this.name = name;
131 }
132
133 /**
134 * Set ignore flag.
135 *
136 * @param ignore
137 * default: <code>false</code>: Exception is thrown when
138 * attribute is not found, set to <code>
139 * true</code> to
140 * ignore missing attributes silently
141 */
142 public void setIgnore(boolean ignore) {
143 this.ignore = ignore;
144 }
145
146 /**
147 * Get ignore flag.
148 *
149 * @return default: <code>false</code>: Exception is thrown when attribute
150 * is not found, set to <code>
151 * true</code> to ignore missing
152 * attributes silently
153 */
154 public boolean isIgnore() {
155 return ignore;
156 }
157
158 /**
159 * Get class name.
160 *
161 * @return class name
162 */
163 public String getClassname() {
164 return classname;
165
166 }
167
168 /**
169 * Set the class name.
170 *
171 * @param name
172 * The new class name.
173 */
174 public void setClassname(String name) {
175 this.classname = name;
176 }
177
178 /** {@inheritDoc} */
179 @Override
180 public void doTag() throws JspException, IOException {
181 AutotagRuntime<Request> runtime = new org.apache.tiles.request.jsp.autotag.JspAutotagRuntime();
182 if (runtime instanceof SimpleTagSupport) {
183 SimpleTagSupport tag = (SimpleTagSupport) runtime;
184 tag.setJspContext(getJspContext());
185 tag.setJspBody(getJspBody());
186 tag.setParent(getParent());
187 tag.doTag();
188 }
189 Request request = runtime.createRequest();
190 model.execute(name, scopeName, id, ignore, request);
191 }
192
193 /**
194 * Returns the scripting variable to use.
195 *
196 * @return The scripting variable.
197 */
198 public String getScriptingVariable() {
199 return id == null ? getName() : id;
200 }
201
202 /**
203 * Implementation of <code>TagExtraInfo</code> which identifies the
204 * scripting object(s) to be made visible.
205 */
206 public static class Tei extends TagExtraInfo {
207
208 /** {@inheritDoc} */
209 @Override
210 public VariableInfo[] getVariableInfo(TagData data) {
211 String classname = data.getAttributeString("classname");
212 if (classname == null) {
213 classname = "java.lang.Object";
214 }
215
216 String id = data.getAttributeString("id");
217 if (id == null) {
218 id = data.getAttributeString("name");
219 }
220
221 return new VariableInfo[] { new VariableInfo(id, classname, true,
222 VariableInfo.AT_END) };
223
224 }
225 }
226 }