001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.form.validator; 016 017 import org.apache.tapestry.IMarkupWriter; 018 import org.apache.tapestry.IRequestCycle; 019 import org.apache.tapestry.form.FormComponentContributorContext; 020 import org.apache.tapestry.form.IFormComponent; 021 import org.apache.tapestry.form.ValidationMessages; 022 import org.apache.tapestry.form.translator.NumberTranslator; 023 import org.apache.tapestry.json.JSONLiteral; 024 import org.apache.tapestry.json.JSONObject; 025 import org.apache.tapestry.valid.ValidationConstants; 026 import org.apache.tapestry.valid.ValidationConstraint; 027 import org.apache.tapestry.valid.ValidationStrings; 028 import org.apache.tapestry.valid.ValidatorException; 029 030 import java.text.DecimalFormat; 031 import java.text.DecimalFormatSymbols; 032 import java.util.Locale; 033 034 /** 035 * Validates that the input value is not larger than a particular maximum value. 036 * 037 * @author Howard Lewis Ship 038 * @since 4.0 039 */ 040 public class Max extends BaseValidator 041 { 042 private double _max; 043 044 public Max() 045 { 046 } 047 048 public Max(String initializer) 049 { 050 super(initializer); 051 } 052 053 /** 054 * Does comparison based on the {@link Number#doubleValue()}. 055 */ 056 057 public void validate(IFormComponent field, ValidationMessages messages, Object object) 058 throws ValidatorException 059 { 060 Number value = (Number) object; 061 062 if (value.doubleValue() > _max) 063 throw new ValidatorException(buildMessage(messages, field), 064 ValidationConstraint.TOO_LARGE); 065 } 066 067 private String getStringValue(Locale locale, IFormComponent field) 068 { 069 String ret = null; 070 NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class); 071 072 if (translator != null) 073 ret = translator.format(field, locale, new Double(_max)); 074 else 075 ret = String.valueOf(_max); 076 077 return ret; 078 } 079 080 private String buildMessage(ValidationMessages messages, IFormComponent field) 081 { 082 return messages.formatValidationMessage( 083 getMessage(), 084 ValidationStrings.VALUE_TOO_LARGE, 085 new Object[] 086 { field.getDisplayName(), getStringValue(messages.getLocale(), field) }); 087 } 088 089 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, 090 FormComponentContributorContext context, IFormComponent field) 091 { 092 JSONObject profile = context.getProfile(); 093 094 if (!profile.has(ValidationConstants.CONSTRAINTS)) { 095 profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); 096 } 097 JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); 098 099 String maxString = getStringValue(context.getLocale(), field); 100 String grouping = ""; 101 102 DecimalFormatSymbols symbols = null; 103 NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class); 104 105 if (translator != null) { 106 DecimalFormat format = translator.getDecimalFormat(context.getLocale()); 107 108 if (format.isGroupingUsed()) { 109 110 grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator()); 111 grouping += ",groupSize:" + format.getGroupingSize(); 112 } else { 113 114 grouping += ",separator:\"\""; 115 } 116 117 symbols = format.getDecimalFormatSymbols(); 118 } else { 119 120 symbols = new DecimalFormatSymbols(context.getLocale()); 121 } 122 123 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 accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, buildMessage(context, field)); 132 } 133 134 public void setMax(double max) 135 { 136 _max = max; 137 } 138 139 public double getMax() 140 { 141 return _max; 142 } 143 }