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.packager;
020
021import org.jpos.iso.ISOComponent;
022import org.jpos.iso.ISOException;
023import org.jpos.iso.ISOField;
024import org.jpos.iso.validator.ISOVException;
025import org.jpos.util.LogEvent;
026import org.jpos.util.Logger;
027
028import java.util.Map;
029
030/**
031 * Tester validating packager for subfields in field 48.
032 *
033 * <p>Title: jPOS</p>
034 * <p>Description: Java Framework for Financial Systems</p>
035 * <p>Copyright: Copyright (c) 2000 jPOS.org.  All rights reserved.</p>
036 * <p>Company: www.jPOS.org</p>
037 * @author Jose Eduardo Leon
038 * @version 1.0
039 */
040public class CTCSubFieldPackager extends ISOBaseValidatingPackager {
041
042    /** Default constructor. */
043    public CTCSubFieldPackager() {
044        super();
045    }
046
047    public byte[] pack ( ISOComponent c ) throws ISOException {
048        try     {
049            Map tab = c.getChildren();
050            StringBuilder sb = new StringBuilder();
051            for ( int i = 0; i < fld.length; i++ ) {
052                ISOField f = (ISOField) tab.get (i);
053                if ( f != null ) {
054                    sb.append ( new String( fld[i].pack( f ) ) );
055                }
056            }
057            return sb.toString().getBytes();
058        }
059        catch ( Exception ex ) {
060            throw new ISOException ( this.getRealm() + ": " +  ex.getMessage(), ex );
061        }
062    }
063
064    public int unpack ( ISOComponent m, byte[] b ) throws ISOException {
065        LogEvent evt = new LogEvent ( this, "unpack" );
066        int consumed = 0;
067        for ( int i=0; consumed < b.length ; i++ ) {
068            ISOComponent c = fld[i].createComponent( i );
069            consumed += fld[i].unpack ( c, b, consumed );
070            if ( logger != null )       {
071                evt.addMessage ("<unpack fld=\"" + i
072                                +"\" packager=\""
073                                +fld[i].getClass().getName()+ "\">");
074                evt.addMessage ("  <value>"
075                                +c.getValue().toString()
076                                + "</value>");
077                evt.addMessage ("</unpack>");
078            }
079            m.set(c);
080        }
081        Logger.log (evt);
082        return consumed;
083    }
084
085    /**
086     * Always return false.
087     * <br><br>
088     **/
089    protected boolean emitBitMap() {
090        return false;
091    }
092
093    public ISOComponent validate( ISOComponent c ) throws org.jpos.iso.ISOException {
094        LogEvent evt = new LogEvent( this, "validate" );
095        try {
096            Map tab = c.getChildren();
097            for ( int i = 0; i < fldVld.length; i++ ) {
098                ISOField f = (ISOField) tab.get (i);
099                if ( f != null )
100                    c.set( fldVld[i].validate( f ) );
101            }
102            return c;
103        } catch ( ISOVException ex ) {
104            if ( !ex.treated() ) {
105                c.set( ex.getErrComponent() );
106                ex.setTreated( true );
107            }
108            evt.addMessage( ex );
109            throw ex;
110        } finally {
111            Logger.log( evt );
112        }
113    }
114}