1 /********************************************************************
2  * sixtp.h -- header file for XML parsing                           *
3  * Copyright 2001 Gnumatic, Inc.                                    *
4  *                                                                  *
5  * This program is free software; you can redistribute it and/or    *
6  * modify it under the terms of the GNU General Public License as   *
7  * published by the Free Software Foundation; either version 2 of   *
8  * the License, or (at your option) any later version.              *
9  *                                                                  *
10  * This program is distributed in the hope that it will be useful,  *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
13  * GNU General Public License for more details.                     *
14  *                                                                  *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact:                        *
17  *                                                                  *
18  * Free Software Foundation           Voice:  +1-617-542-5942       *
19  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
20  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
21  *                                                                  *
22  ********************************************************************/
23 
24 #ifndef SIXTP_H
25 #define SIXTP_H
26 
27 #include <glib.h>
28 
29 extern "C"
30 {
31 #include <stdio.h>
32 
33 #include <stdarg.h>
34 #include "gnc-engine.h"
35 }
36 
37 #include "gnc-xml-helper.h"
38 #include "gnc-backend-xml.h"
39 
40 typedef struct sixtp_gdv2 sixtp_gdv2;
41 typedef void (*countCallbackFn) (sixtp_gdv2* gd, const char* type);
42 
43 typedef struct
44 {
45     int accounts_total;
46     int accounts_loaded;
47 
48     int books_total;
49     int books_loaded;
50 
51     int commodities_total;
52     int commodities_loaded;
53 
54     int transactions_total;
55     int transactions_loaded;
56 
57     int prices_total;
58     int prices_loaded;
59 
60     int schedXactions_total;
61     int schedXactions_loaded;
62 
63     int budgets_total;
64     int budgets_loaded;
65 } load_counter;
66 
67 struct sixtp_gdv2
68 {
69     QofBook* book;
70     load_counter counter;
71     countCallbackFn countCallback;
72     QofBePercentageFunc gui_display_fn;
73     gboolean exporting;
74 };
75 typedef struct _sixtp_child_result sixtp_child_result;
76 
77 typedef gboolean (*sixtp_start_handler) (GSList* sibling_data,
78                                          gpointer parent_data,
79                                          gpointer global_data,
80                                          gpointer* data_for_children,
81                                          gpointer* result,
82                                          const gchar* tag,
83                                          gchar** attrs);
84 
85 typedef gboolean (*sixtp_before_child_handler) (gpointer data_for_children,
86                                                 GSList* data_from_children,
87                                                 GSList* sibling_data,
88                                                 gpointer parent_data,
89                                                 gpointer global_data,
90                                                 gpointer* result,
91                                                 const gchar* tag,
92                                                 const gchar* child_tag);
93 
94 typedef gboolean (*sixtp_after_child_handler) (gpointer data_for_children,
95                                                GSList* data_from_children,
96                                                GSList* sibling_data,
97                                                gpointer parent_data,
98                                                gpointer global_data,
99                                                gpointer* result,
100                                                const gchar* tag,
101                                                const gchar* child_tag,
102                                                sixtp_child_result* child_result);
103 
104 typedef gboolean (*sixtp_end_handler) (gpointer data_for_children,
105                                        GSList* data_from_children,
106                                        GSList* sibling_data,
107                                        gpointer parent_data,
108                                        gpointer global_data,
109                                        gpointer* result,
110                                        const gchar* tag);
111 
112 typedef gboolean (*sixtp_characters_handler) (GSList* sibling_data,
113                                               gpointer parent_data,
114                                               gpointer global_data,
115                                               gpointer* result,
116                                               const char* text,
117                                               int length);
118 
119 typedef void (*sixtp_result_handler) (sixtp_child_result* result);
120 
121 typedef void (*sixtp_fail_handler) (gpointer data_for_children,
122                                     GSList* data_from_children,
123                                     GSList* sibling_data,
124                                     gpointer parent_data,
125                                     gpointer global_data,
126                                     gpointer* result,
127                                     const gchar* tag);
128 
129 typedef void (*sixtp_push_handler) (xmlParserCtxtPtr xml_context,
130                                     gpointer user_data);
131 
132 typedef struct sixtp
133 {
134     /* If you change this, don't forget to modify all the copy/etc. functions */
135     sixtp_start_handler start_handler;
136     sixtp_before_child_handler before_child;
137     sixtp_after_child_handler after_child;
138     sixtp_end_handler end_handler;
139     sixtp_characters_handler characters_handler;
140 
141     sixtp_fail_handler fail_handler;
142     /* called for failures before the close tag */
143 
144     sixtp_result_handler cleanup_result; /* called unless failure */
145     sixtp_result_handler cleanup_chars; /* called unless failure */
146 
147     sixtp_result_handler result_fail_handler;
148     /* called to cleanup results from this node on failure */
149 
150     sixtp_result_handler chars_fail_handler;
151     /* called to cleanup character results when cleaning up this node's
152        children. */
153 
154     GHashTable* child_parsers;
155 } sixtp;
156 
157 typedef enum
158 {
159     SIXTP_NO_MORE_HANDLERS,
160 
161     SIXTP_START_HANDLER_ID,
162     SIXTP_BEFORE_CHILD_HANDLER_ID,
163     SIXTP_AFTER_CHILD_HANDLER_ID,
164     SIXTP_END_HANDLER_ID,
165     SIXTP_CHARACTERS_HANDLER_ID,
166 
167     SIXTP_FAIL_HANDLER_ID,
168 
169     SIXTP_CLEANUP_RESULT_ID,
170     SIXTP_CLEANUP_CHARS_ID,
171 
172     SIXTP_RESULT_FAIL_ID,
173 
174     SIXTP_CHARS_FAIL_ID,
175 } sixtp_handler_type;
176 
177 /* completely invalid tag for xml */
178 #define SIXTP_MAGIC_CATCHER "&MAGIX&"
179 
180 typedef enum
181 {
182     SIXTP_CHILD_RESULT_CHARS,
183     SIXTP_CHILD_RESULT_NODE
184 } sixtp_child_result_type;
185 
186 struct _sixtp_child_result
187 {
188     sixtp_child_result_type type;
189     gchar* tag; /* NULL for a CHARS node. */
190     gpointer data;
191     gboolean should_cleanup;
192     sixtp_result_handler cleanup_handler;
193     sixtp_result_handler fail_handler;
194 };
195 
196 typedef struct sixtp_sax_data
197 {
198     gboolean parsing_ok;
199     GSList* stack;
200     gpointer global_data;
201     xmlParserCtxtPtr saxParserCtxt;
202     sixtp* bad_xml_parser;
203 } sixtp_sax_data;
204 
205 gboolean is_child_result_from_node_named (sixtp_child_result* cr,
206                                           const char* tag);
207 void sixtp_child_free_data (sixtp_child_result* result);
208 void sixtp_child_result_destroy (sixtp_child_result* r);
209 void sixtp_child_result_print (sixtp_child_result* cr, FILE* f);
210 
211 void sixtp_sax_start_handler (void* user_data, const xmlChar* name,
212                               const xmlChar** attrs);
213 void sixtp_sax_characters_handler (void* user_data, const xmlChar* text,
214                                    int len);
215 void sixtp_sax_end_handler (void* user_data, const xmlChar* name);
216 
217 sixtp* sixtp_new (void);
218 void sixtp_destroy (sixtp* sp);
219 
220 void sixtp_handle_catastrophe (sixtp_sax_data* sax_data);
221 xmlEntityPtr sixtp_sax_get_entity_handler (void* user_data,
222                                            const xmlChar* name);
223 
224 gboolean sixtp_parse_file (sixtp* sixtp, const char* filename,
225                            gpointer data_for_top_level, gpointer global_data,
226                            gpointer* parse_result);
227 gboolean sixtp_parse_fd (sixtp* sixtp, FILE* fd,
228                          gpointer data_for_top_level, gpointer global_data,
229                          gpointer* parse_result);
230 gboolean sixtp_parse_buffer (sixtp* sixtp, char* bufp, int bufsz,
231                              gpointer data_for_top_level, gpointer global_data,
232                              gpointer* parse_result);
233 gboolean sixtp_parse_push (sixtp* sixtp, sixtp_push_handler push_handler,
234                            gpointer push_user_data, gpointer data_for_top_level,
235                            gpointer global_data, gpointer* parse_result);
236 
237 void sixtp_set_start (sixtp* parser, sixtp_start_handler start_handler);
238 void sixtp_set_before_child (sixtp* parser,
239                              sixtp_before_child_handler handler);
240 void sixtp_set_after_child (sixtp* parser, sixtp_after_child_handler handler);
241 void sixtp_set_end (sixtp* parser, sixtp_end_handler end_handler);
242 void sixtp_set_chars (sixtp* parser, sixtp_characters_handler char_handler);
243 void sixtp_set_cleanup_result (sixtp* parser, sixtp_result_handler handler);
244 void sixtp_set_cleanup_chars (sixtp* parser, sixtp_result_handler handler);
245 void sixtp_set_fail (sixtp* parser, sixtp_fail_handler handler);
246 void sixtp_set_result_fail (sixtp* parser, sixtp_result_handler handler);
247 void sixtp_set_chars_fail (sixtp* parser, sixtp_result_handler handler);
248 
249 sixtp* sixtp_set_any (sixtp* tochange, gboolean cleanup, ...);
250 sixtp* sixtp_add_some_sub_parsers (sixtp* tochange, gboolean cleanup, ...);
251 
252 gboolean sixtp_add_sub_parser (sixtp* parser, const gchar* tag,
253                                sixtp* sub_parser);
254 
255 QofBookFileType gnc_is_our_xml_file (const char* filename,
256                                      gboolean* with_encoding);
257 
258 QofBookFileType gnc_is_our_first_xml_chunk (char* chunk,
259                                             gboolean* with_encoding);
260 /** Call after loading each record */
261 void sixtp_run_callback (sixtp_gdv2* data, const char* type);
262 
263 #endif /* _SIXTP_H_ */
264