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.iso.validator.ISOVException;
022
023/**
024 * Validator for ASCII alphanumeric fields.
025 *
026 * <p>Title: jPOS</p>
027 * <p>Description: Java Framework for Financial Systems</p>
028 * <p>Copyright: Copyright (c) 2000 jPOS.org.  All rights reserved.</p>
029 * <p>Company: www.jPOS.org</p>
030 * @author Jose Eduardo Leon
031 * @version 1.0
032 */
033public class IVA_ALPHANUM extends ISOFieldValidator {
034
035    public IVA_ALPHANUM() {
036        super();
037    }
038
039    public IVA_ALPHANUM( String Description ) {
040        super( Description );
041    }
042
043    public IVA_ALPHANUM( int minLen, int maxLen, String Description ) {
044        super( minLen, maxLen, Description );
045    }
046
047    public IVA_ALPHANUM( int maxLen, String Description ) {
048        super( maxLen, Description );
049    }
050
051    public IVA_ALPHANUM( boolean breakOnError, String Description ) {
052        this( Description );
053        this.breakOnError = breakOnError;
054    }
055
056    public IVA_ALPHANUM( boolean breakOnError, int maxLen, String Description ) {
057        this( maxLen, Description );
058        this.breakOnError = breakOnError;
059    }
060
061    public IVA_ALPHANUM( boolean breakOnError, int minLen, int maxLen, String Description ) {
062        this( minLen, maxLen, Description );
063        this.breakOnError = breakOnError;
064    }
065
066    /**
067     * Validate that component has alphanumeric value.
068     *
069     * @see ISOUtil#isAlphaNumeric method
070     */
071    public ISOComponent validate ( ISOComponent f ) throws ISOException {
072        ISOField c = (ISOField)f;
073        try {
074            /** length validation **/
075            c = (ISOField)super.validate( c );
076            /** alphanum validations **/
077            if ( !ISOUtil.isAlphaNumeric( (String)c.getValue() ) ){
078                ISOVError e = new ISOVError( "Invalid Value Error. " + c.getValue() + " is not an alphanumeric value. ", getRejCode( ISOVError.ERR_INVALID_VALUE ) );
079                if ( c instanceof ISOVField )
080                    ((ISOVField)c).addISOVError( e );
081                else
082                    c = new ISOVField( c, e );
083                if ( breakOnError )
084                    throw new ISOVException ( "Error on field " + c.getKey(), c );
085            }
086            return c;
087        }
088        catch (Exception ex) {
089            if ( ex instanceof ISOVException ) throw (ISOVException)ex;
090            return c;
091        }
092    }
093}