[gxml] Implemented create/write CDATA for TwDocument



commit 297dfa3b3a7210a097a0cedde0461f7f53b0841d
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue May 12 14:52:41 2015 -0500

    Implemented create/write CDATA for TwDocument
    
    * Added TwCDATA and Unit tests
    * Added Xmlx.text_writer_write_cdata() (File a bug)

 gxml/CDATA.vala           |    9 +++++++-
 gxml/Document.vala        |   40 +++--------------------------------
 gxml/Makefile.am          |    1 +
 gxml/TwCDATA.vala         |   43 ++++++++++++++++++++++++++++++++++++++
 gxml/TwDocument.vala      |   29 +++++++++++++++++++++----
 gxml/libxml-Document.vala |    1 +
 gxml/xlibxml.c            |    5 ++++
 gxml/xlibxml.h            |    1 +
 test/GXmlTest.vala        |    1 +
 test/Makefile.am          |    1 +
 test/TwCDATATest.vala     |   50 +++++++++++++++++++++++++++++++++++++++++++++
 test/TwDocumentTest.vala  |    2 -
 vapi/xlibxml-1.0.vapi     |    2 +
 13 files changed, 141 insertions(+), 44 deletions(-)
---
diff --git a/gxml/CDATA.vala b/gxml/CDATA.vala
index a694a18..264abc2 100644
--- a/gxml/CDATA.vala
+++ b/gxml/CDATA.vala
@@ -25,4 +25,11 @@ using Gee;
 /**
  * CDATA sections in XML documents.
  */
-public interface GXml.CDATA : Object, GXml.Text {}
+public interface GXml.CDATA : Object, GXml.Node
+{
+  
+  /**
+   * This should be implemented by returning { link GXml.Node.value}
+   */
+  public abstract string str { get; }
+}
diff --git a/gxml/Document.vala b/gxml/Document.vala
index 33e2414..8fe1c96 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -54,19 +54,6 @@ public interface GXml.Document : Object, GXml.Node
    */
   public abstract GXml.Node create_element (string name);
   /**
-   * This method should finalize a new created { link GXml.Element}.
-   *
-   * Once a { link GXml.Element} was created and setup, you should finalize it
-   * by calling this method. Is a good practice to call this function, even if
-   * current implemention doesn't requires it.
-   *
-   * Setup a new { link GXml.Element} include: set its attributes and add childs
-   * { link GXml.Node}. When finish, call this function.
-   *
-   * This function is useful when using { link GXml.TextWriter} implementations.
-   */
-  public virtual void finalize_element () { return; }
-  /**
    * Creates a new { link GXml.Text}.
    *
    * Is a matter of you to add as a child to any other
@@ -74,19 +61,6 @@ public interface GXml.Document : Object, GXml.Node
    */
   public abstract GXml.Node create_text (string text);
   /**
-   * This method should finalize a new created { link GXml.Text}.
-   *
-   * Once a { link GXml.Text} was created and setup, you should finalize it
-   * by calling this method. Is a good practice to call this function, even if
-   * current implemention doesn't requires it.
-   *
-   * Setup a new { link GXml.Text} include: set its text, done when it is created.
-   * When finish, call this fucntion.
-   *
-   * This function is useful when using { link GXml.TextWriter} implementations.
-   */
-  public virtual void finalize_text () { return; }
-  /**
    * Creates a new { link GXml.Comment}.
    *
    * Is a matter of you to add as a child to any other
@@ -94,18 +68,12 @@ public interface GXml.Document : Object, GXml.Node
    */
   public abstract GXml.Node create_comment (string text);
   /**
-   * This method should finalize a new created { link GXml.Comment}.
-   *
-   * Once a { link GXml.Comment} was created and setup, you should finalize it
-   * by calling this method. Is a good practice to call this function, even if
-   * current implemention doesn't requires it.
+   * Creates a new { link GXml.CDATA}.
    *
-   * Setup a new { link GXml.Comment} include: set its text, done when it is created.
-   * When finish, call this fucntion.
-   *
-   * This function is useful when using { link GXml.TextWriter} implementations.
+   * Is a matter of you to add as a child to any other
+   * { link GXml.Node}, like a { link GXml.Element} node.
    */
