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 java.util.ArrayList; 022import java.util.List; 023import java.util.Map; 024 025import org.jpos.iso.*; 026import org.xml.sax.Attributes; 027 028/** 029 * This packager is used to package subfields such as field 60 of GICC. 030 */ 031public class GICCSubFieldPackager extends GenericPackager implements ISOSubFieldPackager, GenericPackagerParams { 032 private int fieldId = 0; 033 034 /** 035 * Default constructor. 036 * 037 * @throws ISOException if configuration fails 038 */ 039 public GICCSubFieldPackager() throws ISOException { 040 super(); 041 } 042 043 /** 044 * Always return false 045 */ 046 protected boolean emitBitMap() { 047 return false; 048 } 049 050 @Override 051 public int getFieldNumber() { 052 return fieldId; 053 } 054 055 @Override 056 public void setGenericPackagerParams(Attributes atts) { 057 super.setGenericPackagerParams(atts); 058 fieldId = Integer.parseInt(atts.getValue("id")); 059 } 060 061 public byte[] pack(ISOComponent c) throws ISOException { 062 try { 063 int len = 0; 064 byte[] result; 065 Map tab = c.getChildren(); 066 List list = new ArrayList(); 067 068 // Handle first IF_CHAR field 069 ISOField f0 = (ISOField) tab.get(0); 070 if (f0 != null) { 071 String s = (String) f0.getValue(); 072 list.add(s.getBytes()); 073 len += s.getBytes().length; 074 } 075 for (int i = 1; i < fld.length; i++) { 076 Object obj = tab.get(i); 077 if (obj instanceof ISOComponent) { 078 ISOComponent f = (ISOComponent) obj; 079 byte[] b = fld[i].pack(f); 080 list.add(b); 081 len += b.length; 082 } 083 } 084 085 result = new byte[len]; 086 int k = 0; 087 for (int i = 0; i < list.size(); i++) { 088 byte[] b = (byte[]) list.get(i); 089 for (int j = 0; j < b.length; j++) 090 result[k++] = b[j]; 091 } 092 093 return result; 094 } catch (Exception ex) { 095 throw new ISOException(ex); 096 } 097 } 098 099 public int unpack(ISOComponent m, byte[] b) throws ISOException { 100 // Unpack the IF_CHAR field 101 int consumed = 0; 102 ISOComponent c; 103 if (fld[0] != null && b[consumed] == 0x20) { 104 // Hack to support a nine-byte filler 105 c = fld[0].createComponent(0); 106 consumed += fld[0].unpack(c, b, consumed); 107 m.set(c); 108 } 109 110 // Now unpack the IFEP_LLCHAR fields 111 for (; consumed < b.length; ) { 112 int fieldNumber = Integer.parseInt(ISOUtil.ebcdicToAscii(b, 113 consumed + 3, 2)); 114 if (fld[fieldNumber] == null) 115 break; 116 c = fld[fieldNumber].createComponent(fieldNumber); 117 consumed += fld[fieldNumber].unpack(c, b, consumed); 118 m.set(c); 119 } 120 121 return consumed; 122 } 123} 124