1 16 package javax.faces.convert; 17 18 import javax.faces.component.UIComponent; 19 import javax.faces.context.FacesContext; 20 21 36 public class IntegerConverter 37 implements Converter 38 { 39 private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.IntegerConverter.CONVERSION"; 40 41 public static final String CONVERTER_ID = "javax.faces.Integer"; 43 44 public IntegerConverter() 46 { 47 } 48 49 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) 51 { 52 if (facesContext == null) throw new NullPointerException ("facesContext"); 53 if (uiComponent == null) throw new NullPointerException ("uiComponent"); 54 55 if (value != null) 56 { 57 value = value.trim(); 58 if (value.length() > 0) 59 { 60 try 61 { 62 return Integer.valueOf(value); 63 } 64 catch (NumberFormatException e) 65 { 66 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, 67 CONVERSION_MESSAGE_ID, 68 new Object []{uiComponent.getId(),value}), e); 69 } 70 } 71 } 72 return null; 73 } 74 75 public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) 76 { 77 if (facesContext == null) throw new NullPointerException ("facesContext"); 78 if (uiComponent == null) throw new NullPointerException ("uiComponent"); 79 80 if (value == null) 81 { 82 return ""; 83 } 84 if (value instanceof String ) 85 { 86 return (String )value; 87 } 88 try 89 { 90 return Integer.toString(((Number )value).intValue()); 91 } 92 catch (Exception e) 93 { 94 throw new ConverterException(e); 95 } 96 } 97 } 98 | Popular Tags |