[gxml] examples/js: add JavaScript examples



commit bf25fef5e76c7c2e5d72abee8bf77d07a348c1ff
Author: Richard Schwarting <aquarichy gmail com>
Date:   Tue Sep 24 06:27:49 2013 -0400

    examples/js: add JavaScript examples

 examples/js/Makefile.am              |    3 +-
 examples/js/attr.js                  |    1 +
 examples/js/bookshelf2.xml           |    9 +++
 examples/js/bookshelf_node.xml       |    4 ++
 examples/js/cdata_section.js         |    8 +++
 examples/js/character_data.js        |   51 +++++++++++++++++
 examples/js/comment.js               |   54 ++++++++++++++++++
 examples/js/element.js               |   47 ++++++++++++++++
 examples/js/node_child_management.js |   99 ++++++++++++++++++++++++++++++++++
 examples/js/node_list.js             |   69 +++++++++++++++++++++++
 examples/js/node_self.js             |   69 +++++++++++++++++++++++
 examples/js/text.js                  |   36 ++++++++++++
 12 files changed, 449 insertions(+), 1 deletions(-)
---
diff --git a/examples/js/Makefile.am b/examples/js/Makefile.am
index e787fed..0dc83c1 100644
--- a/examples/js/Makefile.am
+++ b/examples/js/Makefile.am
@@ -1,2 +1,3 @@
 
