]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added functions ZakFormForm::load_from_xml and ::load_from_file.
authorAndrea Zagli <azagli@libero.it>
Thu, 5 Nov 2015 19:38:48 +0000 (20:38 +0100)
committerAndrea Zagli <azagli@libero.it>
Thu, 5 Nov 2015 19:38:48 +0000 (20:38 +0100)
configure.ac
libzakform.pc.in
src/form.c
src/form.h

index b05d4461fa2f28fccb14798258c601a1f6d11dae..5d14ebb07ff843be419da2fef47c35a9e12f97d7 100644 (file)
@@ -41,7 +41,8 @@ AM_GLIB_GNU_GETTEXT
 # Checks for libraries.
 PKG_CHECK_MODULES(ZAKFORM, [glib-2.0 >= 2.36
                             gobject-2.0 >= 2.36
-                            gmodule-2.0 >= 2.0.0])
+                            gmodule-2.0 >= 2.0.0
+                            libxml-2.0 >= 2.0.0])
 
 AC_SUBST(ZAKFORM_CFLAGS)
 AC_SUBST(ZAKFORM_LIBS)
index 0c0cf06b44dddc5da91d2f7fab635ebeb6c69a9a..9cfc80a7ef2ec5f101f4e05913a43a12ace653ad 100644 (file)
@@ -7,6 +7,6 @@ modulesdir=@libdir@/@PACKAGE@/modules
 Name: @PACKAGE_NAME@
 Description: Class for form.
 Version: @PACKAGE_VERSION@
-Requires: glib-2.0 >= 2.36 gobject-2.0
+Requires: glib-2.0 >= 2.36 gobject-2.0 gmodule-2.0 libxml-2.0
 Libs: -L${libdir} -lzakform
 Cflags: -I${includedir}
index 166262b44417c5473f6f003e3ace434f2bf89ea1..1661830353e46ef4e284f1c1e722a5089c1cc459 100644 (file)
 #include <glib/gi18n-lib.h>
 #include <gmodule.h>
 
+#include <libxml/xpath.h>
+
 #include "form.h"
+#include "formelement.h"
 
 #ifdef G_OS_WIN32
        #include <windows.h>
 #endif
 
+typedef ZakFormElement *(* FormElementConstructorFunc) (void);
+typedef gboolean (* FormElementXmlParsingFunc) (ZakFormElement *, xmlNodePtr);
+
 static void zak_form_form_class_init (ZakFormFormClass *class);
 static void zak_form_form_init (ZakFormForm *zak_form_form);
 
@@ -136,6 +142,123 @@ zak_form_form_init (ZakFormForm *zak_form_form)
        zak_form_form_load_modules (zak_form_form);
 }
 
+/**
+ * zak_form_form_load_from_xml:
+ * @zakform:
+ * @xmldoc:
+ *
+ */
+gboolean
+zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc)
+{
+       ZakFormFormPrivate *priv;
+       xmlNode *cur;
+
+       gboolean ret;
+
+       ZakFormElement *element;
+       gchar *type;
+       guint i;
+
+       FormElementConstructorFunc element_constructor;
+       FormElementXmlParsingFunc element_xml_parsing;
+
+       g_return_val_if_fail (ZAK_FORM_IS_FORM (zakform), FALSE);
+       g_return_val_if_fail (xmldoc != NULL, FALSE);
+
+       ret = FALSE;
+
+       cur = xmlDocGetRootElement (xmldoc);
+       if (cur != NULL)
+               {
+                       if (xmlStrcmp (cur->name, (const xmlChar *)"zakform") == 0)
+                               {
+                                       ret = TRUE;
+
+                                       priv = zak_form_form_get_instance_private (zakform);
+
+                                       cur = cur->children;
+                                       while (cur)
+                                               {
+                                                       if (xmlStrcmp (cur->name, (const xmlChar *)"element") == 0)
+                                                               {
+                                                                   element = NULL;
+
+                                                                       type = xmlGetProp (cur, (const xmlChar *)"type");
+
+                                                                       /* for each module */
+                                                                       for (i = 0; i < priv->ar_modules->len; i++)
+                                                                               {
+                                                                                       if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i),
+                                                                                                                                g_strconcat (type, "_new", NULL),
+                                                                                                                                (gpointer *)&element_constructor))
+                                                                                               {
+                                                                                                       if (element_constructor != NULL)
+                                                                                                               {
+                                                                                                                       element = element_constructor ();
+                                                                                                                       if (element != NULL)
+                                                                                                                               {
+                                                                                                                                       zak_form_form_add_element (zakform, element);
+                                                                                                                                       if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i),
+                                                                                                                                                                g_strconcat (type, "_xml_parsing", NULL),
+                                                                                                                                                                                (gpointer *)&element_xml_parsing))
+                                                                                                                                               {
+                                                                                                                                                       if (element_xml_parsing != NULL)
+                                                                                                                                                               {
+                                                                                                                                                                       element_xml_parsing (element, cur);
+                                                                                                                                                               }
+                                                                                                                                               }
+                                                                                                                               }
+                                                                                                                       break;
+                                                                                                               }
+                                                                                               }
+                                                                               }
+
+                                                                       if (element == NULL)
+                                                                               {
+                                                                                       g_warning (_("Unknown element type «%s»."), type);
+                                                                               }
+                                                               }
+
+                                                       cur = cur->next;
+                                               }
+                               }
+                       else
+                               {
+                                       g_warning (_("The file is not a valid ZakForm definition file."));
+                               }
+               }
+
+       return ret;
+}
+
+/**
+ * zak_form_form_load_from_file:
+ * @zakform:
+ * @filename:
+ *
+ */
+gboolean
+zak_form_form_load_from_file (ZakFormForm *zakform, const gchar *filename)
+{
+       xmlDoc *xdoc;
+
+       gboolean ret;
+
+       g_return_val_if_fail (ZAK_FORM_IS_FORM (zakform), FALSE);
+       g_return_val_if_fail (filename != NULL, FALSE);
+
+       ret = FALSE;
+
+       xdoc = xmlParseFile (filename);
+       if (xdoc != NULL)
+               {
+                       ret = zak_form_form_load_from_xml (zakform, xdoc);
+               }
+
+       return ret;
+}
+
 /**
  * zak_form_form_add_element:
  * @zakform:
index f1179b627437a5a5c1035c8943f07227676b8043..0bb4bdc2b80953f08ef0ccd75172c5faabde5b92 100644 (file)
@@ -22,6 +22,8 @@
 
 #include <glib-object.h>
 
+#include <libxml/tree.h>
+
 #include "formelement.h"
 
 
@@ -39,6 +41,9 @@ struct _ZakFormFormClass
 };
 
 
+gboolean zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc);
+gboolean zak_form_form_load_from_file (ZakFormForm *zakform, const gchar *filename);
+
 gboolean zak_form_form_add_element (ZakFormForm *zakform, ZakFormElement *element);
 
 void zak_form_form_clear (ZakFormForm *zakform);