1 package org.apache.turbine.services.intake.model; 2 3 18 19 import org.apache.commons.fileupload.FileItem; 20 21 import org.apache.turbine.services.intake.IntakeException; 22 import org.apache.turbine.services.intake.validator.FileValidator; 23 import org.apache.turbine.services.intake.validator.ValidationException; 24 import org.apache.turbine.services.intake.xmlmodel.XmlField; 25 import org.apache.turbine.util.TurbineRuntimeException; 26 import org.apache.turbine.util.parser.ParameterParser; 27 import org.apache.turbine.util.parser.ValueParser; 28 29 35 public class FileItemField 36 extends Field 37 { 38 39 46 public FileItemField(XmlField field, Group group) 47 throws IntakeException 48 { 49 super(field, group); 50 } 51 52 60 public void setDefaultValue(String prop) 61 { 62 if (prop != null) 63 { 64 throw new TurbineRuntimeException( 65 "Default values are not valid for " 66 + this.getClass().getName()); 67 } 68 69 defaultValue = null; 70 } 71 72 80 public void setEmptyValue(String prop) 81 { 82 if (prop != null) 83 { 84 throw new TurbineRuntimeException( 85 "Empty values are not valid for " 86 + this.getClass().getName()); 87 } 88 89 emptyValue = null; 90 } 91 92 97 protected String getDefaultValidator() 98 { 99 return FileValidator.class.getName(); 100 } 101 102 112 public Field init(ValueParser vp) 113 throws IntakeException 114 { 115 try 116 { 117 super.parser = (ParameterParser) vp; 118 } 119 catch (ClassCastException e) 120 { 121 throw new IntakeException( 122 "FileItemFields can only be used with ParameterParser"); 123 } 124 125 validFlag = true; 126 127 if (parser.containsKey(getKey())) 128 { 129 setFlag = true; 130 validate(); 131 } 132 133 initialized = true; 134 return this; 135 } 136 137 142 protected boolean validate() 143 { 144 ParameterParser pp = (ParameterParser) super.parser; 145 if (isMultiValued) 146 { 147 FileItem[] ss = pp.getFileItems(getKey()); 148 if (ss.length == 0) 151 { 152 setFlag = false; 153 } 154 155 if (validator != null) 156 { 157 for (int i = 0; i < ss.length; i++) 158 { 159 try 160 { 161 ((FileValidator) validator).assertValidity(ss[i]); 162 } 163 catch (ValidationException ve) 164 { 165 setMessage(ve.getMessage()); 166 } 167 } 168 } 169 170 if (setFlag && validFlag) 171 { 172 doSetValue(); 173 } 174 } 175 else 176 { 177 FileItem s = pp.getFileItem(getKey()); 178 if (s == null || s.getSize() == 0) 179 { 180 setFlag = false; 181 } 182 183 if (validator != null) 184 { 185 try 186 { 187 ((FileValidator) validator).assertValidity(s); 188 189 if (setFlag) 190 { 191 doSetValue(); 192 } 193 } 194 catch (ValidationException ve) 195 { 196 setMessage(ve.getMessage()); 197 } 198 } 199 else if (setFlag) 200 { 201 doSetValue(); 202 } 203 } 204 205 return validFlag; 206 } 207 208 211 protected void doSetValue() 212 { 213 ParameterParser pp = (ParameterParser) super.parser; 214 if (isMultiValued) 215 { 216 setTestValue(pp.getFileItems(getKey())); 217 } 218 else 219 { 220 setTestValue(pp.getFileItem(getKey())); 221 } 222 } 223 } 224 | Popular Tags |