(root)/
glib-2.79.0/
glib/
tests/
markup-example.c
       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  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    g_print ("element: <%s>\n", element_name);
      69  
      70    for (gsize i = 0; attribute_names[i]; i++)
      71      {
      72        g_print ("attribute: %s = \"%s\"\n", attribute_names[i],
      73                                             attribute_values[i]);
      74      }
      75  }
      76  
      77  /*
      78   * Called for closing tags like </foo>
      79   */
      80  static void
      81  foo_parser_end_element (GMarkupParseContext *context,
      82                          const gchar         *element_name,
      83                          gpointer             user_data,
      84                          GError             **error)
      85  {
      86    g_print ("element: </%s>\n", element_name);
      87  }
      88  
      89  /*
      90   * Called for character data. Text is not nul-terminated
      91   */
      92  static void
      93  foo_parser_characters (GMarkupParseContext *context,
      94                         const gchar         *text,
      95                         gsize                text_len,
      96                         gpointer             user_data,
      97                         GError             **error)
      98  {
      99    g_print ("text: [%s]\n", text);
     100  }
     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  foo_parser_passthrough (GMarkupParseContext  *context,
     110                          const gchar          *passthrough_text,
     111                          gsize                 text_len,
     112                          gpointer              user_data,
     113                          GError              **error)
     114  {
     115    g_print ("passthrough: %s\n", passthrough_text);
     116  }
     117  
     118  /*
     119   * Called when any parsing method encounters an error. The GError should not be
     120   * freed.
     121   */
     122  static void
     123  foo_parser_error (GMarkupParseContext *context,
     124                    GError              *error,
     125                    gpointer             user_data)
     126  {
     127    g_printerr ("ERROR: %s\n", error->message);
     128  }
     129  
     130  int
     131  main (void)
     132  {
     133    GMarkupParseContext *context;
     134    gboolean success = FALSE;
     135    glong len;
     136  
     137    /*
     138     * Example XML for the parser.
     139     */
     140    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    len = g_utf8_strlen (foo_xml_example, -1);
     149    g_print ("Parsing: %s\n", foo_xml_example);
     150    g_print ("(%ld UTF-8 characters)\n", len);
     151  
     152    context = g_markup_parse_context_new (&foo_xml_parser, G_MARKUP_DEFAULT_FLAGS, NULL, NULL);
     153  
     154    success = g_markup_parse_context_parse (context, foo_xml_example, len, NULL);
     155  
     156    g_markup_parse_context_free (context);
     157  
     158    if (success)
     159      {
     160        g_print ("DONE\n");
     161        return 0;
     162      }
     163    else
     164      {
     165        g_printerr ("ERROR\n");
     166        return 1;
     167      }
     168  }