GnuCash  5.6-150-g038405b370+
sixtp-to-dom-parser.cpp
1 /********************************************************************
2  * sixtp-to-dom-parser.c *
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 #include <config.h>
24 #include <ctype.h>
25 
26 #include <glib.h>
27 
28 #include "sixtp-parsers.h"
29 #include "sixtp-utils.h"
30 #include "sixtp.h"
31 
32 static xmlNsPtr global_namespace = NULL;
33 
34 /* Don't pass anything in the data_for_children value to this
35  function. It'll cause a segfault */
36 static gboolean dom_start_handler (
37  GSList* sibling_data, gpointer parent_data, gpointer global_data,
38  gpointer* data_for_children, gpointer* result, const gchar* tag,
39  gchar** attrs)
40 {
41  xmlNodePtr thing;
42  gchar** atptr = attrs;
43 
44  if (parent_data == NULL)
45  {
46  thing = xmlNewNode (global_namespace, BAD_CAST tag);
47  /* only publish the result if we're the parent */
48  *result = thing;
49  }
50  else
51  {
52  thing = xmlNewChild ((xmlNodePtr) parent_data,
53  global_namespace,
54  BAD_CAST tag,
55  NULL);
56  *result = NULL;
57  }
58  *data_for_children = thing;
59 
60  if (attrs != NULL)
61  {
62  while (*atptr != 0)
63  {
64  gchar* attr0 = g_strdup (atptr[0]);
65  gchar* attr1 = g_strdup (atptr[1]);
66  xmlSetProp (thing, checked_char_cast (attr0),
67  checked_char_cast (attr1));
68  g_free (attr0);
69  g_free (attr1);
70  atptr += 2;
71  }
72  }
73  return TRUE;
74 }
75 
76 static void
77 dom_fail_handler (gpointer data_for_children,
78  GSList* data_from_children,
79  GSList* sibling_data,
80  gpointer parent_data,
81  gpointer global_data,
82  gpointer* result,
83  const gchar* tag)
84 {
85  if (*result) xmlFreeNode (static_cast<xmlNodePtr> (*result));
86 }
87 
88 static gboolean dom_chars_handler (
89  GSList* sibling_data, gpointer parent_data, gpointer global_data,
90  gpointer* result, const char* text, int length)
91 {
92  if (length > 0)
93  {
94  gchar* newtext = g_strndup (text,length);
95  xmlNodeAddContentLen ((xmlNodePtr)parent_data,
96  checked_char_cast (newtext), length);
97  g_free (newtext);
98  }
99  return TRUE;
100 }
101 
102 sixtp*
103 sixtp_dom_parser_new (sixtp_end_handler ender,
104  sixtp_result_handler cleanup_result_by_default_func,
105  sixtp_result_handler cleanup_result_on_fail_func)
106 {
107  sixtp* top_level;
108 
109  g_return_val_if_fail (ender, NULL);
110 
111  if (! (top_level =
112  sixtp_set_any (sixtp_new (), FALSE,
113  SIXTP_START_HANDLER_ID, dom_start_handler,
114  SIXTP_CHARACTERS_HANDLER_ID, dom_chars_handler,
115  SIXTP_END_HANDLER_ID, ender,
116  SIXTP_FAIL_HANDLER_ID, dom_fail_handler,
117  SIXTP_NO_MORE_HANDLERS)))
118  {
119  return NULL;
120  }
121 
122  if (cleanup_result_by_default_func)
123  {
124  sixtp_set_cleanup_result (top_level, cleanup_result_by_default_func);
125  }
126 
127  if (cleanup_result_by_default_func)
128  {
129  sixtp_set_result_fail (top_level, cleanup_result_on_fail_func);
130  }
131 
132  if (!sixtp_add_sub_parser (top_level, SIXTP_MAGIC_CATCHER, top_level))
133  {
134  sixtp_destroy (top_level);
135  return NULL;
136  }
137 
138  return top_level;
139 }
Definition: sixtp.h:129