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.core;
20  
21  import javax.el.ELException;
22  import javax.faces.FacesException;
23  import javax.faces.convert.Converter;
24  import javax.faces.convert.NumberConverter;
25  import javax.faces.view.facelets.ConverterConfig;
26  import javax.faces.view.facelets.ConverterHandler;
27  import javax.faces.view.facelets.FaceletContext;
28  import javax.faces.view.facelets.FaceletException;
29  import javax.faces.view.facelets.MetaRuleset;
30  import javax.faces.view.facelets.TagAttribute;
31  
32  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
33  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
34  
35  /**
36   * Register a NumberConverter instance on the UIComponent associated with the closest parent UIComponent custom action.
37   * <p/> See <a target="_new"
38   * href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/tlddocs/f/convertNumber.html">tag documentation</a>.
39   * 
40   * @author Jacob Hookom
41   * @version $Id$
42   */
43  @JSFFaceletTag(
44          name = "f:convertNumber",
45          bodyContent = "empty", 
46          converterClass="javax.faces.convert.NumberConverter")
47  public final class ConvertNumberHandler extends ConverterHandler
48  {
49  
50      private final TagAttribute locale;
51  
52      /**
53       * @param config
54       */
55      public ConvertNumberHandler(ConverterConfig config)
56      {
57          super(config);
58          this.locale = this.getAttribute("locale");
59      }
60  
61      /**
62       * Returns a new NumberConverter
63       * 
64       * @see NumberConverter
65       * @see org.apache.myfaces.view.facelets.tag.jsf.ConverterHandler#createConverter(javax.faces.view.facelets.FaceletContext)
66       */
67      protected Converter createConverter(FaceletContext ctx) throws FacesException, ELException, FaceletException
68      {
69          return ctx.getFacesContext().getApplication().createConverter(NumberConverter.CONVERTER_ID);
70      }
71  
72      /*
73       * (non-Javadoc)
74       * 
75       * @see org.apache.myfaces.view.facelets.tag.ObjectHandler#setAttributes(javax.faces.view.facelets.FaceletContext, java.lang.Object)
76       */
77      public void setAttributes(FaceletContext ctx, Object obj)
78      {
79          super.setAttributes(ctx, obj);
80          NumberConverter c = (NumberConverter) obj;
81          if (this.locale != null)
82          {
83              c.setLocale(ComponentSupport.getLocale(ctx, this.locale));
84          }
85      }
86  
87      protected MetaRuleset createMetaRuleset(Class type)
88      {
89          return super.createMetaRuleset(type).ignore("locale");
90      }
91  
92  }