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.core.Configurable; 024import org.jpos.iso.validator.ISOVException; 025 026/** 027 * Validator for ISOField components. 028 * <p>Title: jPOS</p> 029 * <p>Description: Java Framework for Financial Systems</p> 030 * <p>Copyright: Copyright (c) 2000 jPOS.org. All rights reserved.</p> 031 * <p>Company: www.jPOS.org</p> 032 * @author Jose Eduardo Leon 033 * @version 1.0 034 */ 035public class ISOFieldValidator implements Configurable, ISOValidator { 036 037 public ISOFieldValidator( ) { 038 description = ""; 039 } 040 041 public ISOFieldValidator( String Description ) { 042 description = Description; 043 } 044 045 public ISOFieldValidator( int maxLen, String Description ) { 046 description = Description; 047 this.minLen = 0; 048 this.maxLen = maxLen; 049 } 050 051 public ISOFieldValidator( int minLen, int maxLen, String Description ) { 052 description = Description; 053 this.minLen = minLen; this.maxLen = maxLen; 054 } 055 056 public ISOFieldValidator( boolean breakOnError, int minLen, int maxLen, String Description ) { 057 this( minLen, maxLen, Description ); 058 this.breakOnError = breakOnError; 059 } 060 061 public ISOFieldValidator( boolean breakOnError, int maxLen, String Description ) { 062 this( maxLen, Description ); 063 this.breakOnError = breakOnError; 064 } 065 066 public ISOFieldValidator( boolean breakOnError, String Description ) { 067 this( Description ); 068 this.breakOnError = breakOnError; 069 } 070 071 /** 072 * Create a validator instance specifying breaking if any error 073 * during validation process id found. 074 * @param breakOnError break condition 075 */ 076 public ISOFieldValidator( boolean breakOnError ) { 077 this(); 078 this.breakOnError = breakOnError; 079 } 080 081 /** 082 * Default config params are: min-len Minimun length, 083 * max-len Max length, break-on-error break condition. 084 * @param cfg configuration instance 085 * @throws ConfigurationException 086 */ 087 public void setConfiguration(Configuration cfg) throws ConfigurationException { 088 this.cfg = cfg; 089 this.minLen = cfg.getInt( "min-len", 0 ); 090 this.maxLen = cfg.getInt( "max-len", 999999 ); 091 this.breakOnError = cfg.getBoolean( "break-on-error", false ); 092 } 093 094 public void setMaxLength( int maxLen ){ 095 this.maxLen = maxLen; 096 } 097 098 public void setMinLength( int minLen ){ 099 this.minLen = minLen; 100 } 101 102 public void setBreakOnError( boolean breakOnErr ){ 103 this.breakOnError = breakOnErr; 104 } 105 106 public boolean breakOnError(){ 107 return breakOnError; 108 } 109 110 public String getDescription() { 111 return description; 112 } 113 114 public void setDescription(String description) { 115 this.description = description; 116 } 117 118 public void setFieldId ( int f ){ 119 fieldId = f; 120 } 121 122 public int getFieldId(){ 123 return fieldId; 124 } 125 126 /** 127 * Get the reject code for an error type. At this level is empty. 128 * It must be redefined by childs if it is necessary return an 129 * error code for specific errors. ISOVError.ERR_INVALID_LENGTH 130 * and ISOVErro.ERR_INVALID_VALUE are the defaults. 131 * @param ErrType Key for error type. 132 * @return the related error code. At this level return null. 133 */ 134 public String getRejCode( int ErrType ){ 135 /** empty at this level **/ 136 return null; 137 } 138 139 /** 140 * Validate a field component. Default for fields only consider 141 * field length validations. 142 * @param c ISOField component 143 * @return an ISOComponent result of validation process. If there area any 144 * validation error, then an ISOV component replace original c and it's 145 * returned in case of break-on-error condition is false. If break-on-error 146 * is false, then an ISOVException containing the ISOV component is raised. 147 * @throws ISOException if there are some errors during validation. 148 * It contains an ISOV component inside referencing the errors. 149 */ 150 public ISOComponent validate( ISOComponent c ) throws ISOException { 151 ISOField f = (ISOField)c; 152 Object v = f.getValue(); 153 int l=0; 154 if ( v instanceof byte[] ) 155 l = ((byte[])v).length; 156 else if ( v instanceof String ) 157 l = ((String)v).length(); 158 if ( l < minLen || l > maxLen ){ 159 ISOVError e = new ISOVError( 160 "Invalid Length Error. Length must be in [" + minLen + ", " + 161 maxLen + "]. (Current len: " + l + ") ", 162 getRejCode( ISOVError.ERR_INVALID_LENGTH ) ); 163 if ( f instanceof ISOVField ) 164 ((ISOVField)f).addISOVError( e ); 165 else 166 f = new ISOVField( f, e ); 167 if ( breakOnError ) 168 throw new ISOVException ( "Error on field " + f.getKey(), f ); 169 } 170 return f; 171 } 172 173 /** brief field description **/ 174 protected String description; 175 /** field id **/ 176 protected int fieldId; 177 /** field length bounds **/ 178 protected int minLen = 0, maxLen = 999999; 179 /** Flag used to indicate if validat process break on first error or keep an error vector **/ 180 protected boolean breakOnError = false; 181 protected Configuration cfg; 182}