Line data Source code
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 :
36 : static QofLogModule log_module = GNC_MOD_IO;
37 :
38 : const char*
39 37007 : dom_node_to_text (xmlNodePtr node) noexcept
40 : {
41 37007 : if (node && node->children && node->children->type == XML_TEXT_NODE
42 36441 : && !node->children->next)
43 36441 : return reinterpret_cast<const char*>(node->children->content);
44 566 : return nullptr;
45 : }
46 :
47 : std::optional<GncGUID>
48 8537 : dom_tree_to_guid (xmlNodePtr node)
49 : {
50 8537 : auto type = xmlGetProp (node, BAD_CAST "type");
51 8537 : if (!type)
52 0 : return {};
53 :
54 8537 : bool ok = !g_strcmp0 ((char*)type, "guid") || !g_strcmp0 ((char*)type, "new");
55 :
56 8537 : xmlFree (type);
57 :
58 8537 : if (!ok)
59 0 : return {};
60 :
61 8537 : auto extract_guid = [](auto str) -> std::optional<GncGUID>
62 : {
63 8537 : if (GncGUID guid; string_to_guid (str, &guid))
64 8537 : return guid;
65 :
66 0 : return {};
67 : };
68 :
69 8537 : return apply_xmlnode_text<std::optional<GncGUID>>(extract_guid, node);
70 : }
71 :
72 : static KvpValue*
73 1241 : dom_tree_to_integer_kvp_value (xmlNodePtr node)
74 : {
75 1241 : auto node_to_int_kvp = [](auto txt) -> KvpValue*
76 : {
77 1241 : if (gint64 daint; string_to_gint64 (txt, &daint))
78 1241 : return new KvpValue{daint};
79 :
80 0 : return nullptr;
81 : };
82 2482 : return apply_xmlnode_text<KvpValue*> (node_to_int_kvp, node, nullptr);
83 : }
84 :
85 : template <typename T>
86 : static bool
87 719 : dom_tree_to_num (xmlNodePtr node, std::function<bool(const char*, T*)>string_to_num, T* num_ptr)
88 : {
89 1438 : return apply_xmlnode_text<T>([&](auto txt){ return string_to_num (txt, num_ptr);}, node, false);
90 : }
91 :
92 : gboolean
93 712 : dom_tree_to_integer (xmlNodePtr node, gint64* daint)
94 : {
95 712 : return dom_tree_to_num<gint64>(node, string_to_gint64, daint);
96 : }
97 :
98 : gboolean
99 6 : dom_tree_to_guint16 (xmlNodePtr node, guint16* i)
100 : {
101 6 : return dom_tree_to_num<guint16>(node, string_to_guint16, i);
102 : }
103 :
104 : gboolean
105 1 : dom_tree_to_guint (xmlNodePtr node, guint* i)
106 : {
107 1 : return dom_tree_to_num<guint>(node, string_to_guint, i);
108 : }
109 :
110 : gboolean
111 0 : dom_tree_to_boolean (xmlNodePtr node, gboolean* b)
112 : {
113 0 : auto set_bool = [b](auto text) -> gboolean
114 : {
115 0 : if (g_ascii_strncasecmp (text, "true", 4) == 0)
116 : {
117 0 : *b = TRUE;
118 0 : return TRUE;
119 : }
120 0 : else if (g_ascii_strncasecmp (text, "false", 5) == 0)
121 : {
122 0 : *b = FALSE;
123 0 : return TRUE;
124 : }
125 : else
126 : {
127 0 : *b = FALSE;
128 0 : return FALSE;
129 : }
130 0 : };
131 0 : return apply_xmlnode_text<gboolean> (set_bool, node);
132 : }
133 :
134 : static KvpValue*
135 0 : dom_tree_to_double_kvp_value (xmlNodePtr node)
136 : {
137 0 : auto node_to_double_kvp = [](auto txt) -> KvpValue*
138 : {
139 0 : if (double dadoub; string_to_double (txt, &dadoub)) return new KvpValue{dadoub};
140 0 : return nullptr;
141 : };
142 0 : return apply_xmlnode_text<KvpValue*> (node_to_double_kvp, node, nullptr);
143 : }
144 :
145 : static KvpValue*
146 595 : dom_tree_to_numeric_kvp_value (xmlNodePtr node)
147 : {
148 1190 : return new KvpValue {dom_tree_to_gnc_numeric (node)};
149 : }
150 :
151 : static KvpValue*
152 918 : dom_tree_to_string_kvp_value (xmlNodePtr node)
153 : {
154 918 : auto node_to_string_kvp = [](auto txt) -> KvpValue*
155 : {
156 2754 : return new KvpValue {g_strdup (txt)};
157 : };
158 1836 : return apply_xmlnode_text<KvpValue*> (node_to_string_kvp, node, nullptr);
159 : }
160 :
161 : static KvpValue*
162 621 : dom_tree_to_guid_kvp_value (xmlNodePtr node)
163 : {
164 621 : auto daguid = dom_tree_to_guid (node);
165 1242 : return daguid ? new KvpValue {guid_copy (&*daguid)} : nullptr;
166 : }
167 :
168 : static KvpValue*
169 5 : dom_tree_to_time64_kvp_value (xmlNodePtr node)
170 : {
171 5 : Time64 t{dom_tree_to_time64 (node)};
172 10 : return new KvpValue {t};
173 : }
174 :
175 : static KvpValue*
176 7 : dom_tree_to_gdate_kvp_value (xmlNodePtr node)
177 : {
178 7 : auto date = dom_tree_to_gdate (node);
179 7 : if (!date) return nullptr;
180 7 : return new KvpValue {*date};
181 : }
182 :
183 : static KvpValue* dom_tree_to_kvp_value (xmlNodePtr node);
184 : //needed for test access as well as internal use.
185 : KvpFrame* dom_tree_to_kvp_frame (xmlNodePtr node);
186 :
187 : static KvpValue*
188 356 : dom_tree_to_list_kvp_value (xmlNodePtr node)
189 : {
190 356 : GList* list = NULL;
191 : xmlNodePtr mark;
192 356 : KvpValue* ret = NULL;
193 :
194 2355 : for (mark = node->xmlChildrenNode; mark; mark = mark->next)
195 : {
196 : KvpValue* new_val;
197 :
198 1999 : if (g_strcmp0 ((char*)mark->name, "text") == 0)
199 925 : continue;
200 :
201 1074 : new_val = dom_tree_to_kvp_value (mark);
202 1074 : if (new_val)
203 : {
204 1074 : list = g_list_prepend (list, (gpointer)new_val);
205 : }
206 : }
207 :
208 356 : list = g_list_reverse (list);
209 :
210 356 : ret = new KvpValue {list};
211 :
212 356 : return ret;
213 : }
214 :
215 : static KvpValue*
216 368 : dom_tree_to_frame_kvp_value (xmlNodePtr node)
217 : {
218 368 : KvpFrame* frame = dom_tree_to_kvp_frame (node);
219 736 : return frame ? new KvpValue {frame} : nullptr;
220 : }
221 :
222 :
223 : struct kvp_val_converter
224 : {
225 : const gchar* tag;
226 : KvpValue* (*converter) (xmlNodePtr node);
227 : };
228 : /* Note: The type attribute must remain 'timespec' to maintain compatibility.
229 : */
230 :
231 : struct 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 :
245 : static KvpValue*
246 4111 : dom_tree_to_kvp_value (xmlNodePtr node)
247 : {
248 : xmlChar* xml_type;
249 : struct kvp_val_converter* mark;
250 4111 : KvpValue* ret = NULL;
251 :
252 4111 : xml_type = xmlGetProp (node, BAD_CAST "type");
253 :
254 41110 : for (mark = val_converters; mark->tag; mark++)
255 : {
256 36999 : if (g_strcmp0 (reinterpret_cast<char*>(xml_type), mark->tag) == 0)
257 : {
258 4111 : ret = (mark->converter) (node);
259 : }
260 : }
261 :
262 4111 : if (!mark->tag)
263 : {
264 : /* FIXME: deal with unknown type tag here */
265 : }
266 :
267 4111 : xmlFree (xml_type);
268 :
269 4111 : return ret;
270 : }
271 :
272 : static gboolean
273 904 : dom_tree_to_kvp_frame_given (xmlNodePtr node, KvpFrame* frame)
274 : {
275 : xmlNodePtr mark;
276 :
277 904 : g_return_val_if_fail (node, FALSE);
278 904 : g_return_val_if_fail (frame, FALSE);
279 :
280 6751 : for (mark = node->xmlChildrenNode; mark; mark = mark->next)
281 : {
282 5847 : if (g_strcmp0 ((char*)mark->name, "slot") == 0)
283 : {
284 : xmlNodePtr mark2;
285 3037 : const gchar* key = NULL;
286 3037 : std::optional<std::string> maybe_key;
287 3037 : KvpValue* val = NULL;
288 :
289 15459 : for (mark2 = mark->xmlChildrenNode; mark2; mark2 = mark2->next)
290 : {
291 12422 : if (g_strcmp0 ((char*)mark2->name, "slot:key") == 0)
292 : {
293 3037 : key = dom_node_to_text (mark2);
294 3037 : if (!key)
295 : {
296 0 : maybe_key = dom_tree_to_text (mark2);
297 0 : key = maybe_key ? maybe_key->c_str() : nullptr;
298 : }
299 : }
300 9385 : else if (g_strcmp0 ((char*)mark2->name, "slot:value") == 0)
301 : {
302 3037 : 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 3037 : if (key)
312 : {
313 3037 : if (val)
314 : {
315 : //We're deleting the old KvpValue returned by replace_nc().
316 12148 : delete frame->set ({key}, val);
317 : }
318 : else
319 : {
320 : /* FIXME: should put some error here */
321 : }
322 : }
323 3037 : }
324 : }
325 :
326 904 : return TRUE;
327 6074 : }
328 :
329 :
330 : KvpFrame*
331 459 : dom_tree_to_kvp_frame (xmlNodePtr node)
332 : {
333 459 : g_return_val_if_fail (node, NULL);
334 :
335 459 : auto ret = new KvpFrame;
336 :
337 459 : if (dom_tree_to_kvp_frame_given (node, ret))
338 459 : return ret;
339 :
340 0 : delete ret;
341 0 : return NULL;
342 : }
343 :
344 : gboolean
345 445 : dom_tree_create_instance_slots (xmlNodePtr node, QofInstance* inst)
346 : {
347 445 : KvpFrame* frame = qof_instance_get_slots (inst);
348 445 : return dom_tree_to_kvp_frame_given (node, frame);
349 : }
350 :
351 : std::optional<std::string>
352 1195 : dom_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 1195 : std::string rv;
364 : gchar* temp;
365 :
366 1195 : g_return_val_if_fail (tree, std::nullopt);
367 :
368 : /* no nodes means it's an empty string text */
369 1195 : if (!tree->xmlChildrenNode)
370 : {
371 575 : DEBUG ("No children");
372 575 : return "";
373 : }
374 :
375 620 : temp = (char*)xmlNodeListGetString (NULL, tree->xmlChildrenNode, TRUE);
376 620 : if (!temp)
377 : {
378 0 : DEBUG ("Null string");
379 0 : return std::nullopt;
380 : }
381 :
382 620 : DEBUG ("node string [%s]", (temp == NULL ? "(null)" : temp));
383 620 : rv = temp;
384 620 : xmlFree (temp);
385 620 : return rv;
386 1195 : }
387 :
388 : gnc_numeric
389 5162 : dom_tree_to_gnc_numeric (xmlNodePtr node)
390 : {
391 5162 : auto node_to_numeric = [](auto txt)
392 : {
393 5162 : gnc_numeric num = gnc_numeric_from_string(txt);
394 10324 : return gnc_numeric_check (num) ? gnc_numeric_zero() : num;
395 : };
396 10324 : return apply_xmlnode_text<gnc_numeric> (node_to_numeric, node, gnc_numeric_zero());
397 : }
398 :
399 :
400 : time64
401 2661 : dom_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 2661 : time64 ret {INT64_MAX};
415 2661 : gboolean seen = FALSE;
416 : xmlNodePtr n;
417 :
418 11852 : for (n = node->xmlChildrenNode; n; n = n->next)
419 : {
420 9191 : switch (n->type)
421 : {
422 5806 : case XML_COMMENT_NODE:
423 : case XML_TEXT_NODE:
424 5806 : break;
425 3385 : case XML_ELEMENT_NODE:
426 3385 : if (g_strcmp0 ("ts:date", (char*)n->name) == 0)
427 : {
428 2661 : if (seen)
429 : {
430 0 : return INT64_MAX;
431 : }
432 2661 : seen = TRUE;
433 2661 : ret = apply_xmlnode_text<time64> (gnc_iso8601_to_time64_gmt, n, INT64_MAX);
434 : }
435 3385 : break;
436 0 : default:
437 0 : PERR ("unexpected sub-node.");
438 0 : return INT64_MAX;
439 : break;
440 : }
441 : }
442 :
443 2661 : if (!seen)
444 : {
445 0 : PERR ("no ts:date node found.");
446 0 : return INT64_MAX;
447 : }
448 :
449 2661 : return ret;
450 : }
451 :
452 : std::optional<GDate>
453 21 : dom_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 21 : gboolean seen_date = FALSE;
465 : xmlNodePtr n;
466 :
467 21 : auto try_setting_date = [&ret](const char *content) -> bool
468 : {
469 21 : gint year = 0, month = 0, day = 0;
470 21 : if (sscanf (content, "%d-%d-%d", &year, &month, &day) != 3) return false;
471 21 : g_date_set_dmy (&ret, day, static_cast<GDateMonth>(month), year);
472 21 : return (g_date_valid (&ret));
473 21 : };
474 :
475 : /* creates an invalid date */
476 21 : g_date_clear (&ret, 1);
477 :
478 84 : for (n = node->xmlChildrenNode; n; n = n->next)
479 : {
480 63 : switch (n->type)
481 : {
482 42 : case XML_COMMENT_NODE:
483 : case XML_TEXT_NODE:
484 42 : break;
485 21 : case XML_ELEMENT_NODE:
486 21 : if (g_strcmp0 ("gdate", (char*)n->name) == 0)
487 : {
488 21 : if (seen_date || !apply_xmlnode_text<bool> (try_setting_date, n))
489 0 : return std::nullopt;
490 21 : seen_date = TRUE;
491 : }
492 21 : break;
493 0 : default:
494 0 : PERR ("unexpected sub-node.");
495 0 : return std::nullopt;
496 : }
497 : }
498 :
499 21 : if (!seen_date)
500 : {
501 0 : PWARN ("no gdate node found.");
502 0 : return std::nullopt;
503 : }
504 :
505 21 : return std::make_optional<GDate>(ret);
506 : }
507 :
508 : struct CommodityRef
509 : {
510 : std::string space;
511 : std::string id;
512 : };
513 :
514 : std::string
515 7462 : gnc_strstrip (std::string_view sv)
516 : {
517 7462 : while (!sv.empty () && g_ascii_isspace (sv.front())) sv.remove_prefix (1);
518 7462 : while (!sv.empty () && g_ascii_isspace (sv.back())) sv.remove_suffix (1);
519 14924 : return std::string (sv);
520 : }
521 :
522 : static std::optional<CommodityRef>
523 3525 : parse_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 3525 : CommodityRef rv;
536 3525 : bool space_set{false};
537 3525 : bool id_set{false};
538 : xmlNodePtr n;
539 :
540 3525 : if (!node) return {};
541 3525 : if (!node->xmlChildrenNode) return {};
542 :
543 21027 : for (n = node->xmlChildrenNode; n; n = n->next)
544 : {
545 17502 : switch (n->type)
546 : {
547 10452 : case XML_COMMENT_NODE:
548 : case XML_TEXT_NODE:
549 10452 : break;
550 7050 : case XML_ELEMENT_NODE:
551 7050 : if (g_strcmp0 ("cmdty:space", (char*)n->name) == 0)
552 : {
553 3525 : if (space_set)
554 : {
555 0 : return {};
556 : }
557 3525 : rv.space = apply_xmlnode_text<std::string> (gnc_strstrip, n);
558 3525 : space_set = true;
559 : }
560 3525 : else if (g_strcmp0 ("cmdty:id", (char*)n->name) == 0)
561 : {
562 3525 : if (id_set)
563 : {
564 0 : return {};
565 : }
566 3525 : rv.id = apply_xmlnode_text<std::string> (gnc_strstrip, n);
567 3525 : id_set = true;
568 : }
569 7050 : break;
570 0 : default:
571 0 : PERR ("unexpected sub-node.");
572 0 : return {};
573 : break;
574 : }
575 : }
576 3525 : if (space_set && id_set)
577 3525 : return rv;
578 :
579 0 : return {};
580 3525 : }
581 :
582 : gnc_commodity*
583 41 : dom_tree_to_commodity_ref_no_engine (xmlNodePtr node, QofBook* book)
584 : {
585 41 : auto ref = parse_commodity_ref (node, book);
586 :
587 41 : if (!ref)
588 0 : return nullptr;
589 :
590 41 : return gnc_commodity_new (book, nullptr, ref->space.c_str(), ref->id.c_str(),
591 41 : nullptr, 0);
592 41 : }
593 :
594 : gnc_commodity*
595 3484 : dom_tree_to_commodity_ref (xmlNodePtr node, QofBook* book)
596 : {
597 : gnc_commodity* ret;
598 : gnc_commodity_table* table;
599 :
600 3484 : auto ref = parse_commodity_ref (node, book);
601 :
602 3484 : if (!ref)
603 0 : return nullptr;
604 :
605 3484 : table = gnc_commodity_table_get_table (book);
606 :
607 3484 : g_return_val_if_fail (table != NULL, NULL);
608 :
609 3484 : ret = gnc_commodity_table_lookup (table, ref->space.c_str(), ref->id.c_str());
610 :
611 3484 : g_return_val_if_fail (ret != NULL, NULL);
612 :
613 3484 : return ret;
614 3484 : }
615 :
616 : /***********************************************************************/
617 : /* generic parser */
618 :
619 : static inline void
620 3806 : dom_tree_handlers_reset (struct dom_tree_handler* handlers)
621 : {
622 48649 : for (; handlers->tag != NULL; handlers++)
623 : {
624 44843 : handlers->gotten = 0;
625 : }
626 3806 : }
627 :
628 : static inline gboolean
629 3806 : dom_tree_handlers_all_gotten_p (struct dom_tree_handler* handlers)
630 : {
631 3806 : gboolean ret = TRUE;
632 48649 : for (; handlers->tag != NULL; handlers++)
633 : {
634 44843 : if (handlers->required && ! handlers->gotten)
635 : {
636 0 : PERR ("Not defined and it should be: %s",
637 : handlers->tag ? handlers->tag : "(null)");
638 0 : ret = FALSE;
639 : }
640 : }
641 3806 : return ret;
642 : }
643 :
644 :
645 : static inline gboolean
646 22831 : gnc_xml_set_data (const gchar* tag, xmlNodePtr node, gpointer item,
647 : struct dom_tree_handler* handlers)
648 : {
649 109302 : for (; handlers->tag != NULL; handlers++)
650 : {
651 109302 : if (g_strcmp0 (tag, handlers->tag) == 0)
652 : {
653 22831 : (handlers->handler) (node, item);
654 22831 : handlers->gotten = TRUE;
655 22831 : break;
656 : }
657 : }
658 :
659 22831 : if (!handlers->tag)
660 : {
661 0 : PERR ("Unhandled tag: %s",
662 : tag ? tag : "(null)");
663 0 : return FALSE;
664 : }
665 :
666 22831 : return TRUE;
667 : }
668 :
669 : gboolean
670 3806 : dom_tree_generic_parse (xmlNodePtr node, struct dom_tree_handler* handlers,
671 : gpointer data)
672 : {
673 : xmlNodePtr achild;
674 3806 : gboolean successful = TRUE;
675 :
676 3806 : dom_tree_handlers_reset (handlers);
677 :
678 53273 : for (achild = node->xmlChildrenNode; achild; achild = achild->next)
679 : {
680 : /* ignore stray text nodes */
681 49467 : if (g_strcmp0 ((char*)achild->name, "text") == 0)
682 26636 : continue;
683 :
684 22831 : if (!gnc_xml_set_data ((char*)achild->name, achild, data, handlers))
685 : {
686 0 : PERR ("gnc_xml_set_data failed");
687 0 : successful = FALSE;
688 0 : continue;
689 : }
690 : }
691 :
692 3806 : if (!dom_tree_handlers_all_gotten_p (handlers))
693 : {
694 0 : PERR ("didn't find all of the expected tags in the input");
695 0 : successful = FALSE;
696 : }
697 :
698 3806 : return successful;
699 : }
700 :
701 : gboolean
702 2556 : dom_tree_valid_time64 (time64 val, const xmlChar * name)
703 : {
704 2556 : if (val != INT64_MAX)
705 2556 : return TRUE;
706 0 : 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 0 : return FALSE;
709 : }
|