1 package org.apache.turbine.services.intake.model; 2 3 18 19 import java.text.ParseException ; 20 21 import org.apache.commons.lang.StringUtils; 22 import org.apache.turbine.services.intake.IntakeException; 23 import org.apache.turbine.services.intake.validator.BooleanValidator; 24 import org.apache.turbine.services.intake.xmlmodel.XmlField; 25 26 34 public class BooleanField 35 extends Field 36 { 37 public BooleanField(XmlField field, Group group) 38 throws IntakeException 39 { 40 super(field, group); 41 } 42 43 48 public void setDefaultValue(String prop) 49 { 50 defaultValue = null; 51 52 if (prop == null) 53 { 54 return; 55 } 56 57 defaultValue = new Boolean (prop); 58 } 59 60 68 public void setEmptyValue(String prop) 69 { 70 emptyValue = null; 71 72 if (prop == null) 73 { 74 return; 75 } 76 77 emptyValue = new Boolean (prop); 78 } 79 80 90 protected Object getSafeEmptyValue() 91 { 92 if (isMultiValued) 93 { 94 return new boolean[0]; 95 } 96 else 97 { 98 return (null == getEmptyValue()) ? Boolean.FALSE : getEmptyValue(); 99 } 100 } 101 102 107 protected String getDefaultValidator() 108 { 109 return BooleanValidator.class.getName(); 110 } 111 112 115 protected void doSetValue() 116 { 117 if (isMultiValued) 118 { 119 String [] inputs = parser.getStrings(getKey()); 120 boolean[] values = new boolean[inputs.length]; 121 for (int i = 0; i < inputs.length; i++) 122 { 123 values[i] = StringUtils.isNotEmpty(inputs[i]) 124 ? getBoolean(inputs[i]).booleanValue() 125 : ((Boolean ) getEmptyValue()).booleanValue(); 126 } 127 setTestValue(values); 128 } 129 else 130 { 131 String val = parser.getString(getKey()); 132 setTestValue(StringUtils.isNotEmpty(val) ? getBoolean(val) : (Boolean ) getEmptyValue()); 133 } 134 } 135 136 146 private Boolean getBoolean(String stringValue) 147 { 148 Boolean result = null; 149 150 if (validator != null && validator instanceof BooleanValidator) 151 { 152 BooleanValidator bValidator = (BooleanValidator) validator; 153 try 154 { 155 result = bValidator.parse(stringValue); 156 } 157 catch (ParseException e) 158 { 159 } 163 } 164 else 165 { 166 result = new Boolean (stringValue); 167 } 168 169 return result; 170 } 171 172 178 public boolean booleanValue() 179 { 180 boolean result = false; 181 try 182 { 183 result = ((Boolean ) getValue()).booleanValue(); 184 } 185 catch (Exception e) 186 { 187 log.error(e); 188 } 189 return result; 190 } 191 192 } 193 | Popular Tags |