]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added property id to FormElementValidator (refs #1102).
authorAndrea Zagli <azagli@libero.it>
Sun, 16 Apr 2017 09:17:18 +0000 (11:17 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 16 Apr 2017 09:21:31 +0000 (11:21 +0200)
Refactored ZakFormElementValidator::xml_parsing.

src/form.c
src/formelementvalidator.c
src/formelementvalidator.h
src/formelementvalidatordate.c
src/formelementvalidatordate.h
src/formelementvalidatornotempty.c
src/formelementvalidatornotempty.h
src/formelementvalidatorregex.c
src/formelementvalidatorregex.h

index 181fe1b3ea4aee480b590bba7c14ad845dd3bcdd..c58bbeb64da2efca17a2357214fa0fcab3920f38 100644 (file)
@@ -40,7 +40,6 @@ typedef gboolean (* FormElementExtensionXmlParsingFunc) (GObject *, xmlNodePtr);
 typedef ZakFormElementFilter *(* FormElementFilterConstructorFunc) (void);
 typedef gboolean (* FormElementFilterXmlParsingFunc) (ZakFormElementFilter *, xmlNodePtr);
 typedef ZakFormElementValidator *(* FormElementValidatorConstructorFunc) (void);
-typedef gboolean (* FormElementValidatorXmlParsingFunc) (ZakFormElementValidator *, xmlNodePtr);
 typedef ZakFormValidator *(* FormValidatorConstructorFunc) (void);
 
 static void zak_form_form_class_init (ZakFormFormClass *class);
@@ -170,7 +169,6 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element
        FormElementFilterConstructorFunc filter_constructor;
        FormElementFilterXmlParsingFunc filter_xml_parsing;
        FormElementValidatorConstructorFunc validator_constructor;
-       FormElementValidatorXmlParsingFunc validator_xml_parsing;
 
        gboolean to_unlink;
        xmlNode *xnode_tmp;
@@ -272,15 +270,7 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element
                                                                                        validator = validator_constructor ();
                                                                                        zak_form_element_add_validator (element, validator);
 
-                                                                                       if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i),
-                                                                                                            g_strconcat (type, "_xml_parsing", NULL),
-                                                                                                            (gpointer *)&validator_xml_parsing))
-                                                                                               {
-                                                                                                       if (validator_xml_parsing != NULL)
-                                                                                                               {
-                                                                                                                       validator_xml_parsing (validator, xnode);
-                                                                                                               }
-                                                                                               }
+                                                                                       zak_form_element_validator_xml_parsing (validator, xnode);
 
                                                                                        break;
                                                                                }
index 65fb8db09c5bae1af2f83cbeee0358def04fb00f..063b3b2ba7cd132e604d30dfd6b0c29faeaa51c8 100644 (file)
@@ -27,6 +27,7 @@
 enum
        {
                PROP_0,
+               PROP_ID,
                PROP_ENABLED,
                PROP_MESSAGE
        };
@@ -48,6 +49,7 @@ static void zak_form_element_validator_finalize (GObject *gobject);
 
 typedef struct
        {
+               gchar *id;
                gboolean enabled;
                gchar *message;
        } ZakFormElementValidatorPrivate;
@@ -64,19 +66,26 @@ zak_form_element_validator_class_init (ZakFormElementValidatorClass *class)
        object_class->dispose = zak_form_element_validator_dispose;
        object_class->finalize = zak_form_element_validator_finalize;
 
