Save This Page
Home » mojarra-1.2_09-b02-FCS-source » javax.faces.convert » [javadoc | source]
    1   /*
    2    * $Id: FloatConverter.java,v 1.20 2007/05/18 16:23:12 rlubke 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.convert;
   42   
   43   
   44   import javax.faces.component.UIComponent;
   45   import javax.faces.context.FacesContext;
   46   
   47   
   48   /**
   49    * <p>{@link Converter} implementation for <code>java.lang.Float</code>
   50    * (and float primitive) values.</p>
   51    */
   52   
   53   public class FloatConverter implements Converter {
   54   
   55       // ------------------------------------------------------ Manifest Constants
   56   
   57   
   58       /**
   59        * <p>The standard converter id for this converter.</p>
   60        */
   61       public static final String CONVERTER_ID = "javax.faces.Float";
   62   
   63       /**
   64        * <p>The message identifier of the {@link javax.faces.application.FacesMessage} to be created if
   65        * the conversion to <code>Float</code> fails.  The message format
   66        * string for this message may optionally include the following
   67        * placeholders:
   68        * <ul>
   69        * <li><code>{0}</code> replaced by the unconverted value.</li>
   70        * <li><code>{1}</code> replaced by an example value.</li>
   71        * <li><code>{2}</code> replaced by a <code>String</code> whose value
   72        * is the label of the input component that produced this message.</li>
   73        * </ul></p>
   74        */
   75       public static final String FLOAT_ID =
   76            "javax.faces.converter.FloatConverter.FLOAT";
   77   
   78       /**
   79        * <p>The message identifier of the {@link javax.faces.application.FacesMessage} to be created if
   80        * the conversion of the <code>Float</code> value to
   81        * <code>String</code> fails.   The message format string for this message
   82        * may optionally include the following placeholders:
   83        * <ul>
   84        * <li><code>{0}</code> relaced by the unconverted value.</li>
   85        * <li><code>{1}</code> replaced by a <code>String</code> whose value
   86        * is the label of the input component that produced this message.</li>
   87        * </ul></p>
   88        */
   89       public static final String STRING_ID =
   90            "javax.faces.converter.STRING";
   91   
   92       // ------------------------------------------------------- Converter Methods
   93   
   94   
   95       /**
   96        * @throws ConverterException   {@inheritDoc}
   97        * @throws NullPointerException {@inheritDoc}
   98        */
   99       public Object getAsObject(FacesContext context, UIComponent component,
  100                                 String value) {
  101   
  102           if (context == null || component == null) {
  103               throw new NullPointerException();
  104           }
  105   
  106           // If the specified value is null or zero-length, return null
  107           if (value == null) {
  108               return (null);
  109           }
  110           value = value.trim();
  111           if (value.length() < 1) {
  112               return (null);
  113           }
  114   
  115           try {
  116               return (Float.valueOf(value));
  117           } catch (NumberFormatException nfe) {
  118               throw new ConverterException(MessageFactory.getMessage(
  119                    context, FLOAT_ID, value, "2000000000",
  120                    MessageFactory.getLabel(context, component)));
  121           } catch (Exception e) {
  122               throw new ConverterException(e);
  123           }
  124       }
  125   
  126       /**
  127        * @throws ConverterException   {@inheritDoc}
  128        * @throws NullPointerException {@inheritDoc}
  129        */
  130       public String getAsString(FacesContext context, UIComponent component,
  131                                 Object value) {
  132   
  133           if (context == null || component == null) {
  134               throw new NullPointerException();
  135           }
  136   
  137           // If the specified value is null, return a zero-length String
  138           if (value == null) {
  139               return "";
  140           }
  141   
  142           // If the incoming value is still a string, play nice
  143           // and return the value unmodified
  144           if (value instanceof String) {
  145               return (String) value;
  146           }
  147   
  148           try {
  149               return (Float.toString(((Number) value).floatValue()));
  150           } catch (Exception e) {
  151               throw new ConverterException(MessageFactory.getMessage(
  152                    context, STRING_ID, value,
  153                    MessageFactory.getLabel(context, component)), e);
  154           }
  155       }
  156   }

Save This Page
Home » mojarra-1.2_09-b02-FCS-source » javax.faces.convert » [javadoc | source]