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