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