Friday, May 25, 2012

StringTokenizer and removing Punctuations from String

Dear Reader,
I am writing a small article to remove all punctuations
from a String and to print only words. This is useful when you
want to read a text document and takes only words.

import java.util.StringTokenizer;

public class StringTokenTest {
    public static void main(String[] args) {
        String sentence="As you can tell from the characters passed into the StringTokenizer, " +
                "this approach handles a space, tab, newline and linefeed characters, period, colon, " +
                "semi-colon, question mark, exclamation mark, brackets, and single-quotes.";

        StringTokenizer tokenizer = new StringTokenizer(sentence, " \t\n\r\f,.:;?![]'"); //This will remove all punctuations.
        //StringTokenizer tokenizer = new StringTokenizer(sentence," ");
        
        System.out.println(tokenizer.countTokens());
        while(tokenizer.hasMoreTokens()){
            System.out.println(tokenizer.nextToken());
        }
    }
}
------------------END-------------

Friday, May 4, 2012

ISO Message: Packing and Un-Packing

Dear Reader,
Here I am writing about how to pack and unpack ISO messages for banking transactions.
For basic necessary stuffs please read my earlier written blog and then come here, link is below:
http://deepakmodi2006.blogspot.in/2012/04/iso-8583-standard-in-java-for-financial.html

jPOS is a Java platform-based, ISO-8583 based financial transaction library/framework that can be 
customized and extended in order to implement financial interchanges.

So first thing to do is download JPOS libraries from its website.
Following Jars are required for the same. Please see the picture below.

