[gxml/gxml-0.8] Most changes improves GIO GLib.File use



commit 91a1ae4fc9d7f3036c9460269d278043fed15249
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Jan 12 22:19:55 2016 -0600

    Most changes improves GIO GLib.File use

 NEWS                          |   28 +++++++++
 gxml/Document.vala            |   14 ++---
 gxml/TwDocument.vala          |   25 ++++----
 gxml/libxml-Document.vala     |  132 ++++++++++++++++++-----------------------
 gxml/libxml-DomException.vala |   17 +++++-
 gxml/xlibxml.c                |   21 +++++++
 gxml/xlibxml.h                |    3 +
 test/DocumentTest.vala        |   54 +++++++++++-----
 test/TwDocumentTest.vala      |   24 ++++----
 vapi/xlibxml-1.0.vapi         |    7 ++
 10 files changed, 199 insertions(+), 126 deletions(-)
---
diff --git a/NEWS b/NEWS
index d092fd4..e795048 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,32 @@
 =============
+Version 0.8.1
+=============
+
+Most changes improves GIO GLib.File use, trying to read/write any file from/to
+local or remote locations.
+
+* GXml.Document gains a backup property, set to true by default to save files
+  with a backup operation
+* GXml.Document.save_as() now is abstract, you should implement it.
+* GXml.Document.save_as() now should not overwrite its file property
+
+* GXml.DomException.warning() now is deprecated use exception(), now message on
+  errors just printed when -D DEBUG is used at compile time
+* Added Xmlx.reset_last_error() and used to cleanup errors before each operation
+* Added Xmlx.context_get_last_error() to deprecate parser_context_get_last_error ()
+* Added Xmlx.context_reset_last_error()
+* Xmlx methods now have sanity checks asserts
+
+* xDocument.from_stream() now use GIO internally to open any GLib.File valid object
+* xDocument.from_string() now don't fails on XML with no root
+* xDocument.save_to_stream()  now use GIO internally to write to any GLib.File valid object
+
+* TwDocument.save_to() deprecated by save_as() from GXml.Document implementation
+
+* New Translations:
+  * Added German translation by Mario Blättermann
+
+=============
 Version 0.8.0
 =============
 
diff --git a/gxml/Document.vala b/gxml/Document.vala
index 601b7c4..41b51e3 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -55,6 +55,11 @@ public interface GXml.Document : Object, GXml.Node
    */
   public abstract bool prefix_default_ns { get; set; }
   /**
+   * Controls if writting to a { link GLib.File} creates a backup, by default
+   * is true;
+   */
+  public abstract bool backup { get; set; }
+  /**
    * XML document root node as a { link GXml.Element}.
    */
   public abstract GXml.Node root { get; }
@@ -105,15 +110,8 @@ public interface GXml.Document : Object, GXml.Node
   public abstract bool save (GLib.Cancellable? cancellable = null) throws GLib.Error;
   /**
    * Save this { link GXml.Document} to given { link GLib.File}
-   *
-   * This overrides actual { link GXml.Document.file}
    */
