Coverage Report - javax.faces.convert.BigDecimalConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
BigDecimalConverter
100%
27/27
100%
8/8
3.4
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package javax.faces.convert;
 17  
 
 18  
 import java.math.BigDecimal;
 19  
 import java.util.Locale;
 20  
 
 21  
 import javax.faces.component.UIComponent;
 22  
 import javax.faces.context.FacesContext;
 23  
 import javax.faces.internal.ConvertUtil;
 24  
 import javax.faces.internal.FacesMessageUtil;
 25  
 
 26  
 import org.seasar.framework.util.AssertionUtil;
 27  
 import org.seasar.framework.util.NumberConversionUtil;
 28  
 
 29  
 /**
 30  
  * @author shot
 31  
  */
 32  
 public class BigDecimalConverter implements Converter {
 33  
 
 34  
     public static final String CONVERTER_ID = "javax.faces.BigDecimal";
 35  
 
 36  2
     public static final String CONVERSION_OBJECT_ID = BigDecimalConverter.class
 37  
             .getName()
 38  
             + ".CONVERSION";
 39  
 
 40  1
     public static final String CONVERSION_STRING_ID = BigDecimalConverter.class
 41  
             .getName()
 42  
             + ".CONVERSION_STRING";
 43  
 
 44  16
     public BigDecimalConverter() {
 45  16
     }
 46  
 
 47  
     public Object getAsObject(FacesContext context, UIComponent component,
 48  
             String value) throws ConverterException {
 49  7
         AssertionUtil.assertNotNull("FacesContext", context);
 50  6
         AssertionUtil.assertNotNull("UIComponent", component);
 51  5
         if (value == null) {
 52  1
             return null;
 53  
         }
 54  4
         value = value.trim();
 55  4
         if (value.length() < 1) {
 56  1
             return null;
 57  
         }
 58  3
         Locale locale = context.getViewRoot().getLocale();
 59  3
         value = NumberConversionUtil.removeDelimeter(value, locale);
 60  
         try {
 61  3
             return new BigDecimal(value);
 62  1
         } catch (NumberFormatException e) {
 63  1
             Object[] args = ConvertUtil.createExceptionMessageArgs(component,
 64  
                     value);
 65  1
             throw new ConverterException(FacesMessageUtil.getMessage(context,
 66  
                     getObjectMessageId(), args), e);
 67  
         }
 68  
     }
 69  
 
 70  
     public String getAsString(FacesContext context, UIComponent component,
 71  
             Object value) throws ConverterException {
 72  6
         AssertionUtil.assertNotNull("FacesContext", context);
 73  5
         AssertionUtil.assertNotNull("UIComponent", component);
 74  4
         if (value == null) {
 75  1
             return "";
 76  
         }
 77  
         try {
 78  3
             return (value instanceof String) ? (String) value
 79  
                     : ((BigDecimal) value).toString();
 80  1
         } catch (Exception e) {
 81  1
             Object[] args = ConvertUtil.createExceptionMessageArgs(component,
 82  
                     value);
 83  1
             throw new ConverterException(FacesMessageUtil.getMessage(context,
 84  
                     getStringMessageId(), args), e);
 85  
         }
 86  
     }
 87  
 
 88  
     protected String getObjectMessageId() {
 89  1
         return CONVERSION_OBJECT_ID;
 90  
     }
 91  
 
 92  
     protected String getStringMessageId() {
 93  1
         return CONVERSION_STRING_ID;
 94  
     }
 95  
 
 96  
 }