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.ValueExpression;
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.ValueHolder;
26  import javax.faces.context.FacesContext;
27  import javax.faces.convert.Converter;
28  import javax.faces.view.ValueHolderAttachedObjectHandler;
29  import javax.faces.view.facelets.ComponentHandler;
30  import javax.faces.view.facelets.ConverterHandler;
31  import javax.faces.view.facelets.FaceletContext;
32  import javax.faces.view.facelets.MetaRuleset;
33  import javax.faces.view.facelets.TagAttribute;
34  import javax.faces.view.facelets.TagException;
35  import javax.faces.view.facelets.TagHandlerDelegate;
36  
37  import org.apache.myfaces.view.facelets.FaceletCompositionContext;
38  import org.apache.myfaces.view.facelets.tag.MetaRulesetImpl;
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   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
47   * @version $Revision: 964310 $ $Date: 2010-07-15 01:17:19 -0500 (Thu, 15 Jul 2010) $
48   *
49   * @since 2.0
50   */
51  public class ConverterTagHandlerDelegate extends TagHandlerDelegate implements ValueHolderAttachedObjectHandler
52  {
53      private ConverterHandler _delegate;
54      
55      public ConverterTagHandlerDelegate(ConverterHandler delegate)
56      {
57          _delegate = delegate;
58      }
59  
60      /**
61       * Set Converter instance on parent ValueHolder if it's not being restored.
62       * <ol>
63       * <li>Cast to ValueHolder</li>
64       * <li>If "binding" attribute was specified, fetch/create and re-bind to expression.</li>
65       * <li>Otherwise, call {@link #createConverter(FaceletContext) createConverter}.</li>
66       * <li>Call {@link ObjectHandler#setAttributes(FaceletContext, Object) setAttributes} on Converter instance.</li>
67       * <li>Set the Converter on the ValueHolder</li>
68       * <li>If the ValueHolder has a localValue, convert it and set the value</li>
69       * </ol>
70       * 
71       * @see ValueHolder
72       * @see Converter
73       * @see #createConverter(FaceletContext)
74       * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
75       */
76      @Override
77      public void apply(FaceletContext ctx, UIComponent parent) throws IOException
78      {
79          // only process if it's been created
80          if (!ComponentHandler.isNew(parent))
81          {
82              return;
83          }
84          if (parent instanceof ValueHolder)
85          {
86              applyAttachedObject(ctx.getFacesContext(), parent);
87          }
88          else if (UIComponent.isCompositeComponent(parent))
89          {
90              if (getFor() == null)
91              {
92                  throw new TagException(_delegate.getTag(), "is nested inside a composite component"
93                          + " but does not have a for attribute.");
94              }
95              FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
96              mctx.addAttachedObjectHandler(parent, _delegate);
97          }
98          else
99          {
100             throw new TagException(_delegate.getTag(), "Parent not composite component or an instance of ValueHolder: " + parent);
101         }      
102     }
103 
104     /**
105      * Create a Converter instance
106      * 
107      * @param ctx
108      *            FaceletContext to use
109      * @return Converter instance, cannot be null
110      */
111     protected Converter createConverter(FaceletContext ctx)
112     {
113         if (_delegate.getConverterId(ctx) == null)
114         {
115             throw new TagException(
116                                    _delegate.getTag(),
117                                    "Default behavior invoked of requiring a converter-id passed in the constructor, must override ConvertHandler(ConverterConfig)");
118         }
119         return ctx.getFacesContext().getApplication().createConverter(_delegate.getConverterId(ctx));
120     }
121 
122     @Override
123     public MetaRuleset createMetaRuleset(Class type)
124     {
125         return new MetaRulesetImpl(_delegate.getTag(), type).ignore("binding");
126     }
127 
128     public void applyAttachedObject(FacesContext context, UIComponent parent)
129     {
130         // Retrieve the current FaceletContext from FacesContext object
131         FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(
132                 FaceletContext.FACELET_CONTEXT_KEY);
133         
134         // cast to a ValueHolder
135         ValueHolder vh = (ValueHolder) parent;
136         ValueExpression ve = null;
137         Converter c = null;
138         if (_delegate.getBinding() != null)
139         {
140             ve = _delegate.getBinding().getValueExpression(faceletContext, Converter.class);
141             c = (Converter) ve.getValue(faceletContext);
142         }
143         if (c == null)
144         {
145             c = this.createConverter(faceletContext);
146             if (ve != null)
147             {
148                 ve.setValue(faceletContext, c);
149             }
150         }
151         if (c == null)
152         {
153             throw new TagException(_delegate.getTag(), "No Converter was created");
154         }
155         _delegate.setAttributes(faceletContext, c);
156         vh.setConverter(c);
157         Object lv = vh.getLocalValue();
158         FacesContext faces = faceletContext.getFacesContext();
159         if (lv instanceof String)
160         {
161             vh.setValue(c.getAsObject(faces, parent, (String) lv));
162         }
163     }
164 
165     public String getFor()
166     {
167         TagAttribute forAttribute = _delegate.getTagAttribute("for");
168         
169         if (forAttribute == null)
170         {
171             return null;
172         }
173         else
174         {
175             return forAttribute.getValue();
176         }
177     }
178 }