1 package org.apache.turbine.services.intake.model; 2 3 18 19 import java.math.BigDecimal ; 20 21 import java.text.DecimalFormatSymbols ; 22 23 import org.apache.commons.lang.StringUtils; 24 25 import org.apache.turbine.services.intake.IntakeException; 26 import org.apache.turbine.services.intake.validator.BigDecimalValidator; 27 import org.apache.turbine.services.intake.xmlmodel.XmlField; 28 29 36 public class BigDecimalField 37 extends Field 38 { 39 46 public BigDecimalField(XmlField field, Group group) 47 throws IntakeException 48 { 49 super(field, group); 50 } 51 52 57 public void setDefaultValue(String prop) 58 { 59 defaultValue = null; 60 61 if (prop == null) 62 { 63 return; 64 } 65 66 defaultValue = new BigDecimal (prop); 67 } 68 69 77 public void setEmptyValue(String prop) 78 { 79 emptyValue = null; 80 81 if (prop == null) 82 { 83 return; 84 } 85 86 emptyValue = new BigDecimal (prop); 87 } 88 89 94 protected String getDefaultValidator() 95 { 96 return BigDecimalValidator.class.getName(); 97 } 98 99 102 protected void doSetValue() 103 { 104 if (isMultiValued) 105 { 106 String [] inputs = parser.getStrings(getKey()); 107 BigDecimal [] values = new BigDecimal [inputs.length]; 108 for (int i = 0; i < inputs.length; i++) 109 { 110 values[i] = StringUtils.isNotEmpty(inputs[i]) 111 ? canonicalizeDecimalInput(inputs[i]) : (BigDecimal ) getEmptyValue(); 112 } 113 setTestValue(values); 114 } 115 else 116 { 117 String val = parser.getString(getKey()); 118 setTestValue(StringUtils.isNotEmpty(val) ? canonicalizeDecimalInput(val) : (BigDecimal ) getEmptyValue()); 119 } 120 } 121 122 130 protected final BigDecimal canonicalizeDecimalInput(String bigDecimal) 131 { 132 if (getLocale() != null) 133 { 134 DecimalFormatSymbols internal = new DecimalFormatSymbols (); 135 DecimalFormatSymbols user = new DecimalFormatSymbols (getLocale()); 136 137 if (!internal.equals(user)) 138 { 139 bigDecimal = bigDecimal.replace(user.getDecimalSeparator(), 140 internal.getDecimalSeparator()); 141 } 142 } 143 return new BigDecimal (bigDecimal); 144 } 145 } 146 | Popular Tags |