[gxml] DomTreeWalker: DomNodeIterator: new implementation
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] DomTreeWalker: DomNodeIterator: new implementation
- Date: Thu, 11 Jul 2019 23:22:40 +0000 (UTC)
commit 09e7195cafb323bfe7f0cfef91b4f61cf3cecb69
Author: Daniel Espinosa <esodan gmail com>
Date: Thu Jul 11 18:19:45 2019 -0500
DomTreeWalker: DomNodeIterator: new implementation
gxml/Document.vala | 8 +++---
gxml/DomCollections.vala | 30 ++++++++++++++--------
gxml/DomDocument.vala | 19 ++++++++++----
gxml/NodeIterator.vala | 46 +++++++++++++++++++++++++++++++++
gxml/TreeWalker.vala | 43 ++++++++++++++-----------------
gxml/XDocument.vala | 66 ++++++------------------------------------------
gxml/meson.build | 2 ++
7 files changed, 113 insertions(+), 101 deletions(-)
---
diff --git a/gxml/Document.vala b/gxml/Document.vala
index b7839e9..0cc57c7 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -292,12 +292,12 @@ public class GXml.Document : GXml.Node,
}
// NodeFilter.SHOW_ALL = 0xFFFFFFFF
- public DomNodeIterator create_node_iterator (DomNode root, int what_to_show = (int) 0xFFFFFFFF,
DomNodeFilter? filter = null)
+ public DomNodeIterator create_node_iterator (DomNode root, int what_to_show = (int) 0xFFFFFFFF)
{
- return new GDomNodeIterator (root, what_to_show, filter);
+ return new NodeIterator (root, what_to_show);
}
- public DomTreeWalker create_tree_walker (DomNode root, int what_to_show = (int) 0xFFFFFFFF, DomNodeFilter?
filter = null) {
- return new GDomTreeWalker (root, what_to_show, filter);
+ public DomTreeWalker create_tree_walker (DomNode root, int what_to_show = (int) 0xFFFFFFFF) {
+ return new TreeWalker (root, what_to_show);
}
// DomParentNode
public DomHTMLCollection children {
diff --git a/gxml/DomCollections.vala b/gxml/DomCollections.vala
index 51652e8..b0a83a4 100644
--- a/gxml/DomCollections.vala
+++ b/gxml/DomCollections.vala
@@ -91,14 +91,21 @@ public interface GXml.DomHTMLCollection : GLib.Object, Gee.BidirList<GXml.DomEle
/**
- * No implemented jet. This can lead to API changes in future versions.
+ * Document's node iterator.
+ *
+ * If you want to filter while you go though the tree
+ * set a handler for the {@link accept_node} signal
*/
public interface GXml.DomNodeIterator : GLib.Object {
public abstract DomNode root { get; }
public abstract DomNode reference_node { get; }
public abstract bool pointer_before_reference_node { get; }
+ /**
+ * This is a value of {@link DomNodeFilter} contant SHOW.
+ */
public abstract int what_to_show { get; }
- public abstract DomNodeFilter? filter { get; }
+
+ public signal DomNodeFilter.Filter accept_node (DomNode node);
public abstract DomNode? next_node();
public abstract DomNode? previous_node();
@@ -110,10 +117,11 @@ public interface GXml.DomNodeIterator : GLib.Object {
* No implemented jet. This can lead to API changes in future versions.
*/
public class GXml.DomNodeFilter : GLib.Object {
- // Constants for acceptNode()
- public const int FILTER_ACCEPT = 1;
- public const int FILTER_REJECT = 2;
- public const int FILTER_SKIP = 3;
+ public enum Filter {
+ ACCEPT = 1,
+ REJECT,
+ SKIP
+ }
// Constants for whatToShow
public const int SHOW_ALL = (int) 0xFFFFFFFF;
@@ -129,20 +137,22 @@ public class GXml.DomNodeFilter : GLib.Object {
public const int SHOW_DOCUMENT_TYPE = (int) 0x200;
public const int SHOW_DOCUMENT_FRAGMENT = (int) 0x400;
public const int SHOW_NOTATION = (int) 0x800; // historical
-
- public delegate int accept_node (DomNode node); // FIXME: Should be a User defined method
}
/**
- * No implemented jet. This can lead to API changes in future versions.
+ * Document tree walker.
+ *
+ * If you want to filter while you go though the tree
+ * set a handler for the {@link accept_node} signal
*/
public interface GXml.DomTreeWalker : GLib.Object {
public abstract DomNode root { get; }
public abstract int what_to_show { get; }
- public abstract DomNodeFilter? filter { get; }
public abstract DomNode current_node { get; }
+ public signal DomNodeFilter.Filter accept_node (DomNode node);
+
public abstract DomNode? parent_node();
public abstract DomNode? first_child();
public abstract DomNode? last_child();
diff --git a/gxml/DomDocument.vala b/gxml/DomDocument.vala
index 55004b1..7361362 100644
--- a/gxml/DomDocument.vala
+++ b/gxml/DomDocument.vala
@@ -60,16 +60,25 @@ public interface GXml.DomDocument : GLib.Object,
*/
public abstract DomRange create_range();
- // NodeFilter.SHOW_ALL = 0xFFFFFFFF
/**
- * No implemented jet. This can lead to API changes in future versions.
+ * Creates a {@link DomNodeIterator}.
+ *
+ * Consider to connect to {@link DomNodeIterator.accept_node} in order to
+ * filter the nodes you iterate on.
+ *
+ * See at {@link DomNodeFilter} fot the constant value of @what_to_show
*/
- public abstract DomNodeIterator create_node_iterator (DomNode root, int whatToShow = (int) 0xFFFFFFFF,
DomNodeFilter? filter = null);
+ public abstract DomNodeIterator create_node_iterator (DomNode root, int whatToShow = (int) 0xFFFFFFFF);
/**
- * No implemented jet. This can lead to API changes in future versions.
+ * Creates a {@link DomTreeWalker}.
+ *
+ * Consider to connect to {@link DomTreeWalker.accept_node} in order to
+ * filter the nodes you iterate on.
+ *
+ * See at {@link DomNodeFilter} fot the constant value of @what_to_show
*/
- public abstract DomTreeWalker create_tree_walker (DomNode root, int what_to_show = (int) 0xFFFFFFFF,
DomNodeFilter? filter = null);
+ public abstract DomTreeWalker create_tree_walker (DomNode root, int what_to_show = (int) 0xFFFFFFFF);
/**
* Writes a dump XML representation of document to a file.
diff --git a/gxml/NodeIterator.vala b/gxml/NodeIterator.vala
new file mode 100644
index 0000000..18bcca2
--- /dev/null
+++ b/gxml/NodeIterator.vala
@@ -0,0 +1,46 @@
+/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
+/*
+ *
+ * Copyright (C) 2016-2019 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>
+ */
+
+/**
+ * Implementation of {@link DomNodeIterator}
+ */
+public class GXml.NodeIterator : GLib.Object, GXml.DomNodeIterator {
+ protected DomNode _root;
+ protected DomNode _reference_node;
+ protected bool _pointer_before_reference_node;
+ protected int _what_to_show;
+
+ public NodeIterator (DomNode n, int what_to_show) {
+ _root = n;
+ _reference_node = _root;
+ _what_to_show = what_to_show;
+ }
+ public DomNode root { get { return _root; } }
+ public DomNode reference_node { get { return _reference_node; } }
+ public bool pointer_before_reference_node { get { return _pointer_before_reference_node; } }
+ public int what_to_show { get { return _what_to_show; } }
+
+ public DomNode? next_node() { return null; } // FIXME
+ public DomNode? previous_node() { return null; } // FIXME
+
+ public void detach() { return; } // FIXME
+}
diff --git a/gxml/TreeWalker.vala b/gxml/TreeWalker.vala
index a4e0239..6c7c1b5 100644
--- a/gxml/TreeWalker.vala
+++ b/gxml/TreeWalker.vala
@@ -20,32 +20,30 @@
* Daniel Espinosa <esodan gmail com>
*/
-
-[Version (since = "0.18")]
+/**
+ * Implementation of {@link DomTreeWalker}
+ */
public class GXml.TreeWalker : GLib.Object, GXml.DomTreeWalker {
protected DomNode _root;
protected int _what_to_show;
- protected DomNodeFilter? _filter;
protected DomNode _current_node = null;
public DomNode root { get { return root; } }
public int what_to_show { get { return _what_to_show; } }
- public DomNodeFilter? filter { get { return _filter; } }
public DomNode current_node { get { return _current_node; } }
- public TreeWalker (DomNode root, DomNode current, int w, DomNodeFilter? filter) {
+ public TreeWalker (DomNode root, int w) {
_root = root;
_what_to_show = w;
- _filter = filter;
- _current_node = current;
+ _current_node = root;
}
public DomNode? parent_node() {
if (current_node == null) return null;
var p = current_node.parent_node;
if (p == null) return null;
- if (_filter != null) {
- if (_filter.accept_node (p) != DomNodeFilter.FILTER_ACCEPT) return null;
+ if (accept_node (p) != DomNodeFilter.Filter.ACCEPT) {
+ return null;
}
if (p == root) return null;
_current_node = p;
@@ -74,17 +72,15 @@ public class GXml.TreeWalker : GLib.Object, GXml.DomTreeWalker {
} else {
n = current_node.last_child;
}
- if¡ (n == null) return null;
+ if (n == null) return null;
while (n != null) {
- var res = DomNodeFilter.FILTER_ACCEPT
- if (_filter != null) {
- res = _filter.accept_node (n);
- }
- if (res == DomNodeFilter.FILTER_ACCEPT) {
+ var res = DomNodeFilter.Filter.ACCEPT;
+ res = accept_node (n);
+ if (res == DomNodeFilter.Filter.ACCEPT) {
_current_node = n;
return _current_node;
}
- if (res == DomNodeFilter.FILTER_SKIP) {
+ if (res == DomNodeFilter.Filter.SKIP) {
DomNode c = null;
if (first) {
c = n.first_child;
@@ -127,11 +123,9 @@ public class GXml.TreeWalker : GLib.Object, GXml.DomTreeWalker {
}
DomNode n = s;
while (n != null) {
- var res = DomNodeFilter.FILTER_ACCEPT;
- if (_filter != null) {
- res = _filter.accept_node (n);
- }
- if (res == DomNodeFilter.FILTER_ACCEPT) {
+ var res = DomNodeFilter.Filter.ACCEPT;
+ res = accept_node (n);
+ if (res == DomNodeFilter.Filter.ACCEPT) {
_current_node = n;
return _current_node;
}
@@ -141,7 +135,7 @@ public class GXml.TreeWalker : GLib.Object, GXml.DomTreeWalker {
} else {
c = n.last_child;
}
- if (res == DomNodeFilter.FILTER_REJECT || c == null) {
+ if (res == DomNodeFilter.Filter.REJECT || c == null) {
if (next) {
s = n.next_sibling;
} else {
@@ -150,9 +144,10 @@ public class GXml.TreeWalker : GLib.Object, GXml.DomTreeWalker {
}
n = n.parent_node;
if (n == null || n == root) return null;
- if (_filter != null) {
- if (_filter.accept_node (n) == DomNodeFilter.FILTER_ACCEPT) return null;
+ if (accept_node (n) == DomNodeFilter.Filter.ACCEPT) {
+ return null;
}
}
+ return s; // FIXME:
}
}
diff --git a/gxml/XDocument.vala b/gxml/XDocument.vala
index 7ea4f5e..644af2e 100644
--- a/gxml/XDocument.vala
+++ b/gxml/XDocument.vala
@@ -160,7 +160,7 @@ public class GXml.XDocument : GXml.XNode,
return true;
}
// DomDocument implementation
- protected GImplementation _implementation = new GImplementation ();
+ protected DomImplementation _implementation = new XImplementation ();
protected string _url = "about:blank";
protected string _origin = "";
protected string _compat_mode = "";
@@ -311,12 +311,12 @@ public class GXml.XDocument : GXml.XNode,
}
// NodeFilter.SHOW_ALL = 0xFFFFFFFF
- public DomNodeIterator create_node_iterator (DomNode root, int what_to_show = (int) 0xFFFFFFFF,
DomNodeFilter? filter = null)
+ public DomNodeIterator create_node_iterator (DomNode root, int what_to_show = (int) 0xFFFFFFFF)
{
- return new GDomNodeIterator (root, what_to_show, filter);
+ return new NodeIterator (root, what_to_show);
}
- public DomTreeWalker create_tree_walker (DomNode root, int what_to_show = (int) 0xFFFFFFFF, DomNodeFilter?
filter = null) {
- return new GDomTreeWalker (root, what_to_show, filter);
+ public DomTreeWalker create_tree_walker (DomNode root, int what_to_show = (int) 0xFFFFFFFF) {
+ return new TreeWalker (root, what_to_show);
}
// DomParentNode
public DomHTMLCollection children {
@@ -369,7 +369,7 @@ public class GXml.XDocument : GXml.XNode,
}
-public class GXml.GImplementation : GLib.Object, GXml.DomImplementation {
+internal class GXml.XImplementation : GLib.Object, GXml.DomImplementation {
public DomDocumentType
create_document_type (string qualified_name, string public_id, string system_id)
throws GLib.Error
@@ -387,7 +387,7 @@ public class GXml.GImplementation : GLib.Object, GXml.DomImplementation {
}
-public class GXml.XDocumentType : GXml.XChildNode,
+internal class GXml.XDocumentType : GXml.XChildNode,
GXml.DomNode,
GXml.DomChildNode,
GXml.DomDocumentType
@@ -416,7 +416,7 @@ public class GXml.XDocumentType : GXml.XChildNode,
}
}
-public class GXml.XDocumentFragment : GXml.XDocument,
+internal class GXml.XDocumentFragment : GXml.XDocument,
GXml.DomDocumentFragment
{
public XDocumentFragment (GXml.XDocument d) {
@@ -424,53 +424,3 @@ public class GXml.XDocumentFragment : GXml.XDocument,
}
}
-
-public class GXml.GDomNodeIterator : GLib.Object, GXml.DomNodeIterator {
- protected DomNode _root;
- protected DomNode _reference_node;
- protected bool _pointer_before_reference_node;
- protected int _what_to_show;
- protected DomNodeFilter _filter;
- public GDomNodeIterator (DomNode n, int what_to_show, DomNodeFilter filter) {
- _root = n;
- _what_to_show = what_to_show;
- _filter = filter;
- }
- public DomNode root { get { return _root; } }
- public DomNode reference_node { get { return _reference_node; } }
- public bool pointer_before_reference_node { get { return _pointer_before_reference_node; } }
- public int what_to_show { get { return _what_to_show; } }
- public DomNodeFilter? filter { get { return _filter; } }
-
- public DomNode? next_node() { return null; } // FIXME
- public DomNode? previous_node() { return null; } // FIXME
-
- public void detach() { return; } // FIXME
-}
-
-
-public class GXml.GDomTreeWalker : GLib.Object, GXml.DomTreeWalker {
- protected DomNode _root;
- protected int _what_to_show;
- protected DomNodeFilter? _filter;
- protected DomNode _current_node;
-
- public DomNode root { get { return root; } }
- public int what_to_show { get { return _what_to_show; } }
- public DomNodeFilter? filter { get { return _filter; } }
- public DomNode current_node { get { return _current_node; } }
-
- public GDomTreeWalker (DomNode r, int w, DomNodeFilter f) {
- _root = r;
- _what_to_show = w;
- _filter = f;
- }
-
- public DomNode? parent_node() { return null; }// FIXME
- public DomNode? first_child() { return null; } // FIXME
- public DomNode? last_child() { return null; }// FIXME
- public DomNode? previous_sibling() { return null; }// FIXME
- public DomNode? next_sibling() { return null; }// FIXME
- public DomNode? previous_node() { return null; }// FIXME
- public DomNode? next_node() { return null; }// FIXME
-}
diff --git a/gxml/meson.build b/gxml/meson.build
index 59772b6..49d49cf 100644
--- a/gxml/meson.build
+++ b/gxml/meson.build
@@ -63,6 +63,7 @@ valasources = files ([
'IXsdSchema.vala',
'LXPathObject.vala',
'Node.vala',
+ 'NodeIterator.vala',
'NodeType.vala',
'Object.vala',
'Parser.vala',
@@ -72,6 +73,7 @@ valasources = files ([
'StringRef.vala',
'Text.vala',
'TokenList.vala',
+ 'TreeWalker.vala',
'XAttribute.vala',
'XCharacter.vala',
'XChildNode.vala',
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]