-EXTRA_DIST = bookshelf.xml document_create.js document_new_from_file.js document_new_from_path.js 
document_new_from_string.js document_new.js document_properties.js document_save_to_path.js node.js output
+EXTRA_DIST = bookshelf.xml bookshelf_node.xml bookshelf2.xml \
+   attr.js cdata_section.js character_data.js comment.js document_create.js document_new_from_file.js 
document_new_from_path.js document_new_from_string.js document_new.js document_properties.js 
document_save_to_path.js element.js node_child_management.js node_list.js node_self.js text.js  output
diff --git a/examples/js/attr.js b/examples/js/attr.js
new file mode 100755
index 0000000..2499028
--- /dev/null
+++ b/examples/js/attr.js
@@ -0,0 +1 @@
+/* To see how Attr nodes are used, check the Element example */
diff --git a/examples/js/bookshelf2.xml b/examples/js/bookshelf2.xml
new file mode 100644
index 0000000..040ca39
--- /dev/null
+++ b/examples/js/bookshelf2.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<Bookshelf>
+  <Owner fullname="John Green"/>
+  <Books>
+    <Book author="John Green" title="The Fault in Our Stars"/>
+    <Book author="Jane Austen" title="Pride &amp; Prejudice"/>
+    <Book author="J.D. Salinger" title="Nine Stories"/>
+  </Books>
+</Bookshelf>
diff --git a/examples/js/bookshelf_node.xml b/examples/js/bookshelf_node.xml
new file mode 100644
index 0000000..d657562
--- /dev/null
+++ b/examples/js/bookshelf_node.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+<Bookshelf><Owner fullname="John Green" /><Books><Book author="John Green" title="The Fault in Our Stars" 
/></Books></Bookshelf>
+    
+    
diff --git a/examples/js/cdata_section.js b/examples/js/cdata_section.js
new file mode 100755
index 0000000..4868953
--- /dev/null
+++ b/examples/js/cdata_section.js
@@ -0,0 +1,8 @@
+#!/usr/bin/gjs
+
+const GXml = imports.gi.GXml;
+
+/* CDATASections are a type of Text node, which are CharacterData
+   nodes.  To see more of what you can do with a CDATASection, view
+   those examples */
+
diff --git a/examples/js/character_data.js b/examples/js/character_data.js
new file mode 100755
index 0000000..ff09f9a
--- /dev/null
+++ b/examples/js/character_data.js
@@ -0,0 +1,51 @@
+#!/usr/bin/gjs
+
+const GXml = imports.gi.GXml;
+
+print ("CharacterData isn't directly instantiable, but its subclasses Text and Comment are!\n");
+
+// Setup
+var doc = GXml.Document.from_path ("bookshelf_node.xml");
+var books_list = doc.document_element.get_elements_by_tag_name ("Book");
+var book = books_list.nth (0);
+
+// Create a Text node for CharacterData
+var text = doc.create_text_node ("Some infinities are bigger than other infinities");
+var comment = doc.create_comment ("As he read, I fell in love the way you fall asleep:");
+
+/* Data representation */
+
+print ("Stringifying Text:\n" +
+       text.to_string (true, 0) + "\n\n" +
+       "Text's CharacterData's data:\n" +
+       text.data + "\n\n" +
+       "Text's CharacterData's length:\n" +
+       text.length + "\n\n" +
+       "Stringifying Comment:\n" +
+       comment.to_string (true, 0) + "\n\n" +
+       "Comment's CharacterData's data:\n" +
+       comment.data + "\n\n" +
+       "Comment's CharacterData's length:\n" +
+       comment.length + "\n\n");
+
+/* CharacterData operations */
+
+book.append_child (text);
+print ("Book with our Text as its child (its content):\n" + book.to_string (true, 0));
+
+book.replace_child (comment, text);
+print ("\nBook with our Comment as its child (its content):\n" + book.to_string (true, 0));
+
+comment.append_data (" slowly, and then all at once");
+print ("\nBook after appending more data to Comment:\n" + book.to_string (true, 0));
+
+comment.replace_data (3, 2, "Gus");
+print ("\nBook after replacing 'he' with 'Gus':\n" + book.to_string (true, 0));
+
+comment.delete_data (20, 8);
+print ("\nBook after deleting 'in love ':\n" + book.to_string (true, 0));
+
+comment.insert_data (20, "in love ");
+print ("\nBook after inserting 'in love ':\n" + book.to_string (true, 0));
+
+/* You can see some more examples by looking at Comment and Text's examples */
diff --git a/examples/js/comment.js b/examples/js/comment.js
new file mode 100755
index 0000000..b8ef43b
--- /dev/null
+++ b/examples/js/comment.js
@@ -0,0 +1,54 @@
+#!/usr/bin/gjs
+
+const GXml = imports.gi.GXml;
+
+/* Setup */
+var doc = GXml.Document.from_path ("bookshelf_node.xml");
+var bookshelf = doc.document_element;
+print ("Bookshelf, without any comments:\n" +
+       bookshelf.to_string (true, 0));
+
+/* Placing comments in the tree */
+
+var comment = doc.create_comment ("this bookshelf is small");
+doc.insert_before (comment, bookshelf);
+print ("\nAfter trying to insert a comment before our root element:\n" +
+       doc.to_string (true, 0));
+
+comment = doc.create_comment ("its owner is an important author");
+var owner = bookshelf.first_child;
+bookshelf.insert_before (comment, owner);
+print ("\nAfter inserting a comment before our <owner>:\n" +
+       bookshelf.to_string (true, 0));
+
+comment = doc.create_comment ("I should buy more books");
+var books = owner.next_sibling;
+books.append_child (comment);
+print ("\nAfter appending a comment in <books>:\n" +
+       bookshelf.to_string (true, 0));
+
+var book = books.first_child;
+comment = doc.create_comment ("this pretty cool book will soon be a pretty cool movie");
+book.append_child (comment);
+print ("\nAfter adding a comment to an empty <book> node:\n" +
+       bookshelf.to_string (true, 0));
+
+/* Comments as CharacterData */
+
+print ("\nStringified Comments have <!-- -->, like this one:\n" +
+       comment.to_string (true, 0));
+
+print ("\nComments are CharacterData, so previous comment's data:\n" +
+       comment.data);
+       
+print ("\nComments are CharacterData, so previous comment's length:\n" +
+       comment.length);
+
+comment.append_data (".  Did you read it?");
+print ("\nComment after using CharacterData's append_data ():\n" +
+       comment.to_string (true, 0));
+
+comment.replace_data (12, 4, "awesome");
+print ("\nComment after using CharacterData's replace_data () (cool -> awesome).\n" +
+       "Note that this affects the data as seen in CharacterData's .data property, excluding the comment's 
surrounding <!-- and -->:\n" +
+       comment.to_string (true, 0));
diff --git a/examples/js/element.js b/examples/js/element.js
new file mode 100755
index 0000000..4b01901
--- /dev/null
+++ b/examples/js/element.js
@@ -0,0 +1,47 @@
+#!/usr/bin/gjs
+
+const GXml = imports.gi.GXml;
+
+/* Elements are a type of Node.  To see more of what you can do with an Element,
+   view the node examples */
+
+// Setup 1
+var doc = GXml.Document.from_string ("<Tree type='leafy' wisdom='great'>Yggdrasil</Tree>");
+var tree = doc.document_element;
+
+print ("Stringify Tree element:\n" + tree.to_string (true, 0));
+
+print ("\nGet Tree element's tag name:\n  " + tree.tag_name);
+print ("\nGet Tree element's content:\n  " + tree.content);
+print ("\nGet Tree element's 'type' attribute:\n  " + tree.get_attribute ('type'));
+
+tree.set_attribute ("type", "lush");
+print ("\nChange Tree element's 'type' to something else:\n  " + tree.to_string (true, 0));
+
+tree.set_attribute ("roots", "abundant");
+print ("\nSet a new attribute, 'roots':\n  " + tree.to_string (true, 0));
+
+tree.remove_attribute ("type");
+print ("\nRemove attribute 'type':\n  " + tree.to_string (true, 0));
+
+var new_attr = doc.create_attribute ("height");
+new_attr.value = "109m";
+tree.set_attribute_node (new_attr);
+print ("\nSet a new attribute node:\n  " + tree.to_string (true, 0));
+
+var old_attr = tree.get_attribute_node ("wisdom");
+print ("\nGet an existing attr as a node:\n  " + old_attr.to_string (true, 0));
+
+tree.remove_attribute_node (old_attr);
+print ("\nRemove wisdom attr by its node:\n  " + tree.to_string (true, 0));
+
+// Setup 2
+var leaves_doc = GXml.Document.from_string ("<Leaves><Leaf name='Aldo' /><Leaf name='Drona' /><Leaf 
name='Elma' /><Leaf name='Hollo' /><Leaf name='Irch' /><Leaf name='Linder' /><Leaf name='Makar' /><Leaf 
name='Oakin' /><Leaf name='Olivio' /><Leaf name='Rown' /></Leaves>");
+var leaves = leaves_doc.document_element;
+
+print ("Our second example:\n" + leaves_doc.to_string (true, 0));
+var leaf_list = leaves_doc.get_elements_by_tag_name ("Leaf");
+print ("Show element descendants of Leaves with the tag name Leaf:\n  " + leaf_list.to_string (true));
+
+/* TODO: when we support sibling text nodes (that is, when we're not
+ * using libxml2), then we can add a normalize () example */
diff --git a/examples/js/node_child_management.js b/examples/js/node_child_management.js
new file mode 100755
index 0000000..272b676
--- /dev/null
+++ b/examples/js/node_child_management.js
@@ -0,0 +1,99 @@
+#!/usr/bin/gjs
+
+/* https://bugzilla.gnome.org/show_bug.cgi?id=706907
+   Sadly, we can't handle GHashTables as properties in gjs yet :( */
+var bug_706907_bypass = true;
+
+const GXml = imports.gi.GXml;
+
+/* Setup */
+var doc = GXml.Document.from_path ("bookshelf_node.xml");
+var bookshelf = doc.document_element;
+var date_clone_shallow = date_today.clone_node (false);
+var date_clone_deep = date_today.clone_node (true);
+
+/* Appending a child */
+var date_today = doc.create_element ("Date");
+bookshelf.append_child (date_today);
+
+var date_today_text = doc.create_text_node ("Today");
+date_today.append_child (date_today_text);
+
+print ("Bookshelf after appending a new element:\n" + bookshelf.to_string (true, 0) + "\n\n");
+
+/* Checking whether children exist */
+print ("Does Bookshelf have child nodes? " + bookshelf.has_child_nodes () + "\n");
+print ("Does the shallow Date clone have child nodes? " +
+       date_clone_shallow.has_child_nodes () + "\n\n");
+
+/* Accessing the first child */
+var bookshelf_first_child = bookshelf.first_child;
+print ("Bookshelf's first child:\n" + bookshelf_first_child.to_string (true, 0) + "\n\n");
+
+var date_clone_cur_text = date_clone_deep.first_child;
+print ("Our deep clone's first child:\n" + date_clone_cur_text.to_string (true, 0) + "\n\n");
+
+/* Replacing a child */
+var date_clone_new_text = doc.create_text_node ("Tomorrow");
+date_clone_deep.replace_child (date_clone_new_text, date_clone_cur_text);
+
+print ("Bookshelf after replacing the Text of our deep clone of Date:\n" +
+       bookshelf.to_string (true, 0) + "\n\n");
+
+/* Inserting a new child before an existing child */
+var date_yesterday = doc.create_element ("Date");
+var date_yesterday_text = doc.create_text_node ("Yesterday");
+date_yesterday.append_child (date_yesterday_text);
+
+bookshelf.insert_before (date_yesterday, date_today);
+
+print ("Bookshelf after inserting Date Yesterday before Date Today:\n" +
+       bookshelf.to_string (true, 0) + "\n\n");
+
+/* Removing a child */
+bookshelf.remove_child (date_clone_shallow);
+
+print ("Bookshelf after removing the shallow date clone:\n" +
+       bookshelf.to_string (true, 0) + "\n\n");
+
+/* Accessing the last child */
+var last_child = bookshelf.last_child;
+print ("Bookshelf's last child:\n" + last_child.to_string (true, 0) + "\n\n");
+
+/* Traversing children via next and previous sibling */
+var cur_child = null;
+var i = 0;
+
+print ("Bookshelf's children, using next_sibling from the first_child:\n");
+for (cur_child = bookshelf.first_child; cur_child != null;
+     cur_child = cur_child.next_sibling) {
+    print ("  Child " + i + "\n    " + cur_child.to_string (true, 2) + "\n");
+    i++;
+}
+
+print ("Bookshelf's children, using previous_sibling from the last_child:\n");
+for (cur_child = bookshelf.last_child; cur_child != null;
+     cur_child = cur_child.previous_sibling) {
+    i--;
+    print ("  Child " + i + "\n    " + cur_child.to_string (true, 2) + "\n");
+}
+print ("\n");
+
+/* Traversing children through its GXmlNodeList of child nodes */
+var children = bookshelf.child_nodes;
+
+var len = children.length;
+print ("Bookshelf's children, using get_child_nodes and incrementing by index:\n");
+for (i = 0; i < len; i++) {
+    var child = children.nth (i);
+    print ("  Child " + i + "\n    " + child.to_string (true, 2) + "\n");
+}
+print ("\n");  
+
+
+/* Access the parent node from a node */
+var first_book_parent = first_book.parent_node;
+print ("The parent of " + first_book.to_string (true, 0) + " looks like:\n  " +
+       first_book_parent.to_string (true, 1) + "\n\n");
+
+
diff --git a/examples/js/node_list.js b/examples/js/node_list.js
new file mode 100755
index 0000000..881ed52
--- /dev/null
+++ b/examples/js/node_list.js
@@ -0,0 +1,69 @@
+#!/usr/bin/gjs
+
+const GXml = imports.gi.GXml;
+
+/* For more examples, view node's child management code */
+
+// Setup
+var doc = GXml.Document.from_string ("<Refrigerator><Food name='cheese' /><Food name='tomatoes' /><Food 
name='avocado' /></Refrigerator>");
+
+var foods_list = doc.get_elements_by_tag_name ("Food");
+
+
+print ("Our refrigerator:\n" + doc.to_string (true, 0));
+
+print ("\nThe length of the list:\n  " + foods_list.length);
+
+print ("\nStringify the list:\n  " + foods_list.to_string (true));
+
+print ("\nAccessing the second food item from a nodelist using .item ():\n  " +
+       foods_list.item (1).get_attribute ('name'));
+
+print ("\nAccessing the second food item from a nodelist using .nth ():\n  " +
+       foods_list.nth (1).get_attribute ('name'));
+
+print ("\nAccessing the second food item from a nodelist using .nth_data ():\n  " +
+       foods_list.nth_data (1).get_attribute ('name'));
+
+print ("\nAccessing the first food item with .first:\n  " +
+       foods_list.first ().get_attribute ('name'));
+
+print ("\nAccessing the last food item with .last:\n  " +
+       foods_list.last ().get_attribute ('name'));
+
+print ("\nAccessing the food item 2 previous to the last with .nth_prev ():\n  " +
+       foods_list.nth_prev (foods_list.last (), 2).get_attribute ('name'));
+
+print ("\nFinding the index for the last food item with .find ():\n  " +
+       foods_list.find (foods_list.last ()));
+
+print ("\nFinding the index for the last food item with .position ():\n  " +
+       foods_list.position (foods_list.last ()));
+
+print ("\nFinding the index for the last food item with .index ():\n  " +
+       foods_list.index (foods_list.last ()));
+
+/* TODO: this isn't wonderfully efficient; we want to do a foreach
+   style loop on it, for (var a : list) {..., or have a NodeList thing
+   to get the next one */
+print ("Traverse the list:");
+for (var i = 0; i < foods_list.length; i++) {
+    var food = foods_list.nth (i);
+    print ("  " + i + ":" + food.to_string (true, 0));
+}
+
+/* TODO:
+   Figure out how to handle GFunc 
+print ("\nOperate on each food item using .foreach ()\n");
+foods_list.foreach (function (a) {
+    });
+*/
+
+/* TODO:
+   Figure out how to handle GCompareFunc
+print ("\nFinding the index for the food item with the same name, using .find_custom ():\n" +
+       foods_list.find_custom (foods_list.last (), function (a, b) {
+          print ("DBG: " + a + "; " + b + "; ");
+          return 2;
+       }, foods_list));
+*/
diff --git a/examples/js/node_self.js b/examples/js/node_self.js
new file mode 100755
index 0000000..7fb08a9
--- /dev/null
+++ b/examples/js/node_self.js
@@ -0,0 +1,69 @@
+#!/usr/bin/gjs
+
+/* https://bugzilla.gnome.org/show_bug.cgi?id=706907
+   Sadly, we can't handle GHashTables as properties in gjs yet :( */
+var bug_706907_bypass = true;
+
+const GXml = imports.gi.GXml;
+
+/* Setup */
+var doc = GXml.Document.from_path ("bookshelf_node.xml");
+var bookshelf = doc.document_element;
+var date_today = doc.create_element ("Date");
+date_today.append_child (doc.create_text_node ("Today"));
+
+
+/* Stringification */
+print ("Stringifying bookshelf:\n" + bookshelf.to_string (true, 0) + "\n\n");
+
+/* Adding clones */
+var date_clone_shallow = date_today.clone_node (false);
+var date_clone_deep = date_today.clone_node (true);
+
+bookshelf.append_child (date_clone_shallow);
+bookshelf.append_child (date_clone_deep);
+
+print ("Bookshelf after adding clones:\n" + bookshelf.to_string (true, 0) + "\n\n");
+
+/* Access the node's attributes map - note that this only applies to Elements
+   http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-1841493061 */
+var book_list = doc.get_elements_by_tag_name ("Book");
+var first_book = book_list.first ();
+
+if (! bug_706907_bypass) {
+    var attrs = first_book.attributes;
+    
+    print ("List attributes from element: " + first_book.to_string (false, 0) + "\n");
+    for (attrs_keys = attrs.get_keys (); attrs_keys != null; attrs_keys = attrs_keys.next) {
+       var attr = attrs.lookup (attrs_keys.data);
+       print ("  " + attr.name + " => " + attr.value +
+              "  (" + (attr.specified ? "specified" : "not specified") + ")\n");
+    }
+    print ("\n");
+}
+
+/* Access the owner document from the node */
+var owner_document = bookshelf.owner_document;
+
+print ("The owner document for Bookshelf looks like:\n" + owner_document.to_string (false, 0) + "\n\n");
+
+/* Check node types */
+var author_attr = first_book.get_attribute ("author");
+
+print ("A:");
+for (x in GXml.NodeType) {
+    print (x);
+}
+var test = GXml.NodeType['DOCUMENT'];
+print (test);
+print (GXml.NodeType.DOCUMENT);
+
+print ("\nGet class");
+print (GXml.NodeType);
+
+print ("Document is of type: " + doc.node_type); // .to_string ());
+/* How do we support macro wrappers from C in JS? 
+print ("Bookshelf is of type: %s\n", bookshelf.node_type.to_string ());
+print ("Bookshelf is of type: %s\n", author_attr.node_type.to_string ());
+*/
+
diff --git a/examples/js/text.js b/examples/js/text.js
new file mode 100755
index 0000000..32859ea
--- /dev/null
+++ b/examples/js/text.js
@@ -0,0 +1,36 @@
+#!/usr/bin/gjs
+
+const GXml = imports.gi.GXml;
+
+// Setup
+var doc = new GXml.Document.new ();
+doc.append_child (doc.create_element ("Diary"));
+
+var text = doc.create_text_node ("I am the very model of a modern Major General.");
+
+print ("Document before inserting text:\n" +
+       doc.to_string (true, 0));
+
+doc.document_element.append_child (text);
+
+print ("Document after inserting text:\n" +
+       doc.to_string (true, 0));
+
+print ("Nodes of document element 'Diary' before split_text:");
+for (var node = doc.document_element.first_child; node != null; node = node.next_sibling) {
+    print ("  Node: " + node.to_string (true, 0));
+}
+
+text.split_text (20);
+
+print ("\nNodes of document element 'Diary' after split_text:");
+for (var node = doc.document_element.first_child; node != null; node = node.next_sibling) {
+    print ("  Node: " + node.to_string (true, 0));
+}
+
+print ("\nWARNING: the above does not work as desired, since libxml2 automatically merges neighbouring Text 
nodes.  You should see two Nodes for each part of the split.  This will hopefully be addressed in the 
future.");
+
+/* To see Text manipulated as CharacterData, go see the CharacterData example */
+
+
+


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