From ba58fa23cc501bd8f4888b491a0c8aea98482ca8 Mon Sep 17 00:00:00 2001 From: Richard Schwarting Date: Tue, 9 Oct 2012 12:17:28 -0400 Subject: [PATCH] Examples: add examples in C, Vala, JS, and Python on how to create and save documents --- examples/c/Makefile | 9 ++ examples/c/bookshelf.xml | 9 ++ examples/c/example.c | 240 ++++++++++++++++++++++++++++++++++ examples/js/bookshelf.xml | 9 ++ examples/js/example.js | 70 ++++++++++ examples/python/bookshelf.xml | 9 ++ examples/python/example.py | 62 +++++++++ examples/vala/Makefile | 9 ++ examples/vala/bookshelf.xml | 9 ++ examples/vala/example.vala | 96 ++++++++++++++ 10 files changed, 522 insertions(+) create mode 100644 examples/c/Makefile create mode 100644 examples/c/bookshelf.xml create mode 100644 examples/c/example.c create mode 100644 examples/js/bookshelf.xml create mode 100755 examples/js/example.js create mode 100644 examples/python/bookshelf.xml create mode 100755 examples/python/example.py create mode 100644 examples/vala/Makefile create mode 100644 examples/vala/bookshelf.xml create mode 100755 examples/vala/example.vala diff --git a/examples/c/Makefile b/examples/c/Makefile new file mode 100644 index 00000000..59a158bd --- /dev/null +++ b/examples/c/Makefile @@ -0,0 +1,9 @@ +CC=gcc +CFLAGS=`pkg-config --cflags gxml gio-2.0` -g -Wall +LDFLAGS=`pkg-config --libs gxml gio-2.0` + +example: example.o + +clean: + rm example *.o + diff --git a/examples/c/bookshelf.xml b/examples/c/bookshelf.xml new file mode 100644 index 00000000..040ca397 --- /dev/null +++ b/examples/c/bookshelf.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/examples/c/example.c b/examples/c/example.c new file mode 100644 index 00000000..a912b232 --- /dev/null +++ b/examples/c/example.c @@ -0,0 +1,240 @@ +#include +#include + +int handle_error (GError *error) { + // TODO: better way to check whether it was set? + if (error != NULL) { + g_error (error->message); + return 1; + } + + return 0; +} + +int create_a_document () { + GError *error = NULL; + GXmlDocument *doc; + GXmlElement *root; + GXmlElement *owner; + GXmlElement *books; + GXmlElement *book; + 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 (&error); + handle_error (error); + + // Add a root node + root = gxml_document_create_element (doc, "Bookshelf", &error); + handle_error (error); + + gxml_dom_node_append_child (GXML_DOM_NODE (doc), GXML_DOM_NODE (root), &error); + handle_error (error); + // if we tried to add a second one, it would fail and GError would be set :) + + // Add an owner node + owner = gxml_document_create_element (doc, "Owner", &error); + handle_error (error); + gxml_dom_node_append_child (GXML_DOM_NODE (root), GXML_DOM_NODE (owner), &error); + handle_error (error); + gxml_element_set_attribute (owner, "fullname", "John Green", &error); + // TODO: need to figure out what sort of errors these would return, + // want the devhelp pages to describe meaningful possible errors + handle_error (error); + + // Add a collection of books + books = gxml_document_create_element (doc, "Books", &error); + handle_error (error); + gxml_dom_node_append_child (GXML_DOM_NODE (root), GXML_DOM_NODE (books), &error); + + for (i = 0; i < sizeof (authors) / sizeof (char*); i++) { + book = gxml_document_create_element (doc, "Book", &error); + handle_error (error); + gxml_element_set_attribute (book, "author", authors[i], &error); + handle_error (error); + gxml_element_set_attribute (book, "title", titles[i], &error); + handle_error (error); + gxml_dom_node_append_child (GXML_DOM_NODE (books), GXML_DOM_NODE (book), &error); + handle_error (error); + } + + // TODO: want a "gxml_dom_node_from_string ()"; make sure document ownership transfer properly + + str = gxml_dom_node_to_string (GXML_DOM_NODE (doc), TRUE, 2); + printf ("create_a_document:\n%s\n", str); + g_free (str); + + g_object_unref (doc); + g_object_unref (root); // TODO: figure out what happens to root if you deallocate doc?! */ + // TODO: perhaps see whether we can make it that all the nodes that are returned are weak references + g_object_unref (owner); + g_object_unref (books); + g_object_unref (book); + /* In util/ there's a small programme that tells you when ownership + is transfered (that is, you are given a return value that you own + and must free. */ + + return 0; +} + +int create_a_document_minimal () { + GXmlDocument *doc; + GXmlElement *root; + GXmlElement *owner; + GXmlElement *books; + GXmlElement *book; + 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 (NULL); + + // Add a root node + root = gxml_document_create_element (doc, "Bookshelf", NULL); + gxml_dom_node_append_child (GXML_DOM_NODE (doc), GXML_DOM_NODE (root), NULL); + // if we tried to add a second one, it would fail and GError would be set :) + + // Add an owner node + owner = gxml_document_create_element (doc, "Owner", NULL); + gxml_dom_node_append_child (GXML_DOM_NODE (root), GXML_DOM_NODE (owner), NULL); + gxml_element_set_attribute (owner, "fullname", "John Green", NULL); + // TODO: need to figure out what sort of errors these would return, + // want the devhelp pages to describe meaningful possible errors + + // Add a collection of books + books = gxml_document_create_element (doc, "Books", NULL); + gxml_dom_node_append_child (GXML_DOM_NODE (root), GXML_DOM_NODE (books), NULL); + for (i = 0; i < sizeof (authors) / sizeof (char*); i++) { + book = gxml_document_create_element (doc, "Book", NULL); + gxml_element_set_attribute (book, "author", authors[i], NULL); + gxml_element_set_attribute (book, "title", titles[i], NULL); + gxml_dom_node_append_child (GXML_DOM_NODE (books), GXML_DOM_NODE (book), NULL); + } + + str = gxml_dom_node_to_string (GXML_DOM_NODE (doc), TRUE, 2); + printf ("create_a_document_minimal:\n%s\n", str); + g_free (str); + + g_object_unref (doc); + g_object_unref (root); + g_object_unref (owner); + g_object_unref (books); + g_object_unref (book); + + // TODO: how do we clean them up? + + return 0; +} + +int create_a_document_from_a_string () { + char *xml; + char *str; + GXmlDocument *doc; + + xml = ""; + + doc = gxml_document_new_from_string (xml, NULL); + + str = gxml_dom_node_to_string (GXML_DOM_NODE (doc), TRUE, 2); + printf ("create_a_document_from_a_string:\n%s\n", str); + g_free (str); + + g_object_unref (doc); + + return 0; +} + +int create_a_document_from_a_file () { + GXmlDocument *doc; + GFile *file; + char *str; + GCancellable *can = g_cancellable_new (); + GError *error = NULL; + + file = g_file_new_for_path ("bookshelf.xml"); + doc = gxml_document_new_from_gfile (file, can, &error); // TODO: want to change this function name to "from file" + handle_error (error); + + str = gxml_dom_node_to_string (GXML_DOM_NODE (doc), TRUE, 2); + printf ("create_a_document_from_a_file:\n%s\n", str); + g_free (str); + + g_object_unref (doc); + // TODO: how do you free a GFile? g_object_unref ()? + + + return 0; +} + +int create_a_document_from_a_path () { + GXmlDocument *doc; + char *str; + + doc = gxml_document_new_from_path ("bookshelf.xml", NULL); + str = gxml_dom_node_to_string (GXML_DOM_NODE (doc), TRUE, 2); + printf ("create_a_document_from_a_file:\n%s\n", str); + g_free (str); + + g_object_unref (doc); + + return 0; +} + +int saving_a_document_to_a_path () { + GXmlDocument *doc; + + doc = gxml_document_new_from_path ("bookshelf.xml", NULL); + + gxml_document_save_to_path (doc, "bookshelf2.xml"); + + g_object_unref (doc); + + return 0; +} + +int saving_a_document_to_a_stream () { + GXmlDocument *doc; + GFile *outfile; + GFileOutputStream *outstream; + GCancellable *can = g_cancellable_new (); + GError *error = NULL; + + doc = gxml_document_new_from_path ("bookshelf.xml", &error); + + outfile = g_file_new_for_path ("bookshelf3.xml"); + outstream = g_file_replace (outfile, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, can, &error); + gxml_document_save_to_stream (doc, G_OUTPUT_STREAM (outstream), can, &error); + + g_object_unref (doc); + // TODO: figure out how to close/unref outstream and outfile + + return 0; +} + +int main (void) { + g_type_init (); + + create_a_document (); // has token error checking + create_a_document_minimal (); + create_a_document_from_a_string (); + create_a_document_from_a_file (); + create_a_document_from_a_path (); + + saving_a_document_to_a_path (); + saving_a_document_to_a_stream (); + + /* TODO: add these examples */ + // find_nodes_of_given_tag (); + // find_nodes_matching_an_attribute (); + + // accessing_attributes (); + + // find_ + + return 0; +} diff --git a/examples/js/bookshelf.xml b/examples/js/bookshelf.xml new file mode 100644 index 00000000..040ca397 --- /dev/null +++ b/examples/js/bookshelf.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/examples/js/example.js b/examples/js/example.js new file mode 100755 index 00000000..e03ac2c6 --- /dev/null +++ b/examples/js/example.js @@ -0,0 +1,70 @@ +#!/usr/bin/gjs + +const GXml = imports.gi.GXml; +const Gio = imports.gi.Gio; + +function create_a_document () { + var authors = [ "John Green", "Jane Austen", "J.D. Salinger" ]; + var titles = [ "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" ]; + + try { + let doc = GXml.Document.new (); + let root = doc.create_element ("Bookshelf"); + doc.append_child (root); + let owner = doc.create_element ("Owner"); + root.append_child (owner); + owner.set_attribute ("fullname", "John Green"); + + let books = doc.create_element ("Books"); + root.append_child (books); + + for (var i = 0; i < authors.length; i++) { + let book = doc.create_element ("Book"); + book.set_attribute ("author", authors[i]); + book.set_attribute ("title", titles[i]); + books.append_child (book); + } + + print ("create_a_document:\n" + doc.to_string (true, 4)); + } catch (error) { + print (error.message); + } +} + +function create_a_document_from_a_string () { + let xml = "\ +\ +\ +\ +\ +\ +\ +\ +"; + let doc = GXml.Document.from_string (xml); + print ("create_a_document_from_a_string:\n" + doc.to_string (true, 4)); + +} + +function create_a_document_from_a_file () { + let file = Gio.File.new_for_path ("bookshelf.xml"); + let can = new Gio.Cancellable (); + let doc = GXml.Document.from_gfile (file, can); + print ("create_a_document_from_a_file:\n" + doc.to_string (true, 4)); +} + +function create_a_document_from_a_path () { + let doc = GXml.Document.from_path ( "bookshelf.xml"); + print ("create_a_document_from_a_path:\n" + doc.to_string (true, 4)); +} + +function saving_a_document_to_a_path () { + let doc = GXml.Document.from_path ("bookshelf.xml"); + doc.save_to_path ("bookshelf2.xml"); +} + +create_a_document (); +create_a_document_from_a_string (); +create_a_document_from_a_file (); +create_a_document_from_a_path (); +saving_a_document_to_a_path (); diff --git a/examples/python/bookshelf.xml b/examples/python/bookshelf.xml new file mode 100644 index 00000000..040ca397 --- /dev/null +++ b/examples/python/bookshelf.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/examples/python/example.py b/examples/python/example.py new file mode 100755 index 00000000..a996a88e --- /dev/null +++ b/examples/python/example.py @@ -0,0 +1,62 @@ +#!/usr/bin/python + +from gi.repository import GXml; +from gi.repository import Gio; + +def create_a_document (): + authors = ["John Green", "Jane Austen", "J.D. Salinger"]; + titles = ["The Fault in Our Stars", "Pride & Prejudice", "Nine Stories"]; + + doc = GXml.Document.new (); + root = doc.create_element ("Bookshelf"); + doc.append_child (root); + root.set_attribute ("fullname", "John Green"); + + books = doc.create_element ("Books"); + root.append_child (books); + + for i in range (len (authors)): + book = doc.create_element ("Book"); + book.set_attribute ("author", authors[i]); + book.set_attribute ("title", titles[i]); + books.append_child (book); + + print "create_a_document:\n", doc.to_string (True, 4); + if None == doc.to_string (True, 4): + print "ERROR: doc to string was null, but shouldn't be"; + +def create_a_document_from_a_string (): + xml = """ + + + + + + + +"""; + doc = GXml.Document.from_string (xml); + print "create_a_document_from_a_string:\n", doc.to_string (True, 4); + # TODO: why does this not indent when printed? + +def create_a_document_from_a_file (): + can = Gio.Cancellable (); + gfile = Gio.File.new_for_path ("bookshelf.xml"); + doc = GXml.Document.from_gfile (gfile, can); + print "create_a_document_from_a_file:\n", doc.to_string (True, 4); # TODO: do these two values actually do anything? + +def create_a_document_from_a_path (): + doc = GXml.Document.from_path ("bookshelf.xml"); + print "create_a_document_from_a_path:\n", doc.to_string (True, 4); # TODO: do these two values actually do anything? + +def saving_a_document_to_a_path (): + doc = GXml.Document.from_path ("bookshelf.xml"); + doc.save_to_path ("bookshelf2.xml"); + + +create_a_document (); +create_a_document_from_a_string (); +create_a_document_from_a_file (); +create_a_document_from_a_path (); +saving_a_document_to_a_path (); + diff --git a/examples/vala/Makefile b/examples/vala/Makefile new file mode 100644 index 00000000..114d00e5 --- /dev/null +++ b/examples/vala/Makefile @@ -0,0 +1,9 @@ +CC=valac +CFLAGS=--vapidir=../../gxml --pkg gxml -o example + +example: example.vala + $(CC) $(CFLAGS) example.vala + +clean: + rm example + diff --git a/examples/vala/bookshelf.xml b/examples/vala/bookshelf.xml new file mode 100644 index 00000000..040ca397 --- /dev/null +++ b/examples/vala/bookshelf.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/examples/vala/example.vala b/examples/vala/example.vala new file mode 100755 index 00000000..f4aa43f5 --- /dev/null +++ b/examples/vala/example.vala @@ -0,0 +1,96 @@ +using GXml; + +void create_a_document () { + string[] authors = { "John Green", "Jane Austen", "J.D. Salinger" }; + string[] titles = { "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" }; + + try { + Document doc = new Document (); + Element root = doc.create_element ("Bookshelf"); + doc.append_child (root); + Element owner = doc.create_element ("Owner"); + root.append_child (owner); + owner.set_attribute ("fullname", "John Green"); + + Element books = doc.create_element ("Books"); + root.append_child (books); + + for (int i = 0; i < authors.length; i++) { + Element book = doc.create_element ("Book"); + book.set_attribute ("author", authors[i]); + book.set_attribute ("title", titles[i]); + books.append_child (book); + } + + stdout.printf ("create_a_document:\n%s\n", doc.to_string (true, 8)); + } catch (GLib.Error e) { + stderr.printf ("%s\n", e.message); + } +} + +void create_a_document_from_a_string () { + string xml; + Document doc; + + xml = """ + + + + + + + +"""; + + try { + doc = new Document.from_string (xml); + stdout.printf ("create_a_document_from_a_string:\n%s\n", doc.to_string (true, 8)); + } catch (GXml.DomError e) { + stderr.printf ("%s\n", e.message); + } +} + +void create_a_document_from_a_file () { + File f = File.new_for_path ("bookshelf.xml"); + Cancellable can = new Cancellable (); + Document doc; + + try { + doc = new Document.from_gfile (f, can); + stdout.printf ("create_a_document_from_a_file:\n%s\n", doc.to_string (true, 8)); + } catch (DomError e) { + stderr.printf ("%s\n", e.message); + } +} + +void create_a_document_from_a_path () { + Document doc; + + try { + doc = new Document.from_path ("bookshelf.xml"); + stdout.printf ("create_a_document_from_a_path:\n%s\n", doc.to_string (true, 8)); + } catch (DomError e) { + stderr.printf ("%s\n", e.message); + } +} + +void saving_a_document_to_a_path () { + Document doc; + + try { + doc = new Document.from_path ("bookshelf.xml"); + doc.save_to_path ("bookshelf2.xml"); + } catch (DomError e) { + stderr.printf ("%s\n", e.message); + } +} + +int main (string[] args) { + create_a_document (); + create_a_document_from_a_string (); + create_a_document_from_a_file (); + create_a_document_from_a_path (); + saving_a_document_to_a_path (); + + return 0; +} -- GitLab