1 /* gmarkup.c - Simple XML-like parser
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.
33 g_markup_error_quark (void)
35 static GQuark error_quark = 0;
38 error_quark = g_quark_from_static_string ("g-markup-error-quark");
46 STATE_AFTER_OPEN_ANGLE,
47 STATE_AFTER_CLOSE_ANGLE,
48 STATE_AFTER_ELISION_SLASH, /* the slash that obviates need for end element */
49 STATE_INSIDE_OPEN_TAG_NAME,
50 STATE_INSIDE_ATTRIBUTE_NAME,
51 STATE_BETWEEN_ATTRIBUTES,
52 STATE_AFTER_ATTRIBUTE_EQUALS_SIGN,
53 STATE_INSIDE_ATTRIBUTE_VALUE_SQ,
54 STATE_INSIDE_ATTRIBUTE_VALUE_DQ,
56 STATE_AFTER_CLOSE_TAG_SLASH,
57 STATE_INSIDE_CLOSE_TAG_NAME,
58 STATE_INSIDE_PASSTHROUGH,
62 struct _GMarkupParseContext
64 const GMarkupParser *parser;
66 GMarkupParseFlags flags;
72 GDestroyNotify dnotify;
74 /* A piece of character data or an element that
75 * hasn't "ended" yet so we haven't yet called
76 * the callback for it.
78 GString *partial_chunk;
80 GMarkupParseState state;
87 const gchar *current_text;
88 gssize current_text_len;
89 const gchar *current_text_end;
91 GString *leftover_char_portion;
93 /* used to save the start of the last interesting thingy */
98 guint document_empty : 1;
104 * g_markup_parse_context_new:
105 * @parser: a #GMarkupParser
106 * @flags: one or more #GMarkupParseFlags
107 * @user_data: user data to pass to #GMarkupParser functions
108 * @user_data_dnotify: user data destroy notifier called when the parse context is freed
110 * Creates a new parse context. A parse context is used to parse
111 * marked-up documents. You can feed any number of documents into
112 * a context, as long as no errors occur; once an error occurs,
113 * the parse context can't continue to parse text (you have to free it
114 * and create a new parse context).
116 * Return value: a new #GMarkupParseContext
118 GMarkupParseContext *
119 g_markup_parse_context_new (const GMarkupParser *parser,
120 GMarkupParseFlags flags,
122 GDestroyNotify user_data_dnotify)
124 GMarkupParseContext *context;
126 g_return_val_if_fail (parser != NULL, NULL);
128 context = g_new (GMarkupParseContext, 1);
130 context->parser = parser;
131 context->flags = flags;
132 context->user_data = user_data;
133 context->dnotify = user_data_dnotify;
135 context->line_number = 1;
136 context->char_number = 1;
138 context->partial_chunk = NULL;
140 context->state = STATE_START;
141 context->tag_stack = NULL;
142 context->attr_names = NULL;
143 context->attr_values = NULL;
144 context->cur_attr = -1;
145 context->alloc_attrs = 0;
147 context->current_text = NULL;
148 context->current_text_len = -1;
149 context->current_text_end = NULL;
150 context->leftover_char_portion = NULL;
152 context->start = NULL;
153 context->iter = NULL;
155 context->document_empty = TRUE;
156 context->parsing = FALSE;
158 context->balance = 0;
164 * g_markup_parse_context_free:
165 * @context: a #GMarkupParseContext
167 * Frees a #GMarkupParseContext. Can't be called from inside
168 * one of the #GMarkupParser functions.
172 g_markup_parse_context_free (GMarkupParseContext *context)
174 g_return_if_fail (context != NULL);
175 g_return_if_fail (!context->parsing);
177 if (context->dnotify)
178 (* context->dnotify) (context->user_data);
180 g_strfreev (context->attr_names);
181 g_strfreev (context->attr_values);
183 g_slist_foreach (context->tag_stack, (GFunc)g_free, NULL);
184 g_slist_free (context->tag_stack);
186 if (context->partial_chunk)
187 g_string_free (context->partial_chunk, TRUE);
189 if (context->leftover_char_portion)
190 g_string_free (context->leftover_char_portion, TRUE);
196 mark_error (GMarkupParseContext *context,
199 context->state = STATE_ERROR;
201 if (context->parser->error)
202 (*context->parser->error) (context, error, context->user_data);
206 set_error (GMarkupParseContext *context,
216 va_start (args, format);
217 s = g_strdup_vprintf (format, args);
220 tmp_error = g_error_new (G_MARKUP_ERROR,
222 _("Error on line %d char %d: %s"),
223 context->line_number,
224 context->char_number,
229 mark_error (context, tmp_error);
231 g_propagate_error (error, tmp_error);
235 is_name_start_char (gunichar c)
237 if (g_unichar_isalpha (c) ||
246 is_name_char (gunichar c)
248 if (g_unichar_isalnum (c) ||
260 char_str (gunichar c,
264 g_unichar_to_utf8 (c, buf);
269 utf8_str (const gchar *utf8,
272 char_str (g_utf8_get_char (utf8), buf);
277 set_unescape_error (GMarkupParseContext *context,
279 const gchar *remaining_text,
280 const gchar *remaining_text_end,
288 gint remaining_newlines;
291 remaining_newlines = 0;
293 while (p != remaining_text_end)
296 ++remaining_newlines;
300 va_start (args, format);
301 s = g_strdup_vprintf (format, args);
304 tmp_error = g_error_new (G_MARKUP_ERROR,
306 _("Error on line %d: %s"),
307 context->line_number - remaining_newlines,
312 mark_error (context, tmp_error);
314 g_propagate_error (error, tmp_error);
320 USTATE_AFTER_AMPERSAND,
321 USTATE_INSIDE_ENTITY_NAME,
322 USTATE_AFTER_CHARREF_HASH
326 unescape_text (GMarkupParseContext *context,
328 const gchar *text_end,
332 #define MAX_ENT_LEN 5
338 str = g_string_new ("");
340 state = USTATE_INSIDE_TEXT;
343 while (p != text_end && context->state != STATE_ERROR)
345 g_assert (p < text_end);
349 case USTATE_INSIDE_TEXT:
351 while (p != text_end && *p != '&')
352 p = g_utf8_next_char (p);
356 g_string_append_len (str, start, p - start);
361 if (p != text_end && *p == '&')
363 p = g_utf8_next_char (p);
364 state = USTATE_AFTER_AMPERSAND;
369 case USTATE_AFTER_AMPERSAND:
373 p = g_utf8_next_char (p);
376 state = USTATE_AFTER_CHARREF_HASH;
378 else if (!is_name_start_char (g_utf8_get_char (p)))
382 set_unescape_error (context, error,
384 G_MARKUP_ERROR_PARSE,
385 _("Empty entity '&;' seen; valid "
386 "entities are: & " < > '"));
392 set_unescape_error (context, error,
394 G_MARKUP_ERROR_PARSE,
395 _("Character '%s' is not valid at "
396 "the start of an entity name; "
397 "the & character begins an entity; "
398 "if this ampersand isn't supposed "
399 "to be an entity, escape it as "
407 state = USTATE_INSIDE_ENTITY_NAME;
413 case USTATE_INSIDE_ENTITY_NAME:
415 gchar buf[MAX_ENT_LEN+1] = {
416 '\0', '\0', '\0', '\0', '\0', '\0'
420 while (p != text_end)
424 else if (!is_name_char (*p))
428 set_unescape_error (context, error,
430 G_MARKUP_ERROR_PARSE,
431 _("Character '%s' is not valid "
432 "inside an entity name"),
437 p = g_utf8_next_char (p);
440 if (context->state != STATE_ERROR)
455 /* move to after semicolon */
456 p = g_utf8_next_char (p);
458 state = USTATE_INSIDE_TEXT;
460 if (strcmp (buf, "lt") == 0)
461 g_string_append_c (str, '<');
462 else if (strcmp (buf, "gt") == 0)
463 g_string_append_c (str, '>');
464 else if (strcmp (buf, "amp") == 0)
465 g_string_append_c (str, '&');
466 else if (strcmp (buf, "quot") == 0)
467 g_string_append_c (str, '"');
468 else if (strcmp (buf, "apos") == 0)
469 g_string_append_c (str, '\'');
472 set_unescape_error (context, error,
474 G_MARKUP_ERROR_PARSE,
475 _("Entity name '%s' is not known"),
481 set_unescape_error (context, error,
482 /* give line number of the & */
484 G_MARKUP_ERROR_PARSE,
485 _("Entity did not end with a semicolon; "
486 "most likely you used an ampersand "
487 "character without intending to start "
488 "an entity - escape ampersand as &"));
494 case USTATE_AFTER_CHARREF_HASH:
496 gboolean is_hex = FALSE;
500 p = g_utf8_next_char (p);
504 while (p != text_end && *p != ';')
505 p = g_utf8_next_char (p);
509 g_assert (*p == ';');
511 /* digit is between start and p */
515 gchar *digit = g_strndup (start, p - start);
518 gchar *digit_end = digit + (p - start);
522 l = strtoul (digit, &end, 16);
524 l = strtoul (digit, &end, 10);
526 if (end != digit_end || errno != 0)
528 set_unescape_error (context, error,
530 G_MARKUP_ERROR_PARSE,
531 _("Failed to parse '%s', which "
532 "should have been a digit "
533 "inside a character reference "
534 "(ê for example) - perhaps "
535 "the digit is too large"),
540 /* characters XML permits */
544 (l >= 0x20 && l <= 0xD7FF) ||
545 (l >= 0xE000 && l <= 0xFFFD) ||
546 (l >= 0x10000 && l <= 0x10FFFF))
549 g_string_append (str, char_str (l, buf));
553 set_unescape_error (context, error,
555 G_MARKUP_ERROR_PARSE,
556 _("Character reference '%s' does not encode a permitted character"),
563 /* Move to next state */
564 p = g_utf8_next_char (p); /* past semicolon */
566 state = USTATE_INSIDE_TEXT;
570 set_unescape_error (context, error,
572 G_MARKUP_ERROR_PARSE,
573 _("Empty character reference; "
574 "should include a digit such as "
580 set_unescape_error (context, error,
582 G_MARKUP_ERROR_PARSE,
583 _("Character reference did not end with a "
585 "most likely you used an ampersand "
586 "character without intending to start "
587 "an entity - escape ampersand as &"));
593 g_assert_not_reached ();
598 if (context->state != STATE_ERROR)
602 case USTATE_INSIDE_TEXT:
604 case USTATE_AFTER_AMPERSAND:
605 case USTATE_INSIDE_ENTITY_NAME:
606 set_unescape_error (context, error,
608 G_MARKUP_ERROR_PARSE,
609 _("Unfinished entity reference"));
611 case USTATE_AFTER_CHARREF_HASH:
612 set_unescape_error (context, error,
614 G_MARKUP_ERROR_PARSE,
615 _("Unfinished character reference"));
620 if (context->state == STATE_ERROR)
622 g_string_free (str, TRUE);
628 *unescaped = g_string_free (str, FALSE);
636 advance_char (GMarkupParseContext *context)
639 context->iter = g_utf8_next_char (context->iter);
640 context->char_number += 1;
641 if (*context->iter == '\n')
643 context->line_number += 1;
644 context->char_number = 1;
647 return context->iter != context->current_text_end;
653 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
657 skip_spaces (GMarkupParseContext *context)
661 if (!xml_isspace (*context->iter))
664 while (advance_char (context));
668 advance_to_name_end (GMarkupParseContext *context)
672 if (!is_name_char (g_utf8_get_char (context->iter)))
675 while (advance_char (context));
679 add_to_partial (GMarkupParseContext *context,
680 const gchar *text_start,
681 const gchar *text_end)
683 if (context->partial_chunk == NULL)
684 context->partial_chunk = g_string_new ("");
686 if (text_start != text_end)
687 g_string_append_len (context->partial_chunk, text_start,
688 text_end - text_start);
690 /* Invariant here that partial_chunk exists */
694 truncate_partial (GMarkupParseContext *context)
696 if (context->partial_chunk != NULL)
698 context->partial_chunk = g_string_truncate (context->partial_chunk, 0);
703 current_element (GMarkupParseContext *context)
705 return context->tag_stack->data;
709 current_attribute (GMarkupParseContext *context)
711 g_assert (context->cur_attr >= 0);
712 return context->attr_names[context->cur_attr];
716 find_current_text_end (GMarkupParseContext *context)
718 /* This function must be safe (non-segfaulting) on invalid UTF8 */
719 const gchar *end = context->current_text + context->current_text_len;
723 g_assert (context->current_text_len > 0);
725 p = context->current_text;
726 next = g_utf8_find_next_char (p, end);
728 while (next && *next)
733 next = g_utf8_find_next_char (p, end);
736 /* p is now the start of the last character or character portion. */
738 next = g_utf8_next_char (p); /* this only touches *p, nothing beyond */
742 /* whole character */
743 context->current_text_end = end;
748 context->leftover_char_portion = g_string_new_len (p, end - p);
749 context->current_text_len -= (end - p);
750 context->current_text_end = p;
755 add_attribute (GMarkupParseContext *context, char *name)
757 if (context->cur_attr + 2 >= context->alloc_attrs)
759 context->alloc_attrs += 5; /* silly magic number */
760 context->attr_names = g_realloc (context->attr_names, sizeof(char*)*context->alloc_attrs);
761 context->attr_values = g_realloc (context->attr_values, sizeof(char*)*context->alloc_attrs);
764 context->attr_names[context->cur_attr] = name;
765 context->attr_values[context->cur_attr] = NULL;
766 context->attr_names[context->cur_attr+1] = NULL;
767 context->attr_values[context->cur_attr+1] = NULL;
771 * g_markup_parse_context_parse:
772 * @context: a #GMarkupParseContext
773 * @text: chunk of text to parse
774 * @text_len: length of @text in bytes
775 * @error: return location for a #GError
777 * Feed some data to the #GMarkupParseContext. The data need not
778 * be valid UTF-8; an error will be signaled if it's invalid.
779 * The data need not be an entire document; you can feed a document
780 * into the parser incrementally, via multiple calls to this function.
781 * Typically, as you receive data from a network connection or file,
782 * you feed each received chunk of data into this function, aborting
783 * the process if an error occurs. Once an error is reported, no further
784 * data may be fed to the #GMarkupParseContext; all errors are fatal.
786 * Return value: %FALSE if an error occurred, %TRUE on success
789 g_markup_parse_context_parse (GMarkupParseContext *context,
794 const gchar *first_invalid;
796 g_return_val_if_fail (context != NULL, FALSE);
797 g_return_val_if_fail (text != NULL, FALSE);
798 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
799 g_return_val_if_fail (!context->parsing, FALSE);
802 text_len = strlen (text);
807 context->parsing = TRUE;
809 if (context->leftover_char_portion)
811 const gchar *first_char;
813 if ((*text & 0xc0) != 0x80)
816 first_char = g_utf8_find_next_char (text, text + text_len);
820 /* leftover_char_portion was completed. Parse it. */
821 GString *portion = context->leftover_char_portion;
823 g_string_append_len (context->leftover_char_portion,
824 text, first_char - text);
826 /* hacks to allow recursion */
827 context->parsing = FALSE;
828 context->leftover_char_portion = NULL;
830 if (!g_markup_parse_context_parse (context,
831 portion->str, portion->len,
834 g_assert (context->state == STATE_ERROR);
837 g_string_free (portion, TRUE);
838 context->parsing = TRUE;
840 /* Skip the fraction of char that was in this text */
841 text_len -= (first_char - text);
846 /* another little chunk of the leftover char; geez
847 * someone is inefficient.
849 g_string_append_len (context->leftover_char_portion,
852 if (context->leftover_char_portion->len > 7)
854 /* The leftover char portion is too big to be
859 G_MARKUP_ERROR_BAD_UTF8,
860 _("Invalid UTF-8 encoded text"));
867 context->current_text = text;
868 context->current_text_len = text_len;
869 context->iter = context->current_text;
870 context->start = context->iter;
872 /* Nothing left after finishing the leftover char, or nothing
873 * passed in to begin with.
875 if (context->current_text_len == 0)
878 /* find_current_text_end () assumes the string starts at
879 * a character start, so we need to validate at least
880 * that much. It doesn't assume any following bytes
883 if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
887 G_MARKUP_ERROR_BAD_UTF8,
888 _("Invalid UTF-8 encoded text"));
892 /* Initialize context->current_text_end, possibly adjusting
893 * current_text_len, and add any leftover char portion
895 find_current_text_end (context);
897 /* Validate UTF8 (must be done after we find the end, since
898 * we could have a trailing incomplete char)
900 if (!g_utf8_validate (context->current_text,
901 context->current_text_len,
906 p = context->current_text;
907 while (p != context->current_text_end)
914 context->line_number += newlines;
918 G_MARKUP_ERROR_BAD_UTF8,
919 _("Invalid UTF-8 encoded text"));
923 while (context->iter != context->current_text_end)
925 switch (context->state)
928 /* Possible next state: AFTER_OPEN_ANGLE */
930 g_assert (context->tag_stack == NULL);
932 /* whitespace is ignored outside of any elements */
933 skip_spaces (context);
935 if (context->iter != context->current_text_end)
937 if (*context->iter == '<')
939 /* Move after the open angle */
940 advance_char (context);
942 context->state = STATE_AFTER_OPEN_ANGLE;
944 /* this could start a passthrough */
945 context->start = context->iter;
947 /* document is now non-empty */
948 context->document_empty = FALSE;
954 G_MARKUP_ERROR_PARSE,
955 _("Document must begin with an element (e.g. <book>)"));
960 case STATE_AFTER_OPEN_ANGLE:
961 /* Possible next states: INSIDE_OPEN_TAG_NAME,
962 * AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
964 if (*context->iter == '?' ||
965 *context->iter == '!')
967 /* include < in the passthrough */
968 const gchar *openangle = "<";
969 add_to_partial (context, openangle, openangle + 1);
970 context->start = context->iter;
971 context->balance = 1;
972 context->state = STATE_INSIDE_PASSTHROUGH;
974 else if (*context->iter == '/')
977 advance_char (context);
979 context->state = STATE_AFTER_CLOSE_TAG_SLASH;
981 else if (is_name_start_char (g_utf8_get_char (context->iter)))
983 context->state = STATE_INSIDE_OPEN_TAG_NAME;
985 /* start of tag name */
986 context->start = context->iter;
993 G_MARKUP_ERROR_PARSE,
994 _("'%s' is not a valid character following "
995 "a '<' character; it may not begin an "
997 utf8_str (context->iter, buf));
1001 /* The AFTER_CLOSE_ANGLE state is actually sort of
1002 * broken, because it doesn't correspond to a range
1003 * of characters in the input stream as the others do,
1004 * and thus makes things harder to conceptualize
1006 case STATE_AFTER_CLOSE_ANGLE:
1007 /* Possible next states: INSIDE_TEXT, STATE_START */
1008 if (context->tag_stack == NULL)
1010 context->start = NULL;
1011 context->state = STATE_START;
1015 context->start = context->iter;
1016 context->state = STATE_INSIDE_TEXT;
1020 case STATE_AFTER_ELISION_SLASH:
1021 /* Possible next state: AFTER_CLOSE_ANGLE */
1024 /* We need to pop the tag stack and call the end_element
1025 * function, since this is the close tag
1027 GError *tmp_error = NULL;
1029 g_assert (context->tag_stack != NULL);
1032 if (context->parser->end_element)
1033 (* context->parser->end_element) (context,
1034 context->tag_stack->data,
1040 mark_error (context, tmp_error);
1041 g_propagate_error (error, tmp_error);
1045 if (*context->iter == '>')
1047 /* move after the close angle */
1048 advance_char (context);
1049 context->state = STATE_AFTER_CLOSE_ANGLE;
1056 G_MARKUP_ERROR_PARSE,
1057 _("Odd character '%s', expected a '>' character "
1058 "to end the start tag of element '%s'"),
1059 utf8_str (context->iter, buf),
1060 current_element (context));
1064 g_free (context->tag_stack->data);
1065 context->tag_stack = g_slist_delete_link (context->tag_stack,
1066 context->tag_stack);
1070 case STATE_INSIDE_OPEN_TAG_NAME:
1071 /* Possible next states: BETWEEN_ATTRIBUTES */
1073 /* if there's a partial chunk then it's the first part of the
1074 * tag name. If there's a context->start then it's the start
1075 * of the tag name in current_text, the partial chunk goes
1076 * before that start though.
1078 advance_to_name_end (context);
1080 if (context->iter == context->current_text_end)
1082 /* The name hasn't necessarily ended. Merge with
1083 * partial chunk, leave state unchanged.
1085 add_to_partial (context, context->start, context->iter);
1089 /* The name has ended. Combine it with the partial chunk
1090 * if any; push it on the stack; enter next state.
1092 add_to_partial (context, context->start, context->iter);
1093 context->tag_stack =
1094 g_slist_prepend (context->tag_stack,
1095 g_string_free (context->partial_chunk,
1098 context->partial_chunk = NULL;
1100 context->state = STATE_BETWEEN_ATTRIBUTES;
1101 context->start = NULL;
1105 case STATE_INSIDE_ATTRIBUTE_NAME:
1106 /* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
1108 /* read the full name, if we enter the equals sign state
1109 * then add the attribute to the list (without the value),
1110 * otherwise store a partial chunk to be prepended later.
1112 advance_to_name_end (context);
1114 if (context->iter == context->current_text_end)
1116 /* The name hasn't necessarily ended. Merge with
1117 * partial chunk, leave state unchanged.
1119 add_to_partial (context, context->start, context->iter);
1123 /* The name has ended. Combine it with the partial chunk
1124 * if any; push it on the stack; enter next state.
1126 add_to_partial (context, context->start, context->iter);
1128 add_attribute (context, g_string_free (context->partial_chunk, FALSE));
1130 context->partial_chunk = NULL;
1131 context->start = NULL;
1133 if (*context->iter == '=')
1135 advance_char (context);
1136 context->state = STATE_AFTER_ATTRIBUTE_EQUALS_SIGN;
1143 G_MARKUP_ERROR_PARSE,
1144 _("Odd character '%s', expected a '=' after "
1145 "attribute name '%s' of element '%s'"),
1146 utf8_str (context->iter, buf),
1147 current_attribute (context),
1148 current_element (context));
1154 case STATE_BETWEEN_ATTRIBUTES:
1155 /* Possible next states: AFTER_CLOSE_ANGLE,
1156 * AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
1158 skip_spaces (context);
1160 if (context->iter != context->current_text_end)
1162 if (*context->iter == '/')
1164 advance_char (context);
1165 context->state = STATE_AFTER_ELISION_SLASH;
1167 else if (*context->iter == '>')
1170 advance_char (context);
1171 context->state = STATE_AFTER_CLOSE_ANGLE;
1173 else if (is_name_start_char (g_utf8_get_char (context->iter)))
1175 context->state = STATE_INSIDE_ATTRIBUTE_NAME;
1176 /* start of attribute name */
1177 context->start = context->iter;
1184 G_MARKUP_ERROR_PARSE,
1185 _("Odd character '%s', expected a '>' or '/' "
1186 "character to end the start tag of "
1187 "element '%s', or optionally an attribute; "
1188 "perhaps you used an invalid character in "
1189 "an attribute name"),
1190 utf8_str (context->iter, buf),
1191 current_element (context));
1194 /* If we're done with attributes, invoke
1195 * the start_element callback
1197 if (context->state == STATE_AFTER_ELISION_SLASH ||
1198 context->state == STATE_AFTER_CLOSE_ANGLE)
1200 const gchar *start_name;
1201 /* Ugly, but the current code expects an empty array instead of NULL */
1202 const gchar *empty = NULL;
1203 const gchar **attr_names = ∅
1204 const gchar **attr_values = ∅
1207 /* Call user callback for element start */
1208 start_name = current_element (context);
1210 if (context->cur_attr >= 0)
1212 attr_names = (const gchar**)context->attr_names;
1213 attr_values = (const gchar**)context->attr_values;
1217 if (context->parser->start_element)
1218 (* context->parser->start_element) (context,
1220 (const gchar **)attr_names,
1221 (const gchar **)attr_values,
1225 /* Go ahead and free the attributes. */
1226 for (; context->cur_attr >= 0; context->cur_attr--)
1228 int pos = context->cur_attr;
1229 g_free (context->attr_names[pos]);
1230 g_free (context->attr_values[pos]);
1231 context->attr_names[pos] = context->attr_values[pos] = NULL;
1233 g_assert (context->cur_attr == -1);
1234 g_assert (context->attr_names == NULL ||
1235 context->attr_names[0] == NULL);
1236 g_assert (context->attr_values == NULL ||
1237 context->attr_values[0] == NULL);
1239 if (tmp_error != NULL)
1241 mark_error (context, tmp_error);
1242 g_propagate_error (error, tmp_error);
1248 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1249 /* Possible next state: INSIDE_ATTRIBUTE_VALUE_[SQ/DQ] */
1250 if (*context->iter == '"')
1252 advance_char (context);
1253 context->state = STATE_INSIDE_ATTRIBUTE_VALUE_DQ;
1254 context->start = context->iter;
1256 else if (*context->iter == '\'')
1258 advance_char (context);
1259 context->state = STATE_INSIDE_ATTRIBUTE_VALUE_SQ;
1260 context->start = context->iter;
1267 G_MARKUP_ERROR_PARSE,
1268 _("Odd character '%s', expected an open quote mark "
1269 "after the equals sign when giving value for "
1270 "attribute '%s' of element '%s'"),
1271 utf8_str (context->iter, buf),
1272 current_attribute (context),
1273 current_element (context));
1277 case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
1278 case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
1279 /* Possible next states: BETWEEN_ATTRIBUTES */
1283 if (context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ)
1294 if (*context->iter == delim)
1297 while (advance_char (context));
1299 if (context->iter == context->current_text_end)
1301 /* The value hasn't necessarily ended. Merge with
1302 * partial chunk, leave state unchanged.
1304 add_to_partial (context, context->start, context->iter);
1308 /* The value has ended at the quote mark. Combine it
1309 * with the partial chunk if any; set it for the current
1312 add_to_partial (context, context->start, context->iter);
1314 g_assert (context->cur_attr >= 0);
1316 if (unescape_text (context,
1317 context->partial_chunk->str,
1318 context->partial_chunk->str +
1319 context->partial_chunk->len,
1320 &context->attr_values[context->cur_attr],
1323 /* success, advance past quote and set state. */
1324 advance_char (context);
1325 context->state = STATE_BETWEEN_ATTRIBUTES;
1326 context->start = NULL;
1329 truncate_partial (context);
1333 case STATE_INSIDE_TEXT:
1334 /* Possible next states: AFTER_OPEN_ANGLE */
1337 if (*context->iter == '<')
1340 while (advance_char (context));
1342 /* The text hasn't necessarily ended. Merge with
1343 * partial chunk, leave state unchanged.
1346 add_to_partial (context, context->start, context->iter);
1348 if (context->iter != context->current_text_end)
1350 gchar *unescaped = NULL;
1352 /* The text has ended at the open angle. Call the text
1356 if (unescape_text (context,
1357 context->partial_chunk->str,
1358 context->partial_chunk->str +
1359 context->partial_chunk->len,
1363 GError *tmp_error = NULL;
1365 if (context->parser->text)
1366 (*context->parser->text) (context,
1374 if (tmp_error == NULL)
1376 /* advance past open angle and set state. */
1377 advance_char (context);
1378 context->state = STATE_AFTER_OPEN_ANGLE;
1379 /* could begin a passthrough */
1380 context->start = context->iter;
1384 mark_error (context, tmp_error);
1385 g_propagate_error (error, tmp_error);
1389 truncate_partial (context);
1393 case STATE_AFTER_CLOSE_TAG_SLASH:
1394 /* Possible next state: INSIDE_CLOSE_TAG_NAME */
1395 if (is_name_start_char (g_utf8_get_char (context->iter)))
1397 context->state = STATE_INSIDE_CLOSE_TAG_NAME;
1399 /* start of tag name */
1400 context->start = context->iter;
1407 G_MARKUP_ERROR_PARSE,
1408 _("'%s' is not a valid character following "
1409 "the characters '</'; '%s' may not begin an "
1411 utf8_str (context->iter, buf),
1412 utf8_str (context->iter, buf));
1416 case STATE_INSIDE_CLOSE_TAG_NAME:
1417 /* Possible next state: AFTER_CLOSE_ANGLE */
1418 advance_to_name_end (context);
1420 if (context->iter == context->current_text_end)
1422 /* The name hasn't necessarily ended. Merge with
1423 * partial chunk, leave state unchanged.
1425 add_to_partial (context, context->start, context->iter);
1429 /* The name has ended. Combine it with the partial chunk
1430 * if any; check that it matches stack top and pop
1431 * stack; invoke proper callback; enter next state.
1435 add_to_partial (context, context->start, context->iter);
1437 close_name = g_string_free (context->partial_chunk, FALSE);
1438 context->partial_chunk = NULL;
1440 if (*context->iter != '>')
1445 G_MARKUP_ERROR_PARSE,
1446 _("'%s' is not a valid character following "
1447 "the close element name '%s'; the allowed "
1448 "character is '>'"),
1449 utf8_str (context->iter, buf),
1452 else if (context->tag_stack == NULL)
1456 G_MARKUP_ERROR_PARSE,
1457 _("Element '%s' was closed, no element "
1458 "is currently open"),
1461 else if (strcmp (close_name, current_element (context)) != 0)
1465 G_MARKUP_ERROR_PARSE,
1466 _("Element '%s' was closed, but the currently "
1467 "open element is '%s'"),
1469 current_element (context));
1474 advance_char (context);
1475 context->state = STATE_AFTER_CLOSE_ANGLE;
1476 context->start = NULL;
1478 /* call the end_element callback */
1480 if (context->parser->end_element)
1481 (* context->parser->end_element) (context,
1487 /* Pop the tag stack */
1488 g_free (context->tag_stack->data);
1489 context->tag_stack = g_slist_delete_link (context->tag_stack,
1490 context->tag_stack);
1494 mark_error (context, tmp_error);
1495 g_propagate_error (error, tmp_error);
1499 g_free (close_name);
1503 case STATE_INSIDE_PASSTHROUGH:
1504 /* Possible next state: AFTER_CLOSE_ANGLE */
1507 if (*context->iter == '<')
1509 if (*context->iter == '>')
1512 add_to_partial (context, context->start, context->iter);
1513 context->start = context->iter;
1514 if ((g_str_has_prefix (context->partial_chunk->str, "<?")
1515 && g_str_has_suffix (context->partial_chunk->str, "?")) ||
1516 (g_str_has_prefix (context->partial_chunk->str, "<!--")
1517 && g_str_has_suffix (context->partial_chunk->str, "--")) ||
1518 (g_str_has_prefix (context->partial_chunk->str, "<![CDATA[")
1519 && g_str_has_suffix (context->partial_chunk->str, "]]")) ||
1520 (g_str_has_prefix (context->partial_chunk->str, "<!DOCTYPE")
1521 && context->balance == 0))
1525 while (advance_char (context));
1527 if (context->iter == context->current_text_end)
1529 /* The passthrough hasn't necessarily ended. Merge with
1530 * partial chunk, leave state unchanged.
1532 add_to_partial (context, context->start, context->iter);
1536 /* The passthrough has ended at the close angle. Combine
1537 * it with the partial chunk if any. Call the passthrough
1538 * callback. Note that the open/close angles are
1539 * included in the text of the passthrough.
1541 GError *tmp_error = NULL;
1543 advance_char (context); /* advance past close angle */
1544 add_to_partial (context, context->start, context->iter);
1546 if (context->parser->passthrough)
1547 (*context->parser->passthrough) (context,
1548 context->partial_chunk->str,
1549 context->partial_chunk->len,
1553 truncate_partial (context);
1555 if (tmp_error == NULL)
1557 context->state = STATE_AFTER_CLOSE_ANGLE;
1558 context->start = context->iter; /* could begin text */
1562 mark_error (context, tmp_error);
1563 g_propagate_error (error, tmp_error);
1573 g_assert_not_reached ();
1579 context->parsing = FALSE;
1581 return context->state != STATE_ERROR;
1585 * g_markup_parse_context_end_parse:
1586 * @context: a #GMarkupParseContext
1587 * @error: return location for a #GError
1589 * Signals to the #GMarkupParseContext that all data has been
1590 * fed into the parse context with g_markup_parse_context_parse().
1591 * This function reports an error if the document isn't complete,
1592 * for example if elements are still open.
1594 * Return value: %TRUE on success, %FALSE if an error was set
1597 g_markup_parse_context_end_parse (GMarkupParseContext *context,
1600 g_return_val_if_fail (context != NULL, FALSE);
1601 g_return_val_if_fail (!context->parsing, FALSE);
1602 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
1604 if (context->partial_chunk != NULL)
1606 g_string_free (context->partial_chunk, TRUE);
1607 context->partial_chunk = NULL;
1610 if (context->document_empty)
1612 set_error (context, error, G_MARKUP_ERROR_EMPTY,
1613 _("Document was empty or contained only whitespace"));
1617 context->parsing = TRUE;
1619 switch (context->state)
1625 case STATE_AFTER_OPEN_ANGLE:
1626 set_error (context, error, G_MARKUP_ERROR_PARSE,
1627 _("Document ended unexpectedly just after an open angle bracket '<'"));
1630 case STATE_AFTER_CLOSE_ANGLE:
1631 if (context->tag_stack != NULL)
1633 /* Error message the same as for INSIDE_TEXT */
1634 set_error (context, error, G_MARKUP_ERROR_PARSE,
1635 _("Document ended unexpectedly with elements still open - "
1636 "'%s' was the last element opened"),
1637 current_element (context));
1641 case STATE_AFTER_ELISION_SLASH:
1642 set_error (context, error, G_MARKUP_ERROR_PARSE,
1643 _("Document ended unexpectedly, expected to see a close angle "
1644 "bracket ending the tag <%s/>"), current_element (context));
1647 case STATE_INSIDE_OPEN_TAG_NAME:
1648 set_error (context, error, G_MARKUP_ERROR_PARSE,
1649 _("Document ended unexpectedly inside an element name"));
1652 case STATE_INSIDE_ATTRIBUTE_NAME:
1653 set_error (context, error, G_MARKUP_ERROR_PARSE,
1654 _("Document ended unexpectedly inside an attribute name"));
1657 case STATE_BETWEEN_ATTRIBUTES:
1658 set_error (context, error, G_MARKUP_ERROR_PARSE,
1659 _("Document ended unexpectedly inside an element-opening "
1663 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1664 set_error (context, error, G_MARKUP_ERROR_PARSE,
1665 _("Document ended unexpectedly after the equals sign "
1666 "following an attribute name; no attribute value"));
1669 case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
1670 case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
1671 set_error (context, error, G_MARKUP_ERROR_PARSE,
1672 _("Document ended unexpectedly while inside an attribute "
1676 case STATE_INSIDE_TEXT:
1677 g_assert (context->tag_stack != NULL);
1678 set_error (context, error, G_MARKUP_ERROR_PARSE,
1679 _("Document ended unexpectedly with elements still open - "
1680 "'%s' was the last element opened"),
1681 current_element (context));
1684 case STATE_AFTER_CLOSE_TAG_SLASH:
1685 case STATE_INSIDE_CLOSE_TAG_NAME:
1686 set_error (context, error, G_MARKUP_ERROR_PARSE,
1687 _("Document ended unexpectedly inside the close tag for "
1688 "element '%s'"), current_element);
1691 case STATE_INSIDE_PASSTHROUGH:
1692 set_error (context, error, G_MARKUP_ERROR_PARSE,
1693 _("Document ended unexpectedly inside a comment or "
1694 "processing instruction"));
1699 g_assert_not_reached ();
1703 context->parsing = FALSE;
1705 return context->state != STATE_ERROR;
1709 * g_markup_parse_context_get_element:
1710 * @context: a #GMarkupParseContext
1711 * @returns: the name of the currently open element, or %NULL
1713 * Retrieves the name of the currently open element.
1717 G_CONST_RETURN gchar *
1718 g_markup_parse_context_get_element (GMarkupParseContext *context)
1720 g_return_val_if_fail (context != NULL, NULL);
1722 if (context->tag_stack == NULL)
1725 return current_element (context);
1729 * g_markup_parse_context_get_position:
1730 * @context: a #GMarkupParseContext
1731 * @line_number: return location for a line number, or %NULL
1732 * @char_number: return location for a char-on-line number, or %NULL
1734 * Retrieves the current line number and the number of the character on
1735 * that line. Intended for use in error messages; there are no strict
1736 * semantics for what constitutes the "current" line number other than
1737 * "the best number we could come up with for error messages."
1741 g_markup_parse_context_get_position (GMarkupParseContext *context,
1745 g_return_if_fail (context != NULL);
1748 *line_number = context->line_number;
1751 *char_number = context->char_number;
1755 append_escaped_text (GString *str,
1763 end = text + length;
1768 next = g_utf8_next_char (p);
1773 g_string_append (str, "&");
1777 g_string_append (str, "<");
1781 g_string_append (str, ">");
1785 g_string_append (str, "'");
1789 g_string_append (str, """);
1793 g_string_append_len (str, p, next - p);
1802 * g_markup_escape_text:
1803 * @text: some valid UTF-8 text
1804 * @length: length of @text in bytes
1806 * Escapes text so that the markup parser will parse it verbatim.
1807 * Less than, greater than, ampersand, etc. are replaced with the
1808 * corresponding entities. This function would typically be used
1809 * when writing out a file to be parsed with the markup parser.
1811 * Return value: escaped text
1814 g_markup_escape_text (const gchar *text,
1819 g_return_val_if_fail (text != NULL, NULL);
1822 length = strlen (text);
1824 str = g_string_new ("");
1825 append_escaped_text (str, text, length);
1827 return g_string_free (str, FALSE);