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.validator;
020
021import org.jpos.iso.*;
022import org.jpos.util.LogEvent;
023import org.jpos.util.Logger;
024
025/**
026 * Tester to validate msg.
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 MSGTEST extends ISOBaseValidator {
035
036    public MSGTEST() {
037        super();
038    }
039
040    public MSGTEST( boolean breakOnError ) {
041        super(breakOnError);
042    }
043
044    private String makeStrFromArray( int[] validFields ){
045        if ( validFields == null ) return null;
046        StringBuilder result = new StringBuilder();
047        for (int validField : validFields) {
048            result.append(validField);
049            result.append(", ");
050        }
051        result.delete( result.length()-2, result.length()-1 );
052        return result.toString(  );
053    }
054
055    public ISOComponent validate(ISOComponent m) throws org.jpos.iso.ISOException {
056        LogEvent evt = new LogEvent( this, "validate" );
057        try {
058            super.validate ( m );
059            ISOMsg msg = (ISOMsg)m;
060            int[] validFields = { 3,7,11 };
061            if ( !msg.hasFields( validFields ) ){
062                ISOVError e = new ISOVError( "Fields " + makeStrFromArray( validFields ) + " must appear in msg.", "001" );
063                if ( msg instanceof ISOVMsg )
064                    ((ISOVMsg)msg).addISOVError( e );
065                else
066                    msg = new ISOVMsg( msg, e );
067                if ( breakOnError )
068                    throw new ISOVException ( "Error on msg. " , msg );
069            }
070            return msg;
071        } finally {
072            Logger.log( evt );
073        }
074    }
075}