001: /*
002: * Created on 09/07/2007
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2007 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023: package br.com.gfp.ofx.parser;
024:
025: import java.io.FileInputStream;
026: import java.io.InputStreamReader;
027: import java.util.ArrayList;
028: import java.util.Calendar;
029:
030: import javax.xml.parsers.DocumentBuilder;
031: import javax.xml.parsers.DocumentBuilderFactory;
032:
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Node;
035: import org.w3c.dom.NodeList;
036: import org.xml.sax.InputSource;
037:
038: import br.com.gfp.data.PendingTransaction;
039: import br.com.gfp.ofc.parser.OFCImportException;
040: import br.com.gfp.ofc.parser.OFCParser;
041: import br.com.gfp.ofc.parser.ParsedAccount;
042:
043: public class GFPOFXParser extends OFCParser {
044:
045: public GFPOFXParser(String xmlPath) throws OFCImportException {
046: try {
047: DocumentBuilderFactory factory = DocumentBuilderFactory
048: .newInstance();
049: factory.setValidating(false);
050: factory.setNamespaceAware(false);
051: DocumentBuilder builder = factory.newDocumentBuilder();
052: Document document = builder.parse(new InputSource(
053: new InputStreamReader(new FileInputStream(xmlPath),
054: "ISO-8859-1")));
055: NodeList parsedXml = document.getLastChild()
056: .getChildNodes();
057: for (int i = 0; i < parsedXml.getLength(); i++) {
058: if (parsedXml.item(i).getNodeName().toString().equals(
059: "BANKMSGSRSV1")
060: || parsedXml.item(i).getNodeName().toString()
061: .equals("STMTTRNRS")
062: || parsedXml.item(i).getNodeName().toString()
063: .equals("STMTRS")) {
064: parsedXml = parsedXml.item(i).getChildNodes();
065: i = 0;
066: continue;
067: }
068: }
069:
070: // The first item is the account details
071: for (int c = 0; c < parsedXml.getLength(); c++) {
072: Node conta = parsedXml.item(c);
073: if (conta.getNodeName().equals("BANKACCTFROM"))
074: parsedAccount = getParsedAccount(conta
075: .getChildNodes());
076: if (conta.getNodeName().equals("BANKTRANLIST")) {
077: NodeList lancamentos = conta.getChildNodes();
078: lancamentosParseados = new ArrayList<PendingTransaction>();
079: Calendar calendar = Calendar.getInstance();
080: for (int i = 0; i < lancamentos.getLength(); i++)
081: if (lancamentos.item(i).getNodeName().equals(
082: "STMTTRN")) {
083: NodeList dadosLancamento = lancamentos
084: .item(i).getChildNodes();
085: PendingTransaction lancamentoPendente = new PendingTransaction();
086: for (int j = 0; j < dadosLancamento
087: .getLength(); j++) {
088: if (dadosLancamento.item(j)
089: .getNodeName().equals(
090: "DTPOSTED")) {
091: String dia = dadosLancamento
092: .item(j).getLastChild()
093: .getNodeValue().trim();
094: dia = dia.substring(0, 8);
095: calendar.set(Calendar.YEAR, Integer
096: .parseInt(dia.substring(0,
097: 4)));
098: calendar
099: .set(
100: Calendar.MONTH,
101: Integer
102: .parseInt(dia
103: .substring(
104: 4,
105: 6)) - 1);
106: calendar.set(Calendar.DAY_OF_MONTH,
107: Integer.parseInt(dia
108: .substring(6)));
109: lancamentoPendente.setDia(calendar
110: .getTime());
111: }
112: if (dadosLancamento.item(j)
113: .getNodeName().equals("TRNAMT"))
114: try {
115: lancamentoPendente
116: .setValor(Double
117: .parseDouble(dadosLancamento
118: .item(j)
119: .getLastChild()
120: .getNodeValue()
121: .trim()));
122: } catch (NumberFormatException nfe) {
123: String valor = dadosLancamento
124: .item(j).getLastChild()
125: .getNodeValue().trim()
126: .replace('.', '*')
127: .replace(',', '.')
128: .replace('*', ',');
129: lancamentoPendente
130: .setValor(Double
131: .parseDouble(valor));
132: }
133: if (dadosLancamento.item(j)
134: .getNodeName().equals("MEMO"))
135: lancamentoPendente
136: .setDescricao(dadosLancamento
137: .item(j)
138: .getLastChild()
139: .getNodeValue()
140: .trim());
141: }
142: if (lancamentoPendente.getDescricao()
143: .startsWith("Compra com Cartao - ")) {
144: lancamentoPendente
145: .setDescricao(lancamentoPendente
146: .getDescricao()
147: .substring(
148: "Compra com Cartao - 01/05 19:37"
149: .length() + 1));
150: }
151: lancamentosParseados
152: .add(lancamentoPendente);
153: }
154: }
155: }
156: } catch (Exception e) {
157: throw new OFCImportException(e);
158: }
159: }
160:
161: /**
162: * @param dadosDaConta
163: */
164: private ParsedAccount getParsedAccount(NodeList dadosDaConta) {
165: String bancoTmp = null, contaTmp = null, tipoTmp = null;
166:
167: for (int i = 0; i < dadosDaConta.getLength(); i++)
168: if (dadosDaConta.item(i).getNodeName().equals("BANKID"))
169: bancoTmp = dadosDaConta.item(i).getFirstChild()
170: .getNodeValue().trim();
171: else if (dadosDaConta.item(i).getNodeName()
172: .equals("ACCTID"))
173: contaTmp = dadosDaConta.item(i).getFirstChild()
174: .getNodeValue().trim();
175: else if (dadosDaConta.item(i).getNodeName().equals(
176: "ACCTTYPE"))
177: tipoTmp = dadosDaConta.item(i).getFirstChild()
178: .getNodeValue().trim();
179:
180: return new ParsedAccount(bancoTmp, contaTmp, tipoTmp);
181: }
182:
183: }
|