+       g_object_class_install_property (object_class, PROP_ID,
+                                        g_param_spec_string ("id",
+                                                             "Id",
+                                                             "Id",
+                                                             "",
+                                                             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+
        g_object_class_install_property (object_class, PROP_ENABLED,
-                                                                        g_param_spec_boolean ("enabled",
-                                                                                                                  "Enabled",
-                                                                                                                  "Enabled",
-                                                                                                                  TRUE,
-                                                                                                                  G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+                                        g_param_spec_boolean ("enabled",
+                                                              "Enabled",
+                                                              "Enabled",
+                                                              TRUE,
+                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
        g_object_class_install_property (object_class, PROP_MESSAGE,
-                                                                        g_param_spec_string ("message",
-                                                                                                                 "Message",
-                                                                                                                 "Message",
-                                                                                                                 _("Invalid value"),
-                                                                                                                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+                                        g_param_spec_string ("message",
+                                                             "Message",
+                                                             "Message",
+                                                             _("Invalid value"),
+                                                             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 }
 
 static void
@@ -85,6 +94,45 @@ zak_form_element_validator_init (ZakFormElementValidator *zak_form_element_valid
        ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator);
 }
 
+gboolean
+zak_form_element_validator_xml_parsing (ZakFormElementValidator *self, xmlNode *xnode)
+{
+       gboolean ret;
+
+       gchar *prop;
+
+       g_return_val_if_fail (ZAK_FORM_IS_ELEMENT_VALIDATOR (self), FALSE);
+
+       ret = TRUE;
+
+       prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"id");
+       if (prop != NULL)
+               {
+                       zak_form_element_validator_set_id (self, prop);
+                       g_free (prop);
+               }
+
+       prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"message");
+       if (prop != NULL)
+               {
+                       zak_form_element_validator_set_message (self, prop);
+                       g_free (prop);
+               }
+
+       prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"disabled");
+       if (prop != NULL)
+               {
+                       zak_form_element_validator_set_enabled (self, FALSE);
+                       g_free (prop);
+               }
+
+       if (ZAK_FORM_ELEMENT_VALIDATOR_GET_CLASS (self)->xml_parsing != NULL)
+               {
+                       ret = ZAK_FORM_ELEMENT_VALIDATOR_GET_CLASS (self)->xml_parsing (self, xnode);
+               }
+       return ret;
+}
+
 gboolean
 zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar *value)
 {
@@ -101,6 +149,42 @@ zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar
        return ret;
 }
 
+/**
+ * zak_form_element_validator_set_id:
+ * @validator:
+ * @id:
+ *
+ */
+void
+zak_form_element_validator_set_id (ZakFormElementValidator *validator,
+                                   const gchar *id)
+{
+       ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator);
+
+       if (id == NULL)
+               {
+                       priv->id = g_strdup ("");
+               }
+       else
+               {
+                       priv->id = g_strdup (id);
+               }
+}
+
+/**
+ * zak_form_element_validator_get_id:
+ * @validator:
+ *
+ * Returns:
+ */
+gchar
+*zak_form_element_validator_get_id (ZakFormElementValidator *validator)
+{
+       ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator);
+
+       return g_strdup (priv->id);
+}
+
 /**
  * zak_form_element_validator_set_message:
  * @validator:
@@ -109,7 +193,7 @@ zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar
  */
 void
 zak_form_element_validator_set_message (ZakFormElementValidator *validator,
-                                                                               const gchar *message)
+                                        const gchar *message)
 {
        ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator);
 
@@ -127,7 +211,7 @@ gchar
 {
        ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator);
 
