001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.iso; 020 021import org.jpos.core.Configuration; 022import org.jpos.core.ConfigurationException; 023import org.jpos.iso.validator.ISOVException; 024 025/** 026 * Validator for ASCII numeric fields. By default radix is 10. 027 * <p>Title: jPOS</p> 028 * <p>Description: Java Framework for Financial Systems</p> 029 * <p>Copyright: Copyright (c) 2000 jPOS.org. All rights reserved.</p> 030 * <p>Company: www.jPOS.org</p> 031 * @author Jose Eduardo Leon 032 * @version 1.0 033 */ 034public class IVA_NUM extends ISOFieldValidator { 035 036 /** Default constructor. */ 037 public IVA_NUM( ) { 038 super( ); 039 } 040 041 /** 042 * radix = 10. 043 * @param Description Brief description. 044 */ 045 public IVA_NUM( String Description ) { 046 super( Description ); 047 } 048 049 /** 050 * Constructs a validator with the given description and numeric radix. 051 * 052 * @param Description Brief description. 053 * @param radix numeric radix 054 */ 055 public IVA_NUM( String Description, int radix ) { 056 super( Description ); 057 this.radix = radix; 058 } 059 060 /** 061 * Constructs a validator with a maximum length. 062 * 063 * @param maxLen maximum acceptable length 064 * @param Description Brief description. 065 */ 066 public IVA_NUM( int maxLen, String Description ) { 067 super( maxLen, Description ); 068 } 069 070 /** 071 * Constructs a validator with a maximum length and numeric radix. 072 * 073 * @param maxLen maximum acceptable length 074 * @param Description Brief description. 075 * @param radix numeric radix 076 */ 077 public IVA_NUM( int maxLen, String Description, int radix ) { 078 super( maxLen, Description ); 079 this.radix = radix; 080 } 081 082 /** 083 * Create the validator. Radix is 10. 084 * @param minLen min length. 085 * @param maxLen max length 086 * @param Description Validator description 087 */ 088 public IVA_NUM( int minLen, int maxLen, String Description ) { 089 super( minLen, maxLen, Description ); 090 } 091 092 /** 093 * Create the validator 094 * @param minLen min length. 095 * @param maxLen max length 096 * @param Description Validator description 097 * @param radix numeric radix for numeric validation 098 */ 099 public IVA_NUM( int minLen, int maxLen, String Description, int radix ) { 100 super( minLen, maxLen, Description ); 101 this.radix = radix; 102 } 103 104 /** 105 * Constructs a validator with a custom break-on-error flag and description. 106 * 107 * @param breakOnError if {@code true}, validation throws on the first error 108 * @param Description Brief description. 109 */ 110 public IVA_NUM( boolean breakOnError, String Description ) { 111 this( Description ); 112 this.breakOnError = breakOnError; 113 } 114 115 /** 116 * Constructs a validator with a custom break-on-error flag, description, and radix. 117 * 118 * @param breakOnError if {@code true}, validation throws on the first error 119 * @param Description Brief description. 120 * @param radix numeric radix 121 */ 122 public IVA_NUM( boolean breakOnError, String Description, int radix ) { 123 this( Description, radix ); 124 this.breakOnError = breakOnError; 125 } 126 127 /** 128 * Constructs a validator with a custom break-on-error flag and a maximum length. 129 * 130 * @param breakOnError if {@code true}, validation throws on the first error 131 * @param maxLen maximum acceptable length 132 * @param Description Brief description. 133 */ 134 public IVA_NUM( boolean breakOnError, int maxLen, String Description ) { 135 this( maxLen, Description ); 136 this.breakOnError = breakOnError; 137 } 138 139 /** 140 * Constructs a validator with a custom break-on-error flag, max length, and radix. 141 * 142 * @param breakOnError if {@code true}, validation throws on the first error 143 * @param maxLen maximum acceptable length 144 * @param Description Brief description. 145 * @param radix numeric radix 146 */ 147 public IVA_NUM( boolean breakOnError, int maxLen, String Description, int radix ) { 148 this( maxLen, Description, radix ); 149 this.breakOnError = breakOnError; 150 } 151 152 /** 153 * Constructs a validator with a custom break-on-error flag and explicit length bounds. 154 * 155 * @param breakOnError if {@code true}, validation throws on the first error 156 * @param minLen minimum acceptable length 157 * @param maxLen maximum acceptable length 158 * @param Description Brief description. 159 */ 160 public IVA_NUM( boolean breakOnError, int minLen, int maxLen, String Description ) { 161 this( minLen, maxLen, Description ); 162 this.breakOnError = breakOnError; 163 } 164 165 /** 166 * Constructs a validator with a custom break-on-error flag, length bounds, and radix. 167 * 168 * @param breakOnError if {@code true}, validation throws on the first error 169 * @param minLen minimum acceptable length 170 * @param maxLen maximum acceptable length 171 * @param Description Brief description. 172 * @param radix numeric radix 173 */ 174 public IVA_NUM( boolean breakOnError, int minLen, int maxLen, String Description, int radix ) { 175 this( minLen, maxLen, Description, radix ); 176 this.breakOnError = breakOnError; 177 } 178 179 /** 180 * Configure the validator. @see ISOFieldValidator class. 181 * Take config param "radix" wich specify the numeric radix. 182 * @param cfg configuration instance 183 * @throws ConfigurationException if configuration is invalid 184 */ 185 public void setConfiguration(Configuration cfg) throws ConfigurationException { 186 super.setConfiguration( cfg ); 187 this.radix = cfg.getInt( "radix", 10 ); 188 } 189 190 /** 191 * Validate numeric condition. @see ISOFieldValidator class. 192 * @param f ISOField to validate 193 * @return see validate method in ISOFieldValidator class. 194 * @throws ISOException if any validation error. 195 */ 196 public ISOComponent validate( ISOComponent f ) throws ISOException { 197 ISOField c = (ISOField)f; 198 c = (ISOField)super.validate( c ); 199 if ( !ISOUtil.isNumeric( (String)c.getValue(), this.radix ) ){ 200 ISOVError e = new ISOVError( 201 "Invalid Value Error. " + c.getValue() + 202 " is not a numeric value in radix " + 203 this.radix, getRejCode( ISOVError.ERR_INVALID_VALUE ) ); 204 if ( c instanceof ISOVField ) 205 ((ISOVField)c).addISOVError( e ); 206 else 207 c = new ISOVField( c, e ); 208 if ( breakOnError ) 209 throw new ISOVException ( "Error on field " + c.getKey(), c ); 210 } 211 return c; 212 } 213 214 /** by default is decimal **/ 215 protected int radix = 10; 216}