[gxml] GomElement,Parser: added async methods



commit 80da7d36a41ef252daf016de020d945f3bef66c5
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Jul 10 22:53:58 2017 -0500

    GomElement,Parser: added async methods
    
    GomElement
    read_from_uri_async(), read_from_file_async(),
    read_from_stream_async(), read_from_string_async()
    
    Parser
    write_stream_async(), create_stream_async()

 gxml/GomElement.vala |   29 +++++++++++++++++++++++
 gxml/Parser.vala     |   17 +++++++++++--
 gxml/XParser.vala    |   62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 3 deletions(-)
---
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index f1224db..6a64113 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -55,6 +55,12 @@ public class GXml.GomElement : GomNode,
     this.read_from_file (File.new_for_uri (uri));
   }
   /**
+   * Parsing asinchronically a URI file.
+   */
+  public async void read_from_uri_async (string uri) throws GLib.Error {
+    yield this.read_from_file_async (File.new_for_uri (uri));
+  }
+  /**
    * Parses an XML file, deserializing it over {@link GomElement}.
    */
   public void read_from_file (GLib.File f,
@@ -63,6 +69,14 @@ public class GXml.GomElement : GomNode,
     parser.read_file (f, cancellable);
   }
   /**
+   * Parses asinchronically an XML file, deserializing it over {@link GomElement}.
+   */
+  public async void read_from_file_async (GLib.File f,
+                      GLib.Cancellable? cancellable = null) throws GLib.Error {
+    var parser = new XParser (this);
+    yield parser.read_file_async (f, cancellable);
+  }
+  /**
    * Parses an XML over a {@link GLib.InputStream}, deserializing it over {@link GomElement}.
    */
   public void read_from_stream (GLib.InputStream istream,
@@ -71,6 +85,14 @@ public class GXml.GomElement : GomNode,
     parser.read_stream (istream, cancellable);
   }
   /**
+   * Parses asynchronically an XML over a {@link GLib.InputStream}, deserializing it over {@link GomElement}.
+   */
+  public async void read_from_stream_async (GLib.InputStream istream,
+                      GLib.Cancellable? cancellable = null) throws GLib.Error {
+    var parser = new XParser (this);
+    yield parser.read_stream_async (istream, cancellable);
+  }
+  /**
    * Parses an XML string, deserializing it over {@link GomElement}.
    */
   public void read_from_string (string str) throws GLib.Error {
@@ -78,6 +100,13 @@ public class GXml.GomElement : GomNode,
     parser.read_string (str, null);
   }
   /**
+   * Parses an XML string, deserializing it over {@link GomElement}.
+   */
+  public async void read_from_string_async (string str) throws GLib.Error {
+    var parser = new XParser (this);
+    yield parser.read_string_async (str, null);
+  }
+  /**
    * Serialize {@link GomElement} to a string.
    */
   public string write_string () throws GLib.Error {
diff --git a/gxml/Parser.vala b/gxml/Parser.vala
index 0dad953..089d250 100644
--- a/gxml/Parser.vala
+++ b/gxml/Parser.vala
@@ -70,11 +70,16 @@ public interface GXml.Parser : Object {
   public abstract void write_stream (OutputStream stream,
                                     GLib.Cancellable? cancellable) throws GLib.Error;
   /**
-   * Writes a {@link GXml.DomDocument} to a {@link GLib.OutputStream}
+   * Writes asynchronically a {@link node} to a {@link GLib.OutputStream}
+   */
+  public abstract async void write_stream_async (OutputStream stream,
+                            GLib.Cancellable? cancellable = null) throws GLib.Error;
+  /**
+   * Writes a {@link node} to a {@link GLib.OutputStream}
    */
   public virtual void read_file (GLib.File file,
-                                    GLib.Cancellable? cancellable)
-                                    throws GLib.Error {
+                                GLib.Cancellable? cancellable)
+                                throws GLib.Error {
     if (!file.query_exists ())
       throw new GXml.ParserError.INVALID_FILE_ERROR (_("File doesn't exist"));
     read_stream (file.read (), cancellable);
@@ -119,6 +124,12 @@ public interface GXml.Parser : Object {
    */
   public abstract InputStream create_stream (GLib.Cancellable? cancellable = null) throws GLib.Error;
   /**
+   * Creates asyncronically an {@link GLib.InputStream} to write a string representation
+   * in XML
+   */
+  public abstract async InputStream
+  create_stream_async (GLib.Cancellable? cancellable = null) throws GLib.Error;
+  /**
    * Iterates in all child nodes and append them to node.
    */
   public virtual void read_child_nodes (DomNode parent) throws GLib.Error {
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index b44b020..bc0c215 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -73,6 +73,38 @@ public class GXml.XParser : Object, GXml.Parser {
     stream.close ();
     tw = null;
   }
+  public async void write_stream_async (OutputStream stream,
+                            GLib.Cancellable? cancellable = null) throws GLib.Error {
+    var buf = new Xml.Buffer ();
+    tw = new TextWriter.memory (buf);
+    if (_node is DomDocument) tw.start_document ();
+    tw.set_indent (indent);
+    Idle.add (write_stream_async.callback);
+    yield;
+    // Root
+    if (_node is DomDocument) {
+      if ((_node as DomDocument).document_element == null)
+        tw.end_document ();
+    }
+    Idle.add (write_stream_async.callback);
+    yield;
+    start_node (_node);
+    tw.end_element ();
+    tw.end_document ();
+    Idle.add (write_stream_async.callback);
+    yield;
+    tw.flush ();
+    Idle.add (write_stream_async.callback);
+    yield;
+    var s = new GLib.StringBuilder ();
+    s.append (buf.content ());
+    Idle.add (write_stream_async.callback);
+    yield;
+    var b = new GLib.MemoryInputStream.from_data (s.data, null);
+    stream.splice (b, GLib.OutputStreamSpliceFlags.NONE);
+    stream.close ();
+    tw = null;
+  }
 
   /**
    * Creates an {@link GLib.InputStream} to write a string representation
@@ -98,6 +130,36 @@ public class GXml.XParser : Object, GXml.Parser {
     tw = null;
     return new GLib.MemoryInputStream.from_data ((uint8[]) s.str.dup (), null);
   }
+  /**
+   * Creates asynchronically an {@link GLib.InputStream} to write a string representation
+   * in XML
+   */
+  public async InputStream
+  create_stream_async (GLib.Cancellable? cancellable = null) throws GLib.Error {
+    var buf = new Xml.Buffer ();
+    tw = new TextWriter.memory (buf);
+    if (_node is DomDocument) tw.start_document ();
+    tw.set_indent (indent);
+    // Root
+    if (_node is DomDocument) {
+      if ((_node as DomDocument).document_element == null)
+        tw.end_document ();
+    }
+    Idle.add (create_stream_async.callback);
+    yield;
+    yield start_node_async (_node);
+    tw.end_element ();
+    tw.end_document ();
+    tw.flush ();
+    Idle.add (create_stream_async.callback);
+    yield;
+    var s = new GLib.StringBuilder ();
+    s.append (buf.content ());
+    tw = null;
+    Idle.add (create_stream_async.callback);
+    yield;
+    return new GLib.MemoryInputStream.from_data ((uint8[]) s.str.dup (), null);
+  }
 
   public string write_string () throws GLib.Error  {
     return dump ();


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