/* XHtmlDocument.c generated by valac 0.56.19, the Vala compiler
 * generated from XHtmlDocument.vala, do not modify */

/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/* xNode.vala
 *
 * Copyright (C) 2015,2016  Yannick Inizan <inizan.yannick@gmail.com>
 * Copyright (C) 2015,2016  Daniel Espinosa <esodan@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library 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
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *      Yannick Inizan <inizan.yannick@gmail.com>
 *      Daniel Espinosa <esodan@gmail.com>
 */
/**
 * Search all {@link GXml.Element} with a property called "class" and with a
 * value as a class apply to a node.
 */

#include "gxml/gxml.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <libxml/HTMLparser.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <libxml/HTMLtree.h>
#include <glib/gi18n-lib.h>
#include "gxml.h"

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	GXML_XHTML_DOCUMENT_0_PROPERTY,
	GXML_XHTML_DOCUMENT_NUM_PROPERTIES
};
static GParamSpec* gxml_xhtml_document_properties[GXML_XHTML_DOCUMENT_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _htmlFreeParserCtxt0(var) ((var == NULL) ? NULL : (var = (htmlFreeParserCtxt (var), NULL)))
#define _xmlFreeDoc0(var) ((var == NULL) ? NULL : (var = (xmlFreeDoc (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

static gpointer gxml_xhtml_document_parent_class = NULL;
static GXmlDomHtmlDocumentIface * gxml_xhtml_document_gxml_dom_html_document_parent_iface = NULL;

static void gxml_xhtml_document_real_read_from_string (GXmlDomHtmlDocument* base,
                                                const gchar* str,
                                                GError** error);
static void gxml_xhtml_document_real_read_from_string_tolerant (GXmlDomHtmlDocument* base,
                                                         const gchar* str,
                                                         GError** error);
static gchar* gxml_xhtml_document_real_to_html (GXmlDomHtmlDocument* base,
                                         GError** error);
static gchar* gxml_xhtml_document_real_to_string (GXmlXNode* base);
static GType gxml_xhtml_document_get_type_once (void);
static void _vala_gxml_xhtml_document_get_property (GObject * object,
                                             guint property_id,
                                             GValue * value,
                                             GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

gint
gxml_xhtml_document_get_default_options (void)
{
	gint result;
	result = (gint) (((HTML_PARSE_NONET | HTML_PARSE_NOWARNING) | HTML_PARSE_NOERROR) | HTML_PARSE_NOBLANKS);
	return result;
}

GXmlXHtmlDocument*
gxml_xhtml_document_construct_from_path (GType object_type,
                                         const gchar* path,
                                         gint options,
                                         GError** error)
{
	GXmlXHtmlDocument * self = NULL;
	GFile* _tmp0_;
	GFile* _tmp1_;
	GError* _inner_error0_ = NULL;
	g_return_val_if_fail (path != NULL, NULL);
	_tmp0_ = g_file_new_for_path (path);
	_tmp1_ = _tmp0_;
	self = (GXmlXHtmlDocument*) gxml_xhtml_document_construct_from_file (object_type, _tmp1_, options, NULL, &_inner_error0_);
	_g_object_unref0 (_tmp1_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (self);
		return NULL;
	}
	return self;
}

GXmlXHtmlDocument*
gxml_xhtml_document_new_from_path (const gchar* path,
                                   gint options,
                                   GError** error)
{
	return gxml_xhtml_document_construct_from_path (GXML_TYPE_XHTML_DOCUMENT, path, options, error);
}

GXmlXHtmlDocument*
gxml_xhtml_document_construct_from_uri (GType object_type,
                                        const gchar* uri,
                                        gint options,
                                        GError** error)
{
	GXmlXHtmlDocument * self = NULL;
	GFile* _tmp0_;
	GFile* _tmp1_;
	GError* _inner_error0_ = NULL;
	g_return_val_if_fail (uri != NULL, NULL);
	_tmp0_ = g_file_new_for_uri (uri);
	_tmp1_ = _tmp0_;
	self = (GXmlXHtmlDocument*) gxml_xhtml_document_construct_from_file (object_type, _tmp1_, options, NULL, &_inner_error0_);
	_g_object_unref0 (_tmp1_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (self);
		return NULL;
	}
	return self;
}

GXmlXHtmlDocument*
gxml_xhtml_document_new_from_uri (const gchar* uri,
                                  gint options,
                                  GError** error)
{
	return gxml_xhtml_document_construct_from_uri (GXML_TYPE_XHTML_DOCUMENT, uri, options, error);
}

/**
 * This method parse strings in a {@link GLib.File} using {@link Html.Doc.read_memory} method.
 * Refer to libxml2 documentation about limitations on parsing.
 *
 * In order to use a different parser, may you want to load in memory your file,
 * then create a new {@link XHtmlDocument} using a constructor better fitting
 * your document content or source.
 */
GXmlXHtmlDocument*
gxml_xhtml_document_construct_from_file (GType object_type,
                                         GFile* file,
                                         gint options,
                                         GCancellable* cancel,
                                         GError** error)
{
	GXmlXHtmlDocument * self = NULL;
	GFileInputStream* _tmp0_ = NULL;
	GFileInputStream* _tmp1_;
	GDataInputStream* istream = NULL;
	GDataInputStream* _tmp2_;
	gchar* text = NULL;
	GDataInputStream* _tmp3_;
	gchar* _tmp4_;
	const gchar* _tmp5_;
	GError* _inner_error0_ = NULL;
	g_return_val_if_fail (file != NULL, NULL);
	_tmp1_ = g_file_read (file, NULL, &_inner_error0_);
	_tmp0_ = _tmp1_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (self);
		return NULL;
	}
	_tmp2_ = g_data_input_stream_new ((GInputStream*) _tmp0_);
	istream = _tmp2_;
	_tmp3_ = istream;
	_tmp4_ = g_data_input_stream_read_upto (_tmp3_, "\0", (gssize) -1, NULL, NULL, &_inner_error0_);
	text = _tmp4_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (istream);
		_g_object_unref0 (_tmp0_);
		_g_object_unref0 (self);
		return NULL;
	}
	_tmp5_ = text;
	self = (GXmlXHtmlDocument*) gxml_xhtml_document_construct_from_string (object_type, _tmp5_, options);
	_g_free0 (text);
	_g_object_unref0 (istream);
	_g_object_unref0 (_tmp0_);
	return self;
}

