From d00c62976f0984e59f7926753dbf56f94fe4542e Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Thu, 5 Nov 2015 20:38:05 +0100 Subject: [PATCH] Started of loading ZakFormForm from xml file. --- .gitignore | 1 + src/Makefile.am | 17 +++++++ src/formelementcheck.c | 84 ++++++++++++++++++++++++++++--- src/formelementcheck.h | 7 ++- src/formelementhidden.c | 81 +++++++++++++++++++++++++++--- src/formelementhidden.h | 7 ++- src/formelementpassword.c | 84 ++++++++++++++++++++++++++++--- src/formelementpassword.h | 7 ++- src/formelementselect.c | 87 +++++++++++++++++++++++++++++--- src/formelementselect.h | 7 ++- src/formelementstring.c | 49 ++++++++++++++++-- src/formelementstring.h | 7 ++- src/formelementsubmit.c | 79 ++++++++++++++++++++++++++--- src/formelementsubmit.h | 7 ++- src/formelementtext.c | 79 ++++++++++++++++++++++++++--- src/formelementtext.h | 7 ++- src/formelementtextarea.c | 79 ++++++++++++++++++++++++++--- src/formelementtextarea.h | 7 ++- src/main.c | 4 +- src/main.h | 2 +- tests/Makefile.am | 4 ++ tests/form.c | 16 +++--- tests/form_xml.c | 101 ++++++++++++++++++++++++++++++++++++++ tests/form_xml.xml | 44 +++++++++++++++++ tests/querystring.c | 4 +- 25 files changed, 797 insertions(+), 74 deletions(-) create mode 100644 tests/form_xml.c create mode 100644 tests/form_xml.xml diff --git a/.gitignore b/.gitignore index 6affd6d..6e5655f 100644 --- a/.gitignore +++ b/.gitignore @@ -53,6 +53,7 @@ Rules-quot tests/cookies tests/env tests/form +tests/form_xml tests/querystring tests/redirect tests/session diff --git a/src/Makefile.am b/src/Makefile.am index 6247716..9a27f2d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,13 @@ +if PLATFORM_WIN32 +libmodulesext = -0.dll +libmodulesdir = $(bindir) +else +libmodulesext = .so +libmodulesdir = $(libdir) +endif + +libzakformmodulesdir = `pkg-config --variable=modulesdir libzakform` + LIBS = $(ZAKCGI_LIBS) AM_CPPFLAGS = $(ZAKCGI_CFLAGS) \ @@ -42,3 +52,10 @@ libzakcgi_include_HEADERS = \ url.h libzakcgi_includedir = $(includedir)/libzakcgi + +install-exec-hook: + cd $(libzakformmodulesdir) && \ + ln -s -f $(libmodulesdir)/libzakcgi$(libmodulesext) . + +uninstall-hook: + rm -f $(libzakformmodulesdir)/libzakcgi$(libmodulesext) diff --git a/src/formelementcheck.c b/src/formelementcheck.c index 21fd3da..c5ed670 100644 --- a/src/formelementcheck.c +++ b/src/formelementcheck.c @@ -80,33 +80,101 @@ zak_cgi_form_element_check_init (ZakCgiFormElementCheck *zak_cgi_form_element_ch /** * zak_cgi_form_element_check_new: + * + * Returns: the newly created #ZakCgiFormElementCheck object. + */ +ZakCgiFormElement +*zak_cgi_form_element_check_new () +{ + ZakCgiFormElementCheck *zak_cgi_form_element_check; + + zak_cgi_form_element_check = ZAK_CGI_FORM_ELEMENT_CHECK (g_object_new (zak_cgi_form_element_check_get_type (), NULL)); + + return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_check); +} + +/** + * zak_cgi_form_element_check_new_attrs: * @id: * @...: * * Returns: the newly created #ZakCgiFormElementCheck object. */ ZakCgiFormElement -*zak_cgi_form_element_check_new (const gchar *id, - ...) +*zak_cgi_form_element_check_new_attrs (const gchar *id, + ...) { va_list ap; GHashTable *ht_attrs; - ZakCgiFormElementCheck *zak_cgi_form_element_check; + ZakCgiFormElement *zak_cgi_form_element_check; - zak_cgi_form_element_check = ZAK_CGI_FORM_ELEMENT_CHECK (g_object_new (zak_cgi_form_element_check_get_type (), NULL)); + zak_cgi_form_element_check = zak_cgi_form_element_check_new (); va_start (ap, id); ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap); g_hash_table_replace (ht_attrs, "type", "checkbox"); - ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_check_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_check), - id, - ht_attrs); + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_check_parent_class)->construct (zak_cgi_form_element_check, + id, + ht_attrs); - return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_check); + return zak_cgi_form_element_check; +} + +gboolean +zak_cgi_form_element_check_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode) +{ + gboolean ret; + + gchar *id; + + GHashTable *ht_attrs; + + xmlNode *cur; + + id = NULL; + + ht_attrs = g_hash_table_new (g_str_hash, g_str_equal); + g_hash_table_replace (ht_attrs, "type", "checkbox"); + + cur = xmlnode->children; + while (cur != NULL) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"id") == 0) + { + id = (gchar *)xmlNodeGetContent (cur); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"label") == 0) + { + zak_cgi_form_element_set_label (ZAK_CGI_FORM_ELEMENT (element), (gchar *)xmlNodeGetContent (cur), NULL); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"text") == 0) + { + } + else + { + g_hash_table_replace (ht_attrs, g_strdup (cur->name), (gchar *)xmlNodeGetContent (cur)); + } + + cur = cur->next; + } + + if (id != NULL) + { + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_check_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (element), + id, + ht_attrs); + ret = TRUE; + } + else + { + ret = FALSE; + } + + return ret; } static gchar diff --git a/src/formelementcheck.h b/src/formelementcheck.h index 1d56774..6fb9ede 100644 --- a/src/formelementcheck.h +++ b/src/formelementcheck.h @@ -22,6 +22,8 @@ #include +#include + #include "formelement.h" @@ -51,7 +53,10 @@ struct _ZakCgiFormElementCheckClass GType zak_cgi_form_element_check_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_check_new (const gchar *id, ...); +ZakCgiFormElement *zak_cgi_form_element_check_new (void); +ZakCgiFormElement *zak_cgi_form_element_check_new_attrs (const gchar *id, ...); + +gboolean zak_cgi_form_element_check_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode); G_END_DECLS diff --git a/src/formelementhidden.c b/src/formelementhidden.c index f612eb9..abf0ce0 100644 --- a/src/formelementhidden.c +++ b/src/formelementhidden.c @@ -78,33 +78,98 @@ zak_cgi_form_element_hidden_init (ZakCgiFormElementHidden *zak_cgi_form_element_ /** * zak_cgi_form_element_hidden_new: + * + * Returns: the newly created #ZakCgiFormElementHidden object. + */ +ZakCgiFormElement +*zak_cgi_form_element_hidden_new () +{ + ZakCgiFormElementHidden *zak_cgi_form_element_hidden; + + zak_cgi_form_element_hidden = ZAK_CGI_FORM_ELEMENT_HIDDEN (g_object_new (zak_cgi_form_element_hidden_get_type (), NULL)); + + return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_hidden); +} + +/** + * zak_cgi_form_element_hidden_new_attrs: * @id: * @...: * * Returns: the newly created #ZakCgiFormElementHidden object. */ ZakCgiFormElement -*zak_cgi_form_element_hidden_new (const gchar *id, - ...) +*zak_cgi_form_element_hidden_new_attrs (const gchar *id, + ...) { va_list ap; GHashTable *ht_attrs; - ZakCgiFormElementHidden *zak_cgi_form_element_hidden; + ZakCgiFormElement *zak_cgi_form_element_hidden; - zak_cgi_form_element_hidden = ZAK_CGI_FORM_ELEMENT_HIDDEN (g_object_new (zak_cgi_form_element_hidden_get_type (), NULL)); + zak_cgi_form_element_hidden = zak_cgi_form_element_hidden_new (); va_start (ap, id); ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap); g_hash_table_replace (ht_attrs, "type", "hidden"); - ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_hidden_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_hidden), - id, - ht_attrs); + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_hidden_parent_class)->construct (zak_cgi_form_element_hidden, + id, + ht_attrs); - return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_hidden); + return zak_cgi_form_element_hidden; +} + + +gboolean +zak_cgi_form_element_hidden_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode) +{ + gboolean ret; + + gchar *id; + + GHashTable *ht_attrs; + + xmlNode *cur; + + id = NULL; + + ht_attrs = g_hash_table_new (g_str_hash, g_str_equal); + g_hash_table_replace (ht_attrs, "type", "hidden"); + + cur = xmlnode->children; + while (cur != NULL) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"id") == 0) + { + id = (gchar *)xmlNodeGetContent (cur); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"text") == 0) + { + } + else + { + g_hash_table_replace (ht_attrs, g_strdup (cur->name), (gchar *)xmlNodeGetContent (cur)); + } + + cur = cur->next; + } + + if (id != NULL) + { + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_hidden_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (element), + id, + ht_attrs); + ret = TRUE; + } + else + { + ret = FALSE; + } + + return ret; } static gchar diff --git a/src/formelementhidden.h b/src/formelementhidden.h index 4f9b3de..0ead29e 100644 --- a/src/formelementhidden.h +++ b/src/formelementhidden.h @@ -22,6 +22,8 @@ #include +#include + #include "formelement.h" @@ -51,7 +53,10 @@ struct _ZakCgiFormElementHiddenClass GType zak_cgi_form_element_hidden_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_hidden_new (const gchar *id, ...); +ZakCgiFormElement *zak_cgi_form_element_hidden_new (void); +ZakCgiFormElement *zak_cgi_form_element_hidden_new_attrs (const gchar *id, ...); + +gboolean zak_cgi_form_element_hidden_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode); G_END_DECLS diff --git a/src/formelementpassword.c b/src/formelementpassword.c index 1797db4..bf06644 100644 --- a/src/formelementpassword.c +++ b/src/formelementpassword.c @@ -78,33 +78,101 @@ zak_cgi_form_element_password_init (ZakCgiFormElementPassword *zak_cgi_form_elem /** * zak_cgi_form_element_password_new: + * + * Returns: the newly created #ZakCgiFormElementPassword object. + */ +ZakCgiFormElement +*zak_cgi_form_element_password_new () +{ + ZakCgiFormElementPassword *zak_cgi_form_element_password; + + zak_cgi_form_element_password = ZAK_CGI_FORM_ELEMENT_PASSWORD (g_object_new (zak_cgi_form_element_password_get_type (), NULL)); + + return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_password); +} + +/** + * zak_cgi_form_element_password_new_attrs: * @id: * @...: * * Returns: the newly created #ZakCgiFormElementPassword object. */ ZakCgiFormElement -*zak_cgi_form_element_password_new (const gchar *id, - ...) +*zak_cgi_form_element_password_new_attrs (const gchar *id, + ...) { va_list ap; GHashTable *ht_attrs; - ZakCgiFormElementPassword *zak_cgi_form_element_password; + ZakCgiFormElement *zak_cgi_form_element_password; - zak_cgi_form_element_password = ZAK_CGI_FORM_ELEMENT_PASSWORD (g_object_new (zak_cgi_form_element_password_get_type (), NULL)); + zak_cgi_form_element_password = zak_cgi_form_element_password_new (); va_start (ap, id); ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap); g_hash_table_replace (ht_attrs, "type", "password"); - ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_password_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_password), - id, - ht_attrs); + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_password_parent_class)->construct (zak_cgi_form_element_password, + id, + ht_attrs); - return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_password); + return zak_cgi_form_element_password; +} + +gboolean +zak_cgi_form_element_password_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode) +{ + gboolean ret; + + gchar *id; + + GHashTable *ht_attrs; + + xmlNode *cur; + + id = NULL; + + ht_attrs = g_hash_table_new (g_str_hash, g_str_equal); + g_hash_table_replace (ht_attrs, "type", "password"); + + cur = xmlnode->children; + while (cur != NULL) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"id") == 0) + { + id = (gchar *)xmlNodeGetContent (cur); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"label") == 0) + { + zak_cgi_form_element_set_label (ZAK_CGI_FORM_ELEMENT (element), (gchar *)xmlNodeGetContent (cur), NULL); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"text") == 0) + { + } + else + { + g_hash_table_replace (ht_attrs, g_strdup (cur->name), (gchar *)xmlNodeGetContent (cur)); + } + + cur = cur->next; + } + + if (id != NULL) + { + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_password_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (element), + id, + ht_attrs); + ret = TRUE; + } + else + { + ret = FALSE; + } + + return ret; } static gchar diff --git a/src/formelementpassword.h b/src/formelementpassword.h index dcba599..4b38eff 100644 --- a/src/formelementpassword.h +++ b/src/formelementpassword.h @@ -22,6 +22,8 @@ #include +#include + #include "formelement.h" @@ -51,7 +53,10 @@ struct _ZakCgiFormElementPasswordClass GType zak_cgi_form_element_password_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_password_new (const gchar *id, ...); +ZakCgiFormElement *zak_cgi_form_element_password_new (); +ZakCgiFormElement *zak_cgi_form_element_password_new_attrs (const gchar *id, ...); + +gboolean zak_cgi_form_element_password_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode); G_END_DECLS diff --git a/src/formelementselect.c b/src/formelementselect.c index 6df66a3..4af569a 100644 --- a/src/formelementselect.c +++ b/src/formelementselect.c @@ -79,28 +79,43 @@ zak_cgi_form_element_select_init (ZakCgiFormElementSelect *zak_cgi_form_element_ /** * zak_cgi_form_element_select_new: + * + * Returns: the newly created #ZakCgiFormElementSelect object. + */ +ZakCgiFormElement +*zak_cgi_form_element_select_new () +{ + ZakCgiFormElementSelect *zak_cgi_form_element_select; + + zak_cgi_form_element_select = ZAK_CGI_FORM_ELEMENT_SELECT (g_object_new (zak_cgi_form_element_select_get_type (), NULL)); + + return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_select); +} + +/** + * zak_cgi_form_element_select_new_attrs: * @id: * @...: * * Returns: the newly created #ZakCgiFormElementSelect object. */ ZakCgiFormElement -*zak_cgi_form_element_select_new (const gchar *id, - ...) +*zak_cgi_form_element_select_new_attrs (const gchar *id, + ...) { va_list ap; - ZakCgiFormElementSelect *zak_cgi_form_element_select; + ZakCgiFormElement *zak_cgi_form_element_select; - zak_cgi_form_element_select = ZAK_CGI_FORM_ELEMENT_SELECT (g_object_new (zak_cgi_form_element_select_get_type (), NULL)); + zak_cgi_form_element_select = zak_cgi_form_element_select_new (); va_start (ap, id); - ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_select_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_select), - id, - zak_cgi_commons_valist_to_ghashtable (ap)); + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_select_parent_class)->construct (zak_cgi_form_element_select, + id, + zak_cgi_commons_valist_to_ghashtable (ap)); - return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_select); + return zak_cgi_form_element_select; } /** @@ -133,6 +148,62 @@ zak_cgi_form_element_select_add_option (ZakCgiFormElementSelect *element, g_hash_table_replace (priv->ht_options, g_strdup (value), ht_attrs); } +gboolean +zak_cgi_form_element_select_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode) +{ + gboolean ret; + + gchar *id; + + GHashTable *ht_attrs; + + xmlNode *cur; + + id = NULL; + + ht_attrs = g_hash_table_new (g_str_hash, g_str_equal); + + cur = xmlnode->children; + while (cur != NULL) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"id") == 0) + { + id = (gchar *)xmlNodeGetContent (cur); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"label") == 0) + { + zak_cgi_form_element_set_label (ZAK_CGI_FORM_ELEMENT (element), (gchar *)xmlNodeGetContent (cur), NULL); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"option") == 0) + { + zak_cgi_form_element_select_add_option (ZAK_CGI_FORM_ELEMENT_SELECT (element), (gchar *)xmlGetProp (cur, (xmlChar *)"id"), (gchar *)xmlNodeGetContent (cur), NULL); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"text") == 0) + { + } + else + { + g_hash_table_replace (ht_attrs, g_strdup (cur->name), (gchar *)xmlNodeGetContent (cur)); + } + + cur = cur->next; + } + + if (id != NULL) + { + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_select_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (element), + id, + ht_attrs); + ret = TRUE; + } + else + { + ret = FALSE; + } + + return ret; +} + static gchar *zak_cgi_form_element_select_render (ZakCgiFormElement *element) { diff --git a/src/formelementselect.h b/src/formelementselect.h index fdbc453..23882ef 100644 --- a/src/formelementselect.h +++ b/src/formelementselect.h @@ -22,6 +22,8 @@ #include +#include + #include "formelement.h" @@ -51,10 +53,13 @@ struct _ZakCgiFormElementSelectClass GType zak_cgi_form_element_select_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_select_new (const gchar *id, ...); +ZakCgiFormElement *zak_cgi_form_element_select_new (void); +ZakCgiFormElement *zak_cgi_form_element_select_new_attrs (const gchar *id, ...); void zak_cgi_form_element_select_add_option (ZakCgiFormElementSelect *element, const gchar *value, const gchar *content, ...); +gboolean zak_cgi_form_element_select_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode); + G_END_DECLS diff --git a/src/formelementstring.c b/src/formelementstring.c index 05ef4e4..50ca759 100644 --- a/src/formelementstring.c +++ b/src/formelementstring.c @@ -82,22 +82,65 @@ zak_cgi_form_element_string_init (ZakCgiFormElementString *zak_cgi_form_element_ /** * zak_cgi_form_element_string_new: - * @str: * * Returns: the newly created #ZakCgiFormElementString object. */ ZakCgiFormElement -*zak_cgi_form_element_string_new (const gchar *str) +*zak_cgi_form_element_string_new () { ZakCgiFormElementString *zak_cgi_form_element_string; zak_cgi_form_element_string = ZAK_CGI_FORM_ELEMENT_STRING (g_object_new (zak_cgi_form_element_string_get_type (), NULL)); + return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_string); +} + +/** + * zak_cgi_form_element_string_new_attrs: + * @str: + * + * Returns: the newly created #ZakCgiFormElementString object. + */ +ZakCgiFormElement +*zak_cgi_form_element_string_new_attrs (const gchar *str) +{ + ZakCgiFormElement *zak_cgi_form_element_string; + + zak_cgi_form_element_string = zak_cgi_form_element_string_new (); + ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string); priv->str = g_strdup (str); - return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_string); + return zak_cgi_form_element_string; +} + +gboolean +zak_cgi_form_element_string_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode) +{ + gboolean ret; + + xmlNode *cur; + + ZakCgiFormElementStringPrivate *priv; + + ret = FALSE; + + cur = xmlnode->children; + while (cur != NULL) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"string") == 0) + { + priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (ZAK_CGI_FORM_ELEMENT_STRING (element)); + priv->str = g_strdup ((gchar *)xmlNodeGetContent (cur)); + ret = TRUE; + break; + } + + cur = cur->next; + } + + return ret; } static gchar diff --git a/src/formelementstring.h b/src/formelementstring.h index a9db7c3..884b52b 100644 --- a/src/formelementstring.h +++ b/src/formelementstring.h @@ -22,6 +22,8 @@ #include +#include + #include "formelement.h" @@ -51,7 +53,10 @@ struct _ZakCgiFormElementStringClass GType zak_cgi_form_element_string_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_string_new (const gchar *str); +ZakCgiFormElement *zak_cgi_form_element_string_new (void); +ZakCgiFormElement *zak_cgi_form_element_string_new_attrs (const gchar *str); + +gboolean zak_cgi_form_element_string_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode); G_END_DECLS diff --git a/src/formelementsubmit.c b/src/formelementsubmit.c index 0a80ab0..6cc0d3f 100644 --- a/src/formelementsubmit.c +++ b/src/formelementsubmit.c @@ -78,28 +78,95 @@ zak_cgi_form_element_submit_init (ZakCgiFormElementSubmit *zak_cgi_form_element_ /** * zak_cgi_form_element_submit_new: + * + * Returns: the newly created #ZakCgiFormElementSubmit object. + */ +ZakCgiFormElement +*zak_cgi_form_element_submit_new () +{ + ZakCgiFormElementSubmit *zak_cgi_form_element_submit; + + zak_cgi_form_element_submit = ZAK_CGI_FORM_ELEMENT_SUBMIT (g_object_new (zak_cgi_form_element_submit_get_type (), NULL)); + + return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_submit); +} + +/** + * zak_cgi_form_element_submit_new_attrs: * @id: * @...: * * Returns: the newly created #ZakCgiFormElementSubmit object. */ ZakCgiFormElement -*zak_cgi_form_element_submit_new (const gchar *id, - ...) +*zak_cgi_form_element_submit_new_attrs (const gchar *id, + ...) { va_list ap; - ZakCgiFormElementSubmit *zak_cgi_form_element_submit; + ZakCgiFormElement *zak_cgi_form_element_submit; - zak_cgi_form_element_submit = ZAK_CGI_FORM_ELEMENT_SUBMIT (g_object_new (zak_cgi_form_element_submit_get_type (), NULL)); + zak_cgi_form_element_submit = zak_cgi_form_element_submit_new (); va_start (ap, id); - ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_submit_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_submit), + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_submit_parent_class)->construct (zak_cgi_form_element_submit, id, zak_cgi_commons_valist_to_ghashtable (ap)); - return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_submit); + return zak_cgi_form_element_submit; +} + +gboolean +zak_cgi_form_element_submit_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode) +{ + gboolean ret; + + gchar *id; + + GHashTable *ht_attrs; + + xmlNode *cur; + + id = NULL; + + ht_attrs = g_hash_table_new (g_str_hash, g_str_equal); + + cur = xmlnode->children; + while (cur != NULL) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"id") == 0) + { + id = (gchar *)xmlNodeGetContent (cur); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"label") == 0) + { + zak_cgi_form_element_set_label (ZAK_CGI_FORM_ELEMENT (element), (gchar *)xmlNodeGetContent (cur), NULL); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"text") == 0) + { + } + else + { + g_hash_table_replace (ht_attrs, g_strdup (cur->name), (gchar *)xmlNodeGetContent (cur)); + } + + cur = cur->next; + } + + if (id != NULL) + { + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_submit_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (element), + id, + ht_attrs); + ret = TRUE; + } + else + { + ret = FALSE; + } + + return ret; } static gchar diff --git a/src/formelementsubmit.h b/src/formelementsubmit.h index 8f71b15..73be758 100644 --- a/src/formelementsubmit.h +++ b/src/formelementsubmit.h @@ -22,6 +22,8 @@ #include +#include + #include "formelement.h" @@ -51,7 +53,10 @@ struct _ZakCgiFormElementSubmitClass GType zak_cgi_form_element_submit_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_submit_new (const gchar *id, ...); +ZakCgiFormElement *zak_cgi_form_element_submit_new (void); +ZakCgiFormElement *zak_cgi_form_element_submit_new_attrs (const gchar *id, ...); + +gboolean zak_cgi_form_element_submit_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode); G_END_DECLS diff --git a/src/formelementtext.c b/src/formelementtext.c index 8be6123..425df8b 100644 --- a/src/formelementtext.c +++ b/src/formelementtext.c @@ -78,28 +78,95 @@ zak_cgi_form_element_text_init (ZakCgiFormElementText *zak_cgi_form_element_text /** * zak_cgi_form_element_text_new: + * + * Returns: the newly created #ZakCgiFormElementText object. + */ +ZakCgiFormElement +*zak_cgi_form_element_text_new () +{ + ZakCgiFormElementText *zak_cgi_form_element_text; + + zak_cgi_form_element_text = ZAK_CGI_FORM_ELEMENT_TEXT (g_object_new (zak_cgi_form_element_text_get_type (), NULL)); + + return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text); +} + +/** + * zak_cgi_form_element_text_new_attrs: * @id: * @...: * * Returns: the newly created #ZakCgiFormElementText object. */ ZakCgiFormElement -*zak_cgi_form_element_text_new (const gchar *id, - ...) +*zak_cgi_form_element_text_new_attrs (const gchar *id, + ...) { va_list ap; - ZakCgiFormElementText *zak_cgi_form_element_text; + ZakCgiFormElement *zak_cgi_form_element_text; - zak_cgi_form_element_text = ZAK_CGI_FORM_ELEMENT_TEXT (g_object_new (zak_cgi_form_element_text_get_type (), NULL)); + zak_cgi_form_element_text = zak_cgi_form_element_text_new (); va_start (ap, id); - ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text), + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_parent_class)->construct (zak_cgi_form_element_text, id, zak_cgi_commons_valist_to_ghashtable (ap)); - return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text); + return zak_cgi_form_element_text; +} + +gboolean +zak_cgi_form_element_text_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode) +{ + gboolean ret; + + gchar *id; + + GHashTable *ht_attrs; + + xmlNode *cur; + + id = NULL; + + ht_attrs = g_hash_table_new (g_str_hash, g_str_equal); + + cur = xmlnode->children; + while (cur != NULL) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"id") == 0) + { + id = (gchar *)xmlNodeGetContent (cur); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"label") == 0) + { + zak_cgi_form_element_set_label (ZAK_CGI_FORM_ELEMENT (element), (gchar *)xmlNodeGetContent (cur), NULL); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"text") == 0) + { + } + else + { + g_hash_table_replace (ht_attrs, g_strdup (cur->name), (gchar *)xmlNodeGetContent (cur)); + } + + cur = cur->next; + } + + if (id != NULL) + { + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (element), + id, + ht_attrs); + ret = TRUE; + } + else + { + ret = FALSE; + } + + return ret; } static gchar diff --git a/src/formelementtext.h b/src/formelementtext.h index 5abdfb6..157e4de 100644 --- a/src/formelementtext.h +++ b/src/formelementtext.h @@ -22,6 +22,8 @@ #include +#include + #include "formelement.h" @@ -51,7 +53,10 @@ struct _ZakCgiFormElementTextClass GType zak_cgi_form_element_text_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_text_new (const gchar *id, ...); +ZakCgiFormElement *zak_cgi_form_element_text_new (void); +ZakCgiFormElement *zak_cgi_form_element_text_new_attrs (const gchar *id, ...); + +gboolean zak_cgi_form_element_text_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode); G_END_DECLS diff --git a/src/formelementtextarea.c b/src/formelementtextarea.c index 49e28e2..f62a8db 100644 --- a/src/formelementtextarea.c +++ b/src/formelementtextarea.c @@ -78,28 +78,95 @@ zak_cgi_form_element_text_area_init (ZakCgiFormElementTextArea *zak_cgi_form_ele /** * zak_cgi_form_element_text_area_new: + * + * Returns: the newly created #ZakCgiFormElementTextArea object. + */ +ZakCgiFormElement +*zak_cgi_form_element_text_area_new () +{ + ZakCgiFormElementTextArea *zak_cgi_form_element_text_area; + + zak_cgi_form_element_text_area = ZAK_CGI_FORM_ELEMENT_TEXT_AREA (g_object_new (zak_cgi_form_element_text_area_get_type (), NULL)); + + return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text_area); +} + +/** + * zak_cgi_form_element_text_area_new_attrs: * @id: * @...: * * Returns: the newly created #ZakCgiFormElementTextArea object. */ ZakCgiFormElement -*zak_cgi_form_element_text_area_new (const gchar *id, - ...) +*zak_cgi_form_element_text_area_new_attrs (const gchar *id, + ...) { va_list ap; - ZakCgiFormElementTextArea *zak_cgi_form_element_text_area; + ZakCgiFormElement *zak_cgi_form_element_text_area; - zak_cgi_form_element_text_area = ZAK_CGI_FORM_ELEMENT_TEXT_AREA (g_object_new (zak_cgi_form_element_text_area_get_type (), NULL)); + zak_cgi_form_element_text_area = zak_cgi_form_element_text_area_new (); va_start (ap, id); - ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_area_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text_area), + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_area_parent_class)->construct (zak_cgi_form_element_text_area, id, zak_cgi_commons_valist_to_ghashtable (ap)); - return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text_area); + return zak_cgi_form_element_text_area; +} + +gboolean +zak_cgi_form_element_text_area_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode) +{ + gboolean ret; + + gchar *id; + + GHashTable *ht_attrs; + + xmlNode *cur; + + id = NULL; + + ht_attrs = g_hash_table_new (g_str_hash, g_str_equal); + + cur = xmlnode->children; + while (cur != NULL) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"id") == 0) + { + id = (gchar *)xmlNodeGetContent (cur); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"label") == 0) + { + zak_cgi_form_element_set_label (ZAK_CGI_FORM_ELEMENT (element), (gchar *)xmlNodeGetContent (cur), NULL); + } + else if (xmlStrcmp (cur->name, (const xmlChar *)"text") == 0) + { + } + else + { + g_hash_table_replace (ht_attrs, g_strdup (cur->name), (gchar *)xmlNodeGetContent (cur)); + } + + cur = cur->next; + } + + if (id != NULL) + { + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_area_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (element), + id, + ht_attrs); + ret = TRUE; + } + else + { + ret = FALSE; + } + + return ret; } static gchar diff --git a/src/formelementtextarea.h b/src/formelementtextarea.h index abbf253..308fcd6 100644 --- a/src/formelementtextarea.h +++ b/src/formelementtextarea.h @@ -22,6 +22,8 @@ #include +#include + #include "formelement.h" @@ -51,7 +53,10 @@ struct _ZakCgiFormElementTextAreaClass GType zak_cgi_form_element_text_area_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_text_area_new (const gchar *id, ...); +ZakCgiFormElement *zak_cgi_form_element_text_area_new (void); +ZakCgiFormElement *zak_cgi_form_element_text_area_new_attrs (const gchar *id, ...); + +gboolean zak_cgi_form_element_text_area_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode); G_END_DECLS diff --git a/src/main.c b/src/main.c index 8d64bd9..efa6046 100644 --- a/src/main.c +++ b/src/main.c @@ -650,14 +650,14 @@ GHashTable } /** - * zak_cgi_main_get_param: + * zak_cgi_main_get_parameter: * @zakcgimain: * @param: * * Returns: */ GValue -*zak_cgi_main_get_param (ZakCgiMain *zakcgimain, const gchar *param) +*zak_cgi_main_get_parameter (ZakCgiMain *zakcgimain, const gchar *param) { ZakCgiMainPrivate *priv; diff --git a/src/main.h b/src/main.h index e24b71e..27b6b1c 100644 --- a/src/main.h +++ b/src/main.h @@ -80,7 +80,7 @@ gchar *zak_cgi_main_set_cookie (const gchar *name, G_DEPRECATED_FOR (zak_cgi_main_get_parameter) GHashTable *zak_cgi_main_get_parameters (ZakCgiMain *zakcgimain, const gchar *query_string); -gchar *zak_cgi_main_get_parameter (ZakCgiMain *zakcgimain, const gchar *param); +GValue *zak_cgi_main_get_parameter (ZakCgiMain *zakcgimain, const gchar *param); void zak_cgi_main_parameters_foreach (ZakCgiMain *zakcgimain, GHFunc func, gpointer user_data); diff --git a/tests/Makefile.am b/tests/Makefile.am index 6a2552d..c888c63 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -12,8 +12,12 @@ noinst_PROGRAMS = \ cookies \ env \ form \ + form_xml \ querystring \ redirect \ session \ tag \ url + +EXTRA_DIST = \ + form_xml.xml diff --git a/tests/form.c b/tests/form.c index 2cfad21..09c722a 100644 --- a/tests/form.c +++ b/tests/form.c @@ -62,7 +62,7 @@ main (int argc, char *argv[]) "action", "form", NULL); - element = zak_cgi_form_element_text_new ("first", NULL); + element = zak_cgi_form_element_text_new_attrs ("first", NULL); zak_cgi_form_element_set_label (element, "The Label for first", NULL); zak_form_element_add_filter (ZAK_FORM_ELEMENT (element), ZAK_FORM_ELEMENT_FILTER (zak_form_element_filter_trim_new ())); @@ -70,31 +70,31 @@ main (int argc, char *argv[]) ZAK_FORM_ELEMENT_VALIDATOR (zak_form_element_validator_regex_new ("^aaa$"))); zak_form_form_add_element (ZAK_FORM_FORM (form), ZAK_FORM_ELEMENT (element)); - element = zak_cgi_form_element_check_new ("chk", NULL); + element = zak_cgi_form_element_check_new_attrs ("chk", NULL); zak_cgi_form_element_set_label (element, "The checkbox", NULL); zak_form_form_add_element (ZAK_FORM_FORM (form), ZAK_FORM_ELEMENT (element)); - element = zak_cgi_form_element_string_new ("

