예제 #1
0
        public static Extract GetExtract(String ofxSourceFile, ParserSettings settings)
        {
            if (settings == null)
            {
                settings = new ParserSettings();
            }

            Boolean temCabecalho  = false;
            Boolean temDadosConta = false;

            // Translating to XML file
            ExportToXml(ofxSourceFile, ofxSourceFile + ".xml");

            // Variáveis úteis para o Parse
            String      elementoSendoLido = "";
            Transaction transacaoAtual    = null;

            // Variávies utilizadas para a leitura do XML
            HeaderExtract cabecalho = new HeaderExtract();
            BankAccount   conta     = new BankAccount();
            Extract       extrato   = new Extract(cabecalho, conta, "");

            // Lendo o XML efetivamente
            using (XmlTextReader meuXml = new XmlTextReader(ofxSourceFile + ".xml"))
            {
                try
                {
                    while (meuXml.Read())
                    {
                        if (meuXml.NodeType == XmlNodeType.EndElement)
                        {
                            switch (meuXml.Name)
                            {
                            case "STMTTRN":
                                if (transacaoAtual != null)
                                {
                                    extrato.AddTransaction(transacaoAtual);
                                    transacaoAtual = null;
                                }

                                break;
                            }
                        }

                        if (meuXml.NodeType == XmlNodeType.Element)
                        {
                            elementoSendoLido = meuXml.Name;

                            switch (elementoSendoLido)
                            {
                            case "STMTTRN":
                                transacaoAtual = new Transaction();
                                break;
                            }
                        }

                        if (meuXml.NodeType == XmlNodeType.Text)
                        {
                            switch (elementoSendoLido)
                            {
                            case "DTSERVER":
                                cabecalho.ServerDate = ConvertOfxDateToDateTime(meuXml.Value, extrato);
                                temCabecalho         = true;
                                break;

                            case "LANGUAGE":
                                cabecalho.Language = meuXml.Value;
                                temCabecalho       = true;
                                break;

                            case "ORG":
                                cabecalho.BankName = meuXml.Value;
                                temCabecalho       = true;
                                break;

                            case "DTSTART":
                                extrato.InitialDate = ConvertOfxDateToDateTime(meuXml.Value, extrato);
                                break;

                            case "DTEND":
                                extrato.FinalDate = ConvertOfxDateToDateTime(meuXml.Value, extrato);
                                break;

                            case "BANKID":
                                conta.Bank    = new Bank(GetBankId(meuXml.Value, extrato), "");
                                temDadosConta = true;
                                break;

                            case "BRANCHID":
                                conta.AgencyCode = meuXml.Value;
                                temDadosConta    = true;
                                break;

                            case "ACCTID":
                                conta.AccountCode = meuXml.Value;
                                temDadosConta     = true;
                                break;

                            case "ACCTTYPE":
                                conta.Type    = meuXml.Value;
                                temDadosConta = true;
                                break;

                            case "TRNTYPE":
                                transacaoAtual.Type = meuXml.Value;
                                break;

                            case "DTPOSTED":
                                transacaoAtual.Date = ConvertOfxDateToDateTime(meuXml.Value, extrato);
                                break;

                            case "TRNAMT":
                                transacaoAtual.TransactionValue = GetTransactionValue(meuXml.Value, extrato);
                                break;

                            case "FITID":
                                transacaoAtual.Id = meuXml.Value;
                                break;

                            case "CHECKNUM":
                                transacaoAtual.Checksum = Convert.ToInt64(meuXml.Value);
                                break;

                            case "MEMO":
                                transacaoAtual.Description = string.IsNullOrEmpty(meuXml.Value) ? "" : meuXml.Value.Trim().Replace("  ", " ");
                                break;

                            case "NAME":
                                transacaoAtual.Name = meuXml.Value;
                                break;
                            }
                        }
                    }
                }
                catch (XmlException xe)
                {
                    throw new OFXParserException("Invalid OFX file " + ofxSourceFile + " : " + xe.Message, xe);
                }
                finally
                {
                    meuXml.Close();
                    if (File.Exists(ofxSourceFile + ".xml"))
                    {
                        File.Delete(ofxSourceFile + ".xml");
                    }
                }
            }

            if ((settings.IsValidateHeader && temCabecalho == false) ||
                (settings.IsValidateAccountData && temDadosConta == false))
            {
                throw new OFXParserException("Invalid OFX file " + ofxSourceFile);
            }
            return(extrato);
        }
