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 *
|
|
|
|
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02111-1307, USA gnu@gnu.org *
|
|
|
|
* *
|
|
|
|
********************************************************************/
|
|
|
|
|
2001-09-07 16:51:13 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-02-07 22:09:15 -06:00
|
|
|
#include <glib.h>
|
|
|
|
|
2001-02-08 01:52:17 -06:00
|
|
|
#include <ctype.h>
|
|
|
|
|
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 */
|
2001-02-07 22:09:15 -06:00
|
|
|
static gboolean dom_start_handler(
|
|
|
|
GSList* sibling_data, gpointer parent_data, gpointer global_data,
|
|
|
|
gpointer *data_for_children, gpointer *result, const gchar *tag,
|
|
|
|
gchar **attrs)
|
|
|
|
{
|
|
|
|
xmlNodePtr thing;
|
|
|
|
gchar** atptr = attrs;
|
|
|
|
|
|
|
|
if(parent_data == NULL)
|
|
|
|
{
|
2005-11-01 21:32:36 -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
|
|
|
|
{
|
2001-02-24 18:42:44 -06:00
|
|
|
thing = xmlNewChild((xmlNodePtr) parent_data,
|
|
|
|
global_namespace,
|
2005-11-01 21:32:36 -06:00
|
|
|
BAD_CAST tag,
|
2001-02-24 18:42:44 -06:00
|
|
|
NULL);
|
|
|
|
*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
|
|
|
|
|
|
|
if(attrs != NULL)
|
2001-02-07 22:09:15 -06:00
|
|
|
{
|
2001-02-08 01:52:17 -06:00
|
|
|
while(*atptr != 0)
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
xmlSetProp(thing, BAD_CAST atptr[0], BAD_CAST atptr[1]);
|
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
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if(*result) xmlFreeNode(*result);
|
|
|
|
}
|
|
|
|
|
2001-02-07 22:09:15 -06:00
|
|
|
static gboolean dom_chars_handler(
|
|
|
|
GSList *sibling_data, gpointer parent_data, gpointer global_data,
|
|
|
|
gpointer *result, const char *text, int length)
|
|
|
|
{
|
2001-10-11 06:35:50 -05:00
|
|
|
if(length > 0)
|
2001-02-08 01:52:17 -06:00
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
xmlNodeAddContentLen((xmlNodePtr)parent_data, BAD_CAST text, length);
|
2001-02-08 01:52:17 -06:00
|
|
|
}
|
2001-02-07 22:09:15 -06:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-02-24 18:42:44 -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
|
|
|
{
|
|
|
|
sixtp *top_level;
|
|
|
|
|
|
|
|
g_return_val_if_fail(ender, NULL);
|
|
|
|
|
|
|
|
if(!(top_level =
|
|
|
|
sixtp_set_any(sixtp_new(), FALSE,
|
|
|
|
SIXTP_START_HANDLER_ID, dom_start_handler,
|
|
|
|
SIXTP_CHARACTERS_HANDLER_ID, dom_chars_handler,
|
|
|
|
SIXTP_END_HANDLER_ID, ender,
|
2001-02-24 18:42:44 -06:00
|
|
|
SIXTP_FAIL_HANDLER_ID, dom_fail_handler,
|
2001-02-07 22:09:15 -06:00
|
|
|
SIXTP_NO_MORE_HANDLERS)))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-02-24 18:42:44 -06:00
|
|
|
if(cleanup_result_by_default_func)
|
|
|
|
{
|
|
|
|
sixtp_set_cleanup_result(top_level, cleanup_result_by_default_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cleanup_result_by_default_func)
|
|
|
|
{
|
|
|
|
sixtp_set_result_fail(top_level, cleanup_result_on_fail_func);
|
|
|
|
}
|
|
|
|
|
2001-02-07 22:09:15 -06:00
|
|
|
if(!sixtp_add_sub_parser(top_level, SIXTP_MAGIC_CATCHER, top_level))
|
|
|
|
{
|
|
|
|
sixtp_destroy(top_level);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return top_level;
|
|
|
|
}
|