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