GXmlXHtmlDocument*
gxml_xhtml_document_new_from_file (GFile* file,
                                   gint options,
                                   GCancellable* cancel,
                                   GError** error)
{
	return gxml_xhtml_document_construct_from_file (GXML_TYPE_XHTML_DOCUMENT, file, options, cancel, error);
}

/**
 * This method parse strings using {@link Html.Doc.read_memory} method.
 * Refer to libxml2 documentation about limitations on parsing.
 */
GXmlXHtmlDocument*
gxml_xhtml_document_construct_from_string (GType object_type,
                                           const gchar* html,
                                           gint options)
{
	GXmlXHtmlDocument * self = NULL;
	gint _tmp0_;
	gint _tmp1_;
	xmlDoc* _tmp2_;
	g_return_val_if_fail (html != NULL, NULL);
	_tmp0_ = strlen (html);
	_tmp1_ = _tmp0_;
	_tmp2_ = htmlReadMemory ((gchar*) html, _tmp1_, "", NULL, options);
	self = (GXmlXHtmlDocument*) gxml_xdocument_construct_from_doc (object_type, _tmp2_);
	return self;
}

GXmlXHtmlDocument*
gxml_xhtml_document_new_from_string (const gchar* html,
                                     gint options)
{
	return gxml_xhtml_document_construct_from_string (GXML_TYPE_XHTML_DOCUMENT, html, options);
}

/**
 * This method parse strings using {@link Html.ParserCtxt} class.
 * Refer to libxml2 documentation about limitations on parsing.
 */
GXmlXHtmlDocument*
gxml_xhtml_document_construct_from_string_context (GType object_type,
                                                   const gchar* html,
                                                   gint options)
{
	GXmlXHtmlDocument * self = NULL;
	htmlParserCtxt* ctx = NULL;
	htmlParserCtxt* _tmp0_;
	xmlDoc* doc = NULL;
	gint _tmp1_;
	gint _tmp2_;
	xmlDoc* _tmp3_;
	g_return_val_if_fail (html != NULL, NULL);
	_tmp0_ = htmlNewParserCtxt ();
	ctx = _tmp0_;
	_tmp1_ = strlen (html);
	_tmp2_ = _tmp1_;
	_tmp3_ = htmlCtxtReadMemory (ctx, (gchar*) html, _tmp2_, "", NULL, options);
	doc = _tmp3_;
	self = (GXmlXHtmlDocument*) gxml_xdocument_construct_from_doc (object_type, doc);
	_htmlFreeParserCtxt0 (ctx);
	return self;
}

GXmlXHtmlDocument*
gxml_xhtml_document_new_from_string_context (const gchar* html,
                                             gint options)
{
	return gxml_xhtml_document_construct_from_string_context (GXML_TYPE_XHTML_DOCUMENT, html, options);
}

