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      private String maxSize;
042      private String contentType;
043    
044      public String getMaxSize() {
045        return maxSize;
046      }
047    
048      @TagAttribute()
049      public void setMaxSize(String maxSize) {
050        this.maxSize = maxSize;
051      }
052    
053      public String getContentType() {
054        return contentType;
055      }
056    
057      @TagAttribute()
058      public void setContentType(String contentType) {
059        this.contentType = contentType;
060      }
061    
062      protected Validator createValidator() throws JspException {
063        setValidatorId(FileItemValidator.VALIDATOR_ID);
064        FileItemValidator validator = (FileItemValidator) super.createValidator();
065    
066        if (maxSize != null) {
067          try {
068            validator.setMaxSize(Integer.parseInt(maxSize));
069          } catch (NumberFormatException e) {
070            // ignore
071          }
072        }
073        if (contentType != null) {
074          validator.setContentType(contentType);
075        }
076        return validator;
077      }
078    
079    
080      public void release() {
081        super.release();
082        maxSize = null;
083        contentType = null;
084      }
085    
086    }