|
GnuCash 2.4.99
|
00001 /******************************************************************** 00002 * sixtp-to-dom-parser.c * 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 #include "config.h" 00025 00026 #include <glib.h> 00027 00028 #include <ctype.h> 00029 00030 #include "sixtp-parsers.h" 00031 #include "sixtp-utils.h" 00032 #include "sixtp.h" 00033 00034 static xmlNsPtr global_namespace = NULL; 00035 00036 /* Don't pass anything in the data_for_children value to this 00037 function. It'll cause a segfault */ 00038 static gboolean dom_start_handler( 00039 GSList* sibling_data, gpointer parent_data, gpointer global_data, 00040 gpointer *data_for_children, gpointer *result, const gchar *tag, 00041 gchar **attrs) 00042 { 00043 xmlNodePtr thing; 00044 gchar** atptr = attrs; 00045 00046 if (parent_data == NULL) 00047 { 00048 thing = xmlNewNode(global_namespace, BAD_CAST tag); 00049 /* only publish the result if we're the parent */ 00050 *result = thing; 00051 } 00052 else 00053 { 00054 thing = xmlNewChild((xmlNodePtr) parent_data, 00055 global_namespace, 00056 BAD_CAST tag, 00057 NULL); 00058 *result = NULL; 00059 } 00060 *data_for_children = thing; 00061 00062 if (attrs != NULL) 00063 { 00064 while (*atptr != 0) 00065 { 00066 xmlSetProp(thing, BAD_CAST atptr[0], BAD_CAST atptr[1]); 00067 atptr += 2; 00068 } 00069 } 00070 return TRUE; 00071 } 00072 00073 static void 00074 dom_fail_handler(gpointer data_for_children, 00075 GSList* data_from_children, 00076 GSList* sibling_data, 00077 gpointer parent_data, 00078 gpointer global_data, 00079 gpointer *result, 00080 const gchar *tag) 00081 { 00082 if (*result) xmlFreeNode(*result); 00083 } 00084 00085 static gboolean dom_chars_handler( 00086 GSList *sibling_data, gpointer parent_data, gpointer global_data, 00087 gpointer *result, const char *text, int length) 00088 { 00089 if (length > 0) 00090 { 00091 xmlNodeAddContentLen((xmlNodePtr)parent_data, BAD_CAST text, length); 00092 } 00093 return TRUE; 00094 } 00095 00096 sixtp * 00097 sixtp_dom_parser_new(sixtp_end_handler ender, 00098 sixtp_result_handler cleanup_result_by_default_func, 00099 sixtp_result_handler cleanup_result_on_fail_func) 00100 { 00101 sixtp *top_level; 00102 00103 g_return_val_if_fail(ender, NULL); 00104 00105 if (!(top_level = 00106 sixtp_set_any(sixtp_new(), FALSE, 00107 SIXTP_START_HANDLER_ID, dom_start_handler, 00108 SIXTP_CHARACTERS_HANDLER_ID, dom_chars_handler, 00109 SIXTP_END_HANDLER_ID, ender, 00110 SIXTP_FAIL_HANDLER_ID, dom_fail_handler, 00111 SIXTP_NO_MORE_HANDLERS))) 00112 { 00113 return NULL; 00114 } 00115 00116 if (cleanup_result_by_default_func) 00117 { 00118 sixtp_set_cleanup_result(top_level, cleanup_result_by_default_func); 00119 } 00120 00121 if (cleanup_result_by_default_func) 00122 { 00123 sixtp_set_result_fail(top_level, cleanup_result_on_fail_func); 00124 } 00125 00126 if (!sixtp_add_sub_parser(top_level, SIXTP_MAGIC_CATCHER, top_level)) 00127 { 00128 sixtp_destroy(top_level); 00129 return NULL; 00130 } 00131 00132 return top_level; 00133 }
1.7.4