Coverage Report - org.apache.tapestry.form.validator.Min
 
Classes in this File Line Coverage Branch Coverage Complexity
Min
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  
  * Expects the object to be a number, and checks that the value not smaller than a specified value.
 36  
  *
 37  
  * @author Howard Lewis Ship
 38  
  * @since 4.0
 39  
  */
 40  
 public class Min extends BaseValidator
 41  
 {
 42  
     private double _min;
 43  
 
 44  
     public Min()
 45  0
     {
 46  0
     }
 47  
 
 48  
     public Min(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 (_min > value.doubleValue())
 63  0
             throw new ValidatorException(buildMessage(messages, field), ValidationConstraint.TOO_SMALL);
 64  0
     }
 65  
 
 66  
     private String getStringValue(Locale locale, IFormComponent field)
 67  
     {
 68  0
         String ret = null;
 69  0
         NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class);
 70  
 
 71  0
         if (translator != null)
 72  0
             ret = translator.format(field, locale, new Double(_min));
 73  
         else
 74  0
             ret = String.valueOf(_min);
 75  
 
 76  0
         return ret;
 77  
     }
 78  
 
 79  
     private String buildMessage(ValidationMessages messages, IFormComponent field)
 80  
     {
 81  0
         return messages.formatValidationMessage(
 82  
                 getMessage(),
 83  
                 ValidationStrings.VALUE_TOO_SMALL,
 84  
                 new Object[] {
 85  
                         field.getDisplayName(), getStringValue(messages.getLocale(), field)
 86  
                 });
 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 minString = 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.greaterThanOrEqual,"
 125  
                                            + JSONObject.quote(minString)
 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 setMin(double min)
 135  
     {
 136  0
         _min = min;
 137  0
     }
 138  
 
 139  
     public double getMin()
 140  
     {
 141  0
         return _min;
 142  
     }
 143  
 }