[gxml] GomDocument: Adding constructors



commit c62433ca307ebc4ca9ac9bebf8411f46dc31ae51
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Oct 31 10:33:21 2016 -0600

    GomDocument: Adding constructors

 gxml/GomDocument.vala     |   59 ++++++++++++++++++++++++++++++++++++++++----
 gxml/Parser.vala          |    3 +-
 gxml/XParser.vala         |    2 +-
 test/GomDocumentTest.vala |    7 ++++-
 4 files changed, 61 insertions(+), 10 deletions(-)
---
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index f5933af..3ff3c9e 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -30,11 +30,13 @@ public class GXml.GomDocument : GomNode,
 {
   // DomDocument implementation
   protected DomImplementation _implementation = new GomImplementation ();
-  protected string _url = "about:blank";
-  protected string _origin = "";
-  protected string _compat_mode = "";
-  protected string _character_set = "utf-8";
-  protected string _content_type = "application/xml";
+  protected string _url;
+  protected string _origin;
+  protected string _compat_mode;
+  protected string _character_set;
+  protected string _content_type;
+  protected Parser parser;
+  protected GXml.DomEvent _constructor;
   public DomImplementation implementation { get { return _implementation; } }
   public string url { get { return _url; } }
   public string document_uri { get { return _url; } }
@@ -58,6 +60,52 @@ public class GXml.GomDocument : GomNode,
     }
   }
 
+  construct {
+    _url = "about:blank";
+    _origin = "";
+    _compat_mode = "";
+    _character_set = "utf-8";
+    _content_type = "application/xml";
+    parser = new XParser (this);
+  }
+  public GomDocument () {}
+  public GomDocument.from_path (string path) {
+    var file = GLib.File.new_for_path (path);
+    if (!file.query_exists ()) return;
+    try { parser.read (file, null); }
+    catch (GLib.Error e) {
+      GLib.warning (_("Can't read document from path: ")+e.message);
+    }
+
+  }
+
+  public GomDocument.from_uri (string uri) {
+    this.from_file (File.new_for_uri (uri));
+  }
+
+  public GomDocument.from_file (GLib.File file) {
+    if (!file.query_exists ()) return;
+    try { parser.read (file, null); }
+    catch (GLib.Error e) {
+      GLib.warning (_("Can't read document from file: ")+e.message);
+    }
+  }
+
+  public GomDocument.from_stream (GLib.InputStream stream) {
+    try { parser.read_stream (stream, null); }
+    catch (GLib.Error e) {
+      GLib.warning (_("Can't read document from stream: ")+e.message);
+    }
+  }
+
+  public GomDocument.from_string (string str) {
+    try { parser.read_string (str, null); }
+    catch (GLib.Error e) {
+      GLib.warning (_("Can't read document from string: ")+e.message);
+    }
+  }
+
+
   public DomElement create_element (string local_name) throws GLib.Error {
       return new GomElement (this, local_name);
   }
@@ -152,7 +200,6 @@ public class GXml.GomDocument : GomNode,
       return (DomNode) dst;
   }
 
-  protected GXml.DomEvent _constructor;
   public DomEvent create_event (string iface) {
       var s = iface.down ();
       if (s == "customevent") _constructor = new GXml.GDomCustomEvent ();
diff --git a/gxml/Parser.vala b/gxml/Parser.vala
index f1f3384..588f805 100644
--- a/gxml/Parser.vala
+++ b/gxml/Parser.vala
@@ -57,7 +57,8 @@ public interface GXml.Parser : Object {
   /**
    * Read a {@link GXml.DomDocument} from a {@link GLib.File}
    */
-  public abstract void read_string (string str);
+  public abstract void read_string (string str,
+                                   GLib.Cancellable? cancellable) throws GLib.Error;
   /**
    * From data stream read until a node is found. You should check its type
    * using {@link current_type}
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index f7f707b..1dd7902 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -41,7 +41,7 @@ public class GXml.XParser : Object, GXml.Parser {
   public string write_string () throws GLib.Error  {
     return dump ();
   }
-  public void read_string (string str) {
+  public void read_string (string str, GLib.Cancellable? cancellable) throws GLib.Error {
     StringBuilder s = new StringBuilder (str);
     tr = new TextReader.for_memory ((char[]) s.data, (int) s.len, "/gxml_memory");
   }
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index 98f556c..8c4b57d 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -216,6 +216,8 @@ class GomDocumentTest : GXmlTest {
                        try {
                                DomDocument doc = new GDocument.from_string ("<document_element />");
                                DomText text = (DomText) doc.create_text_node ("Star of my dreams");
+                               assert (text is GomText);
+                               assert (text is DomText);
 
                                assert (text.node_name == "#text");
                                assert (text.node_value == "Star of my dreams");
@@ -232,8 +234,9 @@ class GomDocumentTest : GXmlTest {
                });
                Test.add_func ("/gxml/gom-document/create_processing_instruction", () => {
                        try {
-                               DomDocument doc = new GDocument.from_string ("<document_element />");
+                               DomDocument doc = new GomDocument.from_string ("<document_element />");
                                DomProcessingInstruction instruction = doc.create_processing_instruction 
("target", "data");
+                               assert (instruction is GomProcessingInstruction);
                                assert (instruction is DomProcessingInstruction);
                                assert (instruction.node_name == "target");
                                assert (instruction.node_value == "data");
@@ -241,8 +244,8 @@ class GomDocumentTest : GXmlTest {
                                GLib.message ("Dat:"+instruction.node_value);
                                assert (instruction.data == "data");
                                assert (instruction.target != null);
-                               assert (instruction.target == "target");
                                assert_not_reached ();
+                               assert (instruction.target == "target");
                        } catch { assert_not_reached (); }
                });
                Test.add_func ("/gxml/gom-document/create_attribute", () => {


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