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