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.myfaces.tobago.apt.annotation.Tag;
021    import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
022    import org.apache.myfaces.tobago.validator.FileItemValidator;
023    
024    import javax.faces.webapp.ValidatorTag;
025    import javax.faces.validator.Validator;
026    import javax.servlet.jsp.JspException;
027    
028    /*
029     * Created by IntelliJ IDEA.
030     * User: bommel
031     * Date: Oct 30, 2006
032     * Time: 11:07:35 PM
033     */
034    
035    /**
036     * Register an FileItemValidator instance on the UIComponent
037     * associated with the closest parent UIComponent custom action.
038     */
039    @Tag(name = "validateFileItem")
040    public class FileItemValidatorTag extends ValidatorTag {
041    
042      private static final long serialVersionUID = -1461244883146997440L;
043    
044      private String maxSize;
045      private String contentType;
046    
047      public String getMaxSize() {
048        return maxSize;
049      }
050    
051      @TagAttribute()
052      public void setMaxSize(String maxSize) {
053        this.maxSize = maxSize;
054      }
055    
056      public String getContentType() {
057        return contentType;
058      }
059    
060      @TagAttribute()
061      public void setContentType(String contentType) {
062        this.contentType = contentType;
063      }
064    
065      protected Validator createValidator() throws JspException {
066        setValidatorId(FileItemValidator.VALIDATOR_ID);
067        FileItemValidator validator = (FileItemValidator) super.createValidator();
068    
069        if (maxSize != null) {
070          try {
071            validator.setMaxSize(Integer.parseInt(maxSize));
072          } catch (NumberFormatException e) {
073            // ignore
074          }
075        }
076        if (contentType != null) {
077          validator.setContentType(contentType);
078        }
079        return validator;
080      }
081    
082    
083      public void release() {
084        super.release();
085        maxSize = null;
086        contentType = null;
087      }
088    
089    }