1 /*
2 * $Id: ConverterTag.java,v 1.25 2007/04/27 22:00:11 ofung 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 import javax.el.ValueExpression;
44 import javax.faces.component.UIComponent;
45 import javax.faces.component.ValueHolder;
46 import javax.faces.context.FacesContext;
47 import javax.faces.convert.Converter;
48 import javax.faces.convert.ConverterException;
49 import javax.servlet.jsp.JspException;
50 import javax.servlet.jsp.tagext.TagSupport;
51
52
53
54
55
56
57 /**
58 * <p><strong>ConverterTag</strong> is a base class for all JSP custom actions
59 * that create and register a <code>Converter</code> instance on the
60 * {@link ValueHolder} associated with our most immediate
61 * surrounding instance of a tag whose implementation class is a subclass
62 * of {@link UIComponentTag}. To avoid creating duplicate instances when
63 * a page is redisplayed, creation and registration of a {@link Converter}
64 * occurs <strong>only</strong> if the corresponding {@link UIComponent} was
65 * created (by the owning {@link UIComponentTag}) during the execution of the
66 * current page.</p>
67 *
68 * <p>This class may be used directly to implement a generic converter
69 * registration tag (based on the converter-id specified by the
70 * <code>converterId</code> attribute), or as a base class for tag
71 * instances that support specific {@link Converter} subclasses. This
72 * <code>converterId</code> attribute must refer to one of the well
73 * known converter-ids, or a custom converter-id as defined in a
74 * <code>faces-config.xml</code> file.</p>
75 *
76 * <p>Subclasses of this class must implement the
77 * <code>createConverter()</code> method, which creates and returns a
78 * {@link Converter} instance. Any configuration properties that specify
79 * behavior of this {@link Converter} must have been set by the
80 * <code>createConverter()</code> method. Generally, this occurs
81 * by copying corresponding attribute values on the tag instance.</p>
82 *
83 * <p>This tag creates no output to the page currently being created. It
84 * is used solely for the side effect of {@link Converter} creation.</p>
85 *
86 * @deprecated This has been partially replaced by {@link
87 * ConverterELTag}. The remainder of the functionality, namely, the
88 * binding facility and the implementation of the {@link
89 * #createConverter} method, is now an implementation detail.
90 */
91
92 public class ConverterTag extends TagSupport {
93
94
95 // ---------------------------------------------------------- Static Members
96
97
98 private static final long serialVersionUID = -5909792518081427720L;
99
100
101 // -------------------------------------------------------------- Attributes
102
103 /**
104 * <p>The identifier of the {@link Converter} instance to be created.</p>
105 */
106 private String converterId = null;
107
108 /**
109 * <p>The {@link ValueExpression} that evaluates to an object that
110 * implements {@link Converter}.</p>
111 */
112 private String binding = null;
113
114 /**
115 * <p>Set the identifer of the {@link Converter} instance to be created.
116 *
117 * @param converterId The identifier of the converter instance to be
118 * created.
119 */
120 public void setConverterId(String converterId) {
121
122 this.converterId = converterId;
123
124 }
125
126 /**
127 * <p>Set the expression that will be used to create a {@link ValueExpression}
128 * that references a backing bean property of the {@link Converter} instance to
129 * be created.</p>
130 *
131 * @param binding The new expression
132 *
133 * @throws JspException if a JSP error occurs
134 */
135 public void setBinding(String binding)
136 throws JspException {
137 if (binding!= null && !UIComponentTag.isValueReference(binding)) {
138 // PENDING i18n
139 throw new JspException("Invalid Expression:"+binding);
140 }
141 this.binding = binding;
142 }
143 // ---------------------------------------------------------- Public Methods
144
145
146 /**
147 * <p>Create a new instance of the specified {@link Converter}
148 * class, and register it with the {@link UIComponent} instance associated
149 * with our most immediately surrounding {@link UIComponentTag} instance, if
150 * the {@link UIComponent} instance was created by this execution of the
151 * containing JSP page. If the localValue of the
152 * {@link UIComponent} is a String, attempt to convert it.</p>
153 *
154 * @throws JspException if a JSP error occurs
155 */
156 public int doStartTag() throws JspException {
157
158 // Locate our parent UIComponentTag
159 UIComponentClassicTagBase tag =
160 UIComponentClassicTagBase.getParentUIComponentClassicTagBase(pageContext);
161 if (tag == null) { // PENDING - i18n
162 throw new JspException("Not nested in a UIComponentTag Error for tag with handler class:"+
163 this.getClass().getName());
164 }
165
166 // Nothing to do unless this tag created a component
167 if (!tag.getCreated()) {
168 return (SKIP_BODY);
169 }
170
171 UIComponent component = tag.getComponentInstance();
172 if (component == null) {
173 //PENDING i18n
174 throw new JspException("Can't create Component from tag.");
175 }
176 if (!(component instanceof ValueHolder)) {
177 //PENDING i18n
178 throw new JspException("Not nested in a tag of proper type. Error for tag with handler class:"+
179 this.getClass().getName());
180 }
181
182 Converter converter = createConverter();
183
184 if (converter == null) {
185 //noinspection NonConstantStringShouldBeStringBuffer
186 String converterError = null;
187 if (binding != null) {
188 converterError = binding;
189 }
190 if (converterId != null) {
191 if (converterError != null) {
192 converterError += " or " + converterId;
193 } else {
194 converterError = converterId;
195 }
196 }
197
198 // PENDING i18n
199 throw new JspException("Can't create class of type:"+
200 "javax.faces.convert.Converter for:"+converterError);
201 }
202
203 ValueHolder vh = (ValueHolder)component;
204 FacesContext context = FacesContext.getCurrentInstance();
205
206 // Register an instance with the appropriate component
207 vh.setConverter(converter);
208
209 // Once the converter has been set, attempt to convert the
210 // incoming "value"
211 Object localValue = vh.getLocalValue();
212 if (localValue instanceof String) {
213 try {
214 localValue = converter.getAsObject(context, (UIComponent)vh, (String) localValue);
215 vh.setValue(localValue);
216 }
217 catch (ConverterException ce) {
218 // PENDING - Ignore? Throw an exception? Set the local
219 // value back to "null" and log a warning?
220 }
221 }
222
223 return (SKIP_BODY);
224
225 }
226
227
228 /**
229 * <p>Release references to any acquired resources.
230 */
231 public void release() {
232
233 this.converterId = null;
234
235 }
236
237
238 // ------------------------------------------------------- Protected Methods
239
240
241 /**
242 * <p>Create and return a new {@link Converter} to be registered
243 * on our surrounding {@link UIComponent}.</p>
244 *
245 * @throws JspException if a new instance cannot be created
246 */
247 protected Converter createConverter()
248 throws JspException {
249
250 FacesContext context = FacesContext.getCurrentInstance();
251 Converter converter = null;
252 ValueExpression vb = null;
253
254 // If "binding" is set, use it to create a converter instance.
255 if (binding != null) {
256 try {
257 vb =
258 context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), binding, Object.class);
259 if (vb != null) {
260 converter = (Converter)vb.getValue(context.getELContext());
261 if (converter != null) {
262 return converter;
263 }
264 }
265 } catch (Exception e) {
266 throw new JspException(e);
267 }
268 }
269 // If "converterId" is set, use it to create the converter
270 // instance. If "converterId" and "binding" are both set, store the
271 // converter instance in the value of the property represented by
272 // the value binding expression.
273 if (converterId != null) {
274 try {
275 String converterIdVal = converterId;
276 if (UIComponentTag.isValueReference(converterId)) {
277 ValueExpression idBinding =
278 context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), converterId, Object.class);
279 converterIdVal = (String) idBinding.getValue(context.getELContext());
280 }
281 converter = context.getApplication().createConverter(converterIdVal);
282 if (converter != null) {
283 if (vb != null) {
284 vb.setValue(context.getELContext(), converter);
285 }
286 }
287 } catch (Exception e) {
288 throw new JspException(e);
289 }
290 }
291 return converter;
292 }
293 }