For packing and unpacking an ISO message I have written four programs: basic.xml genericpackager.dtd BuildISOMessage.java ParseISOMessage.java Output of program.. ------------------------------------------- package dmodi; import java.io.IOException; import org.jpos.iso.ISOException; import org.jpos.iso.ISOMsg; import org.jpos.iso.packager.GenericPackager; public class BuildISOMessage { public static void main(String[] args) throws IOException, ISOException { //Create Packager based on XML that contain DE type GenericPackager packager = new GenericPackager("basic.xml"); // Create ISO Message ISOMsg isoMsg = new ISOMsg(); isoMsg.setPackager(packager); isoMsg.setMTI("0200"); isoMsg.set(3, "201234"); isoMsg.set(4, "10000"); isoMsg.set(7, "110722180"); isoMsg.set(11, "123456"); isoMsg.set(32, "100005"); isoMsg.set(44, "A5DFGR"); isoMsg.set(105, "ABCDEFGHIJ 1234567890"); // print the DE list logISOMsg(isoMsg); // Get and print the output result byte[] data = isoMsg.pack(); String dataString=new String(data); System.out.println("Packed ISO Message : " +dataString); ParseISOMessage parse=new ParseISOMessage(); parse.setData(dataString); parse.parseIsoMessage(); } private static void logISOMsg(ISOMsg msg) { System.out.println("----ISO MESSAGE to Pack-----"); try { System.out.println(" MTI : " + msg.getMTI()); for (int i = 1; i <= msg.getMaxField(); i++) { if (msg.hasField(i)) { System.out.println(" Field-" + i + " : " + msg.getString(i)); } } } catch (ISOException e) { e.printStackTrace(); } finally { System.out.println("--------------------"); } } } ------------------------------------------- //ParseISOMessage.java package dmodi; import java.io.IOException; import org.jpos.iso.ISOException; import org.jpos.iso.ISOMsg; import org.jpos.iso.packager.GenericPackager; public class ParseISOMessage { String data=""; public void parseIsoMessage() throws IOException, ISOException { // Create Packager based on XML that contain DE type GenericPackager packager = new GenericPackager("basic.xml"); System.out.println("Data to unpack : " + data); // Create ISO Message ISOMsg isoMsg = new ISOMsg(); isoMsg.setPackager(packager); isoMsg.unpack(data.getBytes()); // print the DE list logISOMsg(isoMsg); } private static void logISOMsg(ISOMsg msg) { System.out.println("----Unpacked ISO MESSAGE-----"); try { System.out.println(" MTI : " + msg.getMTI()); for (int i = 1; i <= msg.getMaxField(); i++) { if (msg.hasField(i)) { System.out.println(" Field-" + i + " : " + msg.getString(i)); } } } catch (ISOException e) { e.printStackTrace(); } finally { System.out.println("--------------------"); } } public void setData(String strData){ data=strData; } } ------------------------------------------- //basic.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE isopackager SYSTEM "genericpackager.dtd"> <isopackager> <isofield id="0" length="4" name="MESSAGE TYPE INDICATOR" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="1" length="64" name="BIT MAP" class="org.jpos.iso.IFA_BITMAP"/> <isofield id="3" length="6" name="PROCESSING CODE" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="4" length="12" name="AMOUNT, TRANSACTION" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="7" length="10" name="TRANSMISSION DATE AND TIME" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="11" length="6" name="SYSTEM TRACE AUDIT NUMBER" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="32" length="6" name="Acquirer Id" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="44" length="25" name="ADITIONAL RESPONSE DATA" class="org.jpos.iso.IFA_LLCHAR"/> <isofield id="105" length="999" name="RESERVED ISO USE" class="org.jpos.iso.IFA_LLLCHAR"/> </isopackager> ------------------------------------------- //genericpackager.dtd <?xml version="1.0" encoding="UTF-8"?> <!ELEMENT isopackager (isofield+,isofieldpackager*)*> <!ATTLIST isopackager maxValidField CDATA #IMPLIED> <!ATTLIST isopackager bitmapField CDATA #IMPLIED> <!ATTLIST isopackager firstField CDATA #IMPLIED> <!ATTLIST isopackager emitBitmap (true|false) #IMPLIED> <!ATTLIST isopackager headerLength CDATA #IMPLIED> <!-- isofield --> <!ELEMENT isofield (#PCDATA)> <!ATTLIST isofield id CDATA #REQUIRED> <!ATTLIST isofield length CDATA #REQUIRED> <!ATTLIST isofield name CDATA #REQUIRED> <!ATTLIST isofield class NMTOKEN #REQUIRED> <!ATTLIST isofield token CDATA #IMPLIED> <!ATTLIST isofield pad (true|false) #IMPLIED> <!-- isofieldpackager --> <!ELEMENT isofieldpackager (isofield+,isofieldpackager*)*> <!ATTLIST isofieldpackager id CDATA #REQUIRED> <!ATTLIST isofieldpackager name CDATA #REQUIRED> <!ATTLIST isofieldpackager length CDATA #REQUIRED> <!ATTLIST isofieldpackager class NMTOKEN #REQUIRED> <!ATTLIST isofieldpackager token CDATA #IMPLIED> <!ATTLIST isofieldpackager pad (true|false) #IMPLIED> <!ATTLIST isofieldpackager packager NMTOKEN #REQUIRED> <!ATTLIST isofieldpackager emitBitmap (true|false) #IMPLIED> <!ATTLIST isofieldpackager maxValidField CDATA #IMPLIED> <!ATTLIST isofieldpackager bitmapField CDATA #IMPLIED> <!ATTLIST isofieldpackager firstField CDATA #IMPLIED> <!ATTLIST isofieldpackager headerLength CDATA #IMPLIED> ------------------------------------------- Output: ----ISO MESSAGE to Pack----- MTI : 0200 Field-3 : 201234 Field-4 : 10000 Field-7 : 110722180 Field-11 : 123456 Field-32 : 100005 Field-44 : A5DFGR Field-105 : ABCDEFGHIJ 1234567890 -------------------- Packed ISO Message : 0200B2200001001000000000000000800000201234000000010000011072218012345610000506A5DFGR021ABCDEFGHIJ 1234567890 Data to unpack : 0200B2200001001000000000000000800000201234000000010000011072218012345610000506A5DFGR021ABCDEFGHIJ 1234567890 ----Unpacked ISO MESSAGE----- MTI : 0200 Field-3 : 201234 Field-4 : 000000010000 Field-7 : 0110722180 Field-11 : 123456 Field-32 : 100005 Field-44 : A5DFGR Field-105 : ABCDEFGHIJ 1234567890 -------------------- -----------------------------------END--------------------------------