/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package EMIDA.xml;
import EMIDA.CATMainMidlet;
import EMIDA.Constants;
import EMIDA.State;
import EMIDA.ACHAccounts;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.kxml2.io.KXmlParser;
/**
*
* @author nmartinez
*/
public class MasterParser {
protected static XmlNode xml;
/***
*
* @param xmlResponse
* @return
*/
private static boolean parseResponse(String xmlResponse)
{
boolean result=false;
try
{
InputStream xmlIn =new ByteArrayInputStream(xmlResponse.getBytes());
KXmlParser parser = new KXmlParser();
parser.setInput(new InputStreamReader(xmlIn));
GenericXmlParser gParser = new GenericXmlParser();
xml = gParser.parseXML(parser, true);
result=true;
}
catch(Exception e)
{
System.out.println("Exception " +e);
}
return result;
}
public static void parseCarriers(String xmlResponse, CATMainMidlet catMidlet){
if ( parseResponse(xmlResponse) )
{
int size = xml.children.size();
catMidlet.cleanCarriersDataBase();
for(int i = 0; i < size; i++)
{
XmlNode nodeInfo = (XmlNode)xml.children.elementAt(i);
XmlNode carrierNameNode = (XmlNode)nodeInfo.children.elementAt(0);
String carrierName = carrierNameNode.nodeValue;
XmlNode carrierIdNode = (XmlNode)nodeInfo.children.elementAt(1);
String carrierId = carrierIdNode.nodeValue;
if ( nodeInfo.attributes.size()==1){
String isCarrierSelected = (String)nodeInfo.attributes.get("f");
if ( isCarrierSelected != null ){
catMidlet.SetProperty(Constants.PROPERTY_CARRIER_ID, carrierId);
catMidlet.SetProperty(Constants.PROPERTY_CARRIER_NAME, carrierName);
}
}
catMidlet.addCarrier(carrierId, carrierName);
}
if ( size > 0 ){
long today = System.currentTimeMillis();
String dateLastUpdate = String.valueOf(today);
catMidlet.SetProperty(Constants.PROPERTY_DATE_LAST_CARRIER_UPDATE, dateLastUpdate );
}
}
}
public static String parseHistory(String xmlResponse, CATMainMidlet catMidlet) {
StringBuffer result = new StringBuffer();
if ( parseResponse(xmlResponse) )
{
int size = xml.children.size();
System.out.println("Number of locations: (parseHistory) " + size);
if ( size > 0 )
{
String RefId = catMidlet.setLang("RefId");
String Type = catMidlet.setLang("Type");
String Amt = catMidlet.setLang("Amt");
String Fee = catMidlet.setLang("Fee");
result.append("\n").append(RefId).append(" ").append(Type).append(" ").
append(Amt).append(" ").append(Fee).append("\n");
for(int i = 0; i < size; i++)
{
XmlNode nodeInfo = (XmlNode)xml.children.elementAt(i);
XmlNode ReferralNumberNode = (XmlNode)nodeInfo.children.elementAt(1);
String ReferralNumberValue = ReferralNumberNode.nodeValue;
XmlNode AmountNode = (XmlNode)nodeInfo.children.elementAt(3);
String AmountValue = AmountNode.nodeValue;
XmlNode FeeNode = (XmlNode)nodeInfo.children.elementAt(4);
String FeeValue = FeeNode.nodeValue;
XmlNode TransactionTypeNode = (XmlNode)nodeInfo.children.elementAt(5);
String TransactionTypeValue = TransactionTypeNode.nodeValue;
result.append(ReferralNumberValue).append(" ").
append(TransactionTypeValue).append(" ").
append(AmountValue).append(" ").
append(FeeValue).append("\n");
}
}
}
return result.toString();
}
public static void parseTopUp(String xmlResponse, CATMainMidlet catMidlet) {
if ( parseResponse(xmlResponse) )
{
int size = xml.children.size();
if ( size == 8 )
{
XmlNode nodeInfo = (XmlNode)xml.children.elementAt(6);
String NewCarrierValue = nodeInfo.nodeValue;
catMidlet.SetProperty(Constants.PROPERTY_CARRIER_ID, NewCarrierValue);
}
}
}
public static void parseStates(String xmlResponse, CATMainMidlet catMidlet) {
if ( parseResponse(xmlResponse) )
{
int size = xml.children.size();
if ( size > 0 ){
for(int i = 0; i < size; i++)
{
XmlNode nodeInfo = (XmlNode)xml.children.elementAt(i);
XmlNode stateCodeNode = (XmlNode)nodeInfo.children.elementAt(0);
String stateCodeValue = stateCodeNode.nodeValue;
XmlNode stateNameNode = (XmlNode)nodeInfo.children.elementAt(1);
String stateNameValue = stateNameNode.nodeValue;
catMidlet.addState(stateCodeValue, stateNameValue);
State state = new State(stateCodeValue,stateNameValue);
if (catMidlet.statesInMemory != null){
catMidlet.statesInMemory.addElement(state);
}
}
}
}
}
public static String parseLocator(String xmlResponse, CATMainMidlet catMidlet) {
StringBuffer result = new StringBuffer();
if ( parseResponse(xmlResponse) )
{
int size = xml.children.size();
if ( size > 0 )
{
result.append("\n");
for(int i = 0; i < size; i++)
{
XmlNode nodeInfo = (XmlNode)xml.children.elementAt(i);
XmlNode NameNode = (XmlNode)nodeInfo.children.elementAt(0);
String NameValue = NameNode.nodeValue;
XmlNode AddressNode = (XmlNode)nodeInfo.children.elementAt(1);
String AddressValue = AddressNode.nodeValue;
XmlNode PhoneNode = (XmlNode)nodeInfo.children.elementAt(2);
String PhoneValue = PhoneNode.nodeValue;
String count = (i+1)+". ";
result.append(count).append(NameValue).append("\n").
append(AddressValue).append("\n").
append(PhoneValue).append("\n").append("\n\n");
}
}
}
return result.toString();
}
/**
* <AchAccount>
<BankName>null</BankName>
<AchId>1000</AchId>
<AchRoutingNumber>+zstHgkI0SrKdPs9CDaXyZukhWXr</AchRoutingNumber>
<AchAccountNumber>7876</AchAccountNumber>
<Status>PENDING</Status>
<StartDate>null</StartDate>
<EndDate>null</EndDate>
<Frequency>null</Frequency>
<NextFire>null</NextFire>
<AchAmount>null</AchAmount>
</AchAccount>
*
*
* @param xmlResponse
* @param catMidlet
*/
public static void parseACHAccounts(String xmlResponse, CATMainMidlet catMidlet){
StringBuffer result = new StringBuffer();
if ( parseResponse(xmlResponse) )
{
int size = xml.children.size();
if ( size > 0 )
{
result.append("\n");
for(int i = 0; i < size; i++)
{
XmlNode nodeInfo = (XmlNode)xml.children.elementAt(i);
XmlNode BankNameNode = (XmlNode)nodeInfo.children.elementAt(0);
String BankNameValue = BankNameNode.nodeValue;
XmlNode AchIDNode = (XmlNode)nodeInfo.children.elementAt(1);
String AchIDValue = AchIDNode.nodeValue;
XmlNode AchRoutingNode = (XmlNode)nodeInfo.children.elementAt(2);
String AchRoutingValue = AchRoutingNode.nodeValue;
XmlNode AchAccountNumberNode = (XmlNode)nodeInfo.children.elementAt(3);
String AchAccountNumberValue = AchAccountNumberNode.nodeValue;
XmlNode StatusNode = (XmlNode)nodeInfo.children.elementAt(4);
String StatusValue = StatusNode.nodeValue;
XmlNode IsDepositRecurringNode = (XmlNode)nodeInfo.children.elementAt(5);
String DepRecurValue = IsDepositRecurringNode.nodeValue;
XmlNode IsWithdrawalRecurringNode = (XmlNode)nodeInfo.children.elementAt(6);
String WithRecurValue = IsWithdrawalRecurringNode.nodeValue;
//XmlNode StartDateNode = (XmlNode)nodeInfo.children.elementAt(5);
//String StartDateValue = StartDateNode.nodeValue;
//XmlNode EndDateNode = (XmlNode)nodeInfo.children.elementAt(6);
//String EndDateValue = EndDateNode.nodeValue;
//XmlNode NextFireNode = (XmlNode)nodeInfo.children.elementAt(7);
//String FireValue = NextFireNode.nodeValue;
//XmlNode AmtNode = (XmlNode)nodeInfo.children.elementAt(8;
//String AmtValue = AmtNode.nodeValue;
String count = (i+1)+". ";
ACHAccounts ach = new ACHAccounts(AchAccountNumberValue,StatusValue,AchIDValue,
DepRecurValue,WithRecurValue);
//result.append(count).append(AchAccountNumberValue).append(":").
// append(StatusValue).append("\n");
if (catMidlet.achaccountsInMemory != null){
catMidlet.achaccountsInMemory.addElement(ach);
}
}
}
}
}
}