[gxml] Parser added read_file_async()



commit 9161f6031a4ecc24d59abe764f319b69056b8dd9
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Jul 10 18:54:46 2017 -0500

    Parser added read_file_async()

 gxml/Parser.vala  |   33 +++++++++++++++++++++++++++++++++
 gxml/XParser.vala |   22 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 0 deletions(-)
---
diff --git a/gxml/Parser.vala b/gxml/Parser.vala
index 75ed70e..b0b95d7 100644
--- a/gxml/Parser.vala
+++ b/gxml/Parser.vala
@@ -75,12 +75,30 @@ public interface GXml.Parser : Object {
       throw new GXml.ParserError.INVALID_FILE_ERROR (_("File doesn't exist"));
     read_stream (file.read (), cancellable);
   }
+  /**
+   * Writes a {@link GXml.DomDocument} to a {@link GLib.OutputStream}
+   */
+  public async virtual void read_file_async (GLib.File file,
+                                    GLib.Cancellable? cancellable)
+                                    throws GLib.Error {
+    if (!file.query_exists ())
+      throw new GXml.ParserError.INVALID_FILE_ERROR (_("File doesn't exist"));
+    Idle.add (read_file_async.callback);
+    yield;
+    yield read_stream_async (file.read (), cancellable);
+  }
 
   /**
    * Read a {@link GXml.DomDocument} from a {@link GLib.InputStream}
    */
   public abstract void read_stream (InputStream stream,
                                    GLib.Cancellable? cancellable) throws GLib.Error;
+
+  /**
+   * Read a {@link GXml.DomDocument} from a {@link GLib.InputStream}
+   */
+  public async abstract void read_stream_async (InputStream stream,
+                                   GLib.Cancellable? cancellable) throws GLib.Error;
   /**
    * Read a {@link GXml.DomDocument} from a {@link GLib.File}
    */
@@ -105,6 +123,21 @@ public interface GXml.Parser : Object {
     }
   }
   /**
+   * Iterates in all child nodes and append them to node.
+   */
+  public async virtual void read_child_nodes_async (DomNode parent) throws GLib.Error {
+    bool cont = true;
+    while (cont) {
+      Idle.add (read_child_nodes_async.callback);
+      yield;
+      if (!move_next_node ()) return;
+      if (current_is_element ())
+        cont = read_child_element (parent);
+      else
+        cont = read_child_node (parent);
+    }
+  }
+  /**
    * Creates a new {@link DomNode} and append it to
    * parent: depending on current node's type found by parser.
    *
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index e86d708..51f40db 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -118,6 +118,16 @@ public class GXml.XParser : Object, GXml.Parser {
     read_node (_node);
     tr = null;
   }
+  public async void read_stream_async (GLib.InputStream istream,
+                          GLib.Cancellable? cancellable = null) throws GLib.Error {
+    var b = new MemoryOutputStream.resizable ();
+    b.splice (istream, 0);
+    Idle.add (read_stream_async.callback);
+    yield;
+    tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), "/gxml_memory");
+    read_node (_node);
+    tr = null;
+  }
 
   /**
    * Reads a node using current parser.
@@ -162,6 +172,18 @@ public class GXml.XParser : Object, GXml.Parser {
     read_child_nodes (_node);
     tr = null;
   }
+  public async void read_child_nodes_stream_async (GLib.InputStream istream,
+                          GLib.Cancellable? cancellable = null) throws GLib.Error {
+    var b = new MemoryOutputStream.resizable ();
+    b.splice (istream, 0);
+    Idle.add (read_child_nodes_stream_async.callback);
+    yield;
+    tr = new TextReader.for_memory ((char[]) b.data, (int) b.get_data_size (), "/gxml_memory");
+     Idle.add (read_child_nodes_stream_async.callback);
+    yield;
+    yield read_child_nodes_async (_node);
+    tr = null;
+  }
   /**
    * Reads all child nodes as string
    */


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