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    public CTCSubFieldPackager() {
043        super();
044    }
045
046    public byte[] pack ( ISOComponent c ) throws ISOException {
047        try     {
048            Map tab = c.getChildren();
049            StringBuilder sb = new StringBuilder();
050            for ( int i = 0; i < fld.length; i++ ) {
051                ISOField f = (ISOField) tab.get (i);
052                if ( f != null ) {
053                    sb.append ( new String( fld[i].pack( f ) ) );
054                }
055            }
056            return sb.toString().getBytes();
057        }
058        catch ( Exception ex ) {
059            throw new ISOException ( this.getRealm() + ": " +  ex.getMessage(), ex );
060        }
061    }
062
063    public int unpack ( ISOComponent m, byte[] b ) throws ISOException {
064        LogEvent evt = new LogEvent ( this, "unpack" );
065        int consumed = 0;
066        for ( int i=0; consumed < b.length ; i++ ) {
067            ISOComponent c = fld[i].createComponent( i );
068            consumed += fld[i].unpack ( c, b, consumed );
069            if ( logger != null )       {
070                evt.addMessage ("<unpack fld=\"" + i
071                                +"\" packager=\""
072                                +fld[i].getClass().getName()+ "\">");
073                evt.addMessage ("  <value>"
074                                +c.getValue().toString()
075                                + "</value>");
076                evt.addMessage ("</unpack>");
077            }
078            m.set(c);
079        }
080        Logger.log (evt);
081        return consumed;
082    }
083
084    /**
085     * Always return false.
086     * <br><br>
087     **/
088    protected boolean emitBitMap() {
089        return false;
090    }
091
092    public ISOComponent validate( ISOComponent c ) throws org.jpos.iso.ISOException {
093        LogEvent evt = new LogEvent( this, "validate" );
094        try {
095            Map tab = c.getChildren();
096            for ( int i = 0; i < fldVld.length; i++ ) {
097                ISOField f = (ISOField) tab.get (i);
098                if ( f != null )
099                    c.set( fldVld[i].validate( f ) );
100            }
101            return c;
102        } catch ( ISOVException ex ) {
103            if ( !ex.treated() ) {
104                c.set( ex.getErrComponent() );
105                ex.setTreated( true );
106            }
107            evt.addMessage( ex );
108            throw ex;
109        } finally {
110            Logger.log( evt );
111        }
112    }
113}