001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.upload.components;
016:
017: import java.util.Locale;
018:
019: import org.apache.tapestry.Binding;
020: import org.apache.tapestry.ComponentResources;
021: import org.apache.tapestry.FieldValidator;
022: import org.apache.tapestry.MarkupWriter;
023: import org.apache.tapestry.ValidationException;
024: import org.apache.tapestry.ValidationTracker;
025: import org.apache.tapestry.annotations.Environmental;
026: import org.apache.tapestry.annotations.Inject;
027: import org.apache.tapestry.annotations.Parameter;
028: import org.apache.tapestry.corelib.base.AbstractField;
029: import org.apache.tapestry.services.FieldValidatorDefaultSource;
030: import org.apache.tapestry.services.FormSupport;
031: import org.apache.tapestry.upload.services.MultipartDecoder;
032: import org.apache.tapestry.upload.services.UploadedFile;
033:
034: /**
035: * A component to upload a file.
036: */
037: public class Upload extends AbstractField {
038: public static final String MULTIPART_ENCTYPE = "multipart/form-data";
039:
040: /**
041: * The uploaded file. Note: This is only guaranteed to be valid while processing the form
042: * submission. Subsequently the content may have been cleaned up.
043: */
044: @Parameter(required=true,principal=true)
045: private UploadedFile _value;
046:
047: /**
048: * The object that will peform input validation. The "validate:" binding prefix is generally
049: * used to provide this object in a declarative fashion.
050: */
051: @Parameter(defaultPrefix="validate")
052: @SuppressWarnings("unchecked")
053: private FieldValidator<Object> _validate = NOOP_VALIDATOR;
054:
055: @Environmental
056: private ValidationTracker _tracker;
057:
058: @Inject
059: private MultipartDecoder _decoder;
060:
061: @Environmental
062: private FormSupport _formSupport;
063:
064: @Inject
065: private FieldValidatorDefaultSource _fieldValidatorDefaultSource;
066:
067: @Inject
068: private ComponentResources _resources;
069:
070: @Inject
071: private Locale _locale;
072:
073: /**
074: * Computes a default value for the "validate" parameter using
075: * {@link FieldValidatorDefaultSource}.
076: */
077: final FieldValidator defaultValidate() {
078: Class type = _resources.getBoundType("value");
079:
080: if (type == null)
081: return null;
082:
083: return _fieldValidatorDefaultSource.createDefaultValidator(
084: this , _resources.getId(), _resources
085: .getContainerMessages(), _locale, type,
086: _resources.getAnnotationProvider("value"));
087: }
088:
089: public Upload() {
090: }
091:
092: // For testing
093: Upload(UploadedFile value, FieldValidator<Object> validate,
094: MultipartDecoder decoder, ValidationTracker tracker) {
095: _value = value;
096: if (validate != null)
097: _validate = validate;
098: _decoder = decoder;
099: _tracker = tracker;
100: }
101:
102: protected void processSubmission(FormSupport formSupport,
103: String elementName) {
104: UploadedFile uploaded = _decoder.getFileUpload(elementName);
105:
106: if (uploaded != null) {
107: if (uploaded.getFileName() == null
108: || uploaded.getFileName().length() == 0)
109: uploaded = null;
110: }
111:
112: try {
113: _validate.validate(uploaded);
114: } catch (ValidationException ex) {
115: _tracker.recordError(this , ex.getMessage());
116: }
117:
118: _value = uploaded;
119: }
120:
121: /**
122: * Render the upload tags.
123: *
124: * @param writer
125: * Writer to output markup
126: */
127: protected void beginRender(MarkupWriter writer) {
128: _formSupport.setEncodingType(MULTIPART_ENCTYPE);
129:
130: writer.element("input", "type", "file", "name",
131: getElementName(), "id", getClientId());
132:
133: _validate.render(writer);
134: getValidationDecorator().insideField(this );
135: }
136:
137: public void afterRender(MarkupWriter writer) {
138: writer.end();
139: }
140:
141: public UploadedFile getValue() {
142: return _value;
143: }
144:
145: Binding defaultValue() {
146: return createDefaultParameterBinding("value");
147: }
148: }
|