Thursday, March 13, 2008

Parsing Form in Smart Web Server

There are 2 class that have been already provided FormHandler and FormParser.

FormHandler
We should implement FormHandler in class. This class has 3 methods, start , form and end.

Start : start of the document parsing, perhaps you can add some codes that initialized the value of variable

Form : Each form value will be known in here. Just compare the form value with the first input parameter.

End : end of the document parsing

for example :
import com.gemplus.javacard.multimedia.framework.ByteString;
import com.gemplus.javacard.multimedia.framework.FormHandler;

public class FormPosting implements FormHandler {

public byte [] username;
public byte [] usermail;
private static final byte[] USERNAME={'u','s','e','r','n','a','m','e'};
private static final byte[] USERMAIL={'u','s','e','r','m','a','i','l'};

public void start() {
username=null;
usermail=null;
}

public void form(ByteString arg0, ByteString arg1) {

if (arg0.compareTo(USERNAME, (short) 0, (short) USERNAME.length) == 0)
{
username = arg1.getBytes();
}
else if (arg0.compareTo(USERMAIL, (short) 0, (short) USERMAIL.length) == 0)
usermail = arg1.getBytes();

}


public void end() {
// TODO Auto-generated method stub
}

}

FormParser
SharedByteString name = new SharedByteString();
SharedByteString value= new SharedByteString();
StringValueBuffer stringValueBuffer = new StringValueBuffer((short) 50, (short) 100); //short sizeRAM, short sizeEEPROM
FormParser formParser = new FormParser(true);

// some methods
HttpCardServletRequest req
byte data;
byte[] content = JCSystem.makeTransientByteArray((short) req.getContentLength(), JCSystem.CLEAR_ON_RESET);
stringValueBuffer.reset();

HttpCardServletInputStream in = req.getInputStream();
try {
for (short i = 0; i < content.length; i++) {
data = in.read();
content[i] = data;
} } catch (EndOfStreamException e) { }
// URL encoded
formParser.parse(FormPosting, new ByteString(content), stringValueBuffer, name, value);