1 16 package org.apache.cocoon.woody.formmodel; 17 18 import java.util.Locale ; 19 import java.util.StringTokenizer ; 20 21 import org.apache.cocoon.servlet.multipart.Part; 22 import org.apache.cocoon.woody.Constants; 23 import org.apache.cocoon.woody.FormContext; 24 import org.apache.cocoon.woody.validation.ValidationError; 25 import org.apache.cocoon.woody.util.I18nMessage; 26 import org.apache.cocoon.woody.validation.ValidationErrorAware; 27 import org.apache.cocoon.xml.AttributesImpl; 28 import org.xml.sax.ContentHandler ; 29 import org.xml.sax.SAXException ; 30 31 39 public class Upload extends AbstractWidget implements ValidationErrorAware { 40 private UploadDefinition uploadDefinition; 41 private Part part; 42 private ValidationError validationError; 43 44 public Upload(UploadDefinition uploadDefinition) { 45 this.uploadDefinition = uploadDefinition; 46 this.setDefinition(uploadDefinition); 47 setLocation(uploadDefinition.getLocation()); 48 } 49 50 public UploadDefinition getUploadDefinition() { 51 return this.uploadDefinition; 52 } 53 54 public String getId() { 55 return definition.getId(); 56 } 57 58 public Object getValue() { 59 return this.part; 60 } 61 62 public void setValue(Object object) { 63 throw new RuntimeException ("Cannot manually set the value of an upload widget for field \"" + getFullyQualifiedId() + "\""); 64 } 65 66 public void readFromRequest(FormContext formContext) { 67 Object obj = formContext.getRequest().get(getFullyQualifiedId()); 68 69 if (obj instanceof Part) { 71 Part requestPart = (Part)obj; 72 if (this.part != null) { 73 this.part.dispose(); 75 } 76 77 requestPart.setDisposeWithRequest(false); 79 this.part = requestPart; 80 this.validationError = null; 81 82 } else if (obj != null || getForm().getSubmitWidget() == this){ 85 if (this.part != null) { 87 this.part.dispose(); 88 this.part = null; 89 } 90 this.validationError = null; 91 } 92 93 } 95 96 public boolean validate(FormContext formContext) { 97 if (this.part == null) { 98 if (this.uploadDefinition.isRequired()) { 99 this.validationError = new ValidationError(new I18nMessage("general.field-required", Constants.I18N_CATALOGUE)); 100 } 101 } else { 102 String mimeTypes = this.uploadDefinition.getMimeTypes(); 103 if (mimeTypes != null) { 104 StringTokenizer tok = new StringTokenizer (this.uploadDefinition.getMimeTypes(), ", "); 105 this.validationError = new ValidationError(new I18nMessage("upload.invalid-type", Constants.I18N_CATALOGUE)); 106 String contentType = this.part.getMimeType(); 107 while (tok.hasMoreTokens()) { 108 if (tok.nextToken().equals(contentType)) { 109 this.validationError = null; 110 } 111 } 112 } else { 113 this.validationError = null; 114 } 115 } 116 117 return validationError == null ? super.validate(formContext) : false; 118 } 119 120 124 public ValidationError getValidationError() { 125 return validationError; 126 } 127 128 134 public void setValidationError(ValidationError error) { 135 this.validationError = error; 136 } 137 138 private static final String FIELD_EL = "upload"; 139 private static final String VALUE_EL = "value"; 140 private static final String VALIDATION_MSG_EL = "validation-message"; 141 142 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 143 AttributesImpl fieldAttrs = new AttributesImpl(); 144 fieldAttrs.addCDATAAttribute("id", getFullyQualifiedId()); 145 fieldAttrs.addCDATAAttribute("required", String.valueOf(uploadDefinition.isRequired())); 146 if (uploadDefinition.getMimeTypes() != null) { 147 fieldAttrs.addCDATAAttribute("mime-types", uploadDefinition.getMimeTypes()); 148 } 149 contentHandler.startElement(Constants.WI_NS, FIELD_EL, Constants.WI_PREFIX_COLON + FIELD_EL, fieldAttrs); 150 151 if (this.part != null) { 152 String name = (String )this.part.getHeaders().get("filename"); 153 contentHandler.startElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL, Constants.EMPTY_ATTRS); 154 contentHandler.characters(name.toCharArray(), 0, name.length()); 155 contentHandler.endElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL); 156 } 157 158 if (validationError != null) { 160 contentHandler.startElement(Constants.WI_NS, VALIDATION_MSG_EL, Constants.WI_PREFIX_COLON + VALIDATION_MSG_EL, Constants.EMPTY_ATTRS); 161 validationError.generateSaxFragment(contentHandler); 162 contentHandler.endElement(Constants.WI_NS, VALIDATION_MSG_EL, Constants.WI_PREFIX_COLON + VALIDATION_MSG_EL); 163 } 164 165 this.definition.generateDisplayData(contentHandler); 167 168 contentHandler.endElement(Constants.WI_NS, FIELD_EL, Constants.WI_PREFIX_COLON + FIELD_EL); 169 } 170 171 public void generateLabel(ContentHandler contentHandler) throws SAXException { 172 definition.generateLabel(contentHandler); 173 } 174 } 175 | Popular Tags |