Add XMLImportEngine

Fix some bugs in ImportPanel and ImportEngines
This commit is contained in:
2011-07-04 20:23:00 +02:00
parent ebe9ef4a62
commit 753b499b48
21 changed files with 597 additions and 117 deletions

View File

@@ -44,6 +44,9 @@ void GrisbiImportEngine::LoadAccount(GrisbiImportEngine* _this, const char** att
account_number += key;
UNESCAPE_CHARS(name);
UNESCAPE_CHARS(account_number);
for (i=0; i<(int)_this->_user->_accounts.size(); i++)
{
if (_this->_user->_accounts[i].number == account_number)
@@ -56,6 +59,11 @@ void GrisbiImportEngine::LoadAccount(GrisbiImportEngine* _this, const char** att
_this->_accounts[id] = wxT("unknown-") + account_number;
ac.number = account_number;
ac.name = name;
ac.shared = false;
ac.blocked = false;
ac._default = false;
ac.is_owner = true;
ac._virtual = false;
_this->_unresolvedAccounts.push_back(ac);
}
@@ -74,6 +82,8 @@ void GrisbiImportEngine::LoadCategory(GrisbiImportEngine* _this, const char** at
id = wxString(attrs[i+1], wxConvUTF8);
}
UNESCAPE_CHARS(name);
for (i=0; i<(int)_this->_user->_categories.size(); i++)
{
if (_this->_user->_categories[i].name == name)
@@ -86,6 +96,10 @@ void GrisbiImportEngine::LoadCategory(GrisbiImportEngine* _this, const char** at
_this->_categories[id] = wxT("unknown-") + name;
cat.id = id;
cat.name = name;
cat.parent = wxT("0");
cat.backcolor = OWN_GREEN ;
cat.forecolor = *wxBLACK;
cat.fix_cost = false;
_this->_unresolvedCategories.push_back(cat);
}
@@ -134,6 +148,8 @@ void GrisbiImportEngine::LoadOperation(GrisbiImportEngine* _this, const char** a
op.description = wxString(attrs[i+1], wxConvUTF8);
}
UNESCAPE_CHARS(op.description);
_this->_operations.push_back(op);
_this->_descriptions[op.id] = op.description;
@@ -193,7 +209,19 @@ GrisbiImportEngine::~GrisbiImportEngine()
bool GrisbiImportEngine::HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss)
{
int res = -1;
if (!ImportEngine::HandleFile(path, user, db, kiss)) return false;
return xmlSAXUserParseFile(&_sax, this, path.mb_str()) >= 0;
try
{
res = xmlSAXUserParseFile(&_sax, this, path.mb_str());
}
catch (const char* s)
{
std::cout << "GrisbiImportEngine :: " << s << std::endl;
res = -1;
}
return res >= 0;
}

View File

@@ -30,8 +30,6 @@ public:
~GrisbiImportEngine();
virtual bool HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss);
/* virtual std::vector<wxString> ParseFile(); */
/* virtual std::vector<Operation>* GetOperations(std::map<wxString, wxString>& accounts); */
private:
xmlSAXHandler _sax;

View File

@@ -38,6 +38,7 @@ bool ImportEngine::HandleFile(const wxString& path, User* user, Database* db, Ki
_unresolvedAccounts.clear();
_operations.clear();
_descriptions.clear();
_accountAmounts.clear();
return path.EndsWith(_shortExt) || path.EndsWith(_shortExt.Upper());
}
@@ -307,3 +308,8 @@ std::vector<Operation>* ImportEngine::GetOperations(std::map<wxString, wxString>
return &_operations;
}
const std::map<AccountAmount, double, AccountAmount>& ImportEngine::GetAccountAmounts()
{
return _accountAmounts;
}

View File

@@ -22,6 +22,7 @@
#include <model/model.h>
#include <controller/KissCount.h>
#include <model/AccountAmount.h>
class KissCount;
@@ -52,10 +53,13 @@ public:
// Final Step
virtual std::vector<Operation>* GetOperations(std::map<wxString, wxString>& accounts, std::map<wxString, wxString>& categories);
const std::map<AccountAmount, double, AccountAmount>& GetAccountAmounts();
void MatchPattern(wxString& key, Operation& op);
int UpdatePattern(int pos);
static wxString RemoveUnused(const wxString& s);
protected:
Database* _db;
User* _user;
@@ -71,6 +75,7 @@ protected:
std::vector<Category> _unresolvedCategories;
std::vector<Operation> _operations;
std::map<wxString, wxString> _descriptions;
std::map<AccountAmount, double, AccountAmount> _accountAmounts;
void ApplyPattern(ImportPattern& pattern, Operation& op);
wxString FindPattern(wxString& s1, wxString& s2);

View File

@@ -30,6 +30,8 @@ int OFXImportEngine::account_cb(const struct OfxAccountData data, void * account
_this->_curAccount = wxT("");
UNESCAPE_CHARS(account_number);
for (i=0; i<(int)_this->_user->_accounts.size(); i++)
{
if (_this->_user->_accounts[i].number == account_number)
@@ -46,6 +48,11 @@ int OFXImportEngine::account_cb(const struct OfxAccountData data, void * account
{
_this->_curAccount = _this->_accounts[account_number] = wxT("unknown-") + account_number;
ac.number = account_number;
ac.shared = false;
ac.blocked = false;
ac._default = false;
ac.is_owner = true;
ac._virtual = false;
_this->_unresolvedAccounts.push_back(ac);
}
@@ -94,6 +101,8 @@ int OFXImportEngine::transaction_cb(const struct OfxTransactionData data, void *
op.description += wxString(data.memo, wxConvUTF8);
}
UNESCAPE_CHARS(op.description);
_this->_operations.push_back(op);
_this->_descriptions[op.id] = op.description;

View File

@@ -0,0 +1,294 @@
/*
Copyright 2010-2011 Grégory Soutadé
This file is part of KissCount.
KissCount is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KissCount is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
*/
#include "XMLImportEngine.h"
static XMLImportEngine xmlImportEngine;
void XMLImportEngine::LoadAccount(XMLImportEngine* _this, const char** attrs)
{
int i;
wxString account_number, name, id;
Account ac;
for (i=0; attrs[i]; i+=2)
{
if (!strcmp(attrs[i], "name"))
ac.name = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "id"))
ac.id = id = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "number"))
ac.number = account_number = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "blocked"))
ac.blocked = (wxString(attrs[i+1], wxConvUTF8) == wxT("1"));
else if (!strcmp(attrs[i], "virtual"))
ac._virtual = (wxString(attrs[i+1], wxConvUTF8) == wxT("1"));
}
UNESCAPE_CHARS(ac.name);
UNESCAPE_CHARS(ac.number);
ac._default = false;
ac.shared = false;
ac.is_owner = true;
if (account_number.Length())
{
for (i=0; i<(int)_this->_user->_accounts.size(); i++)
{
if (_this->_user->_accounts[i].number == account_number)
{
_this->_accounts[id] = _this->_user->_accounts[i].id;
return;
}
}
}
_this->_accounts[id] = wxT("unknown-") + account_number;
_this->_unresolvedAccounts.push_back(ac);
}
void XMLImportEngine::LoadAccountAmount(XMLImportEngine* _this, const char** attrs)
{
AccountAmount accountAmount;
int i;
long v;
double amount;
for (i=0; attrs[i]; i+=2)
{
if (!strcmp(attrs[i], "account"))
accountAmount.account = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "month"))
{
wxString(attrs[i+1], wxConvUTF8).ToLong(&v);
accountAmount.month = v;
}
else if (!strcmp(attrs[i], "year"))
{
wxString(attrs[i+1], wxConvUTF8).ToLong(&v);
accountAmount.year = v;
}
else if (!strcmp(attrs[i], "amount"))
wxString(attrs[i+1], wxConvUTF8).ToDouble(&amount);
}
_this->_accountAmounts[accountAmount] = amount;
}
void XMLImportEngine::LoadCategory(XMLImportEngine* _this, const char** attrs)
{
wxString name, id;
int i;
long rgb;
Category cat;
for (i=0; attrs[i]; i+=2)
{
if (!strcmp(attrs[i], "name"))
cat.name = name = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "id"))
cat.id = id = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "font"))
cat.font = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "backcolor"))
{
wxString(attrs[i+1], wxConvUTF8).ToLong(&rgb, 16);
cat.backcolor = wxColour((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
}
else if (!strcmp(attrs[i], "forecolor"))
{
wxString(attrs[i+1], wxConvUTF8).ToLong(&rgb, 16);
cat.forecolor = wxColour((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
}
else if (!strcmp(attrs[i], "fix_cost"))
cat.fix_cost = (wxString(attrs[i+1], wxConvUTF8) == wxT("1"));
}
UNESCAPE_CHARS(cat.name);
for (i=0; i<(int)_this->_user->_categories.size(); i++)
{
if (_this->_user->_categories[i].name == name)
{
_this->_categories[id] = _this->_user->_categories[i].id;
return;
}
}
_this->_categories[id] = wxT("unknown-") + name;
_this->_unresolvedCategories.push_back(cat);
}
void XMLImportEngine::LoadOperation(XMLImportEngine* _this, const char** attrs)
{
int i;
Operation op;
long v;
double amount;
wxString id;
for (i=0; attrs[i]; i+=2)
{
if (!strcmp(attrs[i], "id"))
op.id = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "parent"))
op.parent = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "day"))
{
wxString(attrs[i+1], wxConvUTF8).ToLong(&v);
op.day = v;
}
else if (!strcmp(attrs[i], "month"))
{
wxString(attrs[i+1], wxConvUTF8).ToLong(&v);
op.month = v;
}
else if (!strcmp(attrs[i], "year"))
{
wxString(attrs[i+1], wxConvUTF8).ToLong(&v);
op.year = v;
}
else if (!strcmp(attrs[i], "amount"))
{
wxString(attrs[i+1], wxConvUTF8).ToDouble(&amount);
op.amount = amount;
}
else if (!strcmp(attrs[i], "description"))
op.description = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "category"))
op.category = _this->_categories[wxString(attrs[i+1], wxConvUTF8)];
else if (!strcmp(attrs[i], "fix_cost"))
op.fix_cost = (wxString(attrs[i+1], wxConvUTF8) == wxT("1"));
else if (!strcmp(attrs[i], "account"))
op.account = _this->_accounts[wxString(attrs[i+1], wxConvUTF8)];
else if (!strcmp(attrs[i], "checked"))
op.checked = (wxString(attrs[i+1], wxConvUTF8) == wxT("1"));
else if (!strcmp(attrs[i], "transfert"))
op.transfert = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "formula"))
op.formula = wxString(attrs[i+1], wxConvUTF8);
else if (!strcmp(attrs[i], "meta"))
op.meta = (wxString(attrs[i+1], wxConvUTF8) == wxT("1"));
else if (!strcmp(attrs[i], "virtual"))
op._virtual = (wxString(attrs[i+1], wxConvUTF8) == wxT("1"));
}
UNESCAPE_CHARS(op.description);
_this->_operations.push_back(op);
_this->_descriptions[op.id] = op.description;
_this->MatchPattern(op.description, op);
}
void XMLImportEngine::XmlStartElement(void* user_data, const xmlChar* name_, const xmlChar** attrs_)
{
XMLImportEngine* _this = (XMLImportEngine*) user_data;
int i;
const char** attrs = (const char**) attrs_;
const char* name = (const char*) name_;
// if (!first && strcmp(name, "Xml"))
// {
// throw "Invalid file !";
// }
// else
// first = 1;
if (!strcmp(name, "kisscount"))
{
for (i=0; attrs[i]; i+=2)
{
if (!strcmp(attrs[i], "version") && strcmp(attrs[i+1], "1"))
throw "Unsupported version !";
}
}
else if (!strcmp(name, "account"))
LoadAccount(_this, attrs);
else if (!strcmp(name, "account_amount"))
LoadAccountAmount(_this, attrs);
else if (!strcmp(name, "category"))
LoadCategory(_this, attrs);
else if (!strcmp(name, "operation"))
LoadOperation(_this, attrs);
}
XMLImportEngine::XMLImportEngine()
{
KissCount::RegisterImportEngine(this);
_shortExt = wxT("xml");
_longExt = _("KissCount xml files (*.xml)|*.xml");
_sax.startElement = XmlStartElement ;
}
XMLImportEngine::~XMLImportEngine()
{
}
bool XMLImportEngine::HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss)
{
int res = -1 ;
if (!ImportEngine::HandleFile(path, user, db, kiss)) return false;
try
{
res = xmlSAXUserParseFile(&_sax, this, path.mb_str()) >= 0;
}
catch (const char* s)
{
std::cout << "XMLImportEngine :: " << s << std::endl;
res = -1;
}
return res >= 0;
}

View File

@@ -0,0 +1,44 @@
/*
Copyright 2010-2011 Grégory Soutadé
This file is part of KissCount.
KissCount is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KissCount is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMLIMPORTENGINE_H
#define XMLIMPORTENGINE_H
#include <libxml/parser.h>
#include "ImportEngine.h"
class XMLImportEngine : public ImportEngine {
public:
XMLImportEngine();
~XMLImportEngine();
virtual bool HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss);
private:
xmlSAXHandler _sax;
static void XmlStartElement(void* user_data, const xmlChar* name_, const xmlChar** attrs_);
static void LoadAccount(XMLImportEngine* _this, const char** attrs);
static void LoadAccountAmount(XMLImportEngine* _this, const char** attrs);
static void LoadCategory(XMLImportEngine* _this, const char** attrs);
static void LoadOperation(XMLImportEngine* _this, const char** attrs);
};
#endif