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    /** Default constructor. */
038    public ISOFieldValidator( ) {
039        description = "";
040    }
041
042    /** Constructs a validator with the given description.
043     * @param Description the field description
044     */
045    public ISOFieldValidator( String Description ) {
046        description = Description;
047    }
048
049    /** Constructs a validator with max length and description.
050     * @param maxLen field maximum length
051     * @param Description field description
052     */
053    public ISOFieldValidator( int maxLen, String Description ) {
054        description = Description;
055        this.minLen = 0;
056        this.maxLen = maxLen;
057    }
058
059    /** Constructs a validator with min/max length and description.
060     * @param minLen minimum field length
061     * @param maxLen maximum field length
062     * @param Description field description
063     */
064    public ISOFieldValidator( int minLen, int maxLen, String Description ) {
065        description = Description;
066        this.minLen = minLen;  this.maxLen = maxLen;
067    }
068
069    /** Full constructor with all options.
070     * @param breakOnError if true, stop validation on first error
071     * @param minLen minimum field length
072     * @param maxLen maximum field length
073     * @param Description field description
074     */
075    public ISOFieldValidator( boolean breakOnError, int minLen, int maxLen, String Description ) {
076        this( minLen, maxLen, Description );
077        this.breakOnError = breakOnError;
078    }
079
080    /** Constructs a validator with error-break flag, max length and description.
081     * @param breakOnError if true, stop validation on first error
082     * @param maxLen maximum field length
083     * @param Description field description
084     */
085    public ISOFieldValidator( boolean breakOnError, int maxLen, String Description ) {
086        this( maxLen, Description );
087        this.breakOnError = breakOnError;
088    }
089
090    /** Constructs a validator with break-on-error flag and description.
091     * @param breakOnError if true, stop on first error
092     * @param Description field description
093     */
094    public ISOFieldValidator( boolean breakOnError, String Description ) {
095        this( Description );
096        this.breakOnError = breakOnError;
097    }
098
099    /**
100     * Create a validator instance specifying breaking if any error
101     * during validation process id found.
102     * @param breakOnError break condition
103     */
104    public ISOFieldValidator( boolean breakOnError ) {
105        this();
106        this.breakOnError = breakOnError;
107    }
108
109    /**
110     * Default config params are: min-len Minimun length,
111     * max-len Max length, break-on-error break condition.
112     * @param cfg configuration instance
113     * @throws ConfigurationException if configuration is invalid
114     */
115    public void setConfiguration(Configuration cfg) throws ConfigurationException {
116        this.cfg = cfg;
117        this.minLen =  cfg.getInt( "min-len", 0 );
118        this.maxLen = cfg.getInt( "max-len", 999999 );
119        this.breakOnError = cfg.getBoolean( "break-on-error", false );
120    }
121
122    /** Sets the maximum field length.
123     * @param maxLen maximum length
124     */
125    public void setMaxLength( int maxLen ){
126        this.maxLen = maxLen;
127    }
128
129    /** Sets the minimum field length.
130     * @param minLen minimum length
131     */
132    public void setMinLength( int minLen ){
133        this.minLen = minLen;
134    }
135
136    /** Sets whether to break on first error.
137     * @param breakOnErr if true, stop on first error
138     */
139    public void setBreakOnError( boolean breakOnErr ){
140        this.breakOnError = breakOnErr;
141    }
142
143    /** Returns whether break-on-error is set.
144     * @return true if break-on-error is enabled
145     */
146    public boolean breakOnError(){
147        return breakOnError;
148    }
149
150    /** Returns the field description.
151     * @return the description string
152     */
153    public String getDescription() {
154        return description;
155    }
156
157    /** Sets the field description.
158     * @param description the description
159     */
160    public void setDescription(String description) {
161        this.description = description;
162    }
163
164    /** Sets the field ID.
165     * @param f the field ID
166     */
167    public void setFieldId ( int f ){
168        fieldId = f;
169    }
170
171    /** Returns the field ID.
172     * @return the field ID
173     */
174    public int getFieldId(){
175        return fieldId;
176    }
177
178    /**
179     * Get the reject code for an error type. At this level is empty.
180     * It must be redefined by childs if it is necessary return an
181     * error code for specific errors. ISOVError.ERR_INVALID_LENGTH
182     * and ISOVErro.ERR_INVALID_VALUE are the defaults.
183     * @param ErrType Key for error type.
184     * @return the related error code. At this level return null.
185     */
186    public String getRejCode( int ErrType ){
187        /** empty at this level **/
188        return null;
189    }
190
191    /**
192     * Validate a field component. Default for fields only consider
193     * field length validations.
194     * @param c ISOField component
195     * @return an ISOComponent result of validation process. If there area any
196     * validation error, then an ISOV component replace original c and it's
197     * returned in case of break-on-error condition is false. If break-on-error
198     * is false, then an ISOVException containing the ISOV component is raised.
199     * @throws ISOException if there are some errors during validation.
200     * It contains an ISOV component inside referencing the errors.
201     */
202    public ISOComponent validate( ISOComponent c ) throws ISOException {
203        ISOField f = (ISOField)c;
204        Object v = f.getValue();
205        int l=0;
206        if ( v instanceof byte[] )
207            l = ((byte[])v).length;
208        else if ( v instanceof String )
209            l = ((String)v).length();
210        if ( l < minLen || l > maxLen ){
211            ISOVError e = new ISOVError(
212                    "Invalid Length Error. Length must be in [" + minLen + ", " +
213                    maxLen + "]. (Current len: " + l + ") ",
214                    getRejCode( ISOVError.ERR_INVALID_LENGTH ) );
215            if ( f instanceof ISOVField )
216                ((ISOVField)f).addISOVError( e );
217            else
218                f = new ISOVField( f, e );
219            if ( breakOnError )
220                throw new ISOVException ( "Error on field " + f.getKey(), f );
221        }
222        return f;
223    }
224
225    /** brief field description **/
226    protected String description;
227    /** field id **/
228    protected int fieldId;
229    /** field length bounds **/
230    protected int minLen = 0, maxLen = 999999;
231    /** Flag used to indicate if validat process break on first error or keep an error vector **/
232    protected boolean breakOnError = false;
233    /** The validator configuration. */
234    protected Configuration cfg;
235}