001    package org.apache.myfaces.tobago.taglib.component;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.apache.myfaces.tobago.apt.annotation.Tag;
022    import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
023    import org.apache.myfaces.tobago.validator.FileItemValidator;
024    
025    import javax.faces.webapp.ValidatorTag;
026    import javax.faces.validator.Validator;
027    import javax.servlet.jsp.JspException;
028    
029    /*
030     * Date: Oct 30, 2006
031     * Time: 11:07:35 PM
032     */
033    
034    /**
035     * Register an FileItemValidator instance on the UIComponent
036     * associated with the closest parent UIComponent custom action.
037     */
038    @Tag(name = "validateFileItem")
039    public class FileItemValidatorTag extends ValidatorTag {
040    
041      private static final long serialVersionUID = -1461244883146997440L;
042    
043      private String maxSize;
044      private String contentType;
045    
046      public String getMaxSize() {
047        return maxSize;
048      }
049    
050      @TagAttribute()
051      public void setMaxSize(String maxSize) {
052        this.maxSize = maxSize;
053      }
054    
055      public String getContentType() {
056        return contentType;
057      }
058    
059      @TagAttribute()
060      public void setContentType(String contentType) {
061        this.contentType = contentType;
062      }
063    
064      protected Validator createValidator() throws JspException {
065        setValidatorId(FileItemValidator.VALIDATOR_ID);
066        FileItemValidator validator = (FileItemValidator) super.createValidator();
067    
068        if (maxSize != null) {
069          try {
070            validator.setMaxSize(Integer.parseInt(maxSize));
071          } catch (NumberFormatException e) {
072            // ignore
073          }
074        }
075        if (contentType != null) {
076          validator.setContentType(StringUtils.split(contentType, ", "));
077        }
078        return validator;
079      }
080    
081    
082      public void release() {
083        super.release();
084        maxSize = null;
085        contentType = null;
086      }
087    
088    }