/**
 * This method parse strings using {@link Html.Doc.read_doc} method.
 * Refer to libxml2 documentation about limitations on parsing.
 */
GXmlXHtmlDocument*
gxml_xhtml_document_construct_from_string_doc (GType object_type,
                                               const gchar* html,
                                               gint options)
{
	GXmlXHtmlDocument * self = NULL;
	xmlDoc* _tmp0_;
	g_return_val_if_fail (html != NULL, NULL);
	_tmp0_ = htmlReadDoc ((xmlChar*) html, "", NULL, options);
	self = (GXmlXHtmlDocument*) gxml_xdocument_construct_from_doc (object_type, _tmp0_);
	return self;
}

GXmlXHtmlDocument*
gxml_xhtml_document_new_from_string_doc (const gchar* html,
                                         gint options)
{
	return gxml_xhtml_document_construct_from_string_doc (GXML_TYPE_XHTML_DOCUMENT, html, options);
}

static void
gxml_xhtml_document_real_read_from_string (GXmlDomHtmlDocument* base,
                                           const gchar* str,
                                           GError** error)
{
	GXmlXHtmlDocument * self;
	xmlDoc* _tmp0_;
	gint _tmp2_;
	gint _tmp3_;
	xmlDoc* _tmp4_;
	self = (GXmlXHtmlDocument*) base;
	g_return_if_fail (str != NULL);
	_tmp0_ = ((GXmlXDocument*) self)->doc;
	if (_tmp0_ != NULL) {
		xmlDoc* _tmp1_;
		_tmp1_ = ((GXmlXDocument*) self)->doc;
		_xmlFreeDoc0 (_tmp1_);
		((GXmlXDocument*) self)->doc = NULL;
	}
	_tmp2_ = strlen (str);
	_tmp3_ = _tmp2_;
	_tmp4_ = htmlReadMemory ((gchar*) str, _tmp3_, "", NULL, 0);
	((GXmlXDocument*) self)->doc = _tmp4_;
}

static void
gxml_xhtml_document_real_read_from_string_tolerant (GXmlDomHtmlDocument* base,
                                                    const gchar* str,
                                                    GError** error)
{
	GXmlXHtmlDocument * self;
	htmlParserCtxt* ctx = NULL;
	htmlParserCtxt* _tmp0_;
	xmlDoc* _tmp1_;
	htmlParserCtxt* _tmp3_;
	gint _tmp4_;
	gint _tmp5_;
	xmlDoc* _tmp6_;
	self = (GXmlXHtmlDocument*) base;
	g_return_if_fail (str != NULL);
	_tmp0_ = htmlNewParserCtxt ();
	ctx = _tmp0_;
	_tmp1_ = ((GXmlXDocument*) self)->doc;
	if (_tmp1_ != NULL) {
		xmlDoc* _tmp2_;
		_tmp2_ = ((GXmlXDocument*) self)->doc;
		_xmlFreeDoc0 (_tmp2_);
		((GXmlXDocument*) self)->doc = NULL;
	}
	_tmp3_ = ctx;
	_tmp4_ = strlen (str);
	_tmp5_ = _tmp4_;
	_tmp6_ = htmlCtxtReadMemory (_tmp3_, (gchar*) str, _tmp5_, "", NULL, 0);
	((GXmlXDocument*) self)->doc = _tmp6_;
	_htmlFreeParserCtxt0 (ctx);
}

static gchar*
gxml_xhtml_document_real_to_html (GXmlDomHtmlDocument* base,
                                  GError** error)
{
	GXmlXHtmlDocument * self;
	gchar* buffer = NULL;
	gint len = 0;
	xmlDoc* _tmp0_;
	gchar* _tmp1_ = NULL;
	gint _tmp2_ = 0;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* result;
	self = (GXmlXHtmlDocument*) base;
	len = 0;
	_tmp0_ = ((GXmlXDocument*) self)->doc;
	htmlDocDumpMemory ((xmlDoc*) _tmp0_, (xmlChar**) (&_tmp1_), &_tmp2_);
	_g_free0 (buffer);
	buffer = _tmp1_;
	len = _tmp2_;
	_tmp3_ = g_strdup_printf ("%i", len);
	_tmp4_ = _tmp3_;
	g_message ("XHtmlDocument.vala:106: %s", _tmp4_);
	_g_free0 (_tmp4_);
	_tmp5_ = g_strdup (buffer);
	result = _tmp5_;
	_g_free0 (buffer);
	return result;
}

static gchar*
gxml_xhtml_document_real_to_string (GXmlXNode* base)
{
	GXmlXHtmlDocument * self;
	gchar* t = NULL;
	gchar* _tmp0_;
	GError* _inner_error0_ = NULL;
	gchar* result;
	self = (GXmlXHtmlDocument*) base;
	_tmp0_ = g_strdup ("");
	t = _tmp0_;
	{
		gchar* _tmp1_ = NULL;
		gchar* _tmp2_;
		gchar* _tmp3_;
		_tmp2_ = gxml_dom_html_document_to_html ((GXmlDomHtmlDocument*) self, &_inner_error0_);
		_tmp1_ = _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp3_ = _tmp1_;
		_tmp1_ = NULL;
		_g_free0 (t);
		t = _tmp3_;
		_g_free0 (_tmp1_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp4_;
		const gchar* _tmp5_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp4_ = e;
		_tmp5_ = _tmp4_->message;
		g_warning (_ ("Error while converting HTML document to string: %s"), _tmp5_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (t);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	result = t;
	return result;
}

GXmlXHtmlDocument*
gxml_xhtml_document_construct (GType object_type)
{
	GXmlXHtmlDocument * self = NULL;
	self = (GXmlXHtmlDocument*) gxml_xdocument_construct (object_type);
	return self;
}

GXmlXHtmlDocument*
gxml_xhtml_document_new (void)
{
	return gxml_xhtml_document_construct (GXML_TYPE_XHTML_DOCUMENT);
}

static void
gxml_xhtml_document_class_init (GXmlXHtmlDocumentClass * klass,
                                gpointer klass_data)
{
	gxml_xhtml_document_parent_class = g_type_class_peek_parent (klass);
	((GXmlXNodeClass *) klass)->to_string = (gchar* (*) (GXmlXNode*)) gxml_xhtml_document_real_to_string;
	G_OBJECT_CLASS (klass)->get_property = _vala_gxml_xhtml_document_get_property;
}

static void
gxml_xhtml_document_gxml_dom_html_document_interface_init (GXmlDomHtmlDocumentIface * iface,
                                                           gpointer iface_data)
{
	gxml_xhtml_document_gxml_dom_html_document_parent_iface = g_type_interface_peek_parent (iface);
	iface->read_from_string = (void (*) (GXmlDomHtmlDocument*, const gchar*, GError**)) gxml_xhtml_document_real_read_from_string;
	iface->read_from_string_tolerant = (void (*) (GXmlDomHtmlDocument*, const gchar*, GError**)) gxml_xhtml_document_real_read_from_string_tolerant;
	iface->to_html = (gchar* (*) (GXmlDomHtmlDocument*, GError**)) gxml_xhtml_document_real_to_html;
}

static void
gxml_xhtml_document_instance_init (GXmlXHtmlDocument * self,
                                   gpointer klass)
{
}

/**
   * HML parsing support. Document handling
   */
static GType
gxml_xhtml_document_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GXmlXHtmlDocumentClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gxml_xhtml_document_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GXmlXHtmlDocument), 0, (GInstanceInitFunc) gxml_xhtml_document_instance_init, NULL };
	static const GInterfaceInfo gxml_dom_html_document_info = { (GInterfaceInitFunc) gxml_xhtml_document_gxml_dom_html_document_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType gxml_xhtml_document_type_id;
	gxml_xhtml_document_type_id = g_type_register_static (GXML_TYPE_XDOCUMENT, "GXmlXHtmlDocument", &g_define_type_info, 0);
	g_type_add_interface_static (gxml_xhtml_document_type_id, GXML_TYPE_DOM_HTML_DOCUMENT, &gxml_dom_html_document_info);
	return gxml_xhtml_document_type_id;
}

GType
gxml_xhtml_document_get_type (void)
{
	static volatile gsize gxml_xhtml_document_type_id__once = 0;
	if (g_once_init_enter (&gxml_xhtml_document_type_id__once)) {
		GType gxml_xhtml_document_type_id;
		gxml_xhtml_document_type_id = gxml_xhtml_document_get_type_once ();
		g_once_init_leave (&gxml_xhtml_document_type_id__once, gxml_xhtml_document_type_id);
	}
	return gxml_xhtml_document_type_id__once;
}

static void
_vala_gxml_xhtml_document_get_property (GObject * object,
                                        guint property_id,
                                        GValue * value,
                                        GParamSpec * pspec)
{
	GXmlXHtmlDocument * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GXML_TYPE_XHTML_DOCUMENT, GXmlXHtmlDocument);
	switch (property_id) {
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

