1 /* gmarkup.h - Simple XML-like string parser/writer
3 * Copyright 2000 Red Hat, Inc.
5 * GLib is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * GLib 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with GLib; see the file COPYING.LIB. If not,
17 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
22 #error "Only <glib.h> can be included directly."
25 #ifndef __G_MARKUP_H__
26 #define __G_MARKUP_H__
30 #include <glib/gerror.h>
31 #include <glib/gslist.h>
37 * @G_MARKUP_ERROR_BAD_UTF8: text being parsed was not valid UTF-8
38 * @G_MARKUP_ERROR_EMPTY: document contained nothing, or only whitespace
39 * @G_MARKUP_ERROR_PARSE: document was ill-formed
40 * @G_MARKUP_ERROR_UNKNOWN_ELEMENT: error should be set by #GMarkupParser
41 * functions; element wasn't known
42 * @G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE: error should be set by #GMarkupParser
43 * functions; attribute wasn't known
44 * @G_MARKUP_ERROR_INVALID_CONTENT: error should be set by #GMarkupParser
45 * functions; content was invalid
46 * @G_MARKUP_ERROR_MISSING_ATTRIBUTE: error should be set by #GMarkupParser
47 * functions; a required attribute was missing
49 * Error codes returned by markup parsing.
53 G_MARKUP_ERROR_BAD_UTF8,
56 /* The following are primarily intended for specific GMarkupParser
57 * implementations to set.
59 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
60 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
61 G_MARKUP_ERROR_INVALID_CONTENT,
62 G_MARKUP_ERROR_MISSING_ATTRIBUTE
68 * Error domain for markup parsing.
69 * Errors in this domain will be from the #GMarkupError enumeration.
70 * See #GError for information on error domains.
72 #define G_MARKUP_ERROR g_markup_error_quark ()
74 GQuark g_markup_error_quark (void);
78 * @G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG: flag you should not use
79 * @G_MARKUP_TREAT_CDATA_AS_TEXT: When this flag is set, CDATA marked
80 * sections are not passed literally to the @passthrough function of
81 * the parser. Instead, the content of the section (without the
82 * <literal><![CDATA[</literal> and <literal>]]></literal>) is
83 * passed to the @text function. This flag was added in GLib 2.12
84 * @G_MARKUP_PREFIX_ERROR_POSITION: Normally errors caught by GMarkup
85 * itself have line/column information prefixed to them to let the
86 * caller know the location of the error. When this flag is set the
87 * location information is also prefixed to errors generated by the
88 * #GMarkupParser implementation functions
90 * Flags that affect the behaviour of the parser.
94 G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0,
95 G_MARKUP_TREAT_CDATA_AS_TEXT = 1 << 1,
96 G_MARKUP_PREFIX_ERROR_POSITION = 1 << 2
100 * GMarkupParseContext:
102 * A parse context is used to parse a stream of bytes that
103 * you expect to contain marked-up text.
105 * See g_markup_parse_context_new(), #GMarkupParser, and so
106 * on for more details.
108 typedef struct _GMarkupParseContext GMarkupParseContext;
109 typedef struct _GMarkupParser GMarkupParser;
113 * @start_element: Callback to invoke when the opening tag of an element
115 * @end_element: Callback to invoke when the closing tag of an element
116 * is seen. Note that this is also called for empty tags like
117 * <literal><empty/></literal>.
118 * @text: Callback to invoke when some text is seen (text is always
119 * inside an element). Note that the text of an element may be spread
120 * over multiple calls of this function. If the
121 * %G_MARKUP_TREAT_CDATA_AS_TEXT flag is set, this function is also
122 * called for the content of CDATA marked sections.
123 * @passthrough: Callback to invoke for comments, processing instructions
124 * and doctype declarations; if you're re-writing the parsed document,
125 * write the passthrough text back out in the same position. If the
126 * %G_MARKUP_TREAT_CDATA_AS_TEXT flag is not set, this function is also
127 * called for CDATA marked sections.
128 * @error: Callback to invoke when an error occurs.
130 * Any of the fields in #GMarkupParser can be %NULL, in which case they
131 * will be ignored. Except for the @error function, any of these callbacks
132 * can set an error; in particular the %G_MARKUP_ERROR_UNKNOWN_ELEMENT,
133 * %G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, and %G_MARKUP_ERROR_INVALID_CONTENT
134 * errors are intended to be set from these callbacks. If you set an error
135 * from a callback, g_markup_parse_context_parse() will report that error
136 * back to its caller.
138 struct _GMarkupParser
140 /* Called for open tags <foo bar="baz"> */
141 void (*start_element) (GMarkupParseContext *context,
142 const gchar *element_name,
143 const gchar **attribute_names,
144 const gchar **attribute_values,
148 /* Called for close tags </foo> */
149 void (*end_element) (GMarkupParseContext *context,
150 const gchar *element_name,
154 /* Called for character data */
155 /* text is not nul-terminated */
156 void (*text) (GMarkupParseContext *context,
162 /* Called for strings that should be re-saved verbatim in this same
163 * position, but are not otherwise interpretable. At the moment
164 * this includes comments and processing instructions.
166 /* text is not nul-terminated. */
167 void (*passthrough) (GMarkupParseContext *context,
168 const gchar *passthrough_text,
173 /* Called on error, including one set by other
174 * methods in the vtable. The GError should not be freed.
176 void (*error) (GMarkupParseContext *context,
181 GMarkupParseContext *g_markup_parse_context_new (const GMarkupParser *parser,
182 GMarkupParseFlags flags,
184 GDestroyNotify user_data_dnotify);
185 void g_markup_parse_context_free (GMarkupParseContext *context);
186 gboolean g_markup_parse_context_parse (GMarkupParseContext *context,
190 void g_markup_parse_context_push (GMarkupParseContext *context,
191 const GMarkupParser *parser,
193 gpointer g_markup_parse_context_pop (GMarkupParseContext *context);
195 gboolean g_markup_parse_context_end_parse (GMarkupParseContext *context,
197 G_CONST_RETURN gchar *g_markup_parse_context_get_element (GMarkupParseContext *context);
198 G_CONST_RETURN GSList *g_markup_parse_context_get_element_stack (GMarkupParseContext *context);
200 /* For user-constructed error messages, has no precise semantics */
201 void g_markup_parse_context_get_position (GMarkupParseContext *context,
204 gpointer g_markup_parse_context_get_user_data (GMarkupParseContext *context);
206 /* useful when saving */
207 gchar* g_markup_escape_text (const gchar *text,
210 gchar *g_markup_printf_escaped (const char *format,
211 ...) G_GNUC_PRINTF (1, 2);
212 gchar *g_markup_vprintf_escaped (const char *format,
217 G_MARKUP_COLLECT_INVALID,
218 G_MARKUP_COLLECT_STRING,
219 G_MARKUP_COLLECT_STRDUP,
220 G_MARKUP_COLLECT_BOOLEAN,
221 G_MARKUP_COLLECT_TRISTATE,
223 G_MARKUP_COLLECT_OPTIONAL = (1 << 16)
224 } GMarkupCollectType;
227 /* useful from start_element */
228 gboolean g_markup_collect_attributes (const gchar *element_name,
229 const gchar **attribute_names,
230 const gchar **attribute_values,
232 GMarkupCollectType first_type,
233 const gchar *first_attr,
238 #endif /* __G_MARKUP_H__ */