Search code examples
javaswingtext-parsingmagnetic-cards

How do I parse magnetic strip card data input in Java Swing textbox?


I'm trying to write an application which will search and retrieve a user's profile using data using an id key found using a scanned barcode or embedded in a magnetic strip card's data. The latter one is cause me grief. The magstripe data needs to be parsed prior to searching for the user profile.

My question, is there a way to capture the text scanned into the textbox and parse it before it gets displayed in the textbox?

My reader/scanner is the keyboard emulation type so its as if each character encoded on the stripe is typed out in the textbox. I guess a solution (but is it the best?) would be to intercept each keystroke (emulated by the magnetic stripe reader), store them in a buffer and display an empty character until the end of the read string. Once the end of card's data is read, I could parse and display the id part of id. Problem is... how do you know it's the end of the card's data string if they get inputted as individual char keystroke?


Solution

  • Answer to your first question is to set a DocumentFilter on the Document of the textfield.

    ..how do you know it's the end of the card's data string if they get inputted as individual char keystroke?

    Your MSR would surely emit a START_STRING and an END_OF_LINE_STRING as a combination of some predefined characters. Read the data specification of the MSR device. Once you have that, you could implement the insertString of the filter similar to this pseudo code

    if str == START_CHARACTER
        then clear buffer
    
    if str == EOL_CHARACTER
        then parse and do super.insertString
    
    else
        append string to buffer
    

    Again, the parse logic can be implemented using the data specification of the MSR.

    (MSR = Magnetic Stripe Reader)