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 public IVA_NUM( ) { 037 super( ); 038 } 039 040 /** 041 * radix = 10. 042 * @param Description Brief description. 043 */ 044 public IVA_NUM( String Description ) { 045 super( Description ); 046 } 047 048 public IVA_NUM( String Description, int radix ) { 049 super( Description ); 050 this.radix = radix; 051 } 052 053 public IVA_NUM( int maxLen, String Description ) { 054 super( maxLen, Description ); 055 } 056 057 public IVA_NUM( int maxLen, String Description, int radix ) { 058 super( maxLen, Description ); 059 this.radix = radix; 060 } 061 062 /** 063 * Create the validator. Radix is 10. 064 * @param minLen min length. 065 * @param maxLen max length 066 * @param Description Validator description 067 */ 068 public IVA_NUM( int minLen, int maxLen, String Description ) { 069 super( minLen, maxLen, Description ); 070 } 071 072 /** 073 * Create the validator 074 * @param minLen min length. 075 * @param maxLen max length 076 * @param Description Validator description 077 * @param radix numeric radix for numeric validation 078 */ 079 public IVA_NUM( int minLen, int maxLen, String Description, int radix ) { 080 super( minLen, maxLen, Description ); 081 this.radix = radix; 082 } 083 084 public IVA_NUM( boolean breakOnError, String Description ) { 085 this( Description ); 086 this.breakOnError = breakOnError; 087 } 088 089 public IVA_NUM( boolean breakOnError, String Description, int radix ) { 090 this( Description, radix ); 091 this.breakOnError = breakOnError; 092 } 093 094 public IVA_NUM( boolean breakOnError, int maxLen, String Description ) { 095 this( maxLen, Description ); 096 this.breakOnError = breakOnError; 097 } 098 099 public IVA_NUM( boolean breakOnError, int maxLen, String Description, int radix ) { 100 this( maxLen, Description, radix ); 101 this.breakOnError = breakOnError; 102 } 103 104 public IVA_NUM( boolean breakOnError, int minLen, int maxLen, String Description ) { 105 this( minLen, maxLen, Description ); 106 this.breakOnError = breakOnError; 107 } 108 109 public IVA_NUM( boolean breakOnError, int minLen, int maxLen, String Description, int radix ) { 110 this( minLen, maxLen, Description, radix ); 111 this.breakOnError = breakOnError; 112 } 113 114 /** 115 * Configure the validator. @see ISOFieldValidator class. 116 * Take config param "radix" wich specify the numeric radix. 117 * @param cfg configuration instance 118 * @throws ConfigurationException 119 */ 120 public void setConfiguration(Configuration cfg) throws ConfigurationException { 121 super.setConfiguration( cfg ); 122 this.radix = cfg.getInt( "radix", 10 ); 123 } 124 125 /** 126 * Validate numeric condition. @see ISOFieldValidator class. 127 * @param f ISOField to validate 128 * @return see validate method in ISOFieldValidator class. 129 * @throws ISOException if any validation error. 130 */ 131 public ISOComponent validate( ISOComponent f ) throws ISOException { 132 ISOField c = (ISOField)f; 133 c = (ISOField)super.validate( c ); 134 if ( !ISOUtil.isNumeric( (String)c.getValue(), this.radix ) ){ 135 ISOVError e = new ISOVError( 136 "Invalid Value Error. " + c.getValue() + 137 " is not a numeric value in radix " + 138 this.radix, getRejCode( ISOVError.ERR_INVALID_VALUE ) ); 139 if ( c instanceof ISOVField ) 140 ((ISOVField)c).addISOVError( e ); 141 else 142 c = new ISOVField( c, e ); 143 if ( breakOnError ) 144 throw new ISOVException ( "Error on field " + c.getKey(), c ); 145 } 146 return c; 147 } 148 149 /** by default is decimal **/ 150 protected int radix = 10; 151}