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.ISOMsg;
024import org.jpos.iso.ISOUtil;
025import org.jpos.iso.validator.ISOVException;
026import org.jpos.util.LogEvent;
027import org.jpos.util.Logger;
028
029import java.util.Map;
030
031/**
032 * Test validatingPackager for subelements in field 48.
033 *
034 * <p>Title: jPOS</p>
035 * <p>Description: Java Framework for Financial Systems</p>
036 * <p>Copyright: Copyright (c) 2000 jPOS.org.  All rights reserved.</p>
037 * <p>Company: www.jPOS.org</p>
038 * @author Jose Eduardo Leon
039 * @version 1.0
040 */
041public class CTCSubElementPackager extends ISOBaseValidatingPackager {
042
043    public CTCSubElementPackager() {
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                ISOMsg f = (ISOMsg) tab.get (i);
053                if ( f != null ) {
054                    sb.append ( ISOUtil.zeropad( f.getKey().toString(), 2 ) + 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                if (c.getValue() instanceof ISOMsg)
075                    evt.addMessage (c.getValue());
076                else
077                    evt.addMessage ("  <value>"
078                                    +c.getValue().toString()
079                                    + "</value>");
080                evt.addMessage ("</unpack>");
081            }
082            m.set(c);
083        }
084        Logger.log (evt);
085        return consumed;
086    }
087
088    /**
089     * Always return false
090     * <br><br>
091     **/
092    protected boolean emitBitMap() {
093        return false;
094    }
095
096    public ISOComponent validate( ISOComponent c ) throws org.jpos.iso.ISOException {
097        LogEvent evt = new LogEvent( this, "validate" );
098        try {
099            Map tab = c.getChildren();
100            for ( int i = 0; i < fldVld.length; i++ ) {
101                ISOMsg f = (ISOMsg) tab.get (i);
102                if ( f != null )
103                    c.set(fldVld[i].validate( f ));
104            }
105            return c;
106        } catch ( ISOVException ex ) {
107            if ( !ex.treated() ) {
108                c.set( ex.getErrComponent() );
109                ex.setTreated( true );
110            }
111            evt.addMessage( ex );
112            throw ex;
113        } finally {
114            Logger.log( evt );
115        }
116    }
117
118}