View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.tag.jsf;
20  
21  import java.io.IOException;
22  
23  import javax.el.ELException;
24  import javax.el.ValueExpression;
25  import javax.faces.FacesException;
26  import javax.faces.component.UIComponent;
27  import javax.faces.component.ValueHolder;
28  import javax.faces.context.FacesContext;
29  import javax.faces.convert.Converter;
30  import javax.faces.view.facelets.ConverterConfig;
31  import javax.faces.view.facelets.FaceletContext;
32  import javax.faces.view.facelets.FaceletException;
33  import javax.faces.view.facelets.MetaRuleset;
34  import javax.faces.view.facelets.TagAttribute;
35  import javax.faces.view.facelets.TagConfig;
36  import javax.faces.view.facelets.TagException;
37  
38  import org.apache.myfaces.view.facelets.tag.MetaTagHandlerImpl;
39  
40  /**
41   * Handles setting a Converter instance on a ValueHolder. Will wire all attributes set to the Converter instance
42   * created/fetched. Uses the "binding" attribute for grabbing instances to apply attributes to. <p/> Will only
43   * set/create Converter is the passed UIComponent's parent is null, signifying that it wasn't restored from an existing
44   * tree.
45   * 
46   * @deprecated use javax.faces.view.facelets.ConverterHandler instead
47   * @see javax.faces.webapp.ConverterELTag
48   * @see javax.faces.convert.Converter
49   * @see javax.faces.component.ValueHolder
50   * @author Jacob Hookom
51   * @version $Id$
52   */
53  @Deprecated
54  public class ConvertHandler extends MetaTagHandlerImpl
55  {
56  
57      private final TagAttribute binding;
58  
59      private String converterId;
60  
61      /**
62       * @param config
63       * @deprecated
64       */
65      public ConvertHandler(TagConfig config)
66      {
67          super(config);
68          this.binding = this.getAttribute("binding");
69          this.converterId = null;
70      }
71  
72      public ConvertHandler(ConverterConfig config)
73      {
74          this((TagConfig) config);
75          this.converterId = config.getConverterId();
76      }
77  
78      /**
79       * Set Converter instance on parent ValueHolder if it's not being restored.
80       * <ol>
81       * <li>Cast to ValueHolder</li>
82       * <li>If "binding" attribute was specified, fetch/create and re-bind to expression.</li>
83       * <li>Otherwise, call {@link #createConverter(FaceletContext) createConverter}.</li>
84       * <li>Call {@link ObjectHandler#setAttributes(FaceletContext, Object) setAttributes} on Converter instance.</li>
85       * <li>Set the Converter on the ValueHolder</li>
86       * <li>If the ValueHolder has a localValue, convert it and set the value</li>
87       * </ol>
88       * 
89       * @see ValueHolder
90       * @see Converter
91       * @see #createConverter(FaceletContext)
92       * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
93       */
94      public final void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException,
95              FaceletException, ELException
96      {
97          if (parent == null || !(parent instanceof ValueHolder))
98          {
99              throw new TagException(this.tag, "Parent not an instance of ValueHolder: " + parent);
100         }
101 
102         // only process if it's been created
103         if (parent.getParent() == null)
104         {
105             // cast to a ValueHolder
106             ValueHolder vh = (ValueHolder) parent;
107             ValueExpression ve = null;
108             Converter c = null;
109             if (this.binding != null)
110             {
111                 ve = this.binding.getValueExpression(ctx, Converter.class);
112                 c = (Converter) ve.getValue(ctx);
113             }
114             if (c == null)
115             {
116                 c = this.createConverter(ctx);
117                 if (ve != null)
118                 {
119                     ve.setValue(ctx, c);
120                 }
121             }
122             if (c == null)
123             {
124                 throw new TagException(this.tag, "No Converter was created");
125             }
126             this.setAttributes(ctx, c);
127             vh.setConverter(c);
128             Object lv = vh.getLocalValue();
129             FacesContext faces = ctx.getFacesContext();
130             if (lv instanceof String)
131             {
132                 vh.setValue(c.getAsObject(faces, parent, (String) lv));
133             }
134         }
135     }
136 
137     /**
138      * Create a Converter instance
139      * 
140      * @param ctx
141      *            FaceletContext to use
142      * @return Converter instance, cannot be null
143      */
144     protected Converter createConverter(FaceletContext ctx)
145     {
146         if (this.converterId == null)
147         {
148             throw new TagException(this.tag,
149                                    "Default behavior invoked of requiring a converter-id passed in the constructor, "
150                                    + "must override ConvertHandler(ConverterConfig)");
151         }
152         return ctx.getFacesContext().getApplication().createConverter(this.converterId);
153     }
154 
155     protected MetaRuleset createMetaRuleset(Class type)
156     {
157         return super.createMetaRuleset(type).ignore("binding");
158     }
159 }