GnuCash 7574033+
Loading...
Searching...
No Matches
sixtp-dom-parsers.cpp
1/********************************************************************
2 * sixtp-dom-parsers.c *
3 * Copyright 2001 Gnumatic, Inc. *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation; either version 2 of *
8 * the License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License*
16 * along with this program; if not, contact: *
17 * *
18 * Free Software Foundation Voice: +1-617-542-5942 *
19 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 * Boston, MA 02110-1301, USA gnu@gnu.org *
21 * *
22 ********************************************************************/
23#include <glib.h>
24
25#include <config.h>
26
27#include <string.h>
28
29#include <gnc-engine.h>
30
31#include "gnc-xml-helper.h"
32#include "sixtp-utils.h"
33#include "sixtp-dom-parsers.h"
34#include <kvp-frame.hpp>
35
36static QofLogModule log_module = GNC_MOD_IO;
37
38const char*
39dom_node_to_text (xmlNodePtr node) noexcept
40{
41 if (node && node->children && node->children->type == XML_TEXT_NODE
42 && !node->children->next)
43 return reinterpret_cast<const char*>(node->children->content);
44 return nullptr;
45}
46
47std::optional<GncGUID>
48dom_tree_to_guid (xmlNodePtr node)
49{
50 auto type = xmlGetProp (node, BAD_CAST "type");
51 if (!type)
52 return {};
53
54 bool ok = !g_strcmp0 ((char*)type, "guid") || !g_strcmp0 ((char*)type, "new");
55
56 xmlFree (type);
57
58 if (!ok)
59 return {};
60
61 auto extract_guid = [](auto str) -> std::optional<GncGUID>
62 {
63 if (GncGUID guid; string_to_guid (str, &guid))
64 return guid;
65
66 return {};
67 };
68
69 return apply_xmlnode_text<std::optional<GncGUID>>(extract_guid, node);
70}
71
72static KvpValue*
73dom_tree_to_integer_kvp_value (xmlNodePtr node)
74{
75 auto node_to_int_kvp = [](auto txt) -> KvpValue*
76 {
77 if (gint64 daint; string_to_gint64 (txt, &daint))
78 return new KvpValue{daint};
79
80 return nullptr;
81 };
82 return apply_xmlnode_text<KvpValue*> (node_to_int_kvp, node, nullptr);
83}
84
85template <typename T>
86static bool
87dom_tree_to_num (xmlNodePtr node, std::function<bool(const char*, T*)>string_to_num, T* num_ptr)
88{
89 return apply_xmlnode_text<T>([&](auto txt){ return string_to_num (txt, num_ptr);}, node, false);
90}
91
92gboolean
93dom_tree_to_integer (xmlNodePtr node, gint64* daint)
94{
95 return dom_tree_to_num<gint64>(node, string_to_gint64, daint);
96}
97
98gboolean
99dom_tree_to_guint16 (xmlNodePtr node, guint16* i)
100{
101 return dom_tree_to_num<guint16>(node, string_to_guint16, i);
102}
103
104gboolean
105dom_tree_to_guint (xmlNodePtr node, guint* i)
106{
107 return dom_tree_to_num<guint>(node, string_to_guint, i);
108}
109
110gboolean
111dom_tree_to_boolean (xmlNodePtr node, gboolean* b)
112{
113 auto set_bool = [b](auto text) -> gboolean
114 {
115 if (g_ascii_strncasecmp (text, "true", 4) == 0)
116 {
117 *b = TRUE;
118 return TRUE;
119 }
120 else if (g_ascii_strncasecmp (text, "false", 5) == 0)
121 {
122 *b = FALSE;
123 return TRUE;
124 }
125 else
126 {
127 *b = FALSE;
128 return FALSE;
129 }
130 };
131 return apply_xmlnode_text<gboolean> (set_bool, node);
132}
133
134static KvpValue*
135dom_tree_to_double_kvp_value (xmlNodePtr node)
136{
137 auto node_to_double_kvp = [](auto txt) -> KvpValue*
138 {
139 if (double dadoub; string_to_double (txt, &dadoub)) return new KvpValue{dadoub};
140 return nullptr;
141 };
142 return apply_xmlnode_text<KvpValue*> (node_to_double_kvp, node, nullptr);
143}
144
145static KvpValue*
146dom_tree_to_numeric_kvp_value (xmlNodePtr node)
147{
148 return new KvpValue {dom_tree_to_gnc_numeric (node)};
149}
150
151static KvpValue*
152dom_tree_to_string_kvp_value (xmlNodePtr node)
153{
154 auto node_to_string_kvp = [](auto txt) -> KvpValue*
155 {
156 return new KvpValue {g_strdup (txt)};
157 };
158 return apply_xmlnode_text<KvpValue*> (node_to_string_kvp, node, nullptr);
159}
160
161static KvpValue*
162dom_tree_to_guid_kvp_value (xmlNodePtr node)
163{
164 auto daguid = dom_tree_to_guid (node);
165 return daguid ? new KvpValue {guid_copy (&*daguid)} : nullptr;
166}
167
168static KvpValue*
169dom_tree_to_time64_kvp_value (xmlNodePtr node)
170{
171 Time64 t{dom_tree_to_time64 (node)};
172 return new KvpValue {t};
173}
174
175static KvpValue*
176dom_tree_to_gdate_kvp_value (xmlNodePtr node)
177{
178 auto date = dom_tree_to_gdate (node);
179 if (!date) return nullptr;
180 return new KvpValue {*date};
181}
182
183static KvpValue* dom_tree_to_kvp_value (xmlNodePtr node);
184//needed for test access as well as internal use.
185KvpFrame* dom_tree_to_kvp_frame (xmlNodePtr node);
186
187static KvpValue*
188dom_tree_to_list_kvp_value (xmlNodePtr node)
189{
190 GList* list = NULL;
191 xmlNodePtr mark;
192 KvpValue* ret = NULL;
193
194 for (mark = node->xmlChildrenNode; mark; mark = mark->next)
195 {
196 KvpValue* new_val;
197
198 if (g_strcmp0 ((char*)mark->name, "text") == 0)
199 continue;
200
201 new_val = dom_tree_to_kvp_value (mark);
202 if (new_val)
203 {
204 list = g_list_prepend (list, (gpointer)new_val);
205 }
206 }
207
208 list = g_list_reverse (list);
209
210 ret = new KvpValue {list};
211
212 return ret;
213}
214
215static KvpValue*
216dom_tree_to_frame_kvp_value (xmlNodePtr node)
217{
218 KvpFrame* frame = dom_tree_to_kvp_frame (node);
219 return frame ? new KvpValue {frame} : nullptr;
220}
221
222
224{
225 const gchar* tag;
226 KvpValue* (*converter) (xmlNodePtr node);
227};
228/* Note: The type attribute must remain 'timespec' to maintain compatibility.
229 */
230
231struct kvp_val_converter val_converters[] =
232{
233 { "integer", dom_tree_to_integer_kvp_value },
234 { "double", dom_tree_to_double_kvp_value },
235 { "numeric", dom_tree_to_numeric_kvp_value },
236 { "string", dom_tree_to_string_kvp_value },
237 { "guid", dom_tree_to_guid_kvp_value },
238 { "timespec", dom_tree_to_time64_kvp_value },
239 { "gdate", dom_tree_to_gdate_kvp_value },
240 { "list", dom_tree_to_list_kvp_value },
241 { "frame", dom_tree_to_frame_kvp_value },
242 { 0, 0 },
243};
244
245static KvpValue*
246dom_tree_to_kvp_value (xmlNodePtr node)
247{
248 xmlChar* xml_type;
249 struct kvp_val_converter* mark;
250 KvpValue* ret = NULL;
251
252 xml_type = xmlGetProp (node, BAD_CAST "type");
253
254 for (mark = val_converters; mark->tag; mark++)
255 {
256 if (g_strcmp0 (reinterpret_cast<char*>(xml_type), mark->tag) == 0)
257 {
258 ret = (mark->converter) (node);
259 }
260 }
261
262 if (!mark->tag)
263 {
264 /* FIXME: deal with unknown type tag here */
265 }
266
267 xmlFree (xml_type);
268
269 return ret;
270}
271
272static gboolean
273dom_tree_to_kvp_frame_given (xmlNodePtr node, KvpFrame* frame)
274{
275 xmlNodePtr mark;
276
277 g_return_val_if_fail (node, FALSE);
278 g_return_val_if_fail (frame, FALSE);
279
280 for (mark = node->xmlChildrenNode; mark; mark = mark->next)
281 {
282 if (g_strcmp0 ((char*)mark->name, "slot") == 0)
283 {
284 xmlNodePtr mark2;
285 const gchar* key = NULL;
286 std::optional<std::string> maybe_key;
287 KvpValue* val = NULL;
288
289 for (mark2 = mark->xmlChildrenNode; mark2; mark2 = mark2->next)
290 {
291 if (g_strcmp0 ((char*)mark2->name, "slot:key") == 0)
292 {
293 key = dom_node_to_text (mark2);
294 if (!key)
295 {
296 maybe_key = dom_tree_to_text (mark2);
297 key = maybe_key ? maybe_key->c_str() : nullptr;
298 }
299 }
300 else if (g_strcmp0 ((char*)mark2->name, "slot:value") == 0)
301 {
302 val = dom_tree_to_kvp_value (mark2);
303 }
304 else
305 {
306 /* FIXME: should put some error here.
307 * But ignore text type! */
308 }
309 }
310
311 if (key)
312 {
313 if (val)
314 {
315 //We're deleting the old KvpValue returned by replace_nc().
316 delete frame->set ({key}, val);
317 }
318 else
319 {
320 /* FIXME: should put some error here */
321 }
322 }
323 }
324 }
325
326 return TRUE;
327}
328
329
330KvpFrame*
331dom_tree_to_kvp_frame (xmlNodePtr node)
332{
333 g_return_val_if_fail (node, NULL);
334
335 auto ret = new KvpFrame;
336
337 if (dom_tree_to_kvp_frame_given (node, ret))
338 return ret;
339
340 delete ret;
341 return NULL;
342}
343
344gboolean
345dom_tree_create_instance_slots (xmlNodePtr node, QofInstance* inst)
346{
347 KvpFrame* frame = qof_instance_get_slots (inst);
348 return dom_tree_to_kvp_frame_given (node, frame);
349}
350
351std::optional<std::string>
352dom_tree_to_text (xmlNodePtr tree)
353{
354 /* Expect *only* text and comment sibling nodes in the given tree --
355 which actually may only be a "list". i.e. if you're trying to
356 extract bar from <foo>bar</foo>, pass in <foo>->xmlChildrenNode
357 to this function. This expectation is different from the rest of
358 the dom_tree_to_* converters...
359
360 Ignores comment nodes and collapse text nodes into one string.
361 Returns NULL if expectations are unsatisfied.
362 */
363 std::string rv;
364 gchar* temp;
365
366 g_return_val_if_fail (tree, std::nullopt);
367
368 /* no nodes means it's an empty string text */
369 if (!tree->xmlChildrenNode)
370 {
371 DEBUG ("No children");
372 return "";
373 }
374
375 temp = (char*)xmlNodeListGetString (NULL, tree->xmlChildrenNode, TRUE);
376 if (!temp)
377 {
378 DEBUG ("Null string");
379 return std::nullopt;
380 }
381
382 DEBUG ("node string [%s]", (temp == NULL ? "(null)" : temp));
383 rv = temp;
384 xmlFree (temp);
385 return rv;
386}
387
388gnc_numeric
389dom_tree_to_gnc_numeric (xmlNodePtr node)
390{
391 auto node_to_numeric = [](auto txt)
392 {
393 gnc_numeric num = gnc_numeric_from_string(txt);
394 return gnc_numeric_check (num) ? gnc_numeric_zero() : num;
395 };
396 return apply_xmlnode_text<gnc_numeric> (node_to_numeric, node, gnc_numeric_zero());
397}
398
399
400time64
401dom_tree_to_time64 (xmlNodePtr node)
402{
403 /* Turn something like this
404
405 <date-posted>
406 <ts:date>Mon, 05 Jun 2000 23:16:19 -0500</ts:date>
407 </date-posted>
408
409 into a time64, returning INT64_MAX that we're using to flag an erroneous
410 date if there's a problem. Only one ts:date element is permitted for any
411 date attribute.
412 */
413
414 time64 ret {INT64_MAX};
415 gboolean seen = FALSE;
416 xmlNodePtr n;
417
418 for (n = node->xmlChildrenNode; n; n = n->next)
419 {
420 switch (n->type)
421 {
422 case XML_COMMENT_NODE:
423 case XML_TEXT_NODE:
424 break;
425 case XML_ELEMENT_NODE:
426 if (g_strcmp0 ("ts:date", (char*)n->name) == 0)
427 {
428 if (seen)
429 {
430 return INT64_MAX;
431 }
432 seen = TRUE;
433 ret = apply_xmlnode_text<time64> (gnc_iso8601_to_time64_gmt, n, INT64_MAX);
434 }
435 break;
436 default:
437 PERR ("unexpected sub-node.");
438 return INT64_MAX;
439 break;
440 }
441 }
442
443 if (!seen)
444 {
445 PERR ("no ts:date node found.");
446 return INT64_MAX;
447 }
448
449 return ret;
450}
451
452std::optional<GDate>
453dom_tree_to_gdate (xmlNodePtr node)
454{
455 /* Turn something like this
456
457 <sx:startdate>
458 <gdate>2001-04-03</gdate>
459 </sx:startdate>
460
461 into a GDate. If the xml is invalid, returns NULL. */
462
463 GDate ret;
464 gboolean seen_date = FALSE;
465 xmlNodePtr n;
466
467 auto try_setting_date = [&ret](const char *content) -> bool
468 {
469 gint year = 0, month = 0, day = 0;
470 if (sscanf (content, "%d-%d-%d", &year, &month, &day) != 3) return false;
471 g_date_set_dmy (&ret, day, static_cast<GDateMonth>(month), year);
472 return (g_date_valid (&ret));
473 };
474
475 /* creates an invalid date */
476 g_date_clear (&ret, 1);
477
478 for (n = node->xmlChildrenNode; n; n = n->next)
479 {
480 switch (n->type)
481 {
482 case XML_COMMENT_NODE:
483 case XML_TEXT_NODE:
484 break;
485 case XML_ELEMENT_NODE:
486 if (g_strcmp0 ("gdate", (char*)n->name) == 0)
487 {
488 if (seen_date || !apply_xmlnode_text<bool> (try_setting_date, n))
489 return std::nullopt;
490 seen_date = TRUE;
491 }
492 break;
493 default:
494 PERR ("unexpected sub-node.");
495 return std::nullopt;
496 }
497 }
498
499 if (!seen_date)
500 {
501 PWARN ("no gdate node found.");
502 return std::nullopt;
503 }
504
505 return std::make_optional<GDate>(ret);
506}
507
509{
510 std::string space;
511 std::string id;
512};
513
514std::string
515gnc_strstrip (std::string_view sv)
516{
517 while (!sv.empty () && g_ascii_isspace (sv.front())) sv.remove_prefix (1);
518 while (!sv.empty () && g_ascii_isspace (sv.back())) sv.remove_suffix (1);
519 return std::string (sv);
520}
521
522static std::optional<CommodityRef>
523parse_commodity_ref (xmlNodePtr node, QofBook* book)
524{
525 /* Turn something like this
526
527 <currency>
528 <cmdty:space>NASDAQ</cmdty:space>
529 <cmdty:id>LNUX</cmdty:space>
530 </currency>
531
532 into a gnc_commodity*, returning NULL on failure. Both sub-nodes
533 are required, though for now, order is irrelevant. */
534
535 CommodityRef rv;
536 bool space_set{false};
537 bool id_set{false};
538 xmlNodePtr n;
539
540 if (!node) return {};
541 if (!node->xmlChildrenNode) return {};
542
543 for (n = node->xmlChildrenNode; n; n = n->next)
544 {
545 switch (n->type)
546 {
547 case XML_COMMENT_NODE:
548 case XML_TEXT_NODE:
549 break;
550 case XML_ELEMENT_NODE:
551 if (g_strcmp0 ("cmdty:space", (char*)n->name) == 0)
552 {
553 if (space_set)
554 {
555 return {};
556 }
557 rv.space = apply_xmlnode_text<std::string> (gnc_strstrip, n);
558 space_set = true;
559 }
560 else if (g_strcmp0 ("cmdty:id", (char*)n->name) == 0)
561 {
562 if (id_set)
563 {
564 return {};
565 }
566 rv.id = apply_xmlnode_text<std::string> (gnc_strstrip, n);
567 id_set = true;
568 }
569 break;
570 default:
571 PERR ("unexpected sub-node.");
572 return {};
573 break;
574 }
575 }
576 if (space_set && id_set)
577 return rv;
578
579 return {};
580}
581
582gnc_commodity*
583dom_tree_to_commodity_ref_no_engine (xmlNodePtr node, QofBook* book)
584{
585 auto ref = parse_commodity_ref (node, book);
586
587 if (!ref)
588 return nullptr;
589
590 return gnc_commodity_new (book, nullptr, ref->space.c_str(), ref->id.c_str(),
591 nullptr, 0);
592}
593
594gnc_commodity*
595dom_tree_to_commodity_ref (xmlNodePtr node, QofBook* book)
596{
597 gnc_commodity* ret;
598 gnc_commodity_table* table;
599
600 auto ref = parse_commodity_ref (node, book);
601
602 if (!ref)
603 return nullptr;
604
606
607 g_return_val_if_fail (table != NULL, NULL);
608
609 ret = gnc_commodity_table_lookup (table, ref->space.c_str(), ref->id.c_str());
610
611 g_return_val_if_fail (ret != NULL, NULL);
612
613 return ret;
614}
615
616/***********************************************************************/
617/* generic parser */
618
619static inline void
620dom_tree_handlers_reset (struct dom_tree_handler* handlers)
621{
622 for (; handlers->tag != NULL; handlers++)
623 {
624 handlers->gotten = 0;
625 }
626}
627
628static inline gboolean
629dom_tree_handlers_all_gotten_p (struct dom_tree_handler* handlers)
630{
631 gboolean ret = TRUE;
632 for (; handlers->tag != NULL; handlers++)
633 {
634 if (handlers->required && ! handlers->gotten)
635 {
636 PERR ("Not defined and it should be: %s",
637 handlers->tag ? handlers->tag : "(null)");
638 ret = FALSE;
639 }
640 }
641 return ret;
642}
643
644
645static inline gboolean
646gnc_xml_set_data (const gchar* tag, xmlNodePtr node, gpointer item,
647 struct dom_tree_handler* handlers)
648{
649 for (; handlers->tag != NULL; handlers++)
650 {
651 if (g_strcmp0 (tag, handlers->tag) == 0)
652 {
653 (handlers->handler) (node, item);
654 handlers->gotten = TRUE;
655 break;
656 }
657 }
658
659 if (!handlers->tag)
660 {
661 PERR ("Unhandled tag: %s",
662 tag ? tag : "(null)");
663 return FALSE;
664 }
665
666 return TRUE;
667}
668
669gboolean
670dom_tree_generic_parse (xmlNodePtr node, struct dom_tree_handler* handlers,
671 gpointer data)
672{
673 xmlNodePtr achild;
674 gboolean successful = TRUE;
675
676 dom_tree_handlers_reset (handlers);
677
678 for (achild = node->xmlChildrenNode; achild; achild = achild->next)
679 {
680 /* ignore stray text nodes */
681 if (g_strcmp0 ((char*)achild->name, "text") == 0)
682 continue;
683
684 if (!gnc_xml_set_data ((char*)achild->name, achild, data, handlers))
685 {
686 PERR ("gnc_xml_set_data failed");
687 successful = FALSE;
688 continue;
689 }
690 }
691
692 if (!dom_tree_handlers_all_gotten_p (handlers))
693 {
694 PERR ("didn't find all of the expected tags in the input");
695 successful = FALSE;
696 }
697
698 return successful;
699}
700
701gboolean
702dom_tree_valid_time64 (time64 val, const xmlChar * name)
703{
704 if (val != INT64_MAX)
705 return TRUE;
706 g_warning ("Invalid timestamp in data file. Look for a '%s' entry "
707 "with a year outside of the valid range: 1400..10000", name);
708 return FALSE;
709}
All type declarations for the whole Gnucash engine.
gnc_commodity * gnc_commodity_new(QofBook *book, const char *fullname, const char *name_space, const char *mnemonic, const char *cusip, int fraction)
Create a new commodity.
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition gnc-date.h:87
GncGUID * guid_copy(const GncGUID *guid)
Returns a newly allocated GncGUID that matches the passed-in GUID.
Definition guid.cpp:155
#define DEBUG(format, args...)
Print a debugging message.
Definition qoflog.h:264
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250
gnc_numeric gnc_numeric_from_string(const gchar *str)
Read a gnc_numeric from str, skipping any leading whitespace.
GNCNumericErrorCode gnc_numeric_check(gnc_numeric in)
Check for error signal in value.
The type used to store guids in C.
Definition guid.h:75
QofBook reference.
Definition qofbook-p.hpp:47