[gxml] Added TwProcessingInstruction and Unit Tests



commit e1e4219959686da4b7fdff61c12cfe0e16306d42
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue May 12 16:48:17 2015 -0500

    Added TwProcessingInstruction and Unit Tests

 gxml/Document.vala                    |    7 ++++
 gxml/Makefile.am                      |    1 +
 gxml/TwCDATA.vala                     |    2 +-
 gxml/TwComment.vala                   |    5 ++-
 gxml/TwDocument.vala                  |   13 ++++++++
 gxml/TwProcessingInstruction.vala     |   46 +++++++++++++++++++++++++++++
 gxml/libxml-Document.vala             |    1 +
 gxml/xlibxml.c                        |    5 +++
 gxml/xlibxml.h                        |    1 +
 test/GXmlTest.vala                    |    1 +
 test/Makefile.am                      |    1 +
 test/TwCommentTest.vala               |    2 +-
 test/TwProcessingInstructionTest.vala |   52 +++++++++++++++++++++++++++++++++
 vapi/xlibxml-1.0.vapi                 |    2 +
 14 files changed, 136 insertions(+), 3 deletions(-)
---
diff --git a/gxml/Document.vala b/gxml/Document.vala
index 8fe1c96..44b63f9 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -75,6 +75,13 @@ public interface GXml.Document : Object, GXml.Node
    */
   public abstract GXml.Node create_cdata (string text);
   /**
+   * Creates a new { link GXml.ProcessingInstruction}.
+   *
+   * Is a matter of you to add as a child to any other
+   * { link GXml.Node}, like a { link GXml.Element} node.
+   */
+  public abstract GXml.Node create_pi (string target, string data);
+  /**
    * Save this { link GXml.Document} to { link GXml.Document.file}
    *
    * If { link GXml.Document.file} doesn't exists, it creates a new file to save to.
diff --git a/gxml/Makefile.am b/gxml/Makefile.am
index c31bd97..f893af2 100644
--- a/gxml/Makefile.am
+++ b/gxml/Makefile.am
@@ -73,6 +73,7 @@ sources = \
        TwElement.vala \
        TwNamespace.vala \
        TwNode.vala \
+       TwProcessingInstruction.vala \
        TwText.vala
 
 
diff --git a/gxml/TwCDATA.vala b/gxml/TwCDATA.vala
index 299e247..a93d42d 100644
--- a/gxml/TwCDATA.vala
+++ b/gxml/TwCDATA.vala
@@ -36,7 +36,7 @@ public class GXml.TwCDATA : GXml.TwNode, GXml.CDATA
   // GXml.Node
   public override string @value {
     get { return _str; }
-    set { _str = value; }
+    set {}
   }
   // GXml.CDATA
   public string str { get { return _str; } }
diff --git a/gxml/TwComment.vala b/gxml/TwComment.vala
index 8d9247a..7ad94a7 100644
--- a/gxml/TwComment.vala
+++ b/gxml/TwComment.vala
@@ -24,6 +24,9 @@ using Gee;
 public class GXml.TwComment : GXml.TwNode, GXml.Comment
 {
   private string _str = "";
+  construct {
+    _name = "#comment";
+  }
   public TwComment (GXml.Document doc, string text)
     requires (doc is GXml.TwDocument)
   {
@@ -33,7 +36,7 @@ public class GXml.TwComment : GXml.TwNode, GXml.Comment
   // GXml.Node
   public override string @value {
     get { return _str; }
-    set { _str = value; }
+    set {  }
   }
   // GXml.Comment
   public string str { get { return _str; } }
diff --git a/gxml/TwDocument.vala b/gxml/TwDocument.vala
index 029dde6..0a42017 100644
--- a/gxml/TwDocument.vala
+++ b/gxml/TwDocument.vala
@@ -48,6 +48,11 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
     var c = new TwComment (this, text);
     return c;
   }
+  public GXml.Node create_pi (string target, string data)
+  {
+    var pi = new TwProcessingInstruction (this, target, data);
+    return pi;
+  }
   public GXml.Node create_element (string name)
   {
     return new TwElement (this, name);
@@ -196,6 +201,14 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
           if (size > 1500)
             tw.flush ();
         }
+        if (n is GXml.ProcessingInstruction) {
+#if DEBUG
+    GLib.message (@"Starting Child Element: writting ProcessingInstruction '$(n.value)'");
+#endif
+          size += Xmlx.text_writer_write_pi (tw, ((ProcessingInstruction) n).target, 
((ProcessingInstruction) n).data);
+          if (size > 1500)
+            tw.flush ();
+        }
       }
     }
   }
diff --git a/gxml/TwProcessingInstruction.vala b/gxml/TwProcessingInstruction.vala
new file mode 100644
index 0000000..eb594a7
--- /dev/null
+++ b/gxml/TwProcessingInstruction.vala
@@ -0,0 +1,46 @@
+/* Element.vala
+ *
+ * Copyright (C) 2015  Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser 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 Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+
+using Gee;
+
+public class GXml.TwProcessingInstruction : GXml.TwNode, GXml.ProcessingInstruction
+{
+  private string _target = "";
+  private string _data = "";
+  construct {
+    _name = "#processinginstruction";
+  }
+  public TwProcessingInstruction (GXml.Document doc, string target, string data)
+    requires (doc is GXml.TwDocument)
+  {
+    _doc = doc;
+    _target = target;
+    _data = data;
+  }
+  // GXml.Node
+  public override string @value {
+    get { return _data; }
+    set {}
+  }
+  // GXml.ProcessingInstruction
+  public string target { get { return _target; } }
+  public string data { get { return _data; } }
+}
diff --git a/gxml/libxml-Document.vala b/gxml/libxml-Document.vala
index d2eeb91..abda0c7 100644
--- a/gxml/libxml-Document.vala
+++ b/gxml/libxml-Document.vala
@@ -1020,6 +1020,7 @@ namespace GXml {
                public GXml.Node create_text (string str) { return (GXml.Node) this.create_text_node (str); }
                public GXml.Node create_comment (string text) { return create_managed_comment (text); }
                public GXml.Node create_cdata (string text) { return create_cdata_section (text); }
+               public GXml.Node create_pi (string target, string data) { return 
create_processing_instruction (target, data); }
                public bool save (GLib.Cancellable? cancellable = null)
                        throws GLib.Error
                        requires (file != null)
diff --git a/gxml/xlibxml.c b/gxml/xlibxml.c
index 9629721..1365580 100644
--- a/gxml/xlibxml.c
+++ b/gxml/xlibxml.c
@@ -55,3 +55,8 @@ int gxml_text_writer_write_cdata (xmlTextWriterPtr tw, const xmlChar* text)
 {
   return xmlTextWriterWriteCDATA (tw, text);
 }
+
+int gxml_text_writer_write_pi (xmlTextWriterPtr tw, const xmlChar* target, const xmlChar* data)
+{
+  return xmlTextWriterWritePI (tw, target, data);
+}
diff --git a/gxml/xlibxml.h b/gxml/xlibxml.h
index ae70387..6ec601c 100644
--- a/gxml/xlibxml.h
+++ b/gxml/xlibxml.h
@@ -31,5 +31,6 @@ xmlErrorPtr gxml_parser_context_get_last_error (void* ctx);
 xmlErrorPtr gxml_get_last_error                ();
 xmlNsPtr*   gxml_doc_get_ns_list               (xmlDoc* doc, xmlNode* node);
 int         gxml_text_writer_write_cdata       (xmlTextWriter* tw, const xmlChar* text);
+int         gxml_text_writer_write_pi          (xmlTextWriter* tw, const xmlChar* target, const xmlChar* 
data);
 
 #endif
diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala
index 56a3ac1..3f92356 100644
--- a/test/GXmlTest.vala
+++ b/test/GXmlTest.vala
@@ -75,6 +75,7 @@ class GXmlTest {
                TwCDATATest.add_tests ();
                TwCommentTest.add_tests ();
                TwDocumentTest.add_tests ();
+               TwProcessingInstructionTest.add_tests ();
 
                Test.run ();
 
diff --git a/test/Makefile.am b/test/Makefile.am
index ba2be4c..1143448 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -53,6 +53,7 @@ sources = \
        TwDocumentTest.vala \
        TwCDATATest.vala \
        TwCommentTest.vala \
+       TwProcessingInstructionTest.vala \
        $(NULL)
 
 gxml_test.vala.stamp: $(sources)
diff --git a/test/TwCommentTest.vala b/test/TwCommentTest.vala
index e5473ee..1b7b741 100644
--- a/test/TwCommentTest.vala
+++ b/test/TwCommentTest.vala
@@ -30,9 +30,9 @@ class TwCommentTest : GXmlTest {
                                var r = d.create_element ("root");
                                d.childs.add (r);
                                var c = d.create_comment ("This is a comment");
+                               assert (c.name == "#comment");
                                assert (c.value == "This is a comment");
                                d.root.childs.add (c);
-                               GLib.message (@"$d");
                                assert (d.root.childs.size == 1);
                                string str = d.to_string ();
                                assert ("<root><!--This is a comment--></root>" in str);
diff --git a/test/TwProcessingInstructionTest.vala b/test/TwProcessingInstructionTest.vala
new file mode 100644
index 0000000..e9eb7ec
--- /dev/null
+++ b/test/TwProcessingInstructionTest.vala
@@ -0,0 +1,52 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
+/* TwCDATATest.vala
+ *
+ * Copyright (C) 2011-2015  Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser 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 Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+
+using GXml;
+
+class TwProcessingInstructionTest : GXmlTest {
+       public static void add_tests () {
+               Test.add_func ("/gxml/tw-comment", () => {
+                       try {
+                               var d = new TwDocument ();
+                               var r = d.create_element ("root");
+                               d.childs.add (r);
+                               var pi = d.create_pi ("xslt","transform");
+                               assert (pi.name == "#processinginstruction");
+                               assert (pi.value == "transform");
+                               d.root.childs.add (pi);
+                               assert (d.root.childs.size == 1);
+                               GLib.message (@"Document created: $d");
+                               string str = d.to_string ();
+                               assert ("<root><?xslt transform?></root>" in str);
+#if DEBUG
+                               GLib.message (@"$d");
+#endif
+                       }
+                       catch (GLib.Error e) {
+#if DEBUG
+                               GLib.message (@"ERROR: $(e.message)");
+#endif
+                               assert_not_reached ();
+                       }
+               });
+       }
+}
diff --git a/vapi/xlibxml-1.0.vapi b/vapi/xlibxml-1.0.vapi
index 98a09d6..17fa24d 100644
--- a/vapi/xlibxml-1.0.vapi
+++ b/vapi/xlibxml-1.0.vapi
@@ -40,4 +40,6 @@ namespace Xmlx {
   public static Xml.TextWriter new_text_writer_doc (ref Xml.Doc doc);
   [CCode (cname = "gxml_text_writer_write_cdata", cheader_filename = "gxml/xlibxml.h")]
   public static int text_writer_write_cdata (Xml.TextWriter tw, string text);
+  [CCode (cname = "gxml_text_writer_write_pi", cheader_filename = "gxml/xlibxml.h")]
+  public static int text_writer_write_pi (Xml.TextWriter tw, string target, string data);
 }


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