[gxml] Gom: Added suppòrt for DOCTYPE definitions



commit 53270625d7431bde03581f724102a44f721e25f6
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Oct 24 10:02:14 2017 -0500

    Gom: Added suppòrt for DOCTYPE definitions

 gxml/GomDocument.vala     |    2 +-
 gxml/Parser.vala          |    8 +++-
 gxml/XParser.vala         |   23 +++++++++++--
 test/GomDocumentTest.vala |   82 ++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 108 insertions(+), 7 deletions(-)
---
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index 38ca907..f2bfc34 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -408,7 +408,7 @@ public class GXml.GomDocumentType : GXml.GomNode,
     _node_type = DomNode.NodeType.DOCUMENT_TYPE_NODE;
     _local_name = "!DOCTYPE";
   }
-  public GomDocumentType (DomDocument doc, string name, string public_id, string system_id) {
+  public GomDocumentType (DomDocument doc, string name, string? public_id, string? system_id) {
     _document = doc;
      _name = name;
     _public_id = public_id;
diff --git a/gxml/Parser.vala b/gxml/Parser.vala
index 78564d8..397df58 100644
--- a/gxml/Parser.vala
+++ b/gxml/Parser.vala
@@ -1,6 +1,7 @@
-/* TNode.vala
+/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
+/* Parser.vala
  *
- * Copyright (C) 2016  Daniel Espinosa <esodan gmail com>
+ * Copyright (C) 2016-2017  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
@@ -145,6 +146,9 @@ public interface GXml.Parser : Object {
   public virtual void read_child_nodes (DomNode parent) throws GLib.Error {
     bool cont = true;
     while (cont) {
+      if (parent is DomDocument) {
+        cont = read_child_node (parent);
+      }
       if (!move_next_node ()) return;
       if (current_is_element ())
         cont = read_child_element (parent);
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 017fbb9..bc33727 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -1,7 +1,7 @@
 /* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
 /* XParser.vala
  *
- * Copyright (C) 2016  Daniel Espinosa <esodan gmail com>
+ * Copyright (C) 2016-2017  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
@@ -226,6 +226,9 @@ public class GXml.XParser : Object, GXml.Parser {
     if (current_is_element () && (node is DomDocument))
       read_child_element (node);
     else {
+      if (node is GomDocument) {
+        read_child_nodes (node);
+      }
       if (node is GomElement) {
         if ((node as GomElement).parse_children)
           read_child_nodes (node);
@@ -417,7 +420,22 @@ public class GXml.XParser : Object, GXml.Parser {
       ret = false;
       break;
     case Xml.ReaderType.DOCUMENT_TYPE:
-      ret = false;
+      var bf = new Xml.Buffer ();
+      var xn = tr.current_node ();
+      bf.node_dump (xn->doc, xn, 0, 0);
+      string sids = bf.content ();
+      var reg = new GLib.Regex ("^<!DOCTYPE\\s*((([a-z]*|[A-Z]*))|(([a-z]*|[A-Z]*)\\s*PUBLIC\\s*(?<pid> 
\".*\")\\s*(?<sid> \".*\")))\\s*>");
+                       GLib.MatchInfo info = null;
+                       if (!reg.match (sids, RegexMatchFlags.ANCHORED, out info)) {
+                               warning (_("Invalid sequence for document type definition: "+sids));
+                               return false;
+                       }
+                       string pid = info.fetch_named ("pid");
+                       if (pid != null) pid = pid.replace ("\"","").chomp ().strip ();
+                       string sid = info.fetch_named ("sid");
+                       if (sid != null) sid = sid.replace ("\"","").chomp ().strip ();
+      n = new GomDocumentType (_document, tr.const_local_name (), pid, sid);
+      parent.append_child (n);
       break;
     case Xml.ReaderType.DOCUMENT_FRAGMENT:
       ret = false;
@@ -664,7 +682,6 @@ public class GXml.XParser : Object, GXml.Parser {
         tw.flush ();
     }
     if (n is GXml.DomDocumentType) {
-      message ("Document Type:");
       size += tw.write_document_type ((n as DomDocumentType).name,
                           (n as DomDocumentType).public_id,
                           (n as DomDocumentType).system_id,
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index f290bc0..4048177 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -608,7 +608,7 @@ class GomDocumentTest : GXmlTest {
                    assert_not_reached ();
                  }
                });
-               Test.add_func ("/gxml/gom-document/doc-type/create", () => {
+               Test.add_func ("/gxml/gom-document/doc-type/write/full", () => {
                        try {
                                var d = new GomDocument ();
                                message ("Creating a DocumentType");
@@ -629,6 +629,86 @@ class GomDocumentTest : GXmlTest {
                        } catch (GLib.Error e) {
                    GLib.message ("Error: "+e.message);
                    assert_not_reached ();
+                 } //<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd";>
+               });
+               Test.add_func ("/gxml/gom-document/doc-type/write/no-ids", () => {
+                       try {
+                               var d = new GomDocument ();
+                               message ("Creating a DocumentType");
+                               var dt1 = new GXml.GomDocumentType (d, "svg", null, null);
+                               assert (dt1 is DomDocumentType);
+                               assert (dt1.node_type == DomNode.NodeType.DOCUMENT_TYPE_NODE);
+                               assert (dt1.node_name == "!DOCTYPE");
+                               assert (dt1.name == "svg");
+                               assert (dt1.public_id == null);
+                               assert (dt1.system_id == null);
+                               d.append_child (dt1);
+                               assert (d.child_nodes.length == 1);
+                               var r = d.create_element ("svg");
+                               d.append_child (r);
+                               assert (d.child_nodes.length == 2);
+                               message (d.write_string ());
+                               assert ("<!DOCTYPE svg>" in d.write_string ());
+                       } catch (GLib.Error e) {
+                   GLib.message ("Error: "+e.message);
+                   assert_not_reached ();
+                 } //<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd";>
+               });
+               Test.add_func ("/gxml/gom-document/doc-type/read/full", () => {
+                       try {
+                               var d = new GomDocument.from_string ("<?xml version=\"1.0\"?>\n<!DOCTYPE svg 
PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\";><svg/>");
+                               message (d.write_string ());
+                               message (d.child_nodes.length.to_string ());
+                               assert (d.child_nodes.length == 2);
+                               var dt1 = d.child_nodes[0] as DomDocumentType;
+                               assert (dt1 != null);
+                               assert (dt1 is DomDocumentType);
+                               assert (dt1.node_type == DomNode.NodeType.DOCUMENT_TYPE_NODE);
+                               assert (dt1.node_name == "!DOCTYPE");
+                               assert (dt1.name == "svg");
+                               assert (dt1.public_id == "-//W3C//DTD SVG 1.1//EN");
+                               assert (dt1.system_id == "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd";);
+                       } catch (GLib.Error e) {
+                   GLib.message ("Error: "+e.message);
+                   assert_not_reached ();
+                 }
+               });
+               Test.add_func ("/gxml/gom-document/doc-type/read/spaces", () => {
+                       try {
+                               var d = new GomDocument.from_string ("<?xml version=\"1.0\"?>\n<!DOCTYPE     
svg     PUBLIC     \"-//W3C//DTD SVG 1.1//EN\"     \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\";    
<svg/>");
+                               message (d.write_string ());
+                               message (d.child_nodes.length.to_string ());
+                               assert (d.child_nodes.length == 2);
+                               var dt1 = d.child_nodes[0] as DomDocumentType;
+                               assert (dt1 != null);
+                               assert (dt1 is DomDocumentType);
+                               assert (dt1.node_type == DomNode.NodeType.DOCUMENT_TYPE_NODE);
+                               assert (dt1.node_name == "!DOCTYPE");
+                               assert (dt1.name == "svg");
+                               assert (dt1.public_id == "-//W3C//DTD SVG 1.1//EN");
+                               assert (dt1.system_id == "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd";);
+                       } catch (GLib.Error e) {
+                   GLib.message ("Error: "+e.message);
+                   assert_not_reached ();
+                 }
+               });
+               Test.add_func ("/gxml/gom-document/doc-type/read/no-ids", () => {
+                       try {
+                               var d = new GomDocument.from_string ("<?xml version=\"1.0\"?>\n<!DOCTYPE     
svg     ><svg/>");
+                               message (d.write_string ());
+                               message (d.child_nodes.length.to_string ());
+                               assert (d.child_nodes.length == 2);
+                               var dt1 = d.child_nodes[0] as DomDocumentType;
+                               assert (dt1 != null);
+                               assert (dt1 is DomDocumentType);
+                               assert (dt1.node_type == DomNode.NodeType.DOCUMENT_TYPE_NODE);
+                               assert (dt1.node_name == "!DOCTYPE");
+                               assert (dt1.name == "svg");
+                               assert (dt1.public_id == null);
+                               assert (dt1.system_id == null);
+                       } catch (GLib.Error e) {
+                   GLib.message ("Error: "+e.message);
+                   assert_not_reached ();
                  }
                });
        }


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