From 85a79ab409601dca4109813fbd6009b2486574bb Mon Sep 17 00:00:00 2001 From: Dave Peticolas Date: Mon, 12 Feb 2001 22:35:20 +0000 Subject: [PATCH] James LewisMoss's xml patch. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@3643 57a11ea4-9604-0410-9ed3-97b8803252fd --- debian/rules | 4 +- src/engine/Makefile.am | 5 + src/engine/gnc-account-xml-v2.c | 220 ++++++++++++++++++++++++++++++ src/engine/sixtp-dom-generators.c | 31 +++++ src/engine/sixtp-dom-generators.h | 16 +++ src/engine/sixtp-dom-parsers.c | 61 +++++++++ src/engine/sixtp-dom-parsers.h | 21 +++ src/engine/sixtp-to-dom-parser.c | 4 +- src/engine/sixtp.c | 8 +- src/scm/c-interface.scm | 6 +- 10 files changed, 368 insertions(+), 8 deletions(-) create mode 100644 src/engine/gnc-account-xml-v2.c create mode 100644 src/engine/sixtp-dom-generators.c create mode 100644 src/engine/sixtp-dom-generators.h create mode 100644 src/engine/sixtp-dom-parsers.c create mode 100644 src/engine/sixtp-dom-parsers.h diff --git a/debian/rules b/debian/rules index 2f09934d66..f95b866700 100644 --- a/debian/rules +++ b/debian/rules @@ -19,7 +19,7 @@ configure: configure.in Makefile: Makefile.in configure dh_testdir $(checkdir) - ./configure --prefix=/usr \ + ./autogen.sh --prefix=/usr \ --sysconfdir=/etc \ --infodir=/usr/share/info \ --mandir=/usr/share/man @@ -29,7 +29,7 @@ build-stamp: Makefile dh_testdir $(checkdir) make - touch build-stamp +# touch build-stamp clean: dh_testdir diff --git a/src/engine/Makefile.am b/src/engine/Makefile.am index 5f699d51a7..911d23a577 100644 --- a/src/engine/Makefile.am +++ b/src/engine/Makefile.am @@ -20,6 +20,7 @@ libgncengine_la_SOURCES = \ TransLog.c \ date.c \ GNCId.c \ + gnc-account-xml-v2.c \ guid.c \ io-gncbin-r.c \ io-gncxml-r.c \ @@ -33,6 +34,8 @@ libgncengine_la_SOURCES = \ gnc-engine-util.c \ gnc-event.c \ gnc-numeric.c \ + sixtp-dom-generators.c \ + sixtp-dom-parsers.c \ sixtp-kvp-parser.c \ sixtp-stack.c \ sixtp-utils.c \ @@ -80,6 +83,8 @@ noinst_HEADERS = \ gnc-event-p.h \ gnc-numeric.h \ gnc-xml-helper.h \ + sixtp-dom-generators.h \ + sixtp-dom-parsers.h \ sixtp-parsers.h \ sixtp-writers.h \ sixtp-stack.h \ diff --git a/src/engine/gnc-account-xml-v2.c b/src/engine/gnc-account-xml-v2.c new file mode 100644 index 0000000000..375256e189 --- /dev/null +++ b/src/engine/gnc-account-xml-v2.c @@ -0,0 +1,220 @@ +#include "config.h" + +#include +#include + +#include "gnc-xml-helper.h" + +#include "sixtp.h" +#include "sixtp-utils.h" +#include "sixtp-parsers.h" + +#include "sixtp-dom-parsers.h" +#include "AccountP.h" +#include "Account.h" +#include "Group.h" + +xmlNodePtr +gnc_account_dom_tree_create(Account *act) +{ + return NULL; +} + +/***********************************************************************/ +static gboolean +account_name_handler (xmlNodePtr node, Account* act) +{ + if(node->content != NULL) + { + xaccAccountSetName(act, node->xmlChildrenNode->content); + return TRUE; + } + else + { + return FALSE; + } +} + +static gboolean +account_id_handler (xmlNodePtr node, Account* act) +{ + xaccAccountSetGUID(act, dom_tree_to_guid(node)); + return TRUE; +} + +static gboolean +account_type_handler (xmlNodePtr node, Account* act) +{ + int type; + + xaccAccountStringToType(node->childs->content, &type); + xaccAccountSetType(act, type); + return TRUE; +} + +static gboolean +account_currency_handler (xmlNodePtr node, Account* act) +{ + xaccAccountSetCurrency(act, dom_tree_to_gnc_commodity(node)); + return TRUE; +} + +static gboolean +account_security_handler (xmlNodePtr node, Account* act) +{ + xaccAccountSetCurrency(act, dom_tree_to_gnc_commodity(node)); + return TRUE; +} + +static gboolean +account_slots_handler (xmlNodePtr node, Account* act) +{ + return dom_tree_handle_kvp(act->kvp_data, node); +} + +static gboolean +account_parent_handler (xmlNodePtr node, Account* act) +{ + Account *parent; + GUID *gid = dom_tree_to_guid(node); + + parent = xaccAccountLookup(gid); + + xaccAccountInsertSubAccount(parent, act); + + xaccGUIDFree(gid); + return TRUE; +} + +struct dom_handlers +{ + char *tag; + + gboolean (*handler) (xmlNodePtr, Account*); + + int required; + int gotten; +}; + +static struct dom_handlers account_handlers_v2[] = { + { "act:name", account_name_handler, 1, 0 }, + { "act:id", account_id_handler, 1, 0 }, + { "act:type", account_type_handler, 1, 0 }, + { "act:currency", account_currency_handler, 1, 0 }, + { "act:security", account_security_handler, 0, 0 }, + { "act:slots", account_slots_handler, 0, 0 }, + { "act:parent", account_parent_handler, 0, 0 }, + { NULL, 0, 0, 0 } +}; + +static void +set_handlers(struct dom_handlers *handler_ptr) +{ + while(handler_ptr->tag != NULL) + { + handler_ptr->gotten = 0; + + handler_ptr++; + } +} + +static gboolean +all_required_gotten_p(struct dom_handlers *handler_ptr) +{ + while(handler_ptr->tag != NULL) + { + if(handler_ptr->required && ! handler_ptr->gotten) + { + return FALSE; + } + handler_ptr++; + } + return TRUE; +} + +static gboolean +gnc_xml_set_account_data(const gchar* tag, xmlNodePtr node, Account *acc, + struct dom_handlers *handler_ptr) +{ + while(handler_ptr->tag) + { + if(strcmp(tag, handler_ptr->tag) == 0) + { + (handler_ptr->handler)(node, acc); + break; + } + + handler_ptr++; + } + + if(!handler_ptr->tag) + { + g_warning("Unhandled account tag: %s\n", tag); + return FALSE; + } + + return TRUE; +} + +static gboolean +gnc_account_end_handler(gpointer data_for_children, + GSList* data_from_children, GSList* sibling_data, + gpointer parent_data, gpointer global_data, + gpointer *result, const gchar *tag) +{ + int successful; + Account *acc; + xmlNodePtr achild; + xmlNodePtr tree = (xmlNodePtr)data_for_children; + + successful = TRUE; + + acc = xaccMallocAccount(); + g_return_val_if_fail(acc, FALSE); + xaccAccountBeginEdit(acc); + + achild = tree->xmlChildrenNode; + + set_handlers(account_handlers_v2); + + while(!achild) + { + if(!gnc_xml_set_account_data(achild->name, achild, acc, + account_handlers_v2)) + { + successful = FALSE; + break; + } + achild = achild->next; + } + + xaccAccountCommitEdit(acc); + + if(!all_required_gotten_p(account_handlers_v2)) + { + successful = FALSE; + } + + if(!successful) + { + xaccFreeAccount(acc); + } + else + { + if(!xaccAccountGetParent(acc)) + { + /* FIXME: something like this */ + /* xaccGroupInsertAccount(global_data->accountgroup, acc); */ + } + } + + xmlFreeNode(data_for_children); + + return successful; +} + +sixtp* +gnc_account_sixtp_parser_create() +{ + return sixtp_dom_parser_new(gnc_account_end_handler); +} diff --git a/src/engine/sixtp-dom-generators.c b/src/engine/sixtp-dom-generators.c new file mode 100644 index 0000000000..1e61da7a52 --- /dev/null +++ b/src/engine/sixtp-dom-generators.c @@ -0,0 +1,31 @@ +#include "config.h" + +#include + +#include "gnc-xml-helper.h" + +#include "sixtp-dom-generators.h" +#include "GNCId.h" + + +xmlNodePtr +guid_to_dom_tree(GUID* gid) +{ + char *guid_str; + xmlNodePtr ret; + + ret = xmlNewNode(NULL, "test-guid"); + + xmlSetProp(ret, "type", "guid"); + + guid_str = guid_to_string(gid); + if (!guid_str) + { + printf("FAILURE: guid_to_string failed\n"); + return NULL; + } + + xmlNodeAddContent(ret, guid_str); + + return ret; +} diff --git a/src/engine/sixtp-dom-generators.h b/src/engine/sixtp-dom-generators.h new file mode 100644 index 0000000000..e5fea43d89 --- /dev/null +++ b/src/engine/sixtp-dom-generators.h @@ -0,0 +1,16 @@ +#ifndef _SIXTP_DOM_GENERATORS_H_ +#define _SIXTP_DOM_GENERATORS_H_ + +#include "config.h" + +#include + +#include "gnc-xml-helper.h" + +#include "sixtp-dom-generators.h" +#include "GNCId.h" + + +xmlNodePtr guid_to_dom_tree(GUID* gid); + +#endif /* _SIXTP_DOM_GENERATORS_H_ */ diff --git a/src/engine/sixtp-dom-parsers.c b/src/engine/sixtp-dom-parsers.c new file mode 100644 index 0000000000..47e0897f25 --- /dev/null +++ b/src/engine/sixtp-dom-parsers.c @@ -0,0 +1,61 @@ +#include "config.h" + +#include +#include + +#include "gnc-xml-helper.h" + +#include "sixtp-dom-parsers.h" +#include "GNCId.h" + +GUID* +dom_tree_to_guid(xmlNodePtr node) +{ + if(!node->properties) + { + return NULL; + } + + if(strcmp(node->properties->name, "type") != 0) + { + g_warning("Unknown attribute for id tag: %s\n", + node->properties->name); + return NULL; + } + + { + char *type = node->properties->val->content; + if(strcmp("guid", type) == 0) + { + GUID *gid = g_new(GUID, 1); + string_to_guid(node->xmlChildrenNode->content, gid); + return gid; + } + else if(strcmp("new", type) == 0) + { + /* FIXME: handle this case */ + return NULL; + } + else + { + g_warning("Unknown type %s for attribute type for tag %s", + type, node->properties->name); + return NULL; + } + } +} + +gnc_commodity* +dom_tree_to_gnc_commodity(xmlNodePtr node) +{ + + return NULL; +} + +gboolean +dom_tree_handle_kvp(kvp_frame* frame, xmlNodePtr node) +{ + + return FALSE; +} + diff --git a/src/engine/sixtp-dom-parsers.h b/src/engine/sixtp-dom-parsers.h new file mode 100644 index 0000000000..b1d6cf7224 --- /dev/null +++ b/src/engine/sixtp-dom-parsers.h @@ -0,0 +1,21 @@ +#ifndef _SIXTP_DOM_PARSERS_H_ +#define _SIXTP_DOM_PARSERS_H_ + +#include "config.h" + +#include + +#include "gnc-xml-helper.h" + +#include "gnc-commodity.h" +#include "kvp_frame.h" + +#include "GNCId.h" + +GUID* dom_tree_to_guid(xmlNodePtr node); + +gnc_commodity* dom_tree_to_gnc_commodity(xmlNodePtr node); + +gboolean dom_tree_handle_kvp(kvp_frame* frame, xmlNodePtr node); + +#endif /* _SIXTP_DOM_PARSERS_H_ */ diff --git a/src/engine/sixtp-to-dom-parser.c b/src/engine/sixtp-to-dom-parser.c index ab20904298..e64d725af2 100644 --- a/src/engine/sixtp-to-dom-parser.c +++ b/src/engine/sixtp-to-dom-parser.c @@ -59,8 +59,8 @@ static gboolean dom_chars_handler( { if(length > 0 && !is_whitespace(text, length)) { - gchar *stuff = g_strndup(text, length); - xmlNodeSetContent((xmlNodePtr)parent_data, stuff); + /* gchar *stuff = g_strndup(text, length); */ + xmlNodeSetContentLen((xmlNodePtr)parent_data, text, length); } return TRUE; diff --git a/src/engine/sixtp.c b/src/engine/sixtp.c index 73126dbf4c..3be9b14210 100644 --- a/src/engine/sixtp.c +++ b/src/engine/sixtp.c @@ -180,7 +180,7 @@ sixtp_set_any(sixtp *tochange, int cleanup, ...) default: va_end(ap); - g_warning("Bogus sixtp type %d\n", type); + g_error("Bogus sixtp type %d\n", type); if(cleanup) { sixtp_destroy(tochange); @@ -643,13 +643,15 @@ sixtp_parse_buffer(sixtp *sixtp, if(ctxt->data.parsing_ok) { - *parse_result = ctxt->top_frame->frame_data; + if(parse_result) + *parse_result = ctxt->top_frame->frame_data; sixtp_context_destroy(ctxt); return TRUE; } else { - *parse_result = NULL; + if(parse_result) + *parse_result = NULL; sixtp_handle_catastrophe(&ctxt->data); sixtp_context_destroy(ctxt); return FALSE; diff --git a/src/scm/c-interface.scm b/src/scm/c-interface.scm index 1dc83b45cd..225701dba6 100644 --- a/src/scm/c-interface.scm +++ b/src/scm/c-interface.scm @@ -17,6 +17,8 @@ (require 'hash-table) +(use-modules (ice-9 syncase)) + (define (gnc:error->string tag args) (define (write-error port) (if (and (list? args) (not (null? args))) @@ -41,7 +43,9 @@ (define gnc:gettext gnc:gettext-helper) (define gnc:_ gnc:gettext) (define _ gnc:gettext) -(define (N_ x) x) +(define-syntax N_ + (syntax-rules () + ((_ x) x))) ;; This database can be used to store and retrieve translatable