예제 #2
0
        private static Extract GetExtractByXmlExported(XmlTextReader xmlTextReader, ParserSettings settings)
        {
            if (settings == null)
            {
                settings = new ParserSettings();
            }

            // Variables used by Parser
            string      currentElement     = "";
            Transaction currentTransaction = null;

            // Variables used to read XML
            HeaderExtract header      = new HeaderExtract();
            BankAccount   bankAccount = new BankAccount();
            Extract       extract     = new Extract(header, bankAccount, "");

            bool hasHeader          = false;
            bool hasAccountInfoData = false;

            try
            {
                while (xmlTextReader.Read())
                {
                    if (xmlTextReader.NodeType == XmlNodeType.EndElement)
                    {
                        switch (xmlTextReader.Name)
                        {
                        case "STMTTRN":
                            if (currentTransaction != null)
                            {
                                extract.AddTransaction(currentTransaction);
                                currentTransaction = null;
                            }
                            break;
                        }
                    }
                    if (xmlTextReader.NodeType == XmlNodeType.Element)
                    {
                        currentElement = xmlTextReader.Name;

                        switch (currentElement)
                        {
                        case "STMTTRN":
                            currentTransaction = new Transaction();
                            break;
                        }
                    }
                    if (xmlTextReader.NodeType == XmlNodeType.Text)
                    {
                        switch (currentElement)
                        {
                        case "DTSERVER":
                            header.ServerDate = ConvertOfxDateToDateTime(xmlTextReader.Value, extract);
                            hasHeader         = true;
                            break;

                        case "LANGUAGE":
                            header.Language = xmlTextReader.Value;
                            hasHeader       = true;
                            break;

                        case "ORG":
                            header.BankName = xmlTextReader.Value;
                            hasHeader       = true;
                            break;

                        case "DTSTART":
                            extract.InitialDate = ConvertOfxDateToDateTime(xmlTextReader.Value, extract);
                            break;

                        case "DTEND":
                            extract.FinalDate = ConvertOfxDateToDateTime(xmlTextReader.Value, extract);
                            break;

                        case "BANKID":
                            bankAccount.Bank   = new Bank(GetBankId(xmlTextReader.Value, extract), "");
                            hasAccountInfoData = true;
                            break;

                        case "BRANCHID":
                            bankAccount.AgencyCode = xmlTextReader.Value;
                            hasAccountInfoData     = true;
                            break;

                        case "ACCTID":
                            bankAccount.AccountCode = xmlTextReader.Value;
                            hasAccountInfoData      = true;
                            break;

                        case "ACCTTYPE":
                            bankAccount.Type   = xmlTextReader.Value;
                            hasAccountInfoData = true;
                            break;

                        case "TRNTYPE":
                            if (currentTransaction != null)
                            {
                                currentTransaction.Type = xmlTextReader.Value;
                            }
                            break;

                        case "DTPOSTED":
                            if (currentTransaction != null)
                            {
                                currentTransaction.Date = ConvertOfxDateToDateTime(xmlTextReader.Value, extract);
                            }
                            break;

                        case "TRNAMT":
                            if (currentTransaction != null)
                            {
                                currentTransaction.TransactionValue = GetTransactionValue(xmlTextReader.Value, extract, settings);
                            }
                            break;

                        case "FITID":
                            if (currentTransaction != null)
                            {
                                currentTransaction.Id = xmlTextReader.Value;
                            }
                            break;

                        case "CHECKNUM":
                            if (currentTransaction != null)
                            {
                                currentTransaction.Checksum = Convert.ToInt64(xmlTextReader.Value);
                            }
                            break;

                        case "MEMO":
                            if (currentTransaction != null)
                            {
                                currentTransaction.Description = string.IsNullOrEmpty(xmlTextReader.Value) ? "" : xmlTextReader.Value.Trim().Replace("  ", " ");
                            }
                            break;
                        }
                    }
                }
            }
            catch (XmlException xe)
            {
                throw new OFXParserException($"Invalid OFX file! Internal message: {xe.Message}");
            }
            finally
            {
                xmlTextReader.Close();
            }

            if ((settings.IsValidateHeader && hasHeader == false) ||
                (settings.IsValidateAccountData && hasAccountInfoData == false))
            {
                throw new OFXParserException("Invalid OFX file!");
            }

            return(extract);
        }