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    /** Default constructor. */
036    public IVA_ALPHANUM() {
037        super();
038    }
039
040    /**
041     * Constructs a validator using only a description.
042     *
043     * @param Description human-readable field description
044     */
045    public IVA_ALPHANUM( String Description ) {
046        super( Description );
047    }
048
049    /**
050     * Constructs a validator with explicit length bounds.
051     *
052     * @param minLen minimum acceptable length
053     * @param maxLen maximum acceptable length
054     * @param Description human-readable field description
055     */
056    public IVA_ALPHANUM( int minLen, int maxLen, String Description ) {
057        super( minLen, maxLen, Description );
058    }
059
060    /**
061     * Constructs a validator with a maximum length.
062     *
063     * @param maxLen maximum acceptable length
064     * @param Description human-readable field description
065     */
066    public IVA_ALPHANUM( int maxLen, String Description ) {
067        super( maxLen, Description );
068    }
069
070    /**
071     * Constructs a validator with a custom break-on-error flag.
072     *
073     * @param breakOnError if {@code true}, validation throws on the first error
074     * @param Description human-readable field description
075     */
076    public IVA_ALPHANUM( boolean breakOnError, String Description ) {
077        this( Description );
078        this.breakOnError = breakOnError;
079    }
080
081    /**
082     * Constructs a validator with a custom break-on-error flag and a maximum length.
083     *
084     * @param breakOnError if {@code true}, validation throws on the first error
085     * @param maxLen maximum acceptable length
086     * @param Description human-readable field description
087     */
088    public IVA_ALPHANUM( boolean breakOnError, int maxLen, String Description ) {
089        this( maxLen, Description );
090        this.breakOnError = breakOnError;
091    }
092
093    /**
094     * Constructs a validator with a custom break-on-error flag and explicit length bounds.
095     *
096     * @param breakOnError if {@code true}, validation throws on the first error
097     * @param minLen minimum acceptable length
098     * @param maxLen maximum acceptable length
099     * @param Description human-readable field description
100     */
101    public IVA_ALPHANUM( boolean breakOnError, int minLen, int maxLen, String Description ) {
102        this( minLen, maxLen, Description );
103        this.breakOnError = breakOnError;
104    }
105
106    /**
107     * Validate that component has alphanumeric value.
108     *
109     * @param f component to validate
110     * @return the validated (possibly wrapped) component
111     * @throws ISOException if {@link #breakOnError} is set and the field is not alphanumeric
112     * @see ISOUtil#isAlphaNumeric method
113     */
114    public ISOComponent validate ( ISOComponent f ) throws ISOException {
115        ISOField c = (ISOField)f;
116        try {
117            /** length validation **/
118            c = (ISOField)super.validate( c );
119            /** alphanum validations **/
120            if ( !ISOUtil.isAlphaNumeric( (String)c.getValue() ) ){
121                ISOVError e = new ISOVError( "Invalid Value Error. " + c.getValue() + " is not an alphanumeric value. ", getRejCode( ISOVError.ERR_INVALID_VALUE ) );
122                if ( c instanceof ISOVField )
123                    ((ISOVField)c).addISOVError( e );
124                else
125                    c = new ISOVField( c, e );
126                if ( breakOnError )
127                    throw new ISOVException ( "Error on field " + c.getKey(), c );
128            }
129            return c;
130        }
131        catch (Exception ex) {
132            if ( ex instanceof ISOVException ) throw (ISOVException)ex;
133            return c;
134        }
135    }
136}