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.SubmittedValueLengthValidator;
023    
024    import javax.faces.validator.Validator;
025    import javax.faces.webapp.ValidatorTag;
026    import javax.servlet.jsp.JspException;
027    
028    /**
029     * Register an SubmittedValueLengthValidator instance on the UIComponent
030     * associated with the closest parent UIComponent custom action.
031     * The standard LengthValidator validate the length on the converted value.toString()
032     * not on the submitted value. Sometime you need to check the length of the submitted value.
033     */
034    @Tag(name = "validateSubmittedValueLength")
035    public class SubmittedValueLengthValidatorTag extends ValidatorTag {
036    
037      private static final long serialVersionUID = 6777040780038715924L;
038    
039      private String minimum;
040      private String maximum;
041    
042      public String getMinimum() {
043        return minimum;
044      }
045    
046      @TagAttribute()
047      public void setMinimum(String minimum) {
048        this.minimum = minimum;
049      }
050    
051      public String getMaximum() {
052        return maximum;
053      }
054    
055      @TagAttribute()
056      public void setMaximum(String maximum) {
057        this.maximum = maximum;
058      }
059    
060      protected Validator createValidator() throws JspException {
061        setValidatorId(SubmittedValueLengthValidator.VALIDATOR_ID);
062        SubmittedValueLengthValidator validator = (SubmittedValueLengthValidator) super.createValidator();
063        if (minimum != null) {
064          try {
065            validator.setMinimum(Integer.parseInt(minimum));
066          } catch (NumberFormatException e) {
067            // ignore
068          }
069        }
070        if (maximum != null) {
071          try {
072            validator.setMaximum(Integer.parseInt(maximum));
073          } catch (NumberFormatException e) {
074            // ignore
075          }
076        }
077        return validator;
078      }
079    
080    
081      public void release() {
082        super.release();
083        minimum = null;
084        maximum = null;
085      }
086    }