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