Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-lot-xml-v2.c -- lot xml i/o implementation *
3 : : * *
4 : : * Copyright (C) 2001 James LewisMoss <dres@debian.org> *
5 : : * Copyright (C) 2002 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 "gnc-lot.h"
31 : : #include "gnc-lot-p.h"
32 : :
33 : : #include "gnc-xml-helper.h"
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 "sixtp-dom-parsers.h"
45 : :
46 : : static QofLogModule log_module = GNC_MOD_IO;
47 : :
48 : : const gchar* lot_version_string = "2.0.0";
49 : :
50 : : /* ids */
51 : : #define gnc_lot_string "gnc:lot"
52 : : #define lot_id_string "lot:id"
53 : : #define lot_slots_string "lot:slots"
54 : :
55 : : xmlNodePtr
56 : 2 : gnc_lot_dom_tree_create (GNCLot* lot)
57 : : {
58 : : xmlNodePtr ret;
59 : :
60 : 2 : ENTER ("(lot=%p)", lot);
61 : 2 : ret = xmlNewNode (NULL, BAD_CAST gnc_lot_string);
62 : 2 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST lot_version_string);
63 : :
64 : 2 : xmlAddChild (ret, guid_to_dom_tree (lot_id_string, gnc_lot_get_guid (lot)));
65 : : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
66 : 2 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (lot_slots_string,
67 : 2 : QOF_INSTANCE (lot)));
68 : :
69 : 2 : LEAVE ("");
70 : 2 : return ret;
71 : : }
72 : :
73 : : /* =================================================================== */
74 : :
75 : : struct lot_pdata
76 : : {
77 : : GNCLot* lot;
78 : : QofBook* book;
79 : : };
80 : :
81 : : static gboolean
82 : 4 : lot_id_handler (xmlNodePtr node, gpointer p)
83 : : {
84 : 4 : struct lot_pdata* pdata = static_cast<decltype (pdata)> (p);
85 : :
86 : 4 : ENTER ("(lot=%p)", pdata->lot);
87 : 4 : auto guid = dom_tree_to_guid (node);
88 : 4 : gnc_lot_set_guid (pdata->lot, *guid);
89 : :
90 : 4 : LEAVE ("");
91 : 4 : return TRUE;
92 : : }
93 : :
94 : : static gboolean
95 : 4 : lot_slots_handler (xmlNodePtr node, gpointer p)
96 : : {
97 : 4 : struct lot_pdata* pdata = static_cast<decltype (pdata)> (p);
98 : : gboolean success;
99 : :
100 : 4 : ENTER ("(lot=%p)", pdata->lot);
101 : 4 : success = dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->lot));
102 : :
103 : 4 : LEAVE ("");
104 : 4 : g_return_val_if_fail (success, FALSE);
105 : 4 : return TRUE;
106 : : }
107 : :
108 : :
109 : : static struct dom_tree_handler lot_handlers_v2[] =
110 : : {
111 : : { lot_id_string, lot_id_handler, 1, 0 },
112 : : { lot_slots_string, lot_slots_handler, 0, 0 },
113 : : { NULL, 0, 0, 0 }
114 : : };
115 : :
116 : : static gboolean
117 : 0 : gnc_lot_end_handler (gpointer data_for_children,
118 : : GSList* data_from_children, GSList* sibling_data,
119 : : gpointer parent_data, gpointer global_data,
120 : : gpointer* result, const gchar* tag)
121 : : {
122 : : GNCLot* lot;
123 : 0 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
124 : 0 : gxpf_data* gdata = (gxpf_data*)global_data;
125 : 0 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
126 : :
127 : 0 : if (parent_data)
128 : : {
129 : 0 : return TRUE;
130 : : }
131 : :
132 : : /* OK. For some messed up reason this is getting called again with a
133 : : NULL tag. So we ignore those cases */
134 : 0 : if (!tag)
135 : : {
136 : 0 : return TRUE;
137 : : }
138 : :
139 : 0 : g_return_val_if_fail (tree, FALSE);
140 : :
141 : 0 : lot = dom_tree_to_lot (tree, book);
142 : 0 : ENTER ("(lot=%p)", lot);
143 : 0 : if (lot != NULL)
144 : : {
145 : 0 : gdata->cb (tag, gdata->parsedata, lot);
146 : : }
147 : :
148 : 0 : xmlFreeNode (tree);
149 : :
150 : 0 : LEAVE ("");
151 : 0 : return lot != NULL;
152 : : }
153 : :
154 : : GNCLot*
155 : 4 : dom_tree_to_lot (xmlNodePtr node, QofBook* book)
156 : : {
157 : : struct lot_pdata pdata;
158 : : GNCLot* lot;
159 : : gboolean successful;
160 : :
161 : 4 : lot = gnc_lot_new (book);
162 : 4 : ENTER ("(lot=%p)", lot);
163 : :
164 : 4 : pdata.lot = lot;
165 : 4 : pdata.book = book;
166 : :
167 : 4 : successful = dom_tree_generic_parse (node, lot_handlers_v2,
168 : : &pdata);
169 : 4 : if (!successful)
170 : : {
171 : 0 : PERR ("failed to parse lot");
172 : 0 : gnc_lot_destroy (lot);
173 : 0 : lot = NULL;
174 : : }
175 : :
176 : 4 : LEAVE ("");
177 : 4 : return lot;
178 : : }
179 : :
180 : : sixtp*
181 : 0 : gnc_lot_sixtp_parser_create (void)
182 : : {
183 : 0 : return sixtp_dom_parser_new (gnc_lot_end_handler, NULL, NULL);
184 : : }
185 : :
186 : : /* ================== END OF FILE ========================== */
|