GnuCash  5.6-150-g038405b370+
gnc-address-xml-v2.cpp
1 /********************************************************************\
2  * gnc-address-xml-v2.c -- address xml i/o implementation *
3  * *
4  * Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA gnu@gnu.org *
22  * *
23 \********************************************************************/
24 #include <glib.h>
25 
26 #include <config.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "gnc-xml-helper.h"
31 
32 #include "sixtp.h"
33 #include "sixtp-utils.h"
34 #include "sixtp-parsers.h"
35 #include "sixtp-utils.h"
36 #include "sixtp-dom-parsers.h"
37 #include "sixtp-dom-generators.h"
38 
39 #include "gnc-xml.h"
40 #include "io-gncxml-gen.h"
41 #include "io-gncxml-v2.h"
42 
43 #include "gnc-address-xml-v2.h"
44 
45 static QofLogModule log_module = GNC_MOD_IO;
46 
47 const gchar* address_version_string = "2.0.0";
48 
49 /* ids */
50 #define addr_name_string "addr:name"
51 #define addr_addr1_string "addr:addr1"
52 #define addr_addr2_string "addr:addr2"
53 #define addr_addr3_string "addr:addr3"
54 #define addr_addr4_string "addr:addr4"
55 #define addr_phone_string "addr:phone"
56 #define addr_fax_string "addr:fax"
57 #define addr_email_string "addr:email"
58 #define addr_slots_string "addr:slots"
59 
60 static void
61 maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
62 {
63  if (str && *str)
64  xmlAddChild (ptr, text_to_dom_tree (tag, str));
65 }
66 
67 xmlNodePtr
68 gnc_address_to_dom_tree (const char* tag, GncAddress* addr)
69 {
70  xmlNodePtr ret;
71 
72  ret = xmlNewNode (NULL, BAD_CAST tag);
73  xmlSetProp (ret, BAD_CAST "version", BAD_CAST address_version_string);
74 
75  maybe_add_string (ret, addr_name_string, gncAddressGetName (addr));
76 
77  maybe_add_string (ret, addr_addr1_string, gncAddressGetAddr1 (addr));
78  maybe_add_string (ret, addr_addr2_string, gncAddressGetAddr2 (addr));
79  maybe_add_string (ret, addr_addr3_string, gncAddressGetAddr3 (addr));
80  maybe_add_string (ret, addr_addr4_string, gncAddressGetAddr4 (addr));
81 
82  maybe_add_string (ret, addr_phone_string, gncAddressGetPhone (addr));
83  maybe_add_string (ret, addr_fax_string, gncAddressGetFax (addr));
84  maybe_add_string (ret, addr_email_string, gncAddressGetEmail (addr));
85 
86  /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
87  xmlAddChild (ret, qof_instance_slots_to_dom_tree (addr_slots_string,
88  QOF_INSTANCE (addr)));
89  return ret;
90 }
91 
92 /***********************************************************************/
93 
95 {
96  GncAddress* address;
97 };
98 
99 
100 static gboolean
101 address_name_handler (xmlNodePtr node, gpointer addr_pdata)
102 {
103  struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
104 
105  return apply_xmlnode_text (gncAddressSetName, pdata->address, node);
106 }
107 
108 static gboolean
109 address_addr1_handler (xmlNodePtr node, gpointer addr_pdata)
110 {
111  struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
112 
113  return apply_xmlnode_text (gncAddressSetAddr1, pdata->address, node);
114 }
115 
116 static gboolean
117 address_addr2_handler (xmlNodePtr node, gpointer addr_pdata)
118 {
119  struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
120 
121  return apply_xmlnode_text (gncAddressSetAddr2, pdata->address, node);
122 }
123 
124 static gboolean
125 address_addr3_handler (xmlNodePtr node, gpointer addr_pdata)
126 {
127  struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
128 
129  return apply_xmlnode_text (gncAddressSetAddr3, pdata->address, node);
130 }
131 
132 static gboolean
133 address_addr4_handler (xmlNodePtr node, gpointer addr_pdata)
134 {
135  struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
136 
137  return apply_xmlnode_text (gncAddressSetAddr4, pdata->address, node);
138 }
139 
140 static gboolean
141 address_phone_handler (xmlNodePtr node, gpointer addr_pdata)
142 {
143  struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
144 
145  return apply_xmlnode_text (gncAddressSetPhone, pdata->address, node);
146 }
147 
148 static gboolean
149 address_fax_handler (xmlNodePtr node, gpointer addr_pdata)
150 {
151  struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
152 
153  return apply_xmlnode_text (gncAddressSetFax, pdata->address, node);
154 }
155 
156 static gboolean
157 address_email_handler (xmlNodePtr node, gpointer addr_pdata)
158 {
159  struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
160 
161  return apply_xmlnode_text (gncAddressSetEmail, pdata->address, node);
162 }
163 
164 static gboolean
165 address_slots_handler (xmlNodePtr node, gpointer addr_pdata)
166 {
167  struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
168  return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->address));
169 }
170 
171 static struct dom_tree_handler address_handlers_v2[] =
172 {
173  { addr_name_string, address_name_handler, 0, 0 },
174  { addr_addr1_string, address_addr1_handler, 0, 0 },
175  { addr_addr2_string, address_addr2_handler, 0, 0 },
176  { addr_addr3_string, address_addr3_handler, 0, 0 },
177  { addr_addr4_string, address_addr4_handler, 0, 0 },
178  { addr_phone_string, address_phone_handler, 0, 0 },
179  { addr_fax_string, address_fax_handler, 0, 0 },
180  { addr_email_string, address_email_handler, 0, 0 },
181  { addr_slots_string, address_slots_handler, 0, 0 },
182  { NULL, 0, 0, 0 }
183 };
184 
185 gboolean
186 gnc_dom_tree_to_address (xmlNodePtr node, GncAddress* address)
187 {
188  struct address_pdata addr_pdata;
189  gboolean successful;
190 
191  addr_pdata.address = address;
192 
193  successful = dom_tree_generic_parse (node, address_handlers_v2,
194  &addr_pdata);
195 
196  if (!successful)
197  {
198  PERR ("failed to parse address tree");
199  }
200 
201  return successful;
202 }
203 
204 static gboolean
205 address_ns (FILE* out)
206 {
207  g_return_val_if_fail (out, FALSE);
208  return gnc_xml2_write_namespace_decl (out, "addr");
209 }
210 
211 void
212 gnc_address_xml_initialize (void)
213 {
214  static GncXmlDataType_t be_data =
215  {
216  GNC_FILE_BACKEND_VERS,
217  "gnc:Address",
218  NULL, /* parser_create */
219  NULL, /* add_item */
220  NULL, /* get_count */
221  NULL, /* write */
222  NULL, /* scrub */
223  address_ns,
224  };
225 
226  gnc_xml_register_backend (be_data);
227 }
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
api for GnuCash version 2 XML-based file format