29 #include "gnc-xml-helper.h"
31 #include "sixtp-utils.h"
32 #include "sixtp-dom-parsers.h"
37 dom_tree_to_guid(xmlNodePtr node)
39 if (!node->properties)
44 if (strcmp((
char*) node->properties->name,
"type") != 0)
46 PERR(
"Unknown attribute for id tag: %s",
47 node->properties->name ?
48 (
char *) node->properties->name :
"(null)");
55 type = (
char*)xmlNodeGetContent (node->properties->xmlAttrPropertyValue);
58 if ((g_strcmp0(
"guid", type) == 0) || (g_strcmp0(
"new", type) == 0))
63 guid_str = (
char*)xmlNodeGetContent (node->xmlChildrenNode);
71 PERR(
"Unknown type %s for attribute type for tag %s",
72 type ? type :
"(null)",
73 node->properties->name ?
74 (
char *) node->properties->name :
"(null)");
82 dom_tree_to_integer_kvp_value(xmlNodePtr node)
88 text = dom_tree_to_text(node);
90 if (string_to_gint64(text, &daint))
92 ret = kvp_value_new_gint64(daint);
100 dom_tree_to_integer(xmlNodePtr node, gint64 *daint)
105 text = dom_tree_to_text(node);
107 ret = string_to_gint64(text, daint);
115 dom_tree_to_guint16(xmlNodePtr node, guint16 *i)
120 ret = dom_tree_to_guint(node, &j);
126 dom_tree_to_guint(xmlNodePtr node, guint *i)
128 gchar *text, *endptr;
131 text = dom_tree_to_text(node);
135 *i = (guint) strtoul(text, &endptr, 0);
136 ret = (endptr != text);
142 dom_tree_to_boolean(xmlNodePtr node, gboolean* b)
146 text = dom_tree_to_text(node);
147 if (g_ascii_strncasecmp(text,
"true", 4) == 0)
152 else if (g_ascii_strncasecmp(text,
"false", 5) == 0)
165 dom_tree_to_double_kvp_value(xmlNodePtr node)
171 text = dom_tree_to_text(node);
173 if (string_to_double(text, &dadoub))
175 ret = kvp_value_new_double(dadoub);
184 dom_tree_to_numeric_kvp_value(xmlNodePtr node)
189 danum = dom_tree_to_gnc_numeric(node);
202 dom_tree_to_string_kvp_value(xmlNodePtr node)
207 datext = dom_tree_to_text(node);
210 ret = kvp_value_new_string(datext);
219 dom_tree_to_guid_kvp_value(xmlNodePtr node)
224 daguid = dom_tree_to_guid(node);
227 ret = kvp_value_new_guid(daguid);
236 dom_tree_to_timespec_kvp_value (xmlNodePtr node)
241 ts = dom_tree_to_timespec (node);
242 if (ts.tv_sec || ts.tv_nsec)
244 ret = kvp_value_new_timespec (ts);
250 dom_tree_to_gdate_kvp_value (xmlNodePtr node)
255 date = dom_tree_to_gdate(node);
259 ret = kvp_value_new_gdate(*date);
268 string_to_binary(
const gchar *str,
void **v, guint64 *data_len)
274 g_return_val_if_fail(v != NULL, FALSE);
275 g_return_val_if_fail(data_len != NULL, FALSE);
277 str_len = strlen(str);
282 if ((str_len % 2) != 0)
284 *data_len = str_len / 2;
285 data = g_new0(guchar, *data_len);
287 for (j = 0, i = 0; i < str_len; i += 2, j++)
293 tmpstr[1] = str[i + 1];
296 converted = strtol(tmpstr, NULL, 16);
298 data[j] = (
unsigned char)converted;
307 dom_tree_to_list_kvp_value(xmlNodePtr node)
313 for (mark = node->xmlChildrenNode; mark; mark = mark->next)
317 if (g_strcmp0 ((
char*)mark->name,
"text") == 0)
320 new_val = dom_tree_to_kvp_value(mark);
323 list = g_list_append(list, (gpointer)new_val);
333 dom_tree_to_frame_kvp_value(xmlNodePtr node)
338 frame = dom_tree_to_kvp_frame(node);
342 ret = kvp_value_new_frame(frame);
354 KvpValue* (*converter)(xmlNodePtr node);
359 {
"integer", dom_tree_to_integer_kvp_value },
360 {
"double", dom_tree_to_double_kvp_value },
361 {
"numeric", dom_tree_to_numeric_kvp_value },
362 {
"string", dom_tree_to_string_kvp_value },
363 {
"guid", dom_tree_to_guid_kvp_value },
364 {
"timespec", dom_tree_to_timespec_kvp_value },
365 {
"gdate", dom_tree_to_gdate_kvp_value },
366 {
"list", dom_tree_to_list_kvp_value },
367 {
"frame", dom_tree_to_frame_kvp_value },
372 dom_tree_to_kvp_value(xmlNodePtr node)
379 xml_type = xmlGetProp(node, BAD_CAST
"type");
382 type = g_strdup ((
char*) xml_type);
388 for (mark = val_converters; mark->tag; mark++)
390 if (g_strcmp0(type, mark->tag) == 0)
392 ret = (mark->converter)(node);
407 dom_tree_to_kvp_frame_given(xmlNodePtr node,
KvpFrame *frame)
411 g_return_val_if_fail(node, FALSE);
412 g_return_val_if_fail(frame, FALSE);
414 for (mark = node->xmlChildrenNode; mark; mark = mark->next)
416 if (g_strcmp0((
char*)mark->name,
"slot") == 0)
422 for (mark2 = mark->xmlChildrenNode; mark2; mark2 = mark2->next)
424 if (g_strcmp0((
char*)mark2->name,
"slot:key") == 0)
426 key = dom_tree_to_text(mark2);
428 else if (g_strcmp0((
char*)mark2->name,
"slot:value") == 0)
430 val = dom_tree_to_kvp_value(mark2);
459 dom_tree_to_kvp_frame(xmlNodePtr node)
463 g_return_val_if_fail(node, NULL);
467 if (dom_tree_to_kvp_frame_given(node, ret))
477 dom_tree_to_text(xmlNodePtr tree)
491 g_return_val_if_fail(tree, NULL);
494 if (!tree->xmlChildrenNode)
496 DEBUG(
"No children");
500 temp = (
char*)xmlNodeListGetString (NULL, tree->xmlChildrenNode, TRUE);
503 DEBUG(
"Null string");
507 DEBUG(
"node string [%s]", (temp == NULL ?
"(null)" : temp));
508 result = g_strdup (temp);
514 dom_tree_to_gnc_numeric(xmlNodePtr node)
516 gchar *content = dom_tree_to_text(node);
546 dom_tree_to_timespec(xmlNodePtr node)
560 gboolean seen_s = FALSE;
561 gboolean seen_ns = FALSE;
567 for (n = node->xmlChildrenNode; n; n = n->next)
571 case XML_COMMENT_NODE:
574 case XML_ELEMENT_NODE:
575 if (g_strcmp0(
"ts:date", (
char*)n->name) == 0)
579 return timespec_failure(ret);
583 gchar *content = dom_tree_to_text(n);
586 return timespec_failure(ret);
589 if (!string_to_timespec_secs(content, &ret))
592 return timespec_failure(ret);
598 else if (g_strcmp0(
"ts:ns", (
char*)n->name) == 0)
602 return timespec_failure(ret);
606 gchar *content = dom_tree_to_text(n);
609 return timespec_failure(ret);
612 if (!string_to_timespec_nsecs(content, &ret))
615 return timespec_failure(ret);
623 PERR(
"unexpected sub-node.");
624 return timespec_failure(ret);
631 PERR(
"no ts:date node found.");
632 return timespec_failure(ret);
639 dom_tree_to_gdate(xmlNodePtr node)
650 gboolean seen_date = FALSE;
656 for (n = node->xmlChildrenNode; n; n = n->next)
660 case XML_COMMENT_NODE:
663 case XML_ELEMENT_NODE:
664 if (g_strcmp0(
"gdate", (
char*)n->name) == 0)
672 gchar *content = dom_tree_to_text(n);
673 gint year, month, day;
679 if (sscanf(content,
"%d-%d-%d", &year, &month, &day ) != 3)
686 g_date_set_dmy( ret, day, month, year );
687 if ( !g_date_valid( ret ) )
689 PWARN(
"invalid date");
696 PERR(
"unexpected sub-node.");
703 PWARN(
"no gdate node found.");
715 dom_tree_to_commodity_ref_no_engine(xmlNodePtr node,
QofBook *book)
728 gchar *space_str = NULL;
729 gchar *id_str = NULL;
732 if (!node)
return NULL;
733 if (!node->xmlChildrenNode)
return NULL;
735 for (n = node->xmlChildrenNode; n; n = n->next)
739 case XML_COMMENT_NODE:
742 case XML_ELEMENT_NODE:
743 if (g_strcmp0(
"cmdty:space", (
char*)n->name) == 0)
751 gchar *content = dom_tree_to_text(n);
752 if (!content)
return NULL;
756 else if (g_strcmp0(
"cmdty:id", (
char*)n->name) == 0)
764 gchar *content = dom_tree_to_text(n);
765 if (!content)
return NULL;
771 PERR(
"unexpected sub-node.");
776 if (!(space_str && id_str))
782 g_strstrip(space_str);
794 dom_tree_to_commodity_ref(xmlNodePtr node,
QofBook *book)
800 daref = dom_tree_to_commodity_ref_no_engine(node, book);
804 g_return_val_if_fail (table != NULL, NULL);
806 ret = gnc_commodity_table_lookup (table,
812 g_return_val_if_fail (ret != NULL, NULL);
823 for (; handlers->tag != NULL; handlers++)
825 handlers->gotten = 0;
829 static inline gboolean
833 for (; handlers->tag != NULL; handlers++)
835 if (handlers->required && ! handlers->gotten)
837 PERR(
"Not defined and it should be: %s",
838 handlers->tag ? handlers->tag :
"(null)");
846 static inline gboolean
847 gnc_xml_set_data(
const gchar* tag, xmlNodePtr node, gpointer item,
850 for (; handlers->tag != NULL; handlers++)
852 if (g_strcmp0(tag, handlers->tag) == 0)
854 (handlers->handler)(node, item);
855 handlers->gotten = TRUE;
862 PERR(
"Unhandled tag: %s",
863 tag ? tag :
"(null)");
875 gboolean successful = TRUE;
877 dom_tree_handlers_reset(handlers);
879 for (achild = node->xmlChildrenNode; achild; achild = achild->next)
882 if (g_strcmp0 ((
char*)achild->name,
"text") == 0)
885 if (!gnc_xml_set_data((
char*)achild->name, achild, data, handlers))
887 PERR(
"gnc_xml_set_data failed");
893 if (!dom_tree_handlers_all_gotten_p(handlers))
895 PERR(
"didn't find all of the expected tags in the input");
903 dom_tree_valid_timespec (
Timespec *ts,
const xmlChar *name)
906 if (ts->tv_sec || ts->tv_nsec)
909 g_warning(
"Invalid timestamp in data file. Look for a '%s' entry "
910 "with a date of 1969-12-31 or 1970-01-01.", name);
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
#define DEBUG(format, args...)
gboolean string_to_guid(const gchar *string, GncGUID *guid)
Use a 64-bit unsigned int timespec.
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
#define PERR(format, args...)
gboolean string_to_gnc_numeric(const gchar *str, gnc_numeric *n)
void kvp_frame_delete(KvpFrame *frame)
#define PWARN(format, args...)
gnc_commodity * gnc_commodity_new(QofBook *book, const char *fullname, const char *name_space, const char *mnemonic, const char *cusip, int fraction)
void kvp_frame_set_slot_nc(KvpFrame *frame, const gchar *key, KvpValue *value)
#define kvp_value_new_gnc_numeric
All type declarations for the whole Gnucash engine.
KvpValue * kvp_value_new_glist_nc(GList *lst)
struct KvpFrameImpl KvpFrame
KvpFrame * kvp_frame_new(void)
struct KvpValueImpl KvpValue
void gnc_commodity_destroy(gnc_commodity *cm)
const gchar * QofLogModule