Line data Source code
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 */
49 : const 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 :
56 : static QofLogModule log_module = GNC_MOD_IO;
57 :
58 : /* ================================================================ */
59 :
60 : xmlNodePtr
61 0 : gnc_book_dom_tree_create (QofBook* book)
62 : {
63 : xmlNodePtr ret;
64 0 : G_GNUC_UNUSED gboolean allow_incompat = TRUE;
65 :
66 0 : ret = xmlNewNode (NULL, BAD_CAST gnc_book_string);
67 0 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST gnc_v2_book_version_string);
68 :
69 0 : xmlAddChild (ret, guid_to_dom_tree (book_id_string,
70 0 : qof_book_get_guid (book)));
71 :
72 : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
73 0 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (book_slots_string,
74 0 : QOF_INSTANCE (book)));
75 :
76 0 : 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 :
83 : gboolean
84 4 : write_book_parts (FILE* out, QofBook* book)
85 : {
86 : xmlNodePtr domnode, slotsnode;
87 :
88 4 : domnode = guid_to_dom_tree (book_id_string, qof_book_get_guid (book));
89 4 : xmlElemDump (out, NULL, domnode);
90 4 : xmlFreeNode (domnode);
91 :
92 4 : if (ferror (out) || fprintf (out, "\n") < 0)
93 0 : return FALSE;
94 :
95 :
96 8 : slotsnode = qof_instance_slots_to_dom_tree (book_slots_string,
97 4 : QOF_INSTANCE (book));
98 4 : if (slotsnode)
99 : {
100 2 : xmlElemDump (out, NULL, slotsnode);
101 2 : xmlFreeNode (slotsnode);
102 :
103 2 : if (ferror (out) || fprintf (out, "\n") < 0)
104 0 : return FALSE;
105 : }
106 :
107 4 : return TRUE;
108 : }
109 :
110 :
111 : /* ================================================================ */
112 :
113 : static gboolean
114 22 : book_id_handler (xmlNodePtr node, gpointer book_pdata)
115 : {
116 22 : QofBook* book = static_cast<decltype (book)> (book_pdata);
117 :
118 22 : auto guid = dom_tree_to_guid (node);
119 22 : qof_instance_set_guid (QOF_INSTANCE (book), &*guid);
120 :
121 22 : return TRUE;
122 : }
123 :
124 : static gboolean
125 5 : book_slots_handler (xmlNodePtr node, gpointer book_pdata)
126 : {
127 5 : 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 5 : success = dom_tree_create_instance_slots (node, QOF_INSTANCE (book));
133 :
134 5 : g_return_val_if_fail (success, FALSE);
135 :
136 5 : return TRUE;
137 : }
138 :
139 :
140 : static 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 :
147 : static gboolean
148 0 : gnc_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 0 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
154 0 : gxpf_data* gdata = (gxpf_data*)global_data;
155 0 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
156 :
157 :
158 0 : 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 0 : if (!tag) return TRUE;
163 :
164 0 : g_return_val_if_fail (tree, FALSE);
165 :
166 0 : book = dom_tree_to_book (tree, book);
167 0 : if (!book)
168 0 : gdata->cb (tag, gdata->parsedata, book);
169 :
170 0 : xmlFreeNode (tree);
171 :
172 0 : return book != NULL;
173 : }
174 :
175 : static gboolean
176 22 : gnc_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 22 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
183 22 : gxpf_data* gdata = (gxpf_data*)global_data;
184 22 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
185 :
186 22 : if (parent_data) return TRUE;
187 22 : if (!tag) return TRUE;
188 :
189 22 : g_return_val_if_fail (tree, FALSE);
190 :
191 22 : successful = book_id_handler (tree, book);
192 22 : xmlFreeNode (tree);
193 :
194 22 : return successful;
195 : }
196 :
197 : static gboolean
198 87 : gnc_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 87 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
205 87 : gxpf_data* gdata = (gxpf_data*)global_data;
206 87 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
207 :
208 87 : if (parent_data) return TRUE;
209 5 : if (!tag) return TRUE;
210 :
211 5 : g_return_val_if_fail (tree, FALSE);
212 :
213 5 : successful = book_slots_handler (tree, book);
214 5 : xmlFreeNode (tree);
215 :
216 5 : return successful;
217 : }
218 :
219 : QofBook*
220 0 : dom_tree_to_book (xmlNodePtr node, QofBook* book)
221 : {
222 : gboolean successful;
223 :
224 0 : successful = dom_tree_generic_parse (node, book_handlers_v2,
225 : book);
226 0 : if (!successful)
227 : {
228 0 : PERR ("failed to parse book");
229 0 : book = NULL;
230 : }
231 :
232 0 : return book;
233 : }
234 :
235 : sixtp*
236 0 : gnc_book_sixtp_parser_create (void)
237 : {
238 0 : return sixtp_dom_parser_new (gnc_book_end_handler, NULL, NULL);
239 : }
240 :
241 : sixtp*
242 22 : gnc_book_id_sixtp_parser_create (void)
243 : {
244 22 : return sixtp_dom_parser_new (gnc_book_id_end_handler, NULL, NULL);
245 : }
246 :
247 : sixtp*
248 22 : gnc_book_slots_sixtp_parser_create (void)
249 : {
250 22 : return sixtp_dom_parser_new (gnc_book_slots_end_handler, NULL, NULL);
251 : }
|