KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > formmodel > Upload


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.woody.formmodel;
17
18 import java.util.Locale JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
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 JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 /**
32  * A file-uploading Widget. This widget gives access via Woody, to Cocoon's
33  * file upload functionality.
34  *
35  * @author <a HREF="mailto:uv@upaya.co.uk">Upayavira</a>
36  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
37  * @version CVS $Id: Upload.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

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 JavaDoc getId() {
55         return definition.getId();
56     }
57
58     public Object JavaDoc getValue() {
59         return this.part;
60     }
61
62     public void setValue(Object JavaDoc object) {
63         throw new RuntimeException JavaDoc("Cannot manually set the value of an upload widget for field \"" + getFullyQualifiedId() + "\"");
64     }
65
66     public void readFromRequest(FormContext formContext) {
67         Object JavaDoc obj = formContext.getRequest().get(getFullyQualifiedId());
68         
69         // If the request object is a Part, keep it
70
if (obj instanceof Part) {
71             Part requestPart = (Part)obj;
72             if (this.part != null) {
73                 // Replace the current part
74
this.part.dispose();
75             }
76         
77             // Keep the request part
78
requestPart.setDisposeWithRequest(false);
79             this.part = requestPart;
80             this.validationError = null;
81             
82         // If it's not a part and not null, clear any existing value
83
// We also check if we're the submit widget, as a result of clicking the "..." button
84
} else if (obj != null || getForm().getSubmitWidget() == this){
85             // Clear the part, if any
86
if (this.part != null) {
87                 this.part.dispose();
88                 this.part = null;
89             }
90             this.validationError = null;
91         }
92         
93         // And keep the current state if the parameter doesn't exist or is null
94
}
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 JavaDoc mimeTypes = this.uploadDefinition.getMimeTypes();
103             if (mimeTypes != null) {
104                 StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(this.uploadDefinition.getMimeTypes(), ", ");
105                 this.validationError = new ValidationError(new I18nMessage("upload.invalid-type", Constants.I18N_CATALOGUE));
106                 String JavaDoc 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     /**
121      * Returns the validation error, if any. There will always be a validation error in case the
122      * {@link #validate(FormContext)} method returned false.
123      */

124     public ValidationError getValidationError() {
125         return validationError;
126     }
127     
128     /**
129      * Set a validation error on this field. This allows fields to be externally marked as invalid by
130      * application logic.
131      *
132      * @param error the validation error
133      */

134     public void setValidationError(ValidationError error) {
135         this.validationError = error;
136     }
137
138     private static final String JavaDoc FIELD_EL = "upload";
139     private static final String JavaDoc VALUE_EL = "value";
140     private static final String JavaDoc VALIDATION_MSG_EL = "validation-message";
141
142     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
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 JavaDoc name = (String JavaDoc)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         // validation message element: only present if the value is not valid
159
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         // the display data
166
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 JavaDoc contentHandler) throws SAXException JavaDoc {
172         definition.generateLabel(contentHandler);
173     }
174 }
175
Popular Tags