2001-03-04 05:09:23 -06:00
|
|
|
/********************************************************************
|
|
|
|
* sixtp-to-dom-parser.c *
|
|
|
|
* Copyright 2001 Gnumatic, Inc. *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
2005-11-16 23:35:02 -06:00
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
2001-03-04 05:09:23 -06:00
|
|
|
* *
|
|
|
|
********************************************************************/
|
2015-11-29 19:11:29 -06:00
|
|
|
extern "C"
|
|
|
|
{
|
2017-10-26 04:14:21 -05:00
|
|
|
#include <config.h>
|
2001-09-07 16:51:13 -05:00
|
|
|
|
2001-02-07 22:09:15 -06:00
|
|
|
#include <glib.h>
|
|
|
|
|
2001-02-08 01:52:17 -06:00
|
|
|
#include <ctype.h>
|
2015-11-29 19:11:29 -06:00
|
|
|
}
|
2001-02-08 01:52:17 -06:00
|
|
|
|
2001-02-07 22:09:15 -06:00
|
|
|
#include "sixtp-parsers.h"
|
|
|
|
#include "sixtp-utils.h"
|
|
|
|
#include "sixtp.h"
|
|
|
|
|
2001-02-22 01:29:05 -06:00
|
|
|
static xmlNsPtr global_namespace = NULL;
|
2001-02-07 22:09:15 -06:00
|
|
|
|
2001-02-22 01:29:05 -06:00
|
|
|
/* Don't pass anything in the data_for_children value to this
|
|
|
|
function. It'll cause a segfault */
|
2016-03-12 16:04:40 -06:00
|
|
|
static gboolean dom_start_handler (
|
2001-02-07 22:09:15 -06:00
|
|
|
GSList* sibling_data, gpointer parent_data, gpointer global_data,
|
2016-03-12 16:04:40 -06:00
|
|
|
gpointer* data_for_children, gpointer* result, const gchar* tag,
|
|
|
|
gchar** attrs)
|
2001-02-07 22:09:15 -06:00
|
|
|
{
|
|
|
|
xmlNodePtr thing;
|
|
|
|
gchar** atptr = attrs;
|
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
if (parent_data == NULL)
|
2001-02-07 22:09:15 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
thing = xmlNewNode (global_namespace, BAD_CAST tag);
|
2001-02-24 18:42:44 -06:00
|
|
|
/* only publish the result if we're the parent */
|
|
|
|
*result = thing;
|
2001-02-07 22:09:15 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
thing = xmlNewChild ((xmlNodePtr) parent_data,
|
|
|
|
global_namespace,
|
|
|
|
BAD_CAST tag,
|
|
|
|
NULL);
|
2001-02-24 18:42:44 -06:00
|
|
|
*result = NULL;
|
2001-02-07 22:09:15 -06:00
|
|
|
}
|
2001-02-24 18:42:44 -06:00
|
|
|
*data_for_children = thing;
|
2001-02-08 01:52:17 -06:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
if (attrs != NULL)
|
2001-02-07 22:09:15 -06:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
while (*atptr != 0)
|
2001-02-08 01:52:17 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
gchar* attr0 = g_strdup (atptr[0]);
|
|
|
|
gchar* attr1 = g_strdup (atptr[1]);
|
|
|
|
xmlSetProp (thing, checked_char_cast (attr0),
|
|
|
|
checked_char_cast (attr1));
|
|
|
|
g_free (attr0);
|
|
|
|
g_free (attr1);
|
2001-02-08 01:52:17 -06:00
|
|
|
atptr += 2;
|
|
|
|
}
|
2001-02-07 22:09:15 -06:00
|
|
|
}
|
2001-02-24 18:42:44 -06:00
|
|
|
return TRUE;
|
|
|
|
}
|
2001-02-07 22:09:15 -06:00
|
|
|
|
2001-02-24 18:42:44 -06:00
|
|
|
static void
|
2016-03-12 16:04:40 -06:00
|
|
|
dom_fail_handler (gpointer data_for_children,
|
|
|
|
GSList* data_from_children,
|
|
|
|
GSList* sibling_data,
|
|
|
|
gpointer parent_data,
|
|
|
|
gpointer global_data,
|
|
|
|
gpointer* result,
|
|
|
|
const gchar* tag)
|
2001-02-24 18:42:44 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
if (*result) xmlFreeNode (static_cast<xmlNodePtr> (*result));
|
2001-02-24 18:42:44 -06:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
static gboolean dom_chars_handler (
|
|
|
|
GSList* sibling_data, gpointer parent_data, gpointer global_data,
|
|
|
|
gpointer* result, const char* text, int length)
|
2001-02-07 22:09:15 -06:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
if (length > 0)
|
2001-02-08 01:52:17 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
gchar* newtext = g_strdup (text);
|
|
|
|
xmlNodeAddContentLen ((xmlNodePtr)parent_data,
|
|
|
|
checked_char_cast (newtext), length);
|
|
|
|
g_free (newtext);
|
2001-02-08 01:52:17 -06:00
|
|
|
}
|
2001-02-07 22:09:15 -06:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
sixtp*
|
|
|
|
sixtp_dom_parser_new (sixtp_end_handler ender,
|
|
|
|
sixtp_result_handler cleanup_result_by_default_func,
|
|
|
|
sixtp_result_handler cleanup_result_on_fail_func)
|
2001-02-07 22:09:15 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
sixtp* top_level;
|
2001-02-07 22:09:15 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_val_if_fail (ender, NULL);
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (! (top_level =
|
|
|
|
sixtp_set_any (sixtp_new (), FALSE,
|
2009-12-29 14:12:48 -06:00
|
|
|
SIXTP_START_HANDLER_ID, dom_start_handler,
|
|
|
|
SIXTP_CHARACTERS_HANDLER_ID, dom_chars_handler,
|
|
|
|
SIXTP_END_HANDLER_ID, ender,
|
|
|
|
SIXTP_FAIL_HANDLER_ID, dom_fail_handler,
|
|
|
|
SIXTP_NO_MORE_HANDLERS)))
|
2001-02-07 22:09:15 -06:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
if (cleanup_result_by_default_func)
|
2001-02-24 18:42:44 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
sixtp_set_cleanup_result (top_level, cleanup_result_by_default_func);
|
2001-02-24 18:42:44 -06:00
|
|
|
}
|
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
if (cleanup_result_by_default_func)
|
2001-02-24 18:42:44 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
sixtp_set_result_fail (top_level, cleanup_result_on_fail_func);
|
2001-02-24 18:42:44 -06:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (!sixtp_add_sub_parser (top_level, SIXTP_MAGIC_CATCHER, top_level))
|
2001-02-07 22:09:15 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
sixtp_destroy (top_level);
|
2001-02-07 22:09:15 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return top_level;
|
|
|
|
}
|