[gxml] Implementing TextWriter classes - base clases



commit b9958a0dbf93343a4e7cbf6e592cbee610294e80
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon May 4 19:07:53 2015 -0500

    Implementing TextWriter classes - base clases
    
    * Added Document.save()/save_as() methods
    * Consider to remove static functions from GXml.Document
    * Initial clases to use Xml.TextWriter to save documents

 gxml/Document.vala        |   54 ++++++++++++++++++++++++-----------
 gxml/Element.vala         |    2 +-
 gxml/Makefile.am          |    9 +++++-
 gxml/TwAttribute.vala     |   39 +++++++++++++++++++++++++
 gxml/TwComment.vala       |   36 +++++++++++++++++++++++
 gxml/TwDocument.vala      |   68 +++++++++++++++++++++++++++++++++++++++++++++
 gxml/TwElement.vala       |   44 +++++++++++++++++++++++++++++
 gxml/TwNamespace.vala     |   39 +++++++++++++++++++++++++
 gxml/TwNode.vala          |   56 +++++++++++++++++++++++++++++++++++++
 gxml/TwText.vala          |   36 +++++++++++++++++++++++
 gxml/libxml-Document.vala |    9 ++++-
 11 files changed, 371 insertions(+), 21 deletions(-)
---
diff --git a/gxml/Document.vala b/gxml/Document.vala
index bd394e7..a9419d7 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -103,38 +103,58 @@ public interface GXml.Document : Object, GXml.Node
    */
   public virtual void finalize_comment () { return; }
   /**
+   * Save this { link GXml.Document} to { link GXml.Document.file}
+   */
+  public abstract bool save (GLib.Cancellable? cancellable) 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) throws GLib.Error
+  {
+    if (f.query_exists ()) {
+      f = file;
+      save (cancellable);
+      return true;
+    }
+    return false;
+  }
+  /**
    * Creates a new { link GXml.Document} using default implementation class.
    *
    * As an interface you can create your own implementation of it, but if 
    * default one is required use this.
    */
-   public static GXml.Document new_default ()
-   {
-     return new xDocument ();
-   }
+  public static GXml.Document new_default ()
+  {
+   return new xDocument ();
+  }
   /**
    * Creates a new { link GXml.Document} from a file path using default implementation class.
    *
    * As an interface you can create your own implementation of it, but if 
    * default one is required use this.
    */
-   public static GXml.Document new_default_for_path (string path)
-     throws GLib.Error
-   {
-     File f = File.new_for_path (path);
-     return GXml.Document.new_default_for_file (f);
-   }
+  public static GXml.Document new_default_for_path (string path)
+   throws GLib.Error
+  {
+   File f = File.new_for_path (path);
+   return GXml.Document.new_default_for_file (f);
+  }
   /**
    * Creates a new { link GXml.Document} from a { link GLib.File} using default implementation class.
    *
    * As an interface you can create your own implementation of it, but if 
    * default one is required use this.
    */
-   public static GXml.Document new_default_for_file (GLib.File file)
-     throws GLib.Error
-   {
-     if (!file.query_exists ())
-       throw new DocumentError.INVALID_FILE ("Invalid file");
-     return new xDocument.from_path (file.get_path ());
-   }
+  public static GXml.Document new_default_for_file (GLib.File f)
+   throws GLib.Error
+  {
+    var d = new xDocument.from_path (f.get_path ());
+    if (!f.query_exists ())
+      throw new DocumentError.INVALID_FILE ("Invalid file");
+    d.file = f;
+    return d;
+  }
 }
diff --git a/gxml/Element.vala b/gxml/Element.vala
index dfda260..8eeeb70 100644
--- a/gxml/Element.vala
+++ b/gxml/Element.vala
@@ -1,5 +1,5 @@
 /* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 0; tab-width: 2 -*- */
