GnuCash 2.4.99
sixtp.h
00001 /********************************************************************
00002  * sixtp.h -- header file for XML parsing                           *
00003  * Copyright 2001 Gnumatic, Inc.                                    *
00004  *                                                                  *
00005  * This program is free software; you can redistribute it and/or    *
00006  * modify it under the terms of the GNU General Public License as   *
00007  * published by the Free Software Foundation; either version 2 of   *
00008  * the License, or (at your option) any later version.              *
00009  *                                                                  *
00010  * This program is distributed in the hope that it will be useful,  *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00013  * GNU General Public License for more details.                     *
00014  *                                                                  *
00015  * You should have received a copy of the GNU General Public License*
00016  * along with this program; if not, contact:                        *
00017  *                                                                  *
00018  * Free Software Foundation           Voice:  +1-617-542-5942       *
00019  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00020  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00021  *                                                                  *
00022  ********************************************************************/
00023 
00024 #ifndef SIXTP_H
00025 #define SIXTP_H
00026 
00027 #include <glib.h>
00028 #include <stdio.h>
00029 
00030 #include <stdarg.h>
00031 
00032 #include "gnc-xml-helper.h"
00033 #include "gnc-engine.h"
00034 #include "gnc-backend-xml.h"
00035 
00036 typedef struct _sixtp_child_result sixtp_child_result;
00037 
00038 typedef gboolean (*sixtp_start_handler)(GSList* sibling_data,
00039                                         gpointer parent_data,
00040                                         gpointer global_data,
00041                                         gpointer *data_for_children,
00042                                         gpointer *result,
00043                                         const gchar *tag,
00044                                         gchar **attrs);
00045 
00046 typedef gboolean (*sixtp_before_child_handler)(gpointer data_for_children,
00047         GSList* data_from_children,
00048         GSList* sibling_data,
00049         gpointer parent_data,
00050         gpointer global_data,
00051         gpointer *result,
00052         const gchar *tag,
00053         const gchar *child_tag);
00054 
00055 typedef gboolean (*sixtp_after_child_handler)(gpointer data_for_children,
00056         GSList* data_from_children,
00057         GSList* sibling_data,
00058         gpointer parent_data,
00059         gpointer global_data,
00060         gpointer *result,
00061         const gchar *tag,
00062         const gchar *child_tag,
00063         sixtp_child_result *child_result);
00064 
00065 typedef gboolean (*sixtp_end_handler)(gpointer data_for_children,
00066                                       GSList* data_from_children,
00067                                       GSList* sibling_data,
00068                                       gpointer parent_data,
00069                                       gpointer global_data,
00070                                       gpointer *result,
00071                                       const gchar *tag);
00072 
00073 typedef gboolean (*sixtp_characters_handler)(GSList *sibling_data,
00074         gpointer parent_data,
00075         gpointer global_data,
00076         gpointer *result,
00077         const char *text,
00078         int length);
00079 
00080 typedef void (*sixtp_result_handler)(sixtp_child_result *result);
00081 
00082 typedef void (*sixtp_fail_handler)(gpointer data_for_children,
00083                                    GSList* data_from_children,
00084                                    GSList* sibling_data,
00085                                    gpointer parent_data,
00086                                    gpointer global_data,
00087                                    gpointer *result,
00088                                    const gchar *tag);
00089 
00090 typedef void (*sixtp_push_handler)(xmlParserCtxtPtr xml_context,
00091                                    gpointer user_data);
00092 
00093 typedef struct sixtp
00094 {
00095     /* If you change this, don't forget to modify all the copy/etc. functions */
00096     sixtp_start_handler start_handler;
00097     sixtp_before_child_handler before_child;
00098     sixtp_after_child_handler after_child;
00099     sixtp_end_handler end_handler;
00100     sixtp_characters_handler characters_handler;
00101 
00102     sixtp_fail_handler fail_handler;
00103     /* called for failures before the close tag */
00104 
00105     sixtp_result_handler cleanup_result; /* called unless failure */
00106     sixtp_result_handler cleanup_chars; /* called unless failure */
00107 
00108     sixtp_result_handler result_fail_handler;
00109     /* called to cleanup results from this node on failure */
00110 
00111     sixtp_result_handler chars_fail_handler;
00112     /* called to cleanup character results when cleaning up this node's
00113        children. */
00114 
00115     GHashTable *child_parsers;
00116 } sixtp;
00117 
00118 typedef enum
00119 {
00120     SIXTP_NO_MORE_HANDLERS,
00121 
00122     SIXTP_START_HANDLER_ID,
00123     SIXTP_BEFORE_CHILD_HANDLER_ID,
00124     SIXTP_AFTER_CHILD_HANDLER_ID,
00125     SIXTP_END_HANDLER_ID,
00126     SIXTP_CHARACTERS_HANDLER_ID,
00127 
00128     SIXTP_FAIL_HANDLER_ID,
00129 
00130     SIXTP_CLEANUP_RESULT_ID,
00131     SIXTP_CLEANUP_CHARS_ID,
00132 
00133     SIXTP_RESULT_FAIL_ID,
00134 
00135     SIXTP_CHARS_FAIL_ID,
00136 } sixtp_handler_type;
00137 
00138 /* completely invalid tag for xml */
00139 #define SIXTP_MAGIC_CATCHER "&MAGIX&"
00140 
00141 typedef enum
00142 {
00143     SIXTP_CHILD_RESULT_CHARS,
00144     SIXTP_CHILD_RESULT_NODE
00145 } sixtp_child_result_type;
00146 
00147 struct _sixtp_child_result
00148 {
00149     sixtp_child_result_type type;
00150     gchar *tag; /* NULL for a CHARS node. */
00151     gpointer data;
00152     gboolean should_cleanup;
00153     sixtp_result_handler cleanup_handler;
00154     sixtp_result_handler fail_handler;
00155 };
00156 
00157 typedef struct sixtp_sax_data
00158 {
00159     gboolean parsing_ok;
00160     GSList *stack;
00161     gpointer global_data;
00162     xmlParserCtxtPtr saxParserCtxt;
00163     sixtp *bad_xml_parser;
00164 } sixtp_sax_data;
00165 
00166 
00167 gboolean is_child_result_from_node_named(sixtp_child_result *cr,
00168         const char *tag);
00169 void sixtp_child_free_data(sixtp_child_result *result);
00170 void sixtp_child_result_destroy(sixtp_child_result *r);
00171 void sixtp_child_result_print(sixtp_child_result *cr, FILE *f);
00172 
00173 void sixtp_sax_start_handler(void *user_data, const xmlChar *name,
00174                              const xmlChar **attrs);
00175 void sixtp_sax_characters_handler(void *user_data, const xmlChar *text,
00176                                   int len);
00177 void sixtp_sax_end_handler(void *user_data, const xmlChar *name);
00178 
00179 sixtp* sixtp_new(void);
00180 void sixtp_destroy(sixtp *sp);
00181 
00182 void sixtp_handle_catastrophe(sixtp_sax_data *sax_data);
00183 xmlEntityPtr sixtp_sax_get_entity_handler(void *user_data, const xmlChar *name);
00184 
00185 gboolean sixtp_parse_file(sixtp *sixtp, const char *filename,
00186                           gpointer data_for_top_level, gpointer global_data,
00187                           gpointer *parse_result);
00188 gboolean sixtp_parse_buffer(sixtp *sixtp, char *bufp, int bufsz,
00189                             gpointer data_for_top_level, gpointer global_data,
00190                             gpointer *parse_result);
00191 gboolean sixtp_parse_push(sixtp *sixtp, sixtp_push_handler push_handler,
00192                           gpointer push_user_data, gpointer data_for_top_level,
00193                           gpointer global_data, gpointer *parse_result);
00194 
00195 void sixtp_set_start(sixtp *parser, sixtp_start_handler start_handler);
00196 void sixtp_set_before_child(sixtp *parser, sixtp_before_child_handler handler);
00197 void sixtp_set_after_child(sixtp *parser, sixtp_after_child_handler handler);
00198 void sixtp_set_end(sixtp *parser, sixtp_end_handler end_handler);
00199 void sixtp_set_chars(sixtp *parser, sixtp_characters_handler char_handler);
00200 void sixtp_set_cleanup_result(sixtp *parser, sixtp_result_handler handler);
00201 void sixtp_set_cleanup_chars(sixtp *parser, sixtp_result_handler handler);
00202 void sixtp_set_fail(sixtp *parser, sixtp_fail_handler handler);
00203 void sixtp_set_result_fail(sixtp *parser, sixtp_result_handler handler);
00204 void sixtp_set_chars_fail(sixtp *parser, sixtp_result_handler handler);
00205 
00206 sixtp* sixtp_set_any(sixtp *tochange, gboolean cleanup, ...);
00207 sixtp* sixtp_add_some_sub_parsers(sixtp *tochange, gboolean cleanup, ...);
00208 
00209 gboolean sixtp_add_sub_parser(sixtp *parser, const gchar* tag,
00210                               sixtp *sub_parser);
00211 
00212 QofBookFileType gnc_is_our_xml_file(const char *filename,
00213                                     gboolean *with_encoding);
00214 
00215 QofBookFileType gnc_is_our_first_xml_chunk(char *chunk, gboolean *with_encoding);
00216 
00217 
00218 #endif /* _SIXTP_H_ */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines