001: /*
002: * Created on 26/01/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.ofc.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:
040: public class OFCParser {
041: protected ParsedAccount parsedAccount;
042:
043: protected ArrayList<PendingTransaction> lancamentosParseados;
044:
045: public OFCParser() {
046:
047: }
048:
049: public OFCParser(String xmlPath) throws OFCImportException {
050: try {
051: DocumentBuilderFactory factory = DocumentBuilderFactory
052: .newInstance();
053: factory.setValidating(false);
054: factory.setNamespaceAware(false);
055: DocumentBuilder builder = factory.newDocumentBuilder();
056: Document document = builder.parse(new InputSource(
057: new InputStreamReader(new FileInputStream(xmlPath),
058: "ISO-8859-1")));
059: NodeList parsedXml = document.getLastChild()
060: .getChildNodes();
061: for (int i = 0; i < parsedXml.getLength(); i++) {
062: if (parsedXml.item(i).getNodeName().toString().equals(
063: "ACCTSTMT")) {
064: parsedXml = parsedXml.item(i).getChildNodes();
065: break;
066: }
067: }
068:
069: // The first item is the account details
070: for (int c = 0; c < parsedXml.getLength(); c++) {
071: Node conta = parsedXml.item(c);
072: if (conta.getNodeName().equals("ACCTFROM")) {
073: parsedAccount = getParsedAccount(conta
074: .getChildNodes());
075:
076: int z = 0;
077: while (!parsedXml.item(z).getNodeName().equals(
078: "STMTRS"))
079: z++;
080: NodeList lancamentos = parsedXml.item(z)
081: .getChildNodes();
082:
083: lancamentosParseados = new ArrayList<PendingTransaction>();
084: Calendar calendar = Calendar.getInstance();
085: for (int i = 0; i < lancamentos.getLength(); i++)
086: if (lancamentos.item(i).getNodeName().equals(
087: "STMTTRN")) {
088: NodeList dadosLancamento = lancamentos
089: .item(i).getChildNodes();
090: PendingTransaction lancamentoPendente = new PendingTransaction();
091: for (int j = 0; j < dadosLancamento
092: .getLength(); j++) {
093: if (dadosLancamento.item(j)
094: .getNodeName().equals(
095: "DTPOSTED")) {
096: String dia = dadosLancamento
097: .item(j).getFirstChild()
098: .getNodeValue().trim();
099: calendar.set(Calendar.YEAR, Integer
100: .parseInt(dia.substring(0,
101: 4)));
102: calendar
103: .set(
104: Calendar.MONTH,
105: Integer
106: .parseInt(dia
107: .substring(
108: 4,
109: 6)) - 1);
110: calendar.set(Calendar.DAY_OF_MONTH,
111: Integer.parseInt(dia
112: .substring(6)));
113: lancamentoPendente.setDia(calendar
114: .getTime());
115: }
116: if (dadosLancamento.item(j)
117: .getNodeName().equals("TRNAMT"))
118: lancamentoPendente
119: .setValor(Double
120: .parseDouble(dadosLancamento
121: .item(j)
122: .getFirstChild()
123: .getNodeValue()
124: .trim()));
125: if (dadosLancamento.item(j)
126: .getNodeName().equals("MEMO"))
127: lancamentoPendente
128: .setDescricao(dadosLancamento
129: .item(j)
130: .getFirstChild()
131: .getNodeValue()
132: .trim());
133: }
134: if (lancamentoPendente.getDescricao()
135: .startsWith("Compra com Cartao - ")) {
136: lancamentoPendente
137: .setDescricao(lancamentoPendente
138: .getDescricao()
139: .substring(
140: "Compra com Cartao - 01/05 19:37"
141: .length() + 1));
142: }
143: lancamentosParseados
144: .add(lancamentoPendente);
145: }
146: }
147: }
148: } catch (Exception e) {
149: throw new OFCImportException(e);
150: }
151: }
152:
153: /**
154: * @param dadosDaConta
155: */
156: private ParsedAccount getParsedAccount(NodeList dadosDaConta) {
157: String bancoTmp = null, contaTmp = null, tipoTmp = null;
158:
159: for (int i = 0; i < dadosDaConta.getLength(); i++)
160: if (dadosDaConta.item(i).getNodeName().equals("BANKID"))
161: bancoTmp = dadosDaConta.item(i).getFirstChild()
162: .getNodeValue().trim();
163: else if (dadosDaConta.item(i).getNodeName()
164: .equals("ACCTID"))
165: contaTmp = dadosDaConta.item(i).getFirstChild()
166: .getNodeValue().trim();
167: else if (dadosDaConta.item(i).getNodeName().equals(
168: "ACCTTYPE"))
169: tipoTmp = dadosDaConta.item(i).getFirstChild()
170: .getNodeValue().trim();
171:
172: return new ParsedAccount(bancoTmp, contaTmp, tipoTmp);
173: }
174:
175: public ArrayList<PendingTransaction> getTransactions() {
176: return lancamentosParseados;
177: }
178:
179: public ParsedAccount getConta() {
180: return parsedAccount;
181: }
182: }
|