001 // Copyright 2004, 2005 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.form;
016
017 import org.apache.hivemind.HiveMind;
018 import org.apache.tapestry.IForm;
019 import org.apache.tapestry.IMarkupWriter;
020 import org.apache.tapestry.IRequestCycle;
021 import org.apache.tapestry.multipart.MultipartDecoder;
022 import org.apache.tapestry.request.IUploadFile;
023 import org.apache.tapestry.valid.ValidatorException;
024
025 /**
026 * Form element used to upload files. [ <a
027 * href="../../../../../ComponentReference/Upload.html">Component Reference
028 * </a>]
029 * <p>
030 * As of 4.0, this component can be validated.
031 *
032 * @author Howard Lewis Ship
033 * @author Paul Ferraro
034 * @since 1.0.8
035 */
036
037 public abstract class Upload extends AbstractFormComponent implements ValidatableField
038 {
039 public abstract void setFile(IUploadFile file);
040
041 /**
042 * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
043 * org.apache.tapestry.IRequestCycle)
044 */
045 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
046 {
047 // Force the form to use the correct encoding type for file uploads.
048 IForm form = getForm();
049
050 form.setEncodingType("multipart/form-data");
051
052 renderDelegatePrefix(writer, cycle);
053
054 writer.beginEmpty("input");
055 writer.attribute("type", "file");
056 writer.attribute("name", getName());
057
058 if (isDisabled())
059 {
060 writer.attribute("disabled", "disabled");
061 }
062
063 renderIdAttribute(writer, cycle);
064
065 renderDelegateAttributes(writer, cycle);
066
067 getValidatableFieldSupport().renderContributions(this, writer, cycle);
068
069 renderInformalParameters(writer, cycle);
070
071 writer.closeTag();
072
073 renderDelegateSuffix(writer, cycle);
074 }
075
076 /**
077 * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter,
078 * org.apache.tapestry.IRequestCycle)
079 */
080 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
081 {
082 IUploadFile file = getDecoder().getFileUpload(getName());
083
084 if (file != null && HiveMind.isBlank(file.getFileName()))
085 {
086 file = null;
087 }
088
089 try
090 {
091 getValidatableFieldSupport().validate(this, writer, cycle, file);
092
093 setFile(file);
094 }
095 catch (ValidatorException e)
096 {
097 getForm().getDelegate().record(e);
098 }
099 }
100
101 /**
102 * Injected.
103 */
104 public abstract ValidatableFieldSupport getValidatableFieldSupport();
105
106 /**
107 * Injected.
108 */
109 public abstract MultipartDecoder getDecoder();
110
111 /**
112 * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
113 */
114 public boolean isRequired()
115 {
116 return getValidatableFieldSupport().isRequired(this);
117 }
118 }