-  public virtual void finalize_comment () { return; }
+  public abstract GXml.Node create_cdata (string text);
   /**
    * Save this { link GXml.Document} to { link GXml.Document.file}
    *
diff --git a/gxml/Makefile.am b/gxml/Makefile.am
index 26e1d9e..c31bd97 100644
--- a/gxml/Makefile.am
+++ b/gxml/Makefile.am
@@ -68,6 +68,7 @@ sources = \
        SerializableContainer.vala \
        TwAttribute.vala \
        TwComment.vala \
+       TwCDATA.vala \
        TwDocument.vala \
        TwElement.vala \
        TwNamespace.vala \
diff --git a/gxml/TwCDATA.vala b/gxml/TwCDATA.vala
new file mode 100644
index 0000000..299e247
--- /dev/null
+++ b/gxml/TwCDATA.vala
@@ -0,0 +1,43 @@
+/* TwCDATA.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.TwCDATA : GXml.TwNode, GXml.CDATA
+{
+  private string _str = null;
+  construct {
+    _name = "#cdata";
+  }
+  public TwCDATA (GXml.Document d, string text)
+    requires (d is GXml.TwDocument)
+  {
+    _doc = d;
+    _str = text;
+  }
+  // GXml.Node
+  public override string @value {
+    get { return _str; }
+    set { _str = value; }
+  }
+  // GXml.CDATA
+  public string str { get { return _str; } }
+}
diff --git a/gxml/TwDocument.vala b/gxml/TwDocument.vala
index 7caab90..71c30bb 100644
--- a/gxml/TwDocument.vala
+++ b/gxml/TwDocument.vala
@@ -60,6 +60,11 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
     var t = new TwText (this, text);
     return t;
   }
+  public GXml.Node create_cdata (string text)
+  {
+    var t = new TwCDATA (this, text);
+    return t;
+  }
   public GLib.File file { get; set; }
   public GXml.Node root {
     get {
@@ -160,6 +165,9 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
     GLib.message (@"Starting Element: writting Node '$(node.name)' childs");
 #endif
       foreach (GXml.Node n in node.childs) {
+#if DEBUG
+    GLib.message (@"Child Node is: $(n.get_type ().name ())");
+#endif
         if (n is GXml.Element) {
 #if DEBUG
     GLib.message (@"Starting Child Element: writting Node '$(n.name)'");
@@ -175,13 +183,24 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
           if (size > 1500)
             tw.flush ();
         }
+        if (n is GXml.Comment) {
+#if DEBUG
+    GLib.message (@"Starting Child Element: writting Text '$(n.value)'");
+#endif
+          size += tw.write_comment (n.value);
+          if (size > 1500)
+            tw.flush ();
+        }
+        if (n is GXml.CDATA) {
+#if DEBUG
+    GLib.message (@"Starting Child Element: writting CDATA '$(n.value)'");
+#endif
+          size += Xmlx.text_writer_write_cdata (tw, n.value);
+          if (size > 1500)
+            tw.flush ();
+        }
       }
     }
-    if (node is GXml.Comment) {
-      size += tw.write_comment (node.value);
-      if (size > 1500)
-        tw.flush ();
-    }
   }
   public override string to_string ()
   {
diff --git a/gxml/libxml-Document.vala b/gxml/libxml-Document.vala
index 26df7e0..d2eeb91 100644
--- a/gxml/libxml-Document.vala
+++ b/gxml/libxml-Document.vala
@@ -1019,6 +1019,7 @@ namespace GXml {
                public virtual GXml.Node root { get { return document_element; } }
                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 bool save (GLib.Cancellable? cancellable = null)
                        throws GLib.Error
                        requires (file != null)
diff --git a/gxml/xlibxml.c b/gxml/xlibxml.c
index 7c1ecdc..9629721 100644
--- a/gxml/xlibxml.c
+++ b/gxml/xlibxml.c
@@ -50,3 +50,8 @@ xmlTextWriterPtr gxml_new_text_writer_doc (xmlDoc** doc)
 {
   return xmlNewTextWriterDoc (doc, 0);
 }
+
+int gxml_text_writer_write_cdata (xmlTextWriterPtr tw, const xmlChar* text)
+{
+  return xmlTextWriterWriteCDATA (tw, text);
+}
diff --git a/gxml/xlibxml.h b/gxml/xlibxml.h
index 82daed1..ae70387 100644
--- a/gxml/xlibxml.h
+++ b/gxml/xlibxml.h
@@ -30,5 +30,6 @@ int         gxml_validate_name                 (xmlChar* name, int space);
 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);
 
 #endif
diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala
index 4994931..b3e640a 100644
--- a/test/GXmlTest.vala
+++ b/test/GXmlTest.vala
@@ -72,6 +72,7 @@ class GXmlTest {
                SerializableEnumerationTest.add_tests ();
                Performance.add_tests ();
                TwElementTest.add_tests ();
+               TwCDATATest.add_tests ();
                TwDocumentTest.add_tests ();
 
                Test.run ();
diff --git a/test/Makefile.am b/test/Makefile.am
index 095463a..dc169d5 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -51,6 +51,7 @@ sources = \
        gxml-performance.vala \
        TwElementTest.vala \
        TwDocumentTest.vala \
+       TwCDATATest.vala \
        $(NULL)
 
 gxml_test.vala.stamp: $(sources)
diff --git a/test/TwCDATATest.vala b/test/TwCDATATest.vala
new file mode 100644
index 0000000..b9d6ebd
--- /dev/null
+++ b/test/TwCDATATest.vala
@@ -0,0 +1,50 @@
+/* -*- 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 TwCDATATest : GXmlTest {
+       public static void add_tests () {
+               Test.add_func ("/gxml/tw-cdata", () => {
+                       try {
+                               var d = new TwDocument ();
+                               var r = d.create_element ("root");
+                               d.childs.add (r);
+                               var cd = d.create_cdata ("<test/>");
+                               assert (cd.value == "<test/>");
+                               d.root.childs.add (cd);
+                               assert (d.root.childs.size == 1);
+                               string str = d.to_string ();
+                               assert ("<root><![CDATA[<test/>]]></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/test/TwDocumentTest.vala b/test/TwDocumentTest.vala
index 9465fa2..0d70907 100644
--- a/test/TwDocumentTest.vala
+++ b/test/TwDocumentTest.vala
@@ -1,7 +1,6 @@
 /* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
 /* Notation.vala
  *
- * Copyright (C) 2011-2013  Richard Schwarting <aquarichy gmail com>
  * Copyright (C) 2011-2015  Daniel Espinosa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
@@ -18,7 +17,6 @@
  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Authors:
- *      Richard Schwarting <aquarichy gmail com>
  *      Daniel Espinosa <esodan gmail com>
  */
 
diff --git a/vapi/xlibxml-1.0.vapi b/vapi/xlibxml-1.0.vapi
index a5a7801..98a09d6 100644
--- a/vapi/xlibxml-1.0.vapi
+++ b/vapi/xlibxml-1.0.vapi
@@ -38,4 +38,6 @@ namespace Xmlx {
   public static Xml.Ns*[] doc_get_ns_list (Xml.Doc* doc, Xml.Node* node);
   [CCode (cname = "gxml_new_text_writer_doc", cheader_filename = "gxml/xlibxml.h")]
   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);
 }


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