1 package org.apache.fulcrum.intake.model; 2 3 56 57 import org.apache.commons.fileupload.FileItem; 58 import org.apache.fulcrum.ServiceException; 59 import org.apache.fulcrum.intake.validator.FileValidator; 60 import org.apache.fulcrum.intake.validator.ValidationException; 61 import org.apache.fulcrum.intake.xmlmodel.XmlField; 62 import org.apache.fulcrum.parser.ParameterParser; 63 import org.apache.fulcrum.parser.ValueParser; 64 65 70 public class FileItemField 71 extends Field 72 { 73 public FileItemField(XmlField field, Group group) 74 throws Exception 75 { 76 super(field, group); 77 } 78 79 82 83 protected void setDefaultValue(String prop) 84 { 85 defaultValue = prop; 86 } 87 88 93 protected String getDefaultValidator() 94 { 95 return "org.apache.fulcrum.intake.validator.FileValidator"; 96 } 97 98 108 public Field init(ValueParser vp) 109 throws ServiceException 110 { 111 try 112 { 113 super.pp = (ParameterParser) vp; 114 } 115 catch (ClassCastException e) 116 { 117 throw new ServiceException( 118 "FileItemFields can only be used with ParameterParser"); 119 } 120 121 valid_flag = true; 122 123 if ( pp.containsKey(getKey()) ) 124 { 125 set_flag = true; 126 validate(pp); 127 } 128 129 initialized = true; 130 return this; 131 } 132 133 136 protected boolean validate() 137 { 139 ParameterParser pp = (ParameterParser) super.pp; 140 if ( isMultiValued ) 141 { 142 FileItem[] ss = pp.getFileItems(getKey()); 143 if ( ss.length == 0 ) 146 { 147 set_flag = false; 148 } 149 150 if ( validator != null ) 151 { 152 for (int i=0; i<ss.length; i++) 153 { 154 try 155 { 156 ((FileValidator)validator).assertValidity(ss[i]); 157 } 158 catch (ValidationException ve) 159 { 160 setMessage(ve.getMessage()); 161 } 162 } 163 } 164 165 if ( set_flag && valid_flag ) 166 { 167 doSetValue(pp); 168 } 169 170 } 171 else 172 { 173 FileItem s = pp.getFileItem(getKey()); 174 if ( s == null || s.getSize() == 0 ) 175 { 176 set_flag = false; 177 } 178 179 if ( validator != null ) 180 { 181 try 182 { 183 ((FileValidator)validator).assertValidity(s); 184 185 if ( set_flag ) 186 { 187 doSetValue(pp); 188 } 189 } 190 catch (ValidationException ve) 191 { 192 setMessage(ve.getMessage()); 193 } 194 } 195 else if ( set_flag ) 196 { 197 doSetValue(pp); 198 } 199 } 200 201 return valid_flag; 202 } 203 204 207 protected void doSetValue() 208 { 209 ParameterParser pp = (ParameterParser) super.pp; 210 if ( isMultiValued ) 211 { 212 setTestValue(pp.getFileItems(getKey())); 213 } 214 else 215 { 216 setTestValue(pp.getFileItem(getKey())); 217 } 218 } 219 } 220 | Popular Tags |