GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-lot-xml-v2.c
1 /********************************************************************\
2  * gnc-lot-xml-v2.c -- lot xml i/o implementation *
3  * *
4  * Copyright (C) 2001 James LewisMoss <[email protected]> *
5  * Copyright (C) 2002 Linas Vepstas <[email protected]> *
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 [email protected] *
23  * *
24 \********************************************************************/
25 
26 #include "config.h"
27 
28 #include <glib.h>
29 #include <stdlib.h>
30 #include <string.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 "sixtp-dom-parsers.h"
45 #include "gnc-lot.h"
46 #include "gnc-lot-p.h"
47 
48 static QofLogModule log_module = GNC_MOD_IO;
49 
50 const gchar *lot_version_string = "2.0.0";
51 
52 /* ids */
53 #define gnc_lot_string "gnc:lot"
54 #define lot_id_string "lot:id"
55 #define lot_slots_string "lot:slots"
56 /* EFFECTIVE FRIEND FUNCTION */
57 extern KvpFrame *qof_instance_get_slots (const QofInstance *);
58 
59 xmlNodePtr
60 gnc_lot_dom_tree_create(GNCLot *lot)
61 {
62  xmlNodePtr ret;
63  KvpFrame *kf;
64 
65  ENTER("(lot=%p)", lot);
66  ret = xmlNewNode(NULL, BAD_CAST gnc_lot_string);
67  xmlSetProp(ret, BAD_CAST "version", BAD_CAST lot_version_string);
68 
69  xmlAddChild(ret, guid_to_dom_tree(lot_id_string, gnc_lot_get_guid(lot)));
70 
71  kf = qof_instance_get_slots (QOF_INSTANCE (lot));
72  if (kf)
73  {
74  xmlNodePtr kvpnode = kvp_frame_to_dom_tree(lot_slots_string, kf);
75  if (kvpnode)
76  {
77  xmlAddChild(ret, kvpnode);
78  }
79  }
80 
81  LEAVE("");
82  return ret;
83 }
84 
85 /* =================================================================== */
86 
87 struct lot_pdata
88 {
89  GNCLot *lot;
90  QofBook *book;
91 };
92 
93 static gboolean
94 lot_id_handler (xmlNodePtr node, gpointer p)
95 {
96  struct lot_pdata *pdata = p;
97  GncGUID *guid;
98 
99  ENTER("(lot=%p)", pdata->lot);
100  guid = dom_tree_to_guid(node);
101  gnc_lot_set_guid(pdata->lot, *guid);
102 
103  g_free(guid);
104 
105  LEAVE("");
106  return TRUE;
107 }
108 
109 static gboolean
110 lot_slots_handler (xmlNodePtr node, gpointer p)
111 {
112  struct lot_pdata *pdata = p;
113  gboolean success;
114 
115  ENTER("(lot=%p)", pdata->lot);
116  success = dom_tree_to_kvp_frame_given
117  (node, qof_instance_get_slots (QOF_INSTANCE (pdata->lot)));
118 
119  LEAVE("");
120  g_return_val_if_fail(success, FALSE);
121  return TRUE;
122 }
123 
124 
125 static struct dom_tree_handler lot_handlers_v2[] =
126 {
127  { lot_id_string, lot_id_handler, 1, 0 },
128  { lot_slots_string, lot_slots_handler, 0, 0 },
129  { NULL, 0, 0, 0 }
130 };
131 
132 static gboolean
133 gnc_lot_end_handler(gpointer data_for_children,
134  GSList* data_from_children, GSList* sibling_data,
135  gpointer parent_data, gpointer global_data,
136  gpointer *result, const gchar *tag)
137 {
138  GNCLot *lot;
139  xmlNodePtr tree = (xmlNodePtr)data_for_children;
140  gxpf_data *gdata = (gxpf_data*)global_data;
141  QofBook *book = gdata->bookdata;
142 
143  if (parent_data)
144  {
145  return TRUE;
146  }
147 
148  /* OK. For some messed up reason this is getting called again with a
149  NULL tag. So we ignore those cases */
150  if (!tag)
151  {
152  return TRUE;
153  }
154 
155  g_return_val_if_fail(tree, FALSE);
156 
157  lot = dom_tree_to_lot(tree, book);
158  ENTER("(lot=%p)", lot);
159  if (lot != NULL)
160  {
161  gdata->cb(tag, gdata->parsedata, lot);
162  }
163 
164  xmlFreeNode(tree);
165 
166  LEAVE("");
167  return lot != NULL;
168 }
169 
170 GNCLot*
171 dom_tree_to_lot (xmlNodePtr node, QofBook *book)
172 {
173  struct lot_pdata pdata;
174  GNCLot *lot;
175  gboolean successful;
176 
177  lot = gnc_lot_new(book);
178  ENTER("(lot=%p)", lot);
179 
180  pdata.lot = lot;
181  pdata.book = book;
182 
183  successful = dom_tree_generic_parse (node, lot_handlers_v2,
184  &pdata);
185  if (!successful)
186  {
187  PERR ("failed to parse lot");
188  gnc_lot_destroy (lot);
189  lot = NULL;
190  }
191 
192  LEAVE("");
193  return lot;
194 }
195 
196 sixtp*
197 gnc_lot_sixtp_parser_create(void)
198 {
199  return sixtp_dom_parser_new(gnc_lot_end_handler, NULL, NULL);
200 }
201 
202 /* ================== END OF FILE ========================== */
Definition: sixtp.h:93
#define PERR(format, args...)
Definition: qoflog.h:237
#define ENTER(format, args...)
Definition: qoflog.h:261
Definition: guid.h:65
api for GnuCash version 2 XML-based file format
struct KvpFrameImpl KvpFrame
Definition: kvp_frame.h:76
#define LEAVE(format, args...)
Definition: qoflog.h:271
const gchar * QofLogModule
Definition: qofid.h:89