-    return g_strdup (priv->message);
+       return g_strdup (priv->message);
 }
 
 /**
@@ -161,15 +245,19 @@ zak_form_element_validator_set_enabled (ZakFormElementValidator *validator, gboo
 /* PRIVATE */
 static void
 zak_form_element_validator_set_property (GObject *object,
-                   guint property_id,
-                   const GValue *value,
-                   GParamSpec *pspec)
+                                         guint property_id,
+                                         const GValue *value,
+                                         GParamSpec *pspec)
 {
        ZakFormElementValidator *zak_form_element_validator = (ZakFormElementValidator *)object;
        ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator);
 
        switch (property_id)
                {
+               case PROP_ID:
+                       zak_form_element_validator_set_id (zak_form_element_validator, g_value_dup_string (value));
+                       break;
+
                case PROP_ENABLED:
                        zak_form_element_validator_set_enabled (zak_form_element_validator, g_value_get_boolean (value));
                        break;
@@ -186,15 +274,19 @@ zak_form_element_validator_set_property (GObject *object,
 
 static void
 zak_form_element_validator_get_property (GObject *object,
-                   guint property_id,
-                   GValue *value,
-                   GParamSpec *pspec)
+                                         guint property_id,
+                                         GValue *value,
+                                         GParamSpec *pspec)
 {
        ZakFormElementValidator *zak_form_element_validator = (ZakFormElementValidator *)object;
        ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator);
 
        switch (property_id)
                {
+               case PROP_ID:
+                       g_value_set_string (value, zak_form_element_validator_get_id (zak_form_element_validator));
+                       break;
+
                case PROP_ENABLED:
                        g_value_set_boolean (value, zak_form_element_validator_get_enabled (zak_form_element_validator));
                        break;
index 2ef9407dcfcca18b3c04cdba6853407df570ef5b..4f9d9c534db120e5810e8c7c3a1ce71e810ff6ed 100644 (file)
@@ -21,6 +21,8 @@
 
 #include <glib-object.h>
 
+#include <libxml/xpath.h>
+
 
 G_BEGIN_DECLS
 
@@ -32,13 +34,19 @@ struct _ZakFormElementValidatorClass
 {
        GObjectClass parent_class;
 
+       gboolean (*xml_parsing) (ZakFormElementValidator *self, xmlNode *xnode);
        gboolean (*validate) (ZakFormElementValidator *self, const gchar *value);
 };
 
+gboolean zak_form_element_validator_xml_parsing (ZakFormElementValidator *self, xmlNode *xnode);
 gboolean zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar *value);
 
+void zak_form_element_validator_set_id (ZakFormElementValidator *validator,
+                                        const gchar *id);
+gchar *zak_form_element_validator_get_id (ZakFormElementValidator *validator);
+
 void zak_form_element_validator_set_message (ZakFormElementValidator *validator,
-                                                                                        const gchar *message);
+                                             const gchar *message);
 gchar *zak_form_element_validator_get_message (ZakFormElementValidator *validator);
 
 gboolean zak_form_element_validator_get_enabled (ZakFormElementValidator *validator);
index df7d0f5519c838b9a9426ba5915f38edb46b8eab..b10bc94450e11bc18b53549084831085d4d66f91 100644 (file)
@@ -40,6 +40,7 @@ static void zak_form_element_validator_date_get_property (GObject *object,
 static void zak_form_element_validator_date_dispose (GObject *gobject);
 static void zak_form_element_validator_date_finalize (GObject *gobject);
 
+static gboolean zak_form_element_validator_date_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode);
 static gboolean zak_form_element_validator_date_validate (ZakFormElementValidator *validator_date, const gchar *value);
 
 struct _ZakFormElementValidatorDate
@@ -70,6 +71,7 @@ zak_form_element_validator_date_class_init (ZakFormElementValidatorDateClass *cl
        object_class->dispose = zak_form_element_validator_date_dispose;
        object_class->finalize = zak_form_element_validator_date_finalize;
 
+       parent_class->xml_parsing = zak_form_element_validator_date_xml_parsing;
        parent_class->validate = zak_form_element_validator_date_validate;
 
        g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorDatePrivate));
@@ -135,7 +137,7 @@ gchar
  * @xnode:
  *
  */
-gboolean
+static gboolean
 zak_form_element_validator_date_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode)
 {
        ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator));
index 13b5f38391573f7438f1afd8638a658dd7581b58..84cf21ceb92f9c3d61fc936e18ff686fde7489be 100644 (file)
@@ -38,8 +38,6 @@ ZakFormElementValidatorDate *zak_form_element_validator_date_new (void);
 void zak_form_element_validator_date_set_format (ZakFormElementValidatorDate *validator, const gchar *format);
 gchar *zak_form_element_validator_date_get_format (ZakFormElementValidatorDate *validator);
 
-gboolean zak_form_element_validator_date_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode);
-
 
 G_END_DECLS
 
index a3ef4e277bbc01d79391c3b6fdf112560ed4535c..87d976e1dca319a71b2c72b30d62355053d4760a 100644 (file)
@@ -44,6 +44,7 @@ static void zak_form_element_validator_notempty_get_property (GObject *object,
 static void zak_form_element_validator_notempty_dispose (GObject *gobject);
 static void zak_form_element_validator_notempty_finalize (GObject *gobject);
 
+static gboolean zak_form_element_validator_notempty_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode);
 static gboolean zak_form_element_validator_notempty_validate (ZakFormElementValidator *validator_notempty, const gchar *value);
 
 struct _ZakFormElementValidatorNotempty
