Code

initial implementation of XML::Subtree API for tracking changes on a
authormental <mental@users.sourceforge.net>
Sun, 1 Jul 2007 20:08:30 +0000 (20:08 +0000)
committermental <mental@users.sourceforge.net>
Sun, 1 Jul 2007 20:08:30 +0000 (20:08 +0000)
whole-subtree basis; the initial implementation does not have very good
performance for large subtrees, but should work well for smaller ones

src/xml/Makefile_insert
src/xml/subtree.cpp [new file with mode: 0644]
src/xml/subtree.h [new file with mode: 0644]

index 69f8952a9572faa5fc4bd6d95e8ccc81c9d78d27..0a95a206223b0f352d7d664300efec2e748070aa 100644 (file)
@@ -46,6 +46,8 @@ xml_libspxml_a_SOURCES =      \
        xml/node-event-vector.h \
        xml/node-iterators.h    \
        xml/sp-css-attr.h \
+       xml/subtree.cpp \
+       xml/subtree.h   \
        xml/text-node.h \
        xml/invalid-operation-exception.h
 
diff --git a/src/xml/subtree.cpp b/src/xml/subtree.cpp
new file mode 100644 (file)
index 0000000..7c549e7
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * XML::Subtree - proxy for an XML subtree
+ *
+ * Copyright 2005 MenTaLguY <mental@rydia.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * See the file COPYING for details.
+ *
+ */
+
+#include "xml/node.h"
+#include "xml/subtree.h"
+#include "xml/node-iterators.h"
+
+namespace Inkscape {
+namespace XML {
+
+namespace {
+
+void recursively(void (Node::*m)(NodeObserver &observer),
+                 Node &node, NodeObserver &observer)
+{
+    (node.*m)(observer);
+    for ( NodeSiblingIterator iter = node.firstChild() ; iter ; ++iter ) {
+        recursively(m, *iter, observer);
+    }
+}
+
+}
+
+Subtree::Subtree(Node &root) : _root(&root) {
+    recursively(&Node::addObserver, *_root, *this);
+}
+
+Subtree::~Subtree() {
+    finish();
+}
+
+void Subtree::finish() {
+    if (_root) {
+        recursively(&Node::removeObserver, *_root, *this);
+        _root = NULL;
+    }
+}
+
+void Subtree::synthesizeEvents(NodeObserver &observer) {
+    if (_root) {
+        recursively(&Node::synthesizeEvents, *_root, *this);
+    }
+}
+
+void Subtree::addObserver(NodeObserver &observer) {
+    _observers.add(observer);
+}
+
+void Subtree::removeObserver(NodeObserver &observer) {
+    _observers.remove(observer); 
+}
+
+void Subtree::notifyChildAdded(Node &node, Node &child, Node *prev) {
+    recursively(&Node::addObserver, child, *this);
+    _observers.notifyChildAdded(node, child, prev);
+}
+
+void Subtree::notifyChildRemoved(Node &node, Node &child, Node *prev) {
+    recursively(&Node::removeObserver, child, *this);
+    _observers.notifyChildRemoved(node, child, prev);
+}
+
+void Subtree::notifyChildOrderChanged(Node &node, Node &child,
+                                      Node *old_prev, Node *new_prev)
+{
+    _observers.notifyChildOrderChanged(node, child, old_prev, new_prev);
+}
+
+void Subtree::notifyContentChanged(Node &node,
+                                   Util::ptr_shared<char> old_content,
+                                   Util::ptr_shared<char> new_content)
+{
+    _observers.notifyContentChanged(node, old_content, new_content);
+}
+
+void Subtree::notifyAttributeChanged(Node &node, GQuark name,
+                                     Util::ptr_shared<char> old_value,
+                                     Util::ptr_shared<char> new_value)
+{
+    _observers.notifyAttributeChanged(node, name, old_value, new_value);
+}
+
+}
+}
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
diff --git a/src/xml/subtree.h b/src/xml/subtree.h
new file mode 100644 (file)
index 0000000..73a6485
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * XML::Subtree - proxy for an XML subtree
+ *
+ * Copyright 2005 MenTaLguY <mental@rydia.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * See the file COPYING for details.
+ *
+ */
+
+#ifndef SEEN_INKSCAPE_XML_SUBTREE_H
+#define SEEN_INKSCAPE_XML_SUBTREE_H
+
+#include "xml/node-observer.h"
+#include "xml/composite-node-observer.h"
+
+namespace Inkscape {
+namespace XML {
+
+class Node;
+
+class Subtree : public Inkscape::GC::Anchored,
+                public Inkscape::GC::Managed<>,
+                private NodeObserver
+{
+public:
+    Subtree(Node &root);
+    ~Subtree();
+
+    void finish();
+
+    void synthesizeEvents(NodeObserver &observer);
+    void addObserver(NodeObserver &observer);
+    void removeObserver(NodeObserver &observer);
+
+private:
+    void notifyChildAdded(Node &node, Node &child, Node *prev);
+
+    void notifyChildRemoved(Node &node, Node &child, Node *prev);
+
+    void notifyChildOrderChanged(Node &node, Node &child,
+                                 Node *old_prev, Node *new_prev);
+
+    void notifyContentChanged(Node &node,
+                              Util::ptr_shared<char> old_content,
+                              Util::ptr_shared<char> new_content);
+
+    void notifyAttributeChanged(Node &node, GQuark name,
+                                Util::ptr_shared<char> old_value,
+                                Util::ptr_shared<char> new_value);
+
+    Node *_root;
+    CompositeNodeObserver _observers;
+};
+
+}
+}
+
+#endif
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :