Coverage Report - org.apache.tapestry.form.validator.Max
 
Classes in this File Line Coverage Branch Coverage Complexity
Max
0%
0/38
0%
0/14
1.75
 
 1  
 // Copyright 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.form.validator;
 16  
 
 17  
 import org.apache.tapestry.IMarkupWriter;
 18  
 import org.apache.tapestry.IRequestCycle;
 19  
 import org.apache.tapestry.form.FormComponentContributorContext;
 20  
 import org.apache.tapestry.form.IFormComponent;
 21  
 import org.apache.tapestry.form.ValidationMessages;
 22  
 import org.apache.tapestry.form.translator.NumberTranslator;
 23  
 import org.apache.tapestry.json.JSONLiteral;
 24  
 import org.apache.tapestry.json.JSONObject;
 25  
 import org.apache.tapestry.valid.ValidationConstants;
 26  
 import org.apache.tapestry.valid.ValidationConstraint;
 27  
 import org.apache.tapestry.valid.ValidationStrings;
 28  
 import org.apache.tapestry.valid.ValidatorException;
 29  
 
 30  
 import java.text.DecimalFormat;
 31  
 import java.text.DecimalFormatSymbols;
 32  
 import java.util.Locale;
 33  
 
 34  
 /**
 35  
  * Validates that the input value is not larger than a particular maximum value.
 36  
  * 
 37  
  * @author Howard Lewis Ship
 38  
  * @since 4.0
 39  
  */
 40  
 public class Max extends BaseValidator
 41  
 {
 42  
     private double _max;
 43  
 
 44  
     public Max()
 45  0
     {
 46  0
     }
 47  
 
 48  
     public Max(String initializer)
 49  
     {
 50  0
         super(initializer);
 51  0
     }
 52  
     
 53  
     /**
 54  
      * Does comparison based on the {@link Number#doubleValue()}.
 55  
      */
 56  
 
 57  
     public void validate(IFormComponent field, ValidationMessages messages, Object object)
 58  
             throws ValidatorException
 59  
     {
 60  0
         Number value = (Number) object;
 61  
 
 62  0
         if (value.doubleValue() > _max)
 63  0
             throw new ValidatorException(buildMessage(messages, field),
 64  
                     ValidationConstraint.TOO_LARGE);
 65  0
     }
 66  
 
 67  
     private String getStringValue(Locale locale, IFormComponent field)
 68  
     {
 69  0
         String ret = null;
 70  0
         NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class);
 71  
 
 72  0
         if (translator != null)
 73  0
             ret = translator.format(field, locale, new Double(_max));
 74  
         else
 75  0
             ret = String.valueOf(_max);
 76  
 
 77  0
         return ret;
 78  
     }
 79  
 
 80  
     private String buildMessage(ValidationMessages messages, IFormComponent field)
 81  
     {
 82  0
         return messages.formatValidationMessage(
 83  
                 getMessage(),
 84  
                 ValidationStrings.VALUE_TOO_LARGE,
 85  
                 new Object[]
 86  
                         { field.getDisplayName(), getStringValue(messages.getLocale(), field) });
 87  
     }
 88  
 
 89  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 90  
             FormComponentContributorContext context, IFormComponent field)
 91  
     {
 92  0
         JSONObject profile = context.getProfile();
 93  
         
 94  0
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 95  0
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 96  
         }
 97  0
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 98  
 
 99  0
         String maxString = getStringValue(context.getLocale(), field);
 100  0
         String grouping = "";
 101  
 
 102  0
         DecimalFormatSymbols symbols = null;
 103  0
         NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class);
 104  
 
 105  0
         if (translator != null) {
 106  0
             DecimalFormat format = translator.getDecimalFormat(context.getLocale());
 107  
 
 108  0
             if (format.isGroupingUsed()) {
 109  
 
 110  0
                 grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator());
 111  0
                 grouping += ",groupSize:" + format.getGroupingSize();
 112  
             } else {
 113  
 
 114  0
                 grouping += ",separator:\"\"";
 115  
             }
 116  
 
 117  0
             symbols = format.getDecimalFormatSymbols();
 118  0
         } else {
 119  
 
 120  0
             symbols = new DecimalFormatSymbols(context.getLocale());
 121  
         }
 122  
 
 123  0
         accumulateProperty(cons, field.getClientId(),
 124  
                            new JSONLiteral("[tapestry.form.validation.lessThanOrEqual,"
 125  
                                            + JSONObject.quote(maxString)
 126  
                                            + ",{"
 127  
                                            + "decimal:" + JSONObject.quote(symbols.getDecimalSeparator())
 128  
                                            + grouping
 129  
                                            + "}]"));
 130  
         
 131  0
         accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, buildMessage(context, field));
 132  0
     }
 133  
 
 134  
     public void setMax(double max)
 135  
     {
 136  0
         _max = max;
 137  0
     }
 138  
 
 139  
     public double getMax()
 140  
     {
 141  0
         return _max;
 142  
     }
 143  
 }