📄 ofxbankstatementhandler.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer is ActFact BV.
* Copyright (C) 2003-2004 ActFact BV and Compiere Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.impexp;
import org.compiere.model.*;
import org.compiere.util.Env;
import java.util.*;
import java.io.*;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
/**
* Parser for OFX bank statements
*
* This class is a parser for OFX bankstatements. OFX versions from 102 to 202
* and MS-Money OFC message sets are supported.
* Only fully XML compliant OFX data is supported. Files that are not XML
* compliant, e.g. OFX versions older then 200, will be preprocessed by
* the OFX1ToXML class before parsing.
* This class should be extended by a class that obtains the data to be parsed
* for example from a file, or using HTTP.
*
* @author Eldir Tomassen
* @version $Id: OFXBankStatementHandler.java,v 1.6 2005/09/29 22:01:56 jjanke Exp $
*/
public abstract class OFXBankStatementHandler extends DefaultHandler
{
protected Stack<String> m_context = new Stack<String>();
protected MBankStatementLoader m_controller;
protected String m_errorMessage = "";
protected String m_errorDescription = "";
protected BufferedReader m_reader = null;
protected SAXParser m_parser;
protected boolean m_success = false;
//private boolean m_valid = false;
protected StatementLine m_line;
protected String routingNo = "0";
protected String bankAccountNo = null;
protected String currency = null;
protected int HEADER_SIZE = 20;
protected boolean test = false;
protected Timestamp dateLastRun = null;
protected Timestamp statementDate = null;
/** XML OFX Tag */
public static final String XML_OFX_TAG = "OFX";
/** XML SIGNONMSGSRSV2 Tag */
public static final String XML_SIGNONMSGSRSV2_TAG = "SIGNONMSGSRSV2";
/** XML SIGNONMSGSRSV1 Tag */
public static final String XML_SIGNONMSGSRSV1_TAG = "SIGNONMSGSRSV1";
/** Record-response aggregate */
public static final String XML_SONRS_TAG = "SONRS";
/** Date and time of the server response */
public static final String XML_DTSERVER_TAG = "DTSERVER";
/** Use USERKEY instead of USERID and USEPASS */
public static final String XML_USERKEY_TAG = "USERKEY";
/** Date and time that USERKEY expires */
public static final String XML_TSKEYEXPIRE_TAG = "TSKEYEXPIRE";
/** Language */
public static final String XML_LANGUAGE_TAG = "LANGUAGE";
/** Date and rime last update to profile information*/
public static final String XML_DTPROFUP_TAG = "DTPROFUP";
/** Status aggregate */
public static final String XML_STATUS_TAG = "STATUS";
/** Statement-response aggregate */
public static final String XML_STMTRS_TAG = "STMTRS";
/** XML CURDEF Tag */
public static final String XML_CURDEF_TAG = "CURDEF";
/** Account-from aggregate */
public static final String XML_BANKACCTFROM_TAG = "BANKACCTFROM";
/** Bank identifier */
public static final String XML_BANKID_TAG = "BANKID";
/** Branch identifier */
public static final String XML_BRANCHID_TAG = "BRANCHID";
/** XML ACCTID Tag */
public static final String XML_ACCTID_TAG = "ACCTID";
/** Type of account */
public static final String XML_ACCTTYPE_TAG = "ACCTTYPE";
/** Type of account */
public static final String XML_ACCTTYPE2_TAG = "ACCTTYPE2";
/** Checksum */
public static final String XML_ACCTKEY_TAG = "ACCTKEY";
/** XML BANKTRANLIST Tag */
public static final String XML_BANKTRANLIST_TAG = "BANKTRANLIST";
/** XML DTSTART Tag */
public static final String XML_DTSTART_TAG = "DTSTART";
/** XML DTEND Tag */
public static final String XML_DTEND_TAG = "DTEND";
/** XML STMTTRN Tag */
public static final String XML_STMTTRN_TAG = "STMTTRN";
/** XML TRNTYPE Tag */
public static final String XML_TRNTYPE_TAG = "TRNTYPE";
/** XML TRNAMT Tag */
public static final String XML_TRNAMT_TAG = "TRNAMT";
/** Transaction date */
public static final String XML_DTPOSTED_TAG = "DTPOSTED";
/** Effective date */
public static final String XML_DTAVAIL_TAG = "DTAVAIL";
/** XML FITID Tag */
public static final String XML_FITID_TAG = "FITID";
/** XML CHECKNUM Tag */
public static final String XML_CHECKNUM_TAG = "CHECKNUM";
/** XML CHKNUM Tag (MS-Money OFC) */
public static final String XML_CHKNUM_TAG = "CHKNUM";
/** XML REFNUM Tag */
public static final String XML_REFNUM_TAG = "REFNUM";
/** Transaction Memo */
public static final String XML_MEMO_TAG = "MEMO";
/** XML NAME Tag */
public static final String XML_NAME_TAG = "NAME";
/** XML PAYEEID Tag */
public static final String XML_PAYEEID_TAG = "PAYEEID";
/** TXML PAYEE Tag */
public static final String XML_PAYEE_TAG = "PAYEE";
/** XML LEDGERBAL Tag */
public static final String XML_LEDGERBAL_TAG = "LEDGERBAL";
/** XML BALAMT Tag */
public static final String XML_BALAMT_TAG = "BALAMT";
/** XML DTASOF Tag */
public static final String XML_DTASOF_TAG = "DTASOF";
/** XML AVAILBAL Tag */
public static final String XML_AVAILBAL_TAG = "AVAILBAL";
/** XML MKTGINFO Tag */
public static final String XML_MKTGINFO_TAG = "MKTGINFO";
/**
* Initialize the loader
* * @param controller Reference to the BankStatementLoaderController
@return Initialized succesfully
*/
protected boolean init(MBankStatementLoader controller)
{
boolean result = false;
if (controller == null)
{
m_errorMessage = "ErrorInitializingParser";
m_errorDescription = "ImportController is a null reference";
return result;
}
this.m_controller = controller;
try
{
SAXParserFactory factory = SAXParserFactory.newInstance();
m_parser = factory.newSAXParser();
result = true;
}
catch(ParserConfigurationException e)
{
m_errorMessage = "ErrorInitializingParser";
m_errorDescription = "Unable to configure SAX parser: " + e.getMessage();
}
catch(SAXException e)
{
m_errorMessage = "ErrorInitializingParser";
m_errorDescription = "Unable to initialize SAX parser: " + e.getMessage();
}
return result;
} // init
/**
* Attach OFX input source, detect whether we are dealing with OFX1
* (SGML) or OFX2 (XML). In case of OFX1, process the data to create
* valid XML.
* @param is Reference to the BankStatementLoaderController
* @return true if input is valid OFX data
*/
protected boolean attachInput(InputStream is)
{
boolean isOfx1 = true;
boolean result = false;
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
reader.mark(HEADER_SIZE + 100);
String header = "";
for (int i = 0; i < HEADER_SIZE; i++)
{
header = header + reader.readLine();
}
if ((header.indexOf("<?OFX") != -1) || (header.indexOf("<?ofx") != -1))
{
isOfx1 = false;
}
else if ((header.indexOf("<?XML") != -1) || (header.indexOf("<?xml") != -1))
{
isOfx1 = false;
//deted specific OFX version
}
else
{
isOfx1 = true;
//detect specific OFX version
}
reader.reset();
if (isOfx1)
{
m_reader = new BufferedReader(new InputStreamReader(new OFX1ToXML(reader)));
}
else
{
m_reader = reader;
}
result = true;
}
catch(IOException e)
{
m_errorMessage = "ErrorReadingData";
m_errorDescription = e.getMessage();
return result;
}
return result;
} // attachInput
/** Verify the validity of the OFX data
* @return true if input is valid OFX data
*/
public boolean isValid()
{
boolean result = true;
/*
try
{
if (loadLines())
{
result = true;
test = false;
}
m_reader.reset();
}
catch(IOException e)
{
m_errorMessage = "ErrorReadingData";
m_errorDescription = e.getMessage();
}
*/
return result;
} //isValid
/** Check wether the import was succesfull
* @return true if all statement lines have been imported succesfully
*/
public boolean importSuccessfull()
{
/*
* Currently there are no checks after the statement lines are read.
* Once all lines are read correctly a successfull import is assumed.
*/
return m_success;
} // importSuccessfull
/**
* Read statementlines from InputStream.
* @return load success
* This method will be invoked from ImportController.
*/
public boolean loadLines()
{
boolean result = false;
try
{
m_parser.parse(new InputSource(m_reader), this);
result = true;
m_success = true;
}
catch(SAXException e)
{
m_errorMessage = "ErrorParsingData";
m_errorDescription = e.getMessage();
}
catch(IOException e)
{
m_errorMessage = "ErrorReadingData";
m_errorDescription = e.getMessage();
}
return result;
} // loadLines
/**
* Method getDateLastRun
* @return Timestamp
*/
public Timestamp getDateLastRun()
{
return dateLastRun;
}
/**
* Method getRoutingNo
* @return String
*/
public String getRoutingNo()
{
return m_line.routingNo;
}
/**
* Method getBankAccountNo
* @return String
*/
public String getBankAccountNo()
{
return m_line.bankAccountNo;
}
/**
* Method getStatementReference
* @return String
*/
public String getStatementReference()
{
return m_line.statementReference;
}
/**
* Method getStatementDate
* @return Timestamp
*/
public Timestamp getStatementDate()
{
return statementDate;
}
/**
* Method getReference
* @return String
*/
public String getReference()
{
return m_line.reference;
}
/**
* Method getStatementLineDate
* @return Timestamp
*/
public Timestamp getStatementLineDate()
{
return m_line.statementLineDate;
}
/**
* Method getValutaDate
* @return Timestamp
*/
public Timestamp getValutaDate()
{
return m_line.valutaDate;
}
/**
* Method getTrxType
* @return String
*/
public String getTrxType()
{
return m_line.trxType;
}
/**
* Method getIsReversal
* @return boolean
*/
public boolean getIsReversal()
{
return m_line.isReversal;
}
/**
* Method getCurrency
* @return String
*/
public String getCurrency()
{
return m_line.currency;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -