[gnome-builder/wip/slaf/colored-build-output: 20/36] xml: add a SAX object



commit f69f8303a00981632dfbc869604813d00292a041
Author: Sebastien Lafargue <slafargue gnome org>
Date:   Sun Jan 29 16:09:03 2017 +0100

    xml: add a SAX object
    
    Due to limitations in the reader API,
    we now use the SAX one.

 plugins/xml-pack/Makefile.am   |    2 +
 plugins/xml-pack/ide-xml-sax.c |  213 ++++++++++++++++++++++++++++++++++++++++
 plugins/xml-pack/ide-xml-sax.h |   63 ++++++++++++
 3 files changed, 278 insertions(+), 0 deletions(-)
---
diff --git a/plugins/xml-pack/Makefile.am b/plugins/xml-pack/Makefile.am
index 4f7d316..f05f02b 100644
--- a/plugins/xml-pack/Makefile.am
+++ b/plugins/xml-pack/Makefile.am
@@ -11,6 +11,8 @@ libxml_pack_plugin_la_SOURCES = \
        ide-xml-highlighter.h \
        ide-xml-indenter.c \
        ide-xml-indenter.h \
+       ide-xml-sax.c \
+       ide-xml-sax.h \
        ide-xml-service.c \
        ide-xml-service.h \
        ide-xml-stack.c \
diff --git a/plugins/xml-pack/ide-xml-sax.c b/plugins/xml-pack/ide-xml-sax.c
new file mode 100644
index 0000000..84cec95
--- /dev/null
+++ b/plugins/xml-pack/ide-xml-sax.c
@@ -0,0 +1,213 @@
+/* ide-xml-sax.c
+ *
+ * Copyright (C) 2017 Sebastien Lafargue <slafargue gnome org>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib/gi18n.h>
+#include <string.h>
+#include <libxml/parser.h>
+#include <libxml/parserInternals.h>
+
+#include "ide-xml-sax.h"
+
+#define XML_TO_CHAR(s)  ((char *) (s))
+#define CHAR_TO_XML(s)  ((unsigned char *) (s))
+#define RETURN_STRDUP_AND_XMLFREE(stmt) \
+  G_STMT_START {                        \
+    guchar *x;                          \
+    gchar *y;                           \
+    x = stmt;                           \
+    y = g_strdup((char *)x);            \
+    xmlFree(x);                         \
+    return y;                           \
+  } G_STMT_END
+
+struct _IdeXmlSax
+{
+  GObject        parent_instance;
+
+  xmlSAXHandler  handler;
+  xmlParserCtxt *context;
+
+  guint          initialized : 1;
+};
+
+G_DEFINE_TYPE (IdeXmlSax, ide_xml_sax, G_TYPE_OBJECT)
+
+IdeXmlSax *
+ide_xml_sax_new (void)
+{
+  return g_object_new (IDE_TYPE_XML_SAX, NULL);
+}
+
+static void
+ide_xml_sax_finalize (GObject *object)
+{
+  IdeXmlSax *self = (IdeXmlSax *)object;
+
+  G_OBJECT_CLASS (ide_xml_sax_parent_class)->finalize (object);
+}
+
+static void
+ide_xml_sax_class_init (IdeXmlSaxClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = ide_xml_sax_finalize;
+}
+
+static void
+ide_xml_sax_init (IdeXmlSax *self)
+{
+}
+
+void
+ide_xml_sax_set_callback (IdeXmlSax             *self,
+                          IdeXmlSaxCallbackType  callback_type,
+                          gpointer               callback)
+{
+  xmlSAXHandler *handler;
+
+  g_return_if_fail (IDE_IS_XML_SAX (self));
+  g_return_if_fail (callback != NULL);
+
+  self->initialized = TRUE;
+
+  handler = &self->handler;
+  switch (callback_type)
+    {
+    case IDE_XML_SAX_CALLBACK_TYPE_ATTRIBUTE:
+      handler->attributeDecl = callback;
+      break;
+
+    case IDE_XML_SAX_CALLBACK_TYPE_CDATA:
+      handler->cdataBlock = callback;
+      break;
+
+    case IDE_XML_SAX_CALLBACK_TYPE_CHAR:
+      handler->characters = callback;
+      break;
+
+    case IDE_XML_SAX_CALLBACK_TYPE_COMMENT:
+      handler->comment = callback;
+      break;
+
+    case IDE_XML_SAX_CALLBACK_TYPE_START_DOCUMENT:
+      handler->startDocument = callback;
+      break;
+
+    case IDE_XML_SAX_CALLBACK_TYPE_START_ELEMENT:
+      handler->startElement = callback;
+      break;
+
+    case IDE_XML_SAX_CALLBACK_TYPE_END_DOCUMENT:
+      handler->endDocument = callback;
+      break;
+
+    case IDE_XML_SAX_CALLBACK_TYPE_END_ELEMENT:
+      handler->endElement = callback;
+      break;
+
+    case IDE_XML_SAX_CALLBACK_TYPE_ENTITY:
+      handler->entityDecl = callback;
+      break;
+
+    default:
+      g_assert_not_reached ();
+    }
+}
+
+void
+ide_xml_sax_clear (IdeXmlSax *self)
+{
+  g_return_if_fail (IDE_IS_XML_SAX (self));
+
+  memset ((void *)(&self->handler), 0, sizeof (xmlSAXHandler));
+}
+
+gboolean
+ide_xml_sax_parse (IdeXmlSax   *self,
+                   const gchar *data,
+                   gsize        length,
+                   const gchar *uri,
+                   gpointer     user_data)
+{
+  gboolean wellformed;
+
+  g_return_val_if_fail (IDE_IS_XML_SAX (self), FALSE);
+  g_return_val_if_fail (data != NULL, FALSE);
+  g_return_val_if_fail (length > 0, FALSE);
+
+  g_return_val_if_fail (self->initialized == TRUE, FALSE);
+  g_return_val_if_fail (self->context == NULL, FALSE);
+
+  printf ("parse base:%p\n", data);
+
+  self->context = xmlCreateMemoryParserCtxt (data, length);
+  self->context->userData = user_data;
+
+  self->context->sax = &self->handler;
+  //xmlSAXVersion(&self->handler, 2);
+  self->handler.initialized = XML_SAX2_MAGIC;
+  xmlCtxtUseOptions (self->context, XML_PARSE_RECOVER | XML_PARSE_NOENT);
+
+  xmlParseDocument (self->context);
+  wellformed = self->context->wellFormed;
+
+  self->context->sax = NULL;
+  g_clear_pointer (&self->context, xmlFreeParserCtxt);
+
+  return wellformed;
+}
+
+gboolean
+ide_xml_sax_get_position (IdeXmlSax *self,
+                          gint      *line,
+                          gint      *line_offset)
+{
+  g_return_val_if_fail (IDE_IS_XML_SAX (self), FALSE);
+  g_return_val_if_fail (line != NULL, FALSE);
+  g_return_val_if_fail (line_offset != NULL, FALSE);
+  g_return_val_if_fail (self->context != NULL, FALSE);
+
+  *line = xmlSAX2GetLineNumber (self->context);
+  *line_offset = xmlSAX2GetColumnNumber (self->context);
+
+  return (*line > 0 && *line_offset > 0);
+}
+
+gint
+ide_xml_sax_get_depth (IdeXmlSax *self)
+{
+  g_return_val_if_fail (IDE_IS_XML_SAX (self), FALSE);
+  g_return_val_if_fail (self->context != NULL, FALSE);
+
+  return self->context->nameNr;
+}
+
+gsize
+ide_xml_sax_get_byteconsumed (IdeXmlSax *self)
+{
+  xmlParserInput *input;
+
+  g_return_val_if_fail (IDE_IS_XML_SAX (self), FALSE);
+  g_return_val_if_fail (self->context != NULL, FALSE);
+
+  input = self->context->input;
+
+  return input->base;
+}
+
diff --git a/plugins/xml-pack/ide-xml-sax.h b/plugins/xml-pack/ide-xml-sax.h
new file mode 100644
index 0000000..cf5bf2f
--- /dev/null
+++ b/plugins/xml-pack/ide-xml-sax.h
@@ -0,0 +1,63 @@
+/* ide-xml-sax.h
+ *
+ * Copyright (C) 2017 Sebastien Lafargue <slafargue gnome org>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_XML_SAX_H
+#define IDE_XML_SAX_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_XML_SAX (ide_xml_sax_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeXmlSax, ide_xml_sax, IDE, XML_SAX, GObject)
+
+typedef enum _IdeXmlSaxCallbackType IdeXmlSaxCallbackType;
+
+enum _IdeXmlSaxCallbackType {
+  IDE_XML_SAX_CALLBACK_TYPE_ATTRIBUTE,
+  IDE_XML_SAX_CALLBACK_TYPE_CDATA,
+  IDE_XML_SAX_CALLBACK_TYPE_CHAR,
+  IDE_XML_SAX_CALLBACK_TYPE_COMMENT,
+  IDE_XML_SAX_CALLBACK_TYPE_START_DOCUMENT,
+  IDE_XML_SAX_CALLBACK_TYPE_START_ELEMENT,
+  IDE_XML_SAX_CALLBACK_TYPE_END_DOCUMENT,
+  IDE_XML_SAX_CALLBACK_TYPE_END_ELEMENT,
+  IDE_XML_SAX_CALLBACK_TYPE_ENTITY
+};
+
+void            ide_xml_sax_clear             (IdeXmlSax              *self);
+gsize           ide_xml_sax_get_byteconsumed  (IdeXmlSax              *self);
+gint            ide_xml_sax_get_depth         (IdeXmlSax              *self);
+gboolean        ide_xml_sax_get_position      (IdeXmlSax              *self,
+                                               gint                   *line,
+                                               gint                   *line_offset);
+IdeXmlSax      *ide_xml_sax_new               (void);
+gboolean        ide_xml_sax_parse             (IdeXmlSax              *self,
+                                               const gchar            *data,
+                                               gsize                   length,
+                                               const gchar            *uri,
+                                               gpointer                user_data);
+void            ide_xml_sax_set_callback      (IdeXmlSax              *self,
+                                               IdeXmlSaxCallbackType   callback_type,
+                                               gpointer                callback);
+
+G_END_DECLS
+
+#endif /* IDE_XML_SAX_H */
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]