-/* ObjectModel.vala
+/* Element.vala
  *
  * Copyright (C) 2015  Daniel Espinosa <esodan gmail com>
  *
diff --git a/gxml/Makefile.am b/gxml/Makefile.am
index d98f0d0..af97ed5 100644
--- a/gxml/Makefile.am
+++ b/gxml/Makefile.am
@@ -61,7 +61,14 @@ sources = \
        SerializableGeeDualKeyMap.vala \
        SerializableMapDualKey.vala \
        SerializableGeeArrayList.vala \
-       SerializableContainer.vala
+       SerializableContainer.vala \
+       TwAttribute.vala \
+       TwComment.vala \
+       TwDocument.vala \
+       TwElement.vala \
+       TwNamespace.vala \
+       TwNode.vala \
+       TwText.vala
 
 
 ### General Compilation flags
diff --git a/gxml/TwAttribute.vala b/gxml/TwAttribute.vala
new file mode 100644
index 0000000..7d6adb4
--- /dev/null
+++ b/gxml/TwAttribute.vala
@@ -0,0 +1,39 @@
+/* TwAttribute.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.TwAttribute : GXml.TwNode, GXml.Attribute
+{
+  private string _name = null;
+  private string _value = null;
+  public TwAttribute (GXml.Document d, string name, string value)
+    requires (d is TwDocument)
+  {
+    _doc = d;
+    ((TwDocument) document).tw = ((TwDocument) d).tw;
+    _name = name;
+    _value = value;
+  }
+  // GXml.Attribute
+  public override string name { get { return _name; } }
+  public override string @value { get { return _value; } set  { _value = value;} }
+}
diff --git a/gxml/TwComment.vala b/gxml/TwComment.vala
new file mode 100644
index 0000000..957d319
--- /dev/null
+++ b/gxml/TwComment.vala
@@ -0,0 +1,36 @@
+/* Element.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.TwComment : GXml.TwNode, GXml.Comment
+{
+  private string _str = "";
+  public TwComment (GXml.Document doc, string text)
+    requires (doc is GXml.TwDocument)
+  {
+    _doc = doc;
+    ((TwDocument) document).tw = ((TwDocument) doc).tw;
+    _str = text;
+  }
+  // GXml.Comment
+  public string str { get { return _str; } }
+}
diff --git a/gxml/TwDocument.vala b/gxml/TwDocument.vala
new file mode 100644
index 0000000..012c6c4
--- /dev/null
+++ b/gxml/TwDocument.vala
@@ -0,0 +1,68 @@
+/* Element.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;
+using Xml;
+
+public class GXml.TwDocument : GXml.TwNode, GXml.Document
+{
+  Gee.ArrayList<GXml.Node> _namespaces = new Gee.ArrayList<GXml.Node> ();
+  GXml.Element _root = null;
+  public TwDocument (string file)
+  {
+    var f = File.new_for_path (file);
+    this.file = f;
+    tw = new Xml.TextWriter.filename (file);
+    tw->start_document ();
+  }
+  // GXml.Node
+  public override bool set_namespace (string uri, string prefix)
+  {
+    _namespaces.add (new TwNamespace (this, uri, prefix));
+    return true;
+  }
+  public override GXml.Document document { get { return this; } }
+  // GXml.Document
+  public GXml.Node create_comment (string text)
+  {
+    var c = new TwComment (this, text);
+    if (root == null)
+      return c;
+    root.childs.add (c);
+    return c;
+  }
+  public GXml.Node create_element (string name)
+  {
+    return new TwElement (this, name);
+  }
+  public GXml.Node create_text (string text)
+  {
+    var t = new TwText (this, text);
+    return t;
+  }
+  public GLib.File file { get; set; }
+  public GXml.Node root { get { return _root; } }
+  public bool save (GLib.Cancellable? cancellable)
+  {
+    // TODO:
+    return false;
+  }
+}
diff --git a/gxml/TwElement.vala b/gxml/TwElement.vala
new file mode 100644
index 0000000..a400c73
--- /dev/null
+++ b/gxml/TwElement.vala
@@ -0,0 +1,44 @@
+/* Element.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.TwElement : GXml.TwNode, GXml.Element
+{
+  private string _content = null;
+  public TwElement (GXml.Document d, string name)
+    requires (d is TwDocument)
+  {
+    _doc = d;
+    ((TwDocument) document).tw = ((TwDocument) d).tw;
+  }
+  // GXml.Element
+  public void set_attr (string name, string value)
+  {
+    var att = new TwAttribute (document, name, value);
+    attrs.set (name, att);
+  }
+  public GXml.Node get_attr (string name) { return attrs.get (name); }
+  public void normalize () {}
+  public string content {
+    owned get { return _content; } set { } }
+  public string tag_name { get { return name; } }
+}
diff --git a/gxml/TwNamespace.vala b/gxml/TwNamespace.vala
new file mode 100644
index 0000000..cc987e0
--- /dev/null
+++ b/gxml/TwNamespace.vala
@@ -0,0 +1,39 @@
+/* TwAttribute.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.TwNamespace : GXml.TwNode, GXml.Namespace
+{
+  private string _uri = null;
+  private string _prefix = null;
+  public TwNamespace (GXml.Document d, string uri, string prefix)
+    requires (d is TwDocument)
+  {
+    _doc = d;
+    ((TwDocument) document).tw = ((TwDocument) d).tw;
+    _uri = uri;
+    _prefix = prefix;
+  }
+  // GXml.Namespace
+  public string uri { get { return _uri; } }
+  public string @prefix { get { return _prefix; } }
+}
diff --git a/gxml/TwNode.vala b/gxml/TwNode.vala
new file mode 100644
index 0000000..03a37ab
--- /dev/null
+++ b/gxml/TwNode.vala
@@ -0,0 +1,56 @@
+/* Element.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 abstract class GXml.TwNode : Object, GXml.Node
+{
+  Gee.HashMap<string,GXml.Node> _attrs = new Gee.HashMap<string,GXml.Node> ();
+  Gee.ArrayList<GXml.Node> _childs = new Gee.ArrayList<GXml.Node> ();
+  protected GXml.Document _doc;
+  internal Xml.TextWriter *tw;
+
+  // GXml.Node
+  public virtual bool set_namespace (string uri, string prefix)
+  {
+    bool found = false;
+    foreach (GXml.Namespace ns in document.namespaces) {
+      if (ns.uri == uri && ns.prefix == prefix) {
+        namespaces.add (ns);
+        found = true;
+      }
+    }
+    if (!found) {
+      var nns = new TwNamespace (document, uri, prefix);
+      document.namespaces.add (nns);
+      namespaces.add (nns);
+    }
+    return true;
+  }
+  public virtual string to_string () { return get_type ().name (); }
+  public virtual Gee.Map<string,GXml.Node> attrs { get { return _attrs; } }
+  public virtual Gee.BidirList<GXml.Node> childs { get { return _childs; } }
+  public virtual GXml.Document document { get { return _doc; } }
+  public virtual string name { get { return ""; } }
+  public virtual Gee.List<GXml.Namespace> namespaces { get { return document.namespaces; } }
+  public virtual GXml.NodeType type_node { get { return GXml.NodeType.DOCUMENT; } }
+  public virtual string value { get { return ""; } set  {} }
+}
diff --git a/gxml/TwText.vala b/gxml/TwText.vala
new file mode 100644
index 0000000..9a0f1f4
--- /dev/null
+++ b/gxml/TwText.vala
@@ -0,0 +1,36 @@
+/* TwText.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.TwText : GXml.TwNode, GXml.Text
+{
+  private string _str = null;
+  public TwText (GXml.Document d, string text)
+    requires (d is GXml.TwDocument)
+  {
+    _doc = d;
+    ((TwDocument) document).tw = ((TwDocument) d).tw;
+    _str = text;
+  }
+  // GXml.Text
+  public string str { get { return _str; } }
+}
diff --git a/gxml/libxml-Document.vala b/gxml/libxml-Document.vala
index f4139da..6872c11 100644
--- a/gxml/libxml-Document.vala
+++ b/gxml/libxml-Document.vala
@@ -1017,9 +1017,14 @@ namespace GXml {
                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); }
-               public GXml.Node create_comment (string text)
+               public GXml.Node create_comment (string text) { return create_managed_comment (text); }
+               public bool save (GLib.Cancellable? cancellable = null)
+                       throws GLib.Error
+                       requires (file != null)
+                       requires (file.query_exists ())
                {
-                       return create_managed_comment (text);
+                       save_to_path (file.get_path ());
+                       return true;
                }
        }
 }


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