big big big

"); + element = zak_cgi_form_element_string_new_attrs ("

big big big

"); zak_form_form_add_element (ZAK_FORM_FORM (form), ZAK_FORM_ELEMENT (element)); - element = zak_cgi_form_element_password_new ("pws", NULL); + element = zak_cgi_form_element_password_new_attrs ("pws", NULL); zak_cgi_form_element_set_label (element, "The password", NULL); zak_form_form_add_element (ZAK_FORM_FORM (form), ZAK_FORM_ELEMENT (element)); - element = zak_cgi_form_element_text_area_new ("txtarea", NULL); + element = zak_cgi_form_element_text_area_new_attrs ("txtarea", NULL); zak_cgi_form_element_set_label (element, "The text area", NULL); zak_form_form_add_element (ZAK_FORM_FORM (form), ZAK_FORM_ELEMENT (element)); - element = zak_cgi_form_element_select_new ("slc", NULL); + element = zak_cgi_form_element_select_new_attrs ("slc", NULL); zak_cgi_form_element_select_add_option (ZAK_CGI_FORM_ELEMENT_SELECT (element), "1", "first", NULL); zak_cgi_form_element_select_add_option (ZAK_CGI_FORM_ELEMENT_SELECT (element), "2", "second", NULL); zak_cgi_form_element_select_add_option (ZAK_CGI_FORM_ELEMENT_SELECT (element), "3", "third", NULL); zak_form_form_add_element (ZAK_FORM_FORM (form), ZAK_FORM_ELEMENT (element)); - element = zak_cgi_form_element_hidden_new ("hdn", NULL); + element = zak_cgi_form_element_hidden_new_attrs ("hdn", NULL); zak_form_form_add_element (ZAK_FORM_FORM (form), ZAK_FORM_ELEMENT (element)); - element = zak_cgi_form_element_submit_new ("submit", "zak-cgi-content", "Submit", NULL); + element = zak_cgi_form_element_submit_new_attrs ("submit", "zak-cgi-content", "Submit", NULL); zak_form_form_add_element (ZAK_FORM_FORM (form), ZAK_FORM_ELEMENT (element)); if (zak_cgi_main_is_post (zakcgimain)) diff --git a/tests/form_xml.c b/tests/form_xml.c new file mode 100644 index 0000000..517c903 --- /dev/null +++ b/tests/form_xml.c @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library 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 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 General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int +main (int argc, char *argv[]) +{ + ZakCgiMain *zakcgimain; + + GString *str; + + ZakCgiForm *form; + ZakCgiFormElement *element; + + zakcgimain = zak_cgi_main_new (); + + str = g_string_new ("\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "Form test\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "
\n"); + + form = zak_cgi_form_new (zakcgimain, + "method", "post", + "action", "form", + NULL); + + GValue *val = zak_cgi_main_get_parameter (zakcgimain, "filename"); + if (zak_form_form_load_from_file (ZAK_FORM_FORM (form), g_value_get_string (val))) + { + if (zak_cgi_main_is_post (zakcgimain)) + { + /* validating the form */ + zak_cgi_form_bind (form); + if (zak_form_form_is_valid (ZAK_FORM_FORM (form))) + { + g_string_append (str, "Form is valid!!!"); + } + else + { + g_string_append (str, zak_cgi_form_render (form)); + g_string_append (str, "Form is not valid!!!"); + } + } + else + { + g_string_append (str, zak_cgi_form_render (form)); + } + } + + g_string_append (str, + "\n
\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + ""); + + zak_cgi_main_out (NULL, str->str); + + return 0; +} diff --git a/tests/form_xml.xml b/tests/form_xml.xml new file mode 100644 index 0000000..34ef91f --- /dev/null +++ b/tests/form_xml.xml @@ -0,0 +1,44 @@ + + + + + first + + + + + chk + + + + + <h1>big big big</h1> + + + + pws + + + + + txtarea + + + + + slc + + + + + + + hdn + + + + submit + Submit + + + diff --git a/tests/querystring.c b/tests/querystring.c index 7316cb1..c1ad2ab 100644 --- a/tests/querystring.c +++ b/tests/querystring.c @@ -64,11 +64,11 @@ main (int argc, char *argv[]) g_string_append_printf (str, "IS GET?%s\n", - zak_cgi_main_is_get (NULL) ? "TRUE" : "FALSE"); + zak_cgi_main_is_get (main) ? "TRUE" : "FALSE"); g_string_append_printf (str, "IS POST?%s\n", - zak_cgi_main_is_post (NULL) ? "TRUE" : "FALSE"); + zak_cgi_main_is_post (main) ? "TRUE" : "FALSE"); zak_cgi_main_parameters_foreach (main, ht_foreach, str); -- 2.49.0