[gxml] Porting C examples to new API



commit f808846faac37f8c91f5121b9db6b794276471b7
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Apr 24 19:25:54 2017 -0500

    Porting C examples to new API
    
    Partial fix for bug:
    
    https://bugzilla.gnome.org/show_bug.cgi?id=773428

 .gitignore                |    1 -
 examples/c/Makefile.am    |   23 +++++++++++
 examples/c/document_new.c |   94 ++++++++++++++++++++++++++++++++++-----------
 test/Makefile.am          |    2 +-
 4 files changed, 95 insertions(+), 25 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index f3e435d..c0bf44d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,6 @@
 #### Common Files
 *#
 *~
-*.c
 *.o
 *.bak
 *.la
diff --git a/examples/c/Makefile.am b/examples/c/Makefile.am
index 718bd55..d32e467 100644
--- a/examples/c/Makefile.am
+++ b/examples/c/Makefile.am
@@ -1,2 +1,25 @@
 
 EXTRA_DIST = bookshelf.xml Makefile.example document_create.c document_new.c document_new_from_file.c 
document_new_from_path.c document_new_from_string.c document_new_minimal.c document_properties.c 
document_save_to_path.c document_save_to_stream.c node.c comment.c character_data.c text.c node_list.c 
element.c
+
+noinst_PROGRAMS=document_new
+
+document_new_SOURCES= document_new.c
+document_new_CFLAGS= \
+       $(GLIB_CFLAGS) \
+       $(LIBXML_CFLAGS) \
+       $(GIO_CFLAGS) \
+       $(GEE_CFLAGS) \
+       -I$(top_builddir)
+
+document_new_LDFLAGS= \
+       $(GLIB_LDFLAGS) \
+       $(LIBXML_LDFLAGS) \
+       $(GIO_LDFLAGS) \
+       $(GEE_LDFLAGS) \
+       $(top_builddir)/gxml/libgxml-$(API_VERSION).la
+
+document_new_LDADD=\
+       $(GEE_LIBS) \
+       $(GIO_LIBS) \
+       $(GLIB_LIBS) \
+       $(LIBXML_LIBS)
diff --git a/examples/c/document_new.c b/examples/c/document_new.c
index b2cf670..5581969 100644
--- a/examples/c/document_new.c
+++ b/examples/c/document_new.c
@@ -2,49 +2,97 @@
 #include <stdio.h>
 
 int main () {
-  GXmlDocument *doc;
-  GXmlElement *root;
-  GXmlElement *owner;
-  GXmlElement *books;
-  GXmlElement *book;
+  GXmlDomDocument *doc;
+  GXmlDomElement *root;
+  GXmlDomElement *owner;
+  GXmlDomElement *books;
+  GXmlDomElement *book;
+  GError *error = NULL;
   int i;
   char *str;
 
   char *authors[] = { "John Green", "Jane Austen", "J.D. Salinger" };
   char *titles[] = { "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" };
 
-  doc = gxml_document_new ();
+  doc = (GXmlDomDocument*) gxml_gom_document_new ();
 
   // Add a root node
-  root = gxml_document_create_element (doc, "Bookshelf");
+  root = gxml_dom_document_create_element (doc, "Bookshelf", &error);
+  if (error != NULL) {
+    printf ("Error creating root node: %s", error->message);
+    exit (1);
+  }
 
-  gxml_node_append_child (GXML_NODE (doc), GXML_NODE (root));
-  // if we tried to add a second one, it would fail :), and a g_warning would be printed
+  gxml_dom_node_append_child (GXML_DOM_NODE (doc), GXML_DOM_NODE (root), &error);
+  if (error != NULL) {
+    printf ("Error appending root to document: %s", error->message);
+    exit (1);
+  }
 
   // Add an owner node
-  owner = gxml_document_create_element (doc, "Owner");
-  gxml_node_append_child (GXML_NODE (root), GXML_NODE (owner));
-  gxml_element_set_attribute (owner, "fullname", "John Green");
-  // TODO: need to figure out what sort of errors these would return,
-  // want the devhelp pages to describe meaningful possible errors
+  owner = gxml_dom_document_create_element (doc, "Owner", &error);
+  if (error != NULL) {
+    printf ("Error creating a new element node: %s", error->message);
+    exit (1);
+  }
+  gxml_dom_node_append_child (GXML_DOM_NODE (root), GXML_DOM_NODE (owner), &error);
+  if (error != NULL) {
+    printf ("Error appending child to root: %s", error->message);
+    exit (1);
+  }
+  gxml_dom_element_set_attribute (owner, "fullname", "John Green", &error);
+  if (error != NULL) {
+    printf ("Error setting attribute to Owner element node: %s", error->message);
+    exit (1);
+  }
 
   // Add a collection of books
-  books = gxml_document_create_element (doc, "Books");
-  gxml_node_append_child (GXML_NODE (root), GXML_NODE (books));
+  books = gxml_dom_document_create_element (doc, "Books", &error);
+  if (error != NULL) {
+    printf ("Error creating Books element node: %s", error->message);
+    exit (1);
+  }
+  gxml_dom_node_append_child (GXML_DOM_NODE (root), GXML_DOM_NODE (books), &error);
+  if (error != NULL) {
+    printf ("Error appending Books node to root: %s", error->message);
+    exit (1);
+  }
 
   for (i = 0; i < sizeof (authors) / sizeof (char*); i++) {
-    book = gxml_document_create_element (doc, "Book");
-    gxml_element_set_attribute (book, "author", authors[i]);
-    gxml_element_set_attribute (book, "title", titles[i]);
-    gxml_node_append_child (GXML_NODE (books), GXML_NODE (book));
+    book = gxml_dom_document_create_element (doc, "Book", &error);
+    if (error != NULL) {
+      printf ("Error creating %i element node: %s", i, error->message);
+      exit (1);
+    }
+    gxml_dom_element_set_attribute (book, "author", authors[i], &error);
+    if (error != NULL) {
+      printf ("Error setting author attribute to %i element: %s", i, error->message);
+      exit (1);
+    }
+    gxml_dom_element_set_attribute (book, "title", titles[i], &error);
+    if (error != NULL) {
+      printf ("Error setting title attribute to %i eleemnt: %s", i, error->message);
+      exit (1);
+    }
+    gxml_dom_node_append_child (GXML_DOM_NODE (books), GXML_DOM_NODE (book), &error);
+    if (error != NULL) {
+      printf ("Error appending %i child node to Books node: %s", i, error->message);
+      exit (1);
+    }
+    g_object_unref (book);
   }
 
-  // TODO: want a "gxml_node_from_string ()"; make sure document ownership transfer properly
-
-  str = gxml_node_to_string (GXML_NODE (doc), TRUE, 2);
+  str = gxml_gom_element_write_string (GXML_GOM_ELEMENT (root), &error);
+  if (error != NULL) {
+    printf ("Error writing string from root: %s", error->message);
+    exit (1);
+  }
   printf ("%s:\n%s\n", __FILE__, str);
   g_free (str);
 
+  g_object_unref (owner);
+  g_object_unref (books);
+  g_object_unref (root);
   g_object_unref (doc);
 
   return 0;
diff --git a/test/Makefile.am b/test/Makefile.am
index e4383ce..1a37655 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -121,7 +121,7 @@ gxml_test_LDADD = \
        $(GXML_LIBS) \
        $(LIBXML_LIBS) \
        $(GIO_LIBS) \
-       ../gxml/libgxml-0.14.la \
+       $(top_builddir)/gxml/libgxml-0.14.la \
        $(NULL)
 
 gxml_test_LDFLAGS = $(AM_LDFLAGS)


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