| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractFormatValidator |
|
| 1.4285714285714286;1.429 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.validator.routines; | |
| 18 | ||
| 19 | import java.text.Format; | |
| 20 | import java.text.ParsePosition; | |
| 21 | import java.util.Locale; | |
| 22 | import java.io.Serializable; | |
| 23 | ||
| 24 | /** | |
| 25 | * <p>Abstract class for <i>Format</i> based Validation.</p> | |
| 26 | * | |
| 27 | * <p>This is a <i>base</i> class for building Date and Number | |
| 28 | * Validators using format parsing.</p> | |
| 29 | * | |
| 30 | * @version $Revision: 1649191 $ | |
| 31 | * @since Validator 1.3.0 | |
| 32 | */ | |
| 33 | public abstract class AbstractFormatValidator implements Serializable { | |
| 34 | ||
| 35 | private static final long serialVersionUID = -4690687565200568258L; | |
| 36 | ||
| 37 | private final boolean strict; | |
| 38 | ||
| 39 | /** | |
| 40 | * Construct an instance with the specified strict setting. | |
| 41 | * | |
| 42 | * @param strict <code>true</code> if strict | |
| 43 | * <code>Format</code> parsing should be used. | |
| 44 | */ | |
| 45 | 239 | public AbstractFormatValidator(boolean strict) { |
| 46 | 239 | this.strict = strict; |
| 47 | 239 | } |
| 48 | ||
| 49 | /** | |
| 50 | * <p>Indicates whether validated values should adhere | |
| 51 | * strictly to the <code>Format</code> used.</p> | |
| 52 | * | |
| 53 | * <p>Typically implementations of <code>Format</code> | |
| 54 | * ignore invalid characters at the end of the value | |
| 55 | * and just stop parsing. For example parsing a date | |
| 56 | * value of <code>01/01/20x0</code> using a pattern | |
| 57 | * of <code>dd/MM/yyyy</code> will result in a year | |
| 58 | * of <code>20</code> if <code>strict</code> is set | |
| 59 | * to <code>false</code>, whereas setting <code>strict</code> | |
| 60 | * to <code>true</code> will cause this value to fail | |
| 61 | * validation.</p> | |
| 62 | * | |
| 63 | * @return <code>true</code> if strict <code>Format</code> | |
| 64 | * parsing should be used. | |
| 65 | */ | |
| 66 | public boolean isStrict() { | |
| 67 | 1582 | return strict; |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * <p>Validate using the default <code>Locale</code>. | |
| 72 | * | |
| 73 | * @param value The value validation is being performed on. | |
| 74 | * @return <code>true</code> if the value is valid. | |
| 75 | */ | |
| 76 | public boolean isValid(String value) { | |
| 77 | 25 | return isValid(value, (String)null, (Locale)null); |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * <p>Validate using the specified <i>pattern</i>. | |
| 82 | * | |
| 83 | * @param value The value validation is being performed on. | |
| 84 | * @param pattern The pattern used to validate the value against. | |
| 85 | * @return <code>true</code> if the value is valid. | |
| 86 | */ | |
| 87 | public boolean isValid(String value, String pattern) { | |
| 88 | 83 | return isValid(value, pattern, (Locale)null); |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * <p>Validate using the specified <code>Locale</code>. | |
| 93 | * | |
| 94 | * @param value The value validation is being performed on. | |
| 95 | * @param locale The locale to use for the Format, defaults to the default | |
| 96 | * @return <code>true</code> if the value is valid. | |
| 97 | */ | |
| 98 | public boolean isValid(String value, Locale locale) { | |
| 99 | 89 | return isValid(value, (String)null, locale); |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * <p>Validate using the specified pattern and/or <code>Locale</code>. | |
| 104 | * | |
| 105 | * @param value The value validation is being performed on. | |
| 106 | * @param pattern The pattern used to format the value. | |
| 107 | * @param locale The locale to use for the Format, defaults to the default | |
| 108 | * @return <code>true</code> if the value is valid. | |
| 109 | */ | |
| 110 | public abstract boolean isValid(String value, String pattern, Locale locale); | |
| 111 | ||
| 112 | /** | |
| 113 | * <p>Format an object into a <code>String</code> using | |
| 114 | * the default Locale.</p> | |
| 115 | * | |
| 116 | * @param value The value validation is being performed on. | |
| 117 | * @return The value formatted as a <code>String</code>. | |
| 118 | */ | |
| 119 | public String format(Object value) { | |
| 120 | 3 | return format(value, (String)null, (Locale)null); |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * <p>Format an object into a <code>String</code> using | |
| 125 | * the specified pattern.</p> | |
| 126 | * | |
| 127 | * @param value The value validation is being performed on. | |
| 128 | * @param pattern The pattern used to format the value. | |
| 129 | * @return The value formatted as a <code>String</code>. | |
| 130 | */ | |
| 131 | public String format(Object value, String pattern) { | |
| 132 | 12 | return format(value, pattern, (Locale)null); |
| 133 | } | |
| 134 | ||
| 135 | /** | |
| 136 | * <p>Format an object into a <code>String</code> using | |
| 137 | * the specified Locale.</p> | |
| 138 | * | |
| 139 | * @param value The value validation is being performed on. | |
| 140 | * @param locale The locale to use for the Format. | |
| 141 | * @return The value formatted as a <code>String</code>. | |
| 142 | */ | |
| 143 | public String format(Object value, Locale locale) { | |
| 144 | 19 | return format(value, (String)null, locale); |
| 145 | } | |
| 146 | ||
| 147 | /** | |
| 148 | * <p>Format an object using the specified pattern and/or | |
| 149 | * <code>Locale</code>. | |
| 150 | * | |
| 151 | * @param value The value validation is being performed on. | |
| 152 | * @param pattern The pattern used to format the value. | |
| 153 | * @param locale The locale to use for the Format. | |
| 154 | * @return The value formatted as a <code>String</code>. | |
| 155 | */ | |
| 156 | public String format(Object value, String pattern, Locale locale) { | |
| 157 | 24 | Format formatter = getFormat(pattern, locale); |
| 158 | 24 | return format(value, formatter); |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * <p>Format a value with the specified <code>Format</code>.</p> | |
| 163 | * | |
| 164 | * @param value The value to be formatted. | |
| 165 | * @param formatter The Format to use. | |
| 166 | * @return The formatted value. | |
| 167 | */ | |
| 168 | protected String format(Object value, Format formatter) { | |
| 169 | 24 | return formatter.format(value); |
| 170 | } | |
| 171 | ||
| 172 | /** | |
| 173 | * <p>Parse the value with the specified <code>Format</code>.</p> | |
| 174 | * | |
| 175 | * @param value The value to be parsed. | |
| 176 | * @param formatter The Format to parse the value with. | |
| 177 | * @return The parsed value if valid or <code>null</code> if invalid. | |
| 178 | */ | |
| 179 | protected Object parse(String value, Format formatter) { | |
| 180 | ||
| 181 | 1071 | ParsePosition pos = new ParsePosition(0); |
| 182 | 1071 | Object parsedValue = formatter.parseObject(value, pos); |
| 183 | 1071 | if (pos.getErrorIndex() > -1) { |
| 184 | 366 | return null; |
| 185 | } | |
| 186 | ||
| 187 | 705 | if (isStrict() && pos.getIndex() < value.length()) { |
| 188 | 138 | return null; |
| 189 | } | |
| 190 | ||
| 191 | 567 | if (parsedValue != null) { |
| 192 | 567 | parsedValue = processParsedValue(parsedValue, formatter); |
| 193 | } | |
| 194 | ||
| 195 | 567 | return parsedValue; |
| 196 | ||
| 197 | } | |
| 198 | ||
| 199 | /** | |
| 200 | * <p>Process the parsed value, performing any further validation | |
| 201 | * and type conversion required.</p> | |
| 202 | * | |
| 203 | * @param value The parsed object created. | |
| 204 | * @param formatter The Format used to parse the value with. | |
| 205 | * @return The parsed value converted to the appropriate type | |
| 206 | * if valid or <code>null</code> if invalid. | |
| 207 | */ | |
| 208 | protected abstract Object processParsedValue(Object value, Format formatter); | |
| 209 | ||
| 210 | /** | |
| 211 | * <p>Returns a <code>Format</code> for the specified <i>pattern</i> | |
| 212 | * and/or <code>Locale</code>.</p> | |
| 213 | * | |
| 214 | * @param pattern The pattern used to validate the value against or | |
| 215 | * <code>null</code> to use the default for the <code>Locale</code>. | |
| 216 | * @param locale The locale to use for the currency format, system default if null. | |
| 217 | * @return The <code>NumberFormat</code> to created. | |
| 218 | */ | |
| 219 | protected abstract Format getFormat(String pattern, Locale locale); | |
| 220 | ||
| 221 | } |