Save This Page
Home » mojarra-1.2_09-b02-FCS-source » javax.faces.convert » [javadoc | source]
    1   /*
    2    * $Id: ByteConverter.java,v 1.19 2007/04/27 22:00:07 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.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.Byte</code>
   50    * (and byte primitive) values.</p>
   51    */
   52   
   53   public class ByteConverter 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.Byte";
   62   
   63       /**
   64        * <p>The message identifier of the {@link javax.faces.application.FacesMessage} to be created if
   65        * the conversion to <code>Byte</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 BYTE_ID =
   76            "javax.faces.converter.ByteConverter.BYTE";
   77   
   78       /**
   79        * <p>The message identifier of the {@link javax.faces.application.FacesMessage} to be created if
   80        * the conversion of the <code>Byte</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        * @throws ConverterException   {@inheritDoc}
   96        * @throws NullPointerException {@inheritDoc}
   97        */
   98       public Object getAsObject(FacesContext context, UIComponent component,
   99                                 String value) {
  100   
  101           if (context == null || component == null) {
  102               throw new NullPointerException();
  103           }
  104   
  105           // If the specified value is null or zero-length, return null
  106           if (value == null) {
  107               return (null);
  108           }
  109           value = value.trim();
  110           if (value.length() < 1) {
  111               return (null);
  112           }
  113   
  114           try {
  115               return (Byte.valueOf(value));
  116           } catch (NumberFormatException nfe) {
  117               throw new ConverterException(
  118                    MessageFactory.getMessage(context,
  119                                              BYTE_ID,
  120                                              value,
  121                                              "254",
  122                                              MessageFactory.getLabel(context,
  123                                                                      component)));
  124           } catch (Exception e) {
  125               throw new ConverterException(e);
  126           }
  127       }
  128   
  129       /**
  130        * @throws ConverterException   {@inheritDoc}
  131        * @throws NullPointerException {@inheritDoc}
  132        */
  133       public String getAsString(FacesContext context, UIComponent component,
  134                                 Object value) {
  135   
  136           if (context == null || component == null) {
  137               throw new NullPointerException();
  138           }
  139   
  140           // If the specified value is null, return a zero-length String
  141           if (value == null) {
  142               return "";
  143           }
  144   
  145           // If the incoming value is still a string, play nice
  146           // and return the value unmodified
  147           if (value instanceof String) {
  148               return (String) value;
  149           }
  150   
  151           try {
  152               return (Byte.toString(((Byte) value).byteValue()));
  153           } catch (Exception e) {
  154               throw new ConverterException(
  155                    MessageFactory.getMessage(context,
  156                                              STRING_ID,
  157                                              value,
  158                                              MessageFactory.getLabel(context,
  159                                                                      component)),
  160                                              e);
  161           }
  162       }
  163   }

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