1 /*
2 * $Id: UIComponentTag.java,v 1.63 2007/05/30 20:07:21 kenpaulsen Exp $
3 */
4
5 /*
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7 *
8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package javax.faces.webapp;
42
43
44 import javax.faces.application.Application;
45 import javax.faces.component.UIComponent;
46 import javax.faces.context.FacesContext;
47 import javax.faces.el.ValueBinding;
48 import javax.servlet.jsp.JspException;
49 import javax.servlet.jsp.PageContext;
50 import javax.servlet.jsp.tagext.Tag;
51
52
53 /**
54 * <p>{@link UIComponentTag} is the base class for all JSP custom
55 * actions that correspond to user interface components in a page that is
56 * rendered by JavaServer Faces.</p>
57 *
58 * <p>In this version of the specification, <code>UIComponentTag</code>
59 * extends {@link UIComponentClassicTagBase} to add properties that use
60 * the Faces 1.1 Expression Language.</p>
61 *
62 * @deprecated Use of this class has been replaced with {@link
63 * UIComponentELTag}, which extends
64 * <code>UIComponentClassicTagBase</code> to add properties that use the
65 * EL API introduced as part of JSP 2.1.
66 */
67
68 public abstract class UIComponentTag extends UIComponentClassicTagBase implements Tag {
69
70 // ------------------------------------------------------------- Properties
71
72 /**
73 * <p>The value binding expression (if any) used to wire up this component
74 * to a {@link UIComponent} property of a JavaBean class.</p>
75 */
76 private String binding = null;
77
78
79 /**
80 * <p>Set the value binding expression for our component.</p>
81 *
82 * @param binding The new value binding expression
83 *
84 * @throws IllegalArgumentException if the specified binding is not a
85 * valid value binding expression.
86 */
87 public void setBinding(String binding) throws JspException {
88 if (!isValueReference(binding)) {
89 throw new IllegalArgumentException();
90 }
91
92 this.binding = binding;
93 }
94
95 protected boolean hasBinding() {
96 return null != binding;
97 }
98
99
100
101 /**
102 * <p>An override for the rendered attribute associated with our
103 * {@link UIComponent}.</p>
104 */
105 private String rendered = null;
106
107
108 /**
109 * <p>Set an override for the rendered attribute.</p>
110 *
111 * @param rendered The new value for rendered attribute
112 */
113 public void setRendered(String rendered) {
114
115 this.rendered = rendered;
116
117 }
118
119
120 /**
121 * <p>Flag indicating whether or not rendering should occur.</p>
122 */
123 private boolean suppressed = false;
124
125
126 protected boolean isSuppressed() {
127
128 return (suppressed);
129
130 }
131
132
133 /**
134 * <p>Return <code>true</code> if the specified value conforms to the
135 * syntax requirements of a value binding expression. Such expressions
136 ` * may be used on most component tag attributes to signal a desire for
137 * deferred evaluation of the attribute or property value to be set on
138 * the underlying {@link UIComponent}.</p>
139 *
140 * @param value The value to evaluate
141 *
142 * @throws NullPointerException if <code>value</code> is
143 * <code>null</code>
144 */
145 public static boolean isValueReference(String value) {
146
147 if (value == null) {
148 throw new NullPointerException();
149 }
150 int start = value.indexOf("#{");
151 if ((start != -1) && (start < value.indexOf('}', start))) {
152 return true;
153 }
154 return false;
155
156 }
157
158 // ------------------------------------------ Methods from Tag
159
160 /**
161 * <p>Release any resources allocated during the execution of this
162 * tag handler.</p>
163 */
164 public void release() {
165
166 this.suppressed = false;
167 this.binding = null;
168 this.rendered = null;
169 super.release();
170 }
171
172
173 // ---------------- Concrete Implementations of methods from superclass
174
175 /**
176 * @param component {@inheritDoc}
177 */
178 protected void setProperties(UIComponent component) {
179 // The "id" property is explicitly set when components are created
180 // so it does not need to be set here
181 if (rendered != null) {
182 if (isValueReference(rendered)) {
183 ValueBinding vb =
184 getFacesContext().getApplication().createValueBinding(rendered);
185 component.setValueBinding("rendered", vb);
186 } else {
187 component.setRendered(Boolean.valueOf(rendered).booleanValue());
188 }
189 }
190 if (getRendererType() != null) {
191 component.setRendererType(getRendererType());
192 }
193
194 }
195
196
197
198
199
200 /**
201 * <p>Implement <code>createComponent</code> using Faces 1.1 EL
202 * API.</p>
203 *
204 * @param context {@inheritDoc}
205 * @param newId {@inheritDoc}
206 */
207 protected UIComponent createComponent(FacesContext context, String newId) {
208 UIComponent component;
209 Application application = context.getApplication();
210 if (binding != null) {
211 ValueBinding vb = application.createValueBinding(binding);
212 component = application.createComponent(vb, context,
213 getComponentType());
214 component.setValueBinding("binding", vb);
215 } else {
216 component = application.createComponent(getComponentType());
217 }
218
219 component.setId(newId);
220 setProperties(component);
221
222 return component;
223 }
224
225
226 // Tag tree navigation
227
228 /**
229 * <p>Locate and return the nearest enclosing {@link UIComponentTag}
230 * if any; otherwise, return <code>null</code>.</p>
231 *
232 * @param context <code>PageContext</code> for the current page
233 */
234 public static UIComponentTag getParentUIComponentTag(PageContext context) {
235
236 UIComponentClassicTagBase result =
237 getParentUIComponentClassicTagBase(context);
238 if (!(result instanceof UIComponentTag)) {
239 return new UIComponentTagAdapter(result);
240 }
241 return ((UIComponentTag) result);
242
243 }
244
245
246 // --------------------------------------------------------- Private Classes
247
248
249 /**
250 * This adatper exposes a UIComponentClassicTagBase as a UIComponentTag
251 * for 1.1 component libraries that rely on UIComponent.getParentUIComponentTag().
252 *
253 * This will work for most use cases, but there are probably some edge
254 * cases out there that we're not aware of.
255 */
256 private static class UIComponentTagAdapter extends UIComponentTag {
257
258 UIComponentClassicTagBase classicDelegate;
259
260 public UIComponentTagAdapter(UIComponentClassicTagBase classicDelegate) {
261
262 this.classicDelegate = classicDelegate;
263
264 }
265
266 public String getComponentType() {
267 return classicDelegate.getComponentType();
268 }
269
270 public String getRendererType() {
271 return classicDelegate.getRendererType();
272 }
273
274 public int doStartTag() throws JspException {
275 throw new IllegalStateException();
276 }
277
278 public int doEndTag() throws JspException {
279 throw new IllegalStateException();
280 }
281
282 public UIComponent getComponentInstance() {
283 return classicDelegate.getComponentInstance();
284 }
285
286 public boolean getCreated() {
287 return classicDelegate.getCreated();
288 }
289
290 public Tag getParent() {
291 return classicDelegate.getParent();
292 }
293
294 public void setParent(Tag parent) {
295 throw new IllegalStateException();
296 }
297
298 }
299
300
301 }