Coverage Report - org.apache.tapestry.form.validator.Max
 
Classes in this File Line Coverage Branch Coverage Complexity
Max
79% 
86% 
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  1
     {
 46  1
     }
 47  
 
 48  
     public Max(String initializer)
 49  
     {
 50  5
         super(initializer);
 51  5
     }
 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  3
         Number value = (Number) object;
 61  
 
 62  3
         if (value.doubleValue() > _max)
 63  1
             throw new ValidatorException(buildMessage(messages, field),
 64  
                     ValidationConstraint.TOO_LARGE);
 65  2
     }
 66  
 
 67  
     private String getStringValue(Locale locale, IFormComponent field)
 68  
     {
 69  5
         String ret = null;
 70  6
         NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class);
 71  
 
 72  5
         if (translator != null)
 73  0
             ret = translator.format(field, locale, new Double(_max));
 74  
         else
 75  5
             ret = String.valueOf(_max);
 76  
 
 77  5
         return ret;
 78  
     }
 79  
 
 80  
     private String buildMessage(ValidationMessages messages, IFormComponent field)
 81  
     {
 82  3
         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  2
         JSONObject profile = context.getProfile();
 93  
         
 94  2
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 95  2
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 96  
         }
 97  2
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 98  
 
 99  2
         context.addInitializationScript(field, "dojo.require(\"dojo.i18n.number\");");
 100  
 
 101  2
         String maxString = getStringValue(context.getLocale(), field);
 102  2
         String grouping = "";
 103  
 
 104  2
         DecimalFormatSymbols symbols = null;
 105  2
         NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class);
 106  
 
 107  2
         if (translator != null) {
 108  0
             DecimalFormat format = translator.getDecimalFormat(context.getLocale());
 109  
 
 110  0
             if (format.isGroupingUsed()) {
 111  
 
 112  0
                 grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator());
 113  0
                 grouping += ",groupSize:" + format.getGroupingSize();
 114  
             } else {
 115  
 
 116  0
                 grouping += ",separator:\"\"";
 117  
             }
 118  
 
 119  0
             symbols = format.getDecimalFormatSymbols();
 120  0
         } else {
 121  
 
 122  2
             symbols = new DecimalFormatSymbols(context.getLocale());
 123  
         }
 124  
 
 125  2
         accumulateProperty(cons, field.getClientId(),
 126  
                            new JSONLiteral("[tapestry.form.validation.lessThanOrEqual,"
 127  
                                            + JSONObject.quote(maxString)
 128  
                                            + ",{"
 129  
                                            + "decimal:" + JSONObject.quote(symbols.getDecimalSeparator())
 130  
                                            + grouping
 131  
                                            + "}]"));
 132  
         
 133  2
         accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, buildMessage(context, field));
 134  2
     }
 135  
 
 136  
     public void setMax(double max)
 137  
     {
 138  6
         _max = max;
 139  6
     }
 140  
 
 141  
     public double getMax()
 142  
     {
 143  1
         return _max;
 144  
     }
 145  
 }