GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-book-xml-v2.cpp
1/********************************************************************\
2 * gnc-book-xml-v2.c -- book xml i/o implementation *
3 * *
4 * Copyright (C) 2001 James LewisMoss <dres@debian.org> *
5 * Copyright (C) 2001 Linas Vepstas <linas@linas.org> *
6 * *
7 * This program is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU General Public License as *
9 * published by the Free Software Foundation; either version 2 of *
10 * the License, or (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License*
18 * along with this program; if not, contact: *
19 * *
20 * Free Software Foundation Voice: +1-617-542-5942 *
21 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
22 * Boston, MA 02110-1301, USA gnu@gnu.org *
23 * *
24\********************************************************************/
25#include <glib.h>
26
27#include <config.h>
28#include <stdlib.h>
29#include <string.h>
30#include "qof.h"
31
32#include "gnc-xml-helper.h"
33
34#include "sixtp.h"
35#include "sixtp-utils.h"
36#include "sixtp-parsers.h"
37#include "sixtp-utils.h"
38#include "sixtp-dom-parsers.h"
39#include "sixtp-dom-generators.h"
40
41#include "gnc-xml.h"
42#include "io-gncxml-gen.h"
43#include "io-gncxml-v2.h"
44#include "io-utils.h"
45
46#include "sixtp-dom-parsers.h"
47
48/* non-static because it's used in io-gncxml-v2.c */
49const gchar* gnc_v2_book_version_string = "2.0.0";
50
51/* ids */
52#define gnc_book_string "gnc:book"
53#define book_id_string "book:id"
54#define book_slots_string "book:slots"
55
56static QofLogModule log_module = GNC_MOD_IO;
57
58/* ================================================================ */
59
60xmlNodePtr
61gnc_book_dom_tree_create (QofBook* book)
62{
63 xmlNodePtr ret;
64 G_GNUC_UNUSED gboolean allow_incompat = TRUE;
65
66 ret = xmlNewNode (NULL, BAD_CAST gnc_book_string);
67 xmlSetProp (ret, BAD_CAST "version", BAD_CAST gnc_v2_book_version_string);
68
69 xmlAddChild (ret, guid_to_dom_tree (book_id_string,
70 qof_book_get_guid (book)));
71
72 /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
73 xmlAddChild (ret, qof_instance_slots_to_dom_tree (book_slots_string,
74 QOF_INSTANCE (book)));
75
76 return ret;
77}
78
79/* ================================================================ */
80/* same as above, but we write out directly. Only handle the guid
81 * and slots, everything else is handled elsewhere */
82
83gboolean
84write_book_parts (FILE* out, QofBook* book)
85{
86 xmlNodePtr domnode, slotsnode;
87
88 domnode = guid_to_dom_tree (book_id_string, qof_book_get_guid (book));
89 xmlElemDump (out, NULL, domnode);
90 xmlFreeNode (domnode);
91
92 if (ferror (out) || fprintf (out, "\n") < 0)
93 return FALSE;
94
95
96 slotsnode = qof_instance_slots_to_dom_tree (book_slots_string,
97 QOF_INSTANCE (book));
98 if (slotsnode)
99 {
100 xmlElemDump (out, NULL, slotsnode);
101 xmlFreeNode (slotsnode);
102
103 if (ferror (out) || fprintf (out, "\n") < 0)
104 return FALSE;
105 }
106
107 return TRUE;
108}
109
110
111/* ================================================================ */
112
113static gboolean
114book_id_handler (xmlNodePtr node, gpointer book_pdata)
115{
116 QofBook* book = static_cast<decltype (book)> (book_pdata);
117
118 auto guid = dom_tree_to_guid (node);
119 qof_instance_set_guid (QOF_INSTANCE (book), &*guid);
120
121 return TRUE;
122}
123
124static gboolean
125book_slots_handler (xmlNodePtr node, gpointer book_pdata)
126{
127 QofBook* book = static_cast<decltype (book)> (book_pdata);
128 gboolean success;
129
130 /* the below works only because the get is guaranteed to return
131 * a frame, even if its empty */
132 success = dom_tree_create_instance_slots (node, QOF_INSTANCE (book));
133
134 g_return_val_if_fail (success, FALSE);
135
136 return TRUE;
137}
138
139
140static struct dom_tree_handler book_handlers_v2[] =
141{
142 { book_id_string, book_id_handler, 1, 0 },
143 { book_slots_string, book_slots_handler, 0, 0 },
144 { NULL, 0, 0, 0 }
145};
146
147static gboolean
148gnc_book_end_handler (gpointer data_for_children,
149 GSList* data_from_children, GSList* sibling_data,
150 gpointer parent_data, gpointer global_data,
151 gpointer* result, const gchar* tag)
152{
153 xmlNodePtr tree = (xmlNodePtr)data_for_children;
154 gxpf_data* gdata = (gxpf_data*)global_data;
155 QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
156
157
158 if (parent_data) return TRUE;
159
160 /* OK. For some messed up reason this is getting called again with a
161 NULL tag. So we ignore those cases */
162 if (!tag) return TRUE;
163
164 g_return_val_if_fail (tree, FALSE);
165
166 book = dom_tree_to_book (tree, book);
167 if (!book)
168 gdata->cb (tag, gdata->parsedata, book);
169
170 xmlFreeNode (tree);
171
172 return book != NULL;
173}
174
175static gboolean
176gnc_book_id_end_handler (gpointer data_for_children,
177 GSList* data_from_children, GSList* sibling_data,
178 gpointer parent_data, gpointer global_data,
179 gpointer* result, const gchar* tag)
180{
181 gboolean successful;
182 xmlNodePtr tree = (xmlNodePtr)data_for_children;
183 gxpf_data* gdata = (gxpf_data*)global_data;
184 QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
185
186 if (parent_data) return TRUE;
187 if (!tag) return TRUE;
188
189 g_return_val_if_fail (tree, FALSE);
190
191 successful = book_id_handler (tree, book);
192 xmlFreeNode (tree);
193
194 return successful;
195}
196
197static gboolean
198gnc_book_slots_end_handler (gpointer data_for_children,
199 GSList* data_from_children, GSList* sibling_data,
200 gpointer parent_data, gpointer global_data,
201 gpointer* result, const gchar* tag)
202{
203 gboolean successful;
204 xmlNodePtr tree = (xmlNodePtr)data_for_children;
205 gxpf_data* gdata = (gxpf_data*)global_data;
206 QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
207
208 if (parent_data) return TRUE;
209 if (!tag) return TRUE;
210
211 g_return_val_if_fail (tree, FALSE);
212
213 successful = book_slots_handler (tree, book);
214 xmlFreeNode (tree);
215
216 return successful;
217}
218
219QofBook*
220dom_tree_to_book (xmlNodePtr node, QofBook* book)
221{
222 gboolean successful;
223
224 successful = dom_tree_generic_parse (node, book_handlers_v2,
225 book);
226 if (!successful)
227 {
228 PERR ("failed to parse book");
229 book = NULL;
230 }
231
232 return book;
233}
234
235sixtp*
236gnc_book_sixtp_parser_create (void)
237{
238 return sixtp_dom_parser_new (gnc_book_end_handler, NULL, NULL);
239}
240
241sixtp*
242gnc_book_id_sixtp_parser_create (void)
243{
244 return sixtp_dom_parser_new (gnc_book_id_end_handler, NULL, NULL);
245}
246
247sixtp*
248gnc_book_slots_sixtp_parser_create (void)
249{
250 return sixtp_dom_parser_new (gnc_book_slots_end_handler, NULL, NULL);
251}
#define qof_book_get_guid(X)
deprecated
Definition qofbook.h:362
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244
api for GnuCash version 2 XML-based file format
QofBook reference.
Definition qofbook-p.hpp:47
Definition sixtp.h:130