-  public virtual bool save_as (GLib.File f, GLib.Cancellable? cancellable = null) throws GLib.Error
-  {
-    file = f;
-    save (cancellable);
-    return true;
-  }
+  public abstract bool save_as (GLib.File f, GLib.Cancellable? cancellable = null) throws GLib.Error;
   /**
    * Creates a new { link GXml.Document} using default implementation class.
    *
diff --git a/gxml/TwDocument.vala b/gxml/TwDocument.vala
index 987bf93..2780beb 100644
--- a/gxml/TwDocument.vala
+++ b/gxml/TwDocument.vala
@@ -66,6 +66,7 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
   public bool indent { get; set; default = false; }
   public bool ns_top { get; set; default = false; }
   public bool prefix_default_ns { get; set; default = false; }
+  public bool backup { get; set; default = true; }
   public GLib.File file { get; set; }
   public GXml.Node root {
     get {
@@ -112,13 +113,17 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
   }
   public bool save (GLib.Cancellable? cancellable = null)
     throws GLib.Error
+    requires (file != null)
   {
-    if (file == null) return false;
-    return save_to (file, cancellable);
+    return save_as (file, cancellable);
   }
+  [Deprecated (since="0.8.1", replacement="save_as")]
   public bool save_to (GLib.File f, GLib.Cancellable? cancellable = null)
   {
-    file = f;
+    return save_as (f, cancellable);
+  }
+  public bool save_as (GLib.File f, GLib.Cancellable? cancellable = null)
+  {
     var buf = new Xml.Buffer ();
     var tw = Xmlx.new_text_writer_memory (buf, 0);
     GLib.Test.message ("Writing down to buffer");
@@ -130,18 +135,10 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
     try {
       GLib.Test.message ("Writing down to file: Creating input stream");
       var b = new GLib.MemoryInputStream.from_data (s.data, null);
-      GLib.OutputStream ostream;
-      if (file.query_exists ()) {
       GLib.Test.message ("Writing down to file: Replacing with backup");
-        ostream = file.replace (null, true, GLib.FileCreateFlags.NONE, cancellable);
-        ostream.splice (b, GLib.OutputStreamSpliceFlags.NONE);
-        ostream.close ();
-      } else {
-        GLib.Test.message ("Writing down to file: Creating a new File");
-        ostream = file.create (GLib.FileCreateFlags.NONE, cancellable);
-        ostream.splice (b, GLib.OutputStreamSpliceFlags.NONE);
-        ostream.close ();
-      }
+      var ostream = f.replace (null, backup, GLib.FileCreateFlags.NONE, cancellable);
+      ostream.splice (b, GLib.OutputStreamSpliceFlags.NONE);
+      ostream.close ();
     } catch (GLib.Error e) {
       GLib.warning ("Error on Save to file: "+e.message);
       return false;
diff --git a/gxml/libxml-Document.vala b/gxml/libxml-Document.vala
index dfe2790..95542f9 100644
--- a/gxml/libxml-Document.vala
+++ b/gxml/libxml-Document.vala
@@ -266,12 +266,12 @@ namespace GXml {
                        Xml.Node *root;
 
                        if (doc == null) // should be impossible
-                               GXml.warning (DomException.INVALID_DOC, "Failed to parse document, xmlDoc* 
was NULL");
+                               GXml.exception (DomException.INVALID_DOC, "Failed to parse document, xmlDoc* 
was NULL");
 
                        if (require_root) {
                                root = doc->get_root_element ();
                                if (root == null) {
-                                       GXml.warning (DomException.INVALID_ROOT, "Could not obtain a valid 
root for the document; xmlDoc*'s root was NULL");
+                                       GXml.exception (DomException.INVALID_ROOT, "Could not obtain a valid 
root for the document; xmlDoc*'s root was NULL");
                                }
                        }
 
@@ -302,13 +302,13 @@ namespace GXml {
                        Xml.ParserCtxt ctxt;
                        Xml.Doc *doc;
                        Xml.Error *e;
-
                        ctxt = new Xml.ParserCtxt ();
+                       Xmlx.context_reset_last_error (ctxt);
                        doc = ctxt.read_file (file_path, null /* encoding */, 0 /* options */);
 
                        if (doc == null) {
-                               e = Xmlx.parser_context_get_last_error (ctxt);
-                               GXml.warning (DomException.INVALID_DOC, _("Could not load document from path: 
%s").printf (e->message));
+                               e = Xmlx.context_get_last_error (ctxt);
+                               GXml.exception (DomException.INVALID_DOC, _("Could not load document from 
path: %s").printf (e->message));
                                throw new GXml.Error.PARSER (GXml.libxml2_error_to_string (e));
                        }
 
@@ -404,16 +404,16 @@ namespace GXml {
                 * @throws GLib.Error A { link GLib.Error} if an error cocurs while reading the { link 
GLib.File}
                 * @throws GXml.Error A { link GXml.Error} if an error occurs while reading the file as a 
stream
                 */
-               public xDocument.from_gfile (File fin, Cancellable? can = null) throws GXml.Error, GLib.Error 
{
-                       // TODO: actually handle cancellable
+               public xDocument.from_gfile (File fin, Cancellable? can = null) throws GXml.Error, GLib.Error
+                       requires (fin.query_exists ()) {
                        InputStream instream;
 
                        try {
-                               instream = fin.read (null);
+                               instream = fin.read (can);
                                this.from_stream (instream, can);
                                instream.close ();
                        } catch (GLib.Error e) {
-                               GXml.warning (DomException.INVALID_DOC, "Could not load document from GFile: 
" + e.message);
+                               GXml.exception (DomException.INVALID_DOC, "Could not load document from 
GFile: " + e.message);
                                throw e;
                        }
                }
@@ -432,37 +432,28 @@ namespace GXml {
                 * @throws GXml.Error A { link GXml.Error} if an error occurs while reading the stream
                 */
                public xDocument.from_stream (InputStream instream, Cancellable? can = null) throws 
GXml.Error {
-                       InputStreamBox box = { instream, can };
-                       Xml.Doc *doc;
-                       /* TODO: provide Cancellable as user data so we can actually
-                          cancel these */
-                       Xml.TextReader reader;
-                       Xml.Error *e;
+                       Xml.Error* e = null;
                        string errmsg = null;
-
-                       reader = new Xml.TextReader.for_io ((Xml.InputReadCallback)_ioread,
-                                                           (Xml.InputCloseCallback)_ioinclose,
-                                                           &box, "", null, 0);
-                       if (-1 == reader.read ()) {
-                               errmsg = "Error reading from stream";
-                       } else if (null == reader.expand ()) {
-                               errmsg = "Error expanding from stream";
-                       } else {
-                               // yay
-                               doc = reader.current_doc ();
-                               reader.close ();
-                               this.from_libxml2 (doc);
-
-                               return;
+                       var ostream = new MemoryOutputStream.resizable ();
+                       ostream.splice (instream, GLib.OutputStreamSpliceFlags.NONE);
+                       if (ostream.data == null) {
+                               errmsg = "Document is empty";
+                               GXml.exception (DomException.INVALID_DOC, errmsg);
+                               throw new GXml.Error.PARSER (errmsg);
                        }
-
-                       // uh oh
+                       Xmlx.reset_last_error ();
                        e = Xmlx.get_last_error ();
-                       if (e != null) {
-                               errmsg += ".  " + libxml2_error_to_string (e);
+                       var doc = Xml.Parser.parse_memory ((string) ostream.data, ((string) 
ostream.data).length);
+                       if (doc != null) {
+                               this.from_libxml2 (doc);
+                       } else {
+                               e = Xmlx.get_last_error ();
+                               if (e != null) {
+                                       errmsg += ".  " + libxml2_error_to_string (e);
+                               }
+                               GXml.exception (DomException.INVALID_DOC, errmsg);
+                               throw new GXml.Error.PARSER (errmsg);
                        }
-                       GXml.warning (DomException.INVALID_DOC, errmsg);
-                       throw new GXml.Error.PARSER (errmsg);
                }
 
                /**
@@ -474,9 +465,10 @@ namespace GXml {
                 */
                public xDocument.from_string (string xml) {
                        Xml.Doc *doc;
+                       Xmlx.reset_last_error ();
                        doc = Xml.Parser.parse_memory (xml, (int)xml.length);
                        if (doc == null)
-                         doc = new Xml.Doc ();
+                               doc = new Xml.Doc ();
                        this.from_libxml2 (doc);
                }
                /**
@@ -494,8 +486,15 @@ namespace GXml {
                                                          int options = 0)
                {
                  Xml.Doc *doc;
+                       Xmlx.reset_last_error ();
                  doc = Xml.Parser.read_memory (xml, (int)xml.length, url, encoding, options);
                  this.from_libxml2 (doc);
+                       var e = Xmlx.get_last_error ();
+                       if (e != null) {
+                               var errmsg = ".  " + libxml2_error_to_string (e);
+                               GXml.exception (DomException.INVALID_DOC, errmsg);
+                               throw new GXml.Error.PARSER (errmsg);
+                       }
                }
 
                /**
@@ -539,7 +538,7 @@ namespace GXml {
                        }
 
                        // TODO: use xmlGetLastError to get the real error message
-                       GXml.warning (DomException.X_OTHER, errmsg);
+                       GXml.exception (DomException.X_OTHER, errmsg);
                        throw new GXml.Error.WRITER (errmsg);
                }
 
@@ -556,38 +555,16 @@ namespace GXml {
                 *
                 * @throws GXml.Error A { link GXml.Error} is thrown if saving encounters an error
                 */
-               public void save_to_stream (OutputStream outstream, Cancellable? can = null) throws 
GXml.Error {
-                       OutputStreamBox box = { outstream, can };
-                       string errmsg = null;
-                       Xml.Error *e;
-
-                       /* TODO: provide Cancellable as user data and let these check it
-                                so we can actually be interruptible */
-                       Xml.SaveCtxt *ctxt;
-                       ctxt = new Xml.SaveCtxt.to_io ((Xml.OutputWriteCallback)_iowrite,
-                                                      (Xml.OutputCloseCallback)_iooutclose,
-                                                      &box, null, 0);
-                       if (ctxt == null) {
-                               errmsg = "Failed to create serialization context when saving to stream";
-                       } else if (-1 == ctxt->save_doc (this.xmldoc)) {
-                               errmsg = "Failed to save document";
-                       } else if (-1 == ctxt->flush ()) {
-                               errmsg = "Failed to flush remainder of document while saving to stream";
-                       } else if (-1 == ctxt->close ()) {
-                               errmsg = "Failed to close saving context when saving to stream";
-                       } else {
-                               /* success! */
-                               return;
-                       }
-
-                       /* uh oh */
-                       e = Xmlx.get_last_error ();
-                       if (e != null) {
-                               errmsg += ".  " + libxml2_error_to_string (e);
+               public void save_to_stream (OutputStream outstream, Cancellable? can = null) throws 
GLib.Error {
+                       try {
+                               var s = new GLib.StringBuilder ();
+                               s.append (to_string ());
+                               var istream = new GLib.MemoryInputStream.from_data (s.data, null);
+                               outstream.splice (istream, GLib.OutputStreamSpliceFlags.NONE);
+                       } catch (GLib.Error e) {
+                               GXml.exception (DomException.X_OTHER, e.message);
+                               throw new GXml.Error.WRITER (e.message);
                        }
-
-                       GXml.warning (DomException.X_OTHER, errmsg);
-                       throw new GXml.Error.WRITER (errmsg);
                }
 
                /* Public Methods */
@@ -851,7 +828,7 @@ namespace GXml {
                 */
                private void check_not_supported_html (string feature) {
                        if (this.doctype != null && (this.doctype.name.casefold () == "html".casefold ())) {
-                               GXml.warning (DomException.NOT_SUPPORTED, "HTML documents do not support 
'%s'".printf (feature)); // TODO: i18n
+                               GXml.exception (DomException.NOT_SUPPORTED, "HTML documents do not support 
'%s'".printf (feature)); // TODO: i18n
                        }
                }
 
@@ -861,7 +838,7 @@ namespace GXml {
                internal static bool check_invalid_characters (string name, string subject) {
                        /* TODO: use Xml.validate_name instead  */
                        if (Xmlx.validate_name (name, 0) != 0) { // TODO: define validity
-                               GXml.warning (DomException.INVALID_CHARACTER, "Provided name '%s' for '%s' is 
not a valid XML name".printf (name, subject));
+                               GXml.exception (DomException.INVALID_CHARACTER, "Provided name '%s' for '%s' 
is not a valid XML name".printf (name, subject));
                                return false;
                        }
 
@@ -980,13 +957,13 @@ namespace GXml {
                                if (xmldoc->get_root_element () == null) {
                                        xmldoc->set_root_element (((xElement)new_child).node);
                                } else {
-                                       GXml.warning (DomException.HIERARCHY_REQUEST, "Document already has a 
root element.  Could not add child element with name '%s'".printf (new_child.node_name));
+                                       GXml.exception (DomException.HIERARCHY_REQUEST, "Document already has 
a root element.  Could not add child element with name '%s'".printf (new_child.node_name));
                                }
                        } else if (new_child.node_type == NodeType.DOCUMENT_TYPE) {
                                if (this.doctype == null) {
                                        this.doctype = (xDocumentType)new_child;
                                } else {
-                                       GXml.warning (DomException.HIERARCHY_REQUEST, "Document already has a 
doctype.  Could not add new doctype with name '%s'.".printf (((xDocumentType)new_child).name));
+                                       GXml.exception (DomException.HIERARCHY_REQUEST, "Document already has 
a doctype.  Could not add new doctype with name '%s'.".printf (((xDocumentType)new_child).name));
                                }
                        } else {
                                return this.child_nodes.append_child (new_child);
@@ -1011,6 +988,7 @@ namespace GXml {
                public bool indent { get; set; default = false; }
                public bool ns_top { get; set; default = false; }
                public bool prefix_default_ns { get; set; default = false; }
+               public bool backup { get; set; default = true; }
                public GLib.File file { get; set; }
                public virtual GXml.Node root { get { return document_element; } }
                public GXml.Node create_text (string str) { return (GXml.Node) this.create_text_node (str); }
@@ -1022,7 +1000,13 @@ namespace GXml {
                        requires (file != null)
                        requires (file.query_exists ())
                {
-                       save_to_path (file.get_path ());
+                       return save_as (file, cancellable);
+               }
+               public bool save_as (GLib.File f, GLib.Cancellable? cancellable = null) throws GLib.Error
+               {
+                       var ostream = f.replace (null, backup, GLib.FileCreateFlags.NONE, cancellable);
+                       GLib.message ("Saving...");
+                       save_to_stream (ostream);
                        return true;
                }
        }
diff --git a/gxml/libxml-DomException.vala b/gxml/libxml-DomException.vala
index 1c40eed..3ac99bc 100644
--- a/gxml/libxml-DomException.vala
+++ b/gxml/libxml-DomException.vala
@@ -34,9 +34,22 @@ namespace GXml {
         * @param exception rised
         * @param message message to log
         */
-       public static void warning (GXml.DomException exception, string message) {
-               GXml.last_error = exception;
+       [Deprecated (since="0.8.1", replacement="exeption")]
+       public static void warning (GXml.DomException ex, string message) {
+               GXml.exception (ex, message);
+       }
+
+       /**
+        * Log DOM exception warnings.
+        * 
+        * @param exception rised
+        * @param message message to log
+        */
+       public static void exception (GXml.DomException ex, string message) {
+               GXml.last_error = ex;   
+#if DEBUG
                GLib.warning ("%s", message);
+#endif
        }
 
        /**
diff --git a/gxml/xlibxml.c b/gxml/xlibxml.c
index 887338a..eac9868 100644
--- a/gxml/xlibxml.c
+++ b/gxml/xlibxml.c
@@ -33,16 +33,36 @@ int gxml_validate_name (xmlChar* name, int space)
   return xmlValidateName (name, space);
 }
 
+/**
+ * gxml_parser_context_get_last_error:
+ *
+ * Deprecated: Since 0.8.1
+ */
 xmlErrorPtr gxml_parser_context_get_last_error (void* ctx)
 {
+  return gxml_context_get_last_error (ctx);
+}
+
+xmlErrorPtr gxml_context_get_last_error (void* ctx)
+{
   return xmlCtxtGetLastError (ctx);
 }
 
+void gxml_context_reset_last_error (void* ctx)
+{
+  xmlCtxtResetLastError (ctx);
+}
+
 xmlErrorPtr gxml_get_last_error ()
 {
   return xmlGetLastError ();
 }
 
+void gxml_reset_last_error ()
+{
+  xmlResetLastError ();
+}
+
 xmlNsPtr* gxml_doc_get_ns_list (xmlDoc* doc, xmlNode* node)
 {
   g_return_if_fail (doc != NULL);
@@ -76,3 +96,4 @@ int gxml_text_writer_write_pi (xmlTextWriterPtr tw, const xmlChar* target, const
   g_return_if_fail (data != NULL);
   return xmlTextWriterWritePI (tw, target, data);
 }
+
diff --git a/gxml/xlibxml.h b/gxml/xlibxml.h
index ea173e7..a1431fe 100644
--- a/gxml/xlibxml.h
+++ b/gxml/xlibxml.h
@@ -29,7 +29,10 @@
 void*       gxml_doc_get_intsubset_entities    (xmlDoc *doc);
 int         gxml_validate_name                 (xmlChar* name, int space);
 xmlErrorPtr gxml_parser_context_get_last_error (void* ctx);
+xmlErrorPtr gxml_context_get_last_error        (void* ctx);
+void        gxml_context_reset_last_error      (void* ctx);
 xmlErrorPtr gxml_get_last_error                ();
+void        gxml_reset_last_error              ();
 xmlNsPtr*   gxml_doc_get_ns_list               (xmlDoc* doc, xmlNode* node);
 xmlTextWriterPtr gxml_new_text_writer_doc      (xmlDoc** doc);
 xmlTextWriterPtr gxml_new_text_writer_memory   (xmlBufferPtr buffer, gint compression);
diff --git a/test/DocumentTest.vala b/test/DocumentTest.vala
index 5d1d07b..931b2c0 100644
--- a/test/DocumentTest.vala
+++ b/test/DocumentTest.vala
@@ -65,7 +65,7 @@ class DocumentTest : GXmlTest {
                Test.add_func ("/gxml/document/construct_from_path_error", () => {
                                xDocument doc;
                                try {
-                               GLib.message ("invalid file...");
+                               GLib.Test.message ("invalid file...");
                                        // file does not exist
                                        doc = new xDocument.from_path ("/tmp/asdfjlkansdlfjl");
                                        assert_not_reached ();
@@ -73,7 +73,7 @@ class DocumentTest : GXmlTest {
                                        assert (e is GXml.Error.PARSER);
                                }
                                test_error (DomException.INVALID_DOC);
-                               GLib.message ("invalid is directory...");
+                               GLib.Test.message ("invalid is directory...");
 
                                try {
                                        // file exists, but is not XML (it's a directory!)
@@ -83,7 +83,7 @@ class DocumentTest : GXmlTest {
                                        assert (e is GXml.Error.PARSER);
                                }
                                test_error (DomException.INVALID_DOC);
-                               GLib.message ("invalid xml...");
+                               GLib.Test.message ("invalid xml...");
                                try {
                                        doc = new xDocument.from_path ("test_invalid.xml");
                                        assert_not_reached ();
@@ -93,23 +93,38 @@ class DocumentTest : GXmlTest {
                                test_error (DomException.INVALID_DOC);
                        });
                Test.add_func ("/gxml/document/construct_from_stream", () => {
-                               File fin;
-                               InputStream instream;
-                               xDocument doc;
-
+                               var fin = File.new_for_path (GXmlTestConfig.TEST_DIR + "/test.xml");
+                               assert (fin.query_exists ());
                                try {
-                                       fin = File.new_for_path (GXmlTest.get_test_dir () + "/test.xml");
-                                       instream = fin.read (null);
-                                       /* TODO: test GCancellable */
-
-                                       doc = new xDocument.from_stream (instream);
-
+                                       var instream = fin.read (null);
+                                       var doc = new xDocument.from_stream (instream);
+                                       assert (doc != null);
                                        check_contents (doc);
                                } catch (GLib.Error e) {
-                                       Test.message ("%s", e.message);
+                                       GLib.message ("%s", e.message);
                                        assert_not_reached ();
                                }
                        });
+               Test.add_func ("/gxml/document/gfile/local", () => {
+                       try {
+                               var f = GLib.File.new_for_path 
(GXmlTestConfig.TEST_SAVE_DIR+"/tw-test-file.xml");
+                               if (f.query_exists ()) f.delete ();
+                               var s = new GLib.StringBuilder ();
+                               s.append ("""<root />""");
+                               var d = new xDocument.from_string (s.str);
+                               GLib.message ("Saving to file: "+f.get_uri ()+d.to_string ());
+                               d.save_as (f);
+                               assert (f.query_exists ());
+                               var d2 = new xDocument.from_gfile (f);
+                               assert (d2 != null);
+                               assert (d2.root != null);
+                               assert (d2.root.name == "root");
+                               f.delete ();
+                       } catch (GLib.Error e) {
+                               GLib.message ("Error: "+e.message);
+                               assert_not_reached ();
+                       }
+                       });
                Test.add_func ("/gxml/document/construct_from_stream_error", () => {
                                File fin;
                                InputStream instream;
@@ -118,13 +133,13 @@ class DocumentTest : GXmlTest {
 
                                try {
                                        fin = File.new_tmp ("gxml.XXXXXX", out iostream);
-                                       instream = fin.read (null);
-                                       doc = new xDocument.from_stream (instream);
+                                       doc = new xDocument.from_stream (iostream.input_stream);
+                                       GLib.message ("Passed parse error stream");
                                        assert_not_reached ();
                                } catch (GXml.Error e) {
                                        assert (e is GXml.Error.PARSER);
                                } catch (GLib.Error e) {
-                                       stderr.printf ("Test encountered unexpected error '%s'\n", e.message);
+                                       GLib.message ("Test encountered unexpected error '%s'\n", e.message);
                                        assert_not_reached ();
                                }
                                test_error (DomException.INVALID_DOC);
@@ -144,6 +159,7 @@ class DocumentTest : GXmlTest {
                                assert (root.last_child.node_name == "Orange");
                        });
                Test.add_func ("/gxml/document/construct_from_string_no_root", () => {
+                       try {
                                string xml;
                                xDocument doc;
                                GXml.xNode root;
@@ -154,6 +170,10 @@ class DocumentTest : GXmlTest {
                                assert (doc != null);
                                root = doc.document_element;
                                assert (root == null);
+                       } catch (GLib.Error e) {
+                               GLib.message ("Error: "+ e.message);
+                               assert_not_reached ();
+                       }
                        });
                Test.add_func ("/gxml/document/construct_from_string_invalid", () => {
                                string xml;
diff --git a/test/TwDocumentTest.vala b/test/TwDocumentTest.vala
index 4be9429..600e994 100644
--- a/test/TwDocumentTest.vala
+++ b/test/TwDocumentTest.vala
@@ -317,15 +317,17 @@ class TwDocumentTest : GXmlTest {
                                }
                                assert (d.root.childs.size == 30000);
                                d.save ();
-                               var istream = f.read ();
-                               uint8[] buffer = new uint8[2048];
-                               istream.read (buffer, null);
-                               istream.close ();
-                               assert ("<?xml version=\"1.0\"?>" in ((string)buffer));
-                               assert ("<bookstore name=\"The Great Book\">" in ((string)buffer));
-                               assert ("<book>" in ((string)buffer));
-                               assert ("<Authors>" in ((string)buffer));
-                               assert ("<Author>" in ((string)buffer));
+                               GLib.Test.message ("Reading saved file...");
+                               var fr = GLib.File.new_for_path 
(GXmlTestConfig.TEST_SAVE_DIR+"/tw-large.xml");
+                               assert (fr.query_exists ());
+                               var ostream = new MemoryOutputStream.resizable ();
+                               ostream.splice (fr.read (), GLib.OutputStreamSpliceFlags.NONE);
+                               assert ("<?xml version=\"1.0\"?>" in ((string)ostream.data));
+                               assert ("<bookstore name=\"The Great Book\">" in ((string)ostream.data));
+                               assert ("<book>" in ((string)ostream.data));
+                               assert ("<Authors>" in ((string)ostream.data));
+                               assert ("<Author>" in ((string)ostream.data));
+                               f.delete ();
                        }
                        catch (GLib.Error e) {
 #if DEBUG
@@ -342,7 +344,7 @@ class TwDocumentTest : GXmlTest {
                                        ot.name = "test1";
                                        var dt = new TwDocument ();
                                        ot.serialize (dt);
-                                       dt.save_to (f);
+                                       dt.save_as (f);
                                        var d = new TwDocument ();
                                        var e = d.create_element ("root");
                                        d.childs.add (e);
@@ -350,7 +352,7 @@ class TwDocumentTest : GXmlTest {
                                        assert (d.root != null);
                                        assert (d.root.name == "root");
                                        assert (d.root.value == "");
-                                       d.save_to (f);
+                                       d.save_as (f);
                                        assert (f.query_exists ());
                                        var bf = GLib.File.new_for_path 
(GXmlTestConfig.TEST_SAVE_DIR+"/tw-test.xml~");
                                        assert (bf.query_exists ());
diff --git a/vapi/xlibxml-1.0.vapi b/vapi/xlibxml-1.0.vapi
index 282a962..9eb9c61 100644
--- a/vapi/xlibxml-1.0.vapi
+++ b/vapi/xlibxml-1.0.vapi
@@ -31,9 +31,16 @@ namespace Xmlx {
   [CCode (cname = "gxml_validate_name", cheader_filename = "gxml//xlibxml.h")]
   public static int validate_name (string name, int space);
   [CCode (cname = "gxml_parser_context_get_last_error", cheader_filename = "gxml/xlibxml.h")]
+  [Deprecated (replacement = "context_get_last_error", since = "0.8.1")]
   public static Xml.Error* parser_context_get_last_error (Xml.ParserCtxt ctx);
+  [CCode (cname = "gxml_context_get_last_error", cheader_filename = "gxml/xlibxml.h")]
+  public static Xml.Error* context_get_last_error (Xml.ParserCtxt ctx);
+  [CCode (cname = "gxml_context_reset_last_error", cheader_filename = "gxml/xlibxml.h")]
+  public static void context_reset_last_error (Xml.ParserCtxt ctx);
   [CCode (cname = "gxml_get_last_error", cheader_filename = "gxml/xlibxml.h")]
   public static Xml.Error* get_last_error ();
+  [CCode (cname = "gxml_reset_last_error", cheader_filename = "gxml/xlibxml.h")]
+  public static void reset_last_error ();
   [CCode (cname = "gxml_doc_get_ns_list", array_null_terminated = true, cheader_filename = "gxml/xlibxml.h")]
   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")]


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