@@ -74,16 +75,17 @@ zak_form_element_validator_notempty_class_init (ZakFormElementValidatorNotemptyC
        object_class->dispose = zak_form_element_validator_notempty_dispose;
        object_class->finalize = zak_form_element_validator_notempty_finalize;
 
+       parent_class->xml_parsing = zak_form_element_validator_notempty_xml_parsing;
        parent_class->validate = zak_form_element_validator_notempty_validate;
 
        g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorNotemptyPrivate));
 
        g_object_class_install_property (object_class, PROP_AS_EMPTY_STRING,
-                                                                        g_param_spec_string ("as-empty-string",
-                                                                                                                 "As empty string",
-                                                                                                                 "As empty string",
-                                                                                                                 "",
-                                                                                                                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+                                        g_param_spec_string ("as-empty-string",
+                                                             "As empty string",
+                                                             "As empty string",
+                                                             "",
+                                                             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 }
 
 static void
@@ -113,7 +115,7 @@ ZakFormElementValidatorNotempty
  * @xnode:
  *
  */
-gboolean
+static gboolean
 zak_form_element_validator_notempty_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode)
 {
        ZakFormElementValidatorNotemptyPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NOTEMPTY_GET_PRIVATE (validator);
index 5a6b662c45606144bed11ac8eda7d7ef2c0b4178..f64d06ca1588889c0f725db6870423cbf6b3e07e 100644 (file)
@@ -35,8 +35,6 @@ G_DECLARE_FINAL_TYPE (ZakFormElementValidatorNotempty, zak_form_element_validato
 
 ZakFormElementValidatorNotempty *zak_form_element_validator_notempty_new (void);
 
-gboolean zak_form_element_validator_notempty_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode);
-
 void zak_form_element_validator_notempty_set_as_empty_string (ZakFormElementValidatorNotempty *validator, const gchar *as_empty_string);
 gchar *zak_form_element_validator_notempty_get_as_empty_string (ZakFormElementValidatorNotempty *validator);
 
index a0d5b2b5fa4b22961d1934cc1a5af4c8a2d30288..8614ebf63bf3c344187616a3eb9ac1c1d6b48df7 100644 (file)
@@ -44,6 +44,7 @@ static void zak_form_element_validator_regex_get_property (GObject *object,
 static void zak_form_element_validator_regex_dispose (GObject *gobject);
 static void zak_form_element_validator_regex_finalize (GObject *gobject);
 
+static gboolean zak_form_element_validator_regex_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode);
 static gboolean zak_form_element_validator_regex_validate (ZakFormElementValidator *validator_regex, const gchar *value);
 
 struct _ZakFormElementValidatorRegex
@@ -74,6 +75,7 @@ zak_form_element_validator_regex_class_init (ZakFormElementValidatorRegexClass *
        object_class->dispose = zak_form_element_validator_regex_dispose;
        object_class->finalize = zak_form_element_validator_regex_finalize;
 
+       parent_class->xml_parsing = zak_form_element_validator_regex_xml_parsing;
        parent_class->validate = zak_form_element_validator_regex_validate;
 
        g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorRegexPrivate));
@@ -117,7 +119,7 @@ ZakFormElementValidatorRegex
  * @xnode:
  *
  */
-gboolean
+static gboolean
 zak_form_element_validator_regex_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode)
 {
        zak_form_element_validator_regex_set_regex (ZAK_FORM_ELEMENT_VALIDATOR_REGEX (validator), (gchar *)xmlNodeGetContent (xnode));
index 4bb8b42b8a55932d5d23f9eb9fcade142f731521..127e1a0b3a226adf373c7729fa7137d428c51b5d 100644 (file)
@@ -34,7 +34,6 @@ G_BEGIN_DECLS
 G_DECLARE_FINAL_TYPE (ZakFormElementValidatorRegex, zak_form_element_validator_regex, ZAK_FORM, ELEMENT_VALIDATOR_REGEX, ZakFormElementValidator)
 
 ZakFormElementValidatorRegex *zak_form_element_validator_regex_new (void);
-gboolean zak_form_element_validator_regex_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode);
 
 void zak_form_element_validator_regex_set_regex (ZakFormElementValidatorRegex *validator, const gchar *regex);
 gchar *zak_form_element_validator_regex_get_regex (ZakFormElementValidatorRegex *validator);