Branch data Line data Source code
1 : : /* Copyright (C) 2008 Luc Pionchon
2 : : * Copyright (C) 2012 David King
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
6 : : * This library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public
8 : : * License as published by the Free Software Foundation; either
9 : : * version 2.1 of the License, or (at your option) any later version.
10 : : *
11 : : * This library is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General Public
17 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 : : */
19 : :
20 : : #include <glib.h>
21 : :
22 : : static void foo_parser_start_element (GMarkupParseContext *context,
23 : : const gchar *element_name,
24 : : const gchar **attribute_names,
25 : : const gchar **attribute_values,
26 : : gpointer user_data,
27 : : GError **error);
28 : : static void foo_parser_end_element (GMarkupParseContext *context,
29 : : const gchar *element_name,
30 : : gpointer user_data,
31 : : GError **error);
32 : : static void foo_parser_characters (GMarkupParseContext *context,
33 : : const gchar *text,
34 : : gsize text_len,
35 : : gpointer user_data,
36 : : GError **error);
37 : : static void foo_parser_passthrough (GMarkupParseContext *context,
38 : : const gchar *passthrough_text,
39 : : gsize text_len,
40 : : gpointer user_data,
41 : : GError **error);
42 : : static void foo_parser_error (GMarkupParseContext *context,
43 : : GError *error,
44 : : gpointer user_data);
45 : :
46 : : /*
47 : : * Parser
48 : : */
49 : : static const GMarkupParser foo_xml_parser = {
50 : : foo_parser_start_element,
51 : : foo_parser_end_element,
52 : : foo_parser_characters,
53 : : foo_parser_passthrough,
54 : : foo_parser_error
55 : : };
56 : :
57 : : /*
58 : : * Called for opening tags like <foo bar="baz">
59 : : */
60 : : static void
61 : 0 : foo_parser_start_element (GMarkupParseContext *context,
62 : : const gchar *element_name,
63 : : const gchar **attribute_names,
64 : : const gchar **attribute_values,
65 : : gpointer user_data,
66 : : GError **error)
67 : : {
68 : 0 : g_print ("element: <%s>\n", element_name);
69 : :
70 [ # # ]: 0 : for (gsize i = 0; attribute_names[i]; i++)
71 : : {
72 : 0 : g_print ("attribute: %s = \"%s\"\n", attribute_names[i],
73 : 0 : attribute_values[i]);
74 : : }
75 : 0 : }
76 : :
77 : : /*
78 : : * Called for closing tags like </foo>
79 : : */
80 : : static void
81 : 0 : foo_parser_end_element (GMarkupParseContext *context,
82 : : const gchar *element_name,
83 : : gpointer user_data,
84 : : GError **error)
85 : : {
86 : 0 : g_print ("element: </%s>\n", element_name);
87 : 0 : }
88 : :
89 : : /*
90 : : * Called for character data. Text is not nul-terminated
91 : : */
92 : : static void
93 : 0 : foo_parser_characters (GMarkupParseContext *context,
94 : : const gchar *text,
95 : : gsize text_len,
96 : : gpointer user_data,
97 : : GError **error)
98 : : {
99 : 0 : g_print ("text: [%s]\n", text);
100 : 0 : }
101 : :
102 : : /*
103 : : * Called for strings that should be re-saved verbatim in this same
104 : : * position, but are not otherwise interpretable. At the moment this
105 : : * includes comments and processing instructions. Text is not
106 : : * nul-terminated.
107 : : */
108 : : static void
109 : 0 : foo_parser_passthrough (GMarkupParseContext *context,
110 : : const gchar *passthrough_text,
111 : : gsize text_len,
112 : : gpointer user_data,
113 : : GError **error)
114 : : {
115 : 0 : g_print ("passthrough: %s\n", passthrough_text);
116 : 0 : }
117 : :
118 : : /*
119 : : * Called when any parsing method encounters an error. The GError should not be
120 : : * freed.
121 : : */
122 : : static void
123 : 0 : foo_parser_error (GMarkupParseContext *context,
124 : : GError *error,
125 : : gpointer user_data)
126 : : {
127 : 0 : g_printerr ("ERROR: %s\n", error->message);
128 : 0 : }
129 : :
130 : : int
131 : 0 : main (void)
132 : : {
133 : : GMarkupParseContext *context;
134 : 0 : gboolean success = FALSE;
135 : : glong len;
136 : :
137 : : /*
138 : : * Example XML for the parser.
139 : : */
140 : 0 : const gchar foo_xml_example[] =
141 : : "<foo bar='baz' bir='boz'>"
142 : : " <bar>bar text 1</bar> "
143 : : " <bar>bar text 2</bar> "
144 : : " foo text "
145 : : "<!-- nothing --> "
146 : : "</foo> ";
147 : :
148 : 0 : len = g_utf8_strlen (foo_xml_example, -1);
149 : 0 : g_print ("Parsing: %s\n", foo_xml_example);
150 : 0 : g_print ("(%ld UTF-8 characters)\n", len);
151 : :
152 : 0 : context = g_markup_parse_context_new (&foo_xml_parser, G_MARKUP_DEFAULT_FLAGS, NULL, NULL);
153 : :
154 : 0 : success = g_markup_parse_context_parse (context, foo_xml_example, len, NULL);
155 : :
156 : 0 : g_markup_parse_context_free (context);
157 : :
158 [ # # ]: 0 : if (success)
159 : : {
160 : 0 : g_print ("DONE\n");
161 : 0 : return 0;
162 : : }
163 : : else
164 : : {
165 : 0 : g_printerr ("ERROR\n");
166 : 0 : return 1;
167 : : }
168 : : }
|