rsvg_start_xinclude (RsvgHandle * ctx, RsvgPropertyBag * atts)
{
    RsvgSaxHandlerXinclude *handler;
    GByteArray *data;
    const char *href;
    gboolean success = FALSE;

    href = rsvg_property_bag_lookup (atts, "href");
    if (href) {
        data = _rsvg_acquire_xlink_href_resource (href, rsvg_handle_get_base_uri (ctx), NULL);
        if (data) {
            const char *parse;

            parse = rsvg_property_bag_lookup (atts, "parse");
            if (parse && !strcmp (parse, "text")) {
                const char *encoding;
                char *text_data;
                gsize text_data_len;
                gboolean text_data_needs_free = FALSE;

                encoding = rsvg_property_bag_lookup (atts, "encoding");
                if (encoding) {
                    text_data =
                        g_convert ((const char *) data->data, data->len, "utf-8", encoding, NULL,
                                   &text_data_len, NULL);
                    text_data_needs_free = TRUE;
                } else {
                    text_data = (char *) data->data;
                    text_data_len = data->len;
                }

                rsvg_characters_impl (ctx, (const xmlChar *) text_data, text_data_len);

                if (text_data_needs_free)
                    g_free (text_data);
            } else {
                /* xml */
                xmlDocPtr xml_doc;
                xmlParserCtxtPtr xml_parser;
                int result;

                xml_parser = xmlCreatePushParserCtxt (&rsvgSAXHandlerStruct, ctx, NULL, 0, NULL);
                result = xmlParseChunk (xml_parser, (char *) data->data, data->len, 0);
                result = xmlParseChunk (xml_parser, "", 0, TRUE);

                xml_doc = xml_parser->myDoc;
                xmlFreeParserCtxt (xml_parser);
                xmlFreeDoc (xml_doc);
            }

            g_byte_array_free (data, TRUE);
            success = TRUE;
        }
    }

    /* needed to handle xi:fallback */
    handler = g_new0 (RsvgSaxHandlerXinclude, 1);

    handler->super.free = rsvg_xinclude_handler_free;
    handler->super.characters = rsvg_xinclude_handler_characters;
    handler->super.start_element = rsvg_xinclude_handler_start;
    handler->super.end_element = rsvg_xinclude_handler_end;
    handler->prev_handler = ctx->priv->handler;
    handler->ctx = ctx;
    handler->success = success;

    ctx->priv->handler = &handler->super;
}
rsvg_start_xinclude (RsvgHandle * ctx, RsvgPropertyBag * atts)
{
    RsvgSaxHandlerXinclude *handler;
    GByteArray *data;
    const char *href;
    gboolean success = FALSE;

    href = rsvg_property_bag_lookup (atts, "href");
    if (href) {
        data = _rsvg_acquire_xlink_href_resource (href, rsvg_handle_get_base_uri (ctx), NULL);
        if (data) {
            const char *parse;

            parse = rsvg_property_bag_lookup (atts, "parse");
            if (parse && !strcmp (parse, "text")) {
                const char *encoding;
                char *text_data;
                gsize text_data_len;
                gboolean text_data_needs_free = FALSE;

                encoding = rsvg_property_bag_lookup (atts, "encoding");
                if (encoding) {
                    text_data =
                        g_convert ((const char *) data->data, data->len, "utf-8", encoding, NULL,
                                   &text_data_len, NULL);
                    text_data_needs_free = TRUE;
                } else {
                    text_data = (char *) data->data;
                    text_data_len = data->len;
                }

                rsvg_characters_impl (ctx, (const xmlChar *) text_data, text_data_len);

                if (text_data_needs_free)
                    g_free (text_data);
            } else {
                 
                xmlDocPtr xml_doc;
                xmlParserCtxtPtr xml_parser;
                int result;

                xml_parser = xmlCreatePushParserCtxt (&rsvgSAXHandlerStruct, ctx, NULL, 0, NULL);
                result = xmlParseChunk (xml_parser, (char *) data->data, data->len, 0);
                result = xmlParseChunk (xml_parser, "", 0, TRUE);

                xml_doc = xml_parser->myDoc;
                xmlFreeParserCtxt (xml_parser);
                xmlFreeDoc (xml_doc);
            }

            g_byte_array_free (data, TRUE);
            success = TRUE;
        }
    }

     
    handler = g_new0 (RsvgSaxHandlerXinclude, 1);

    handler->super.free = rsvg_xinclude_handler_free;
    handler->super.characters = rsvg_xinclude_handler_characters;
    handler->super.start_element = rsvg_xinclude_handler_start;
    handler->super.end_element = rsvg_xinclude_handler_end;
    handler->prev_handler = ctx->priv->handler;
    handler->ctx = ctx;
    handler->success = success;

    ctx->priv->handler = &handler->super;
}