[gxml/gsoc2013: 10/69] Change some errors in CharacterData and Text to use g_warning instead of GError



commit 5cd50780e70ba7ec13226edfb8f9c3a146e3b97f
Author: Richard Schwarting <aquarichy gmail com>
Date:   Sat Jul 27 00:00:18 2013 -0400

    Change some errors in CharacterData and Text to use g_warning instead of GError

 gxml/Attr.vala              |   17 ++++++-----------
 gxml/CharacterData.vala     |   22 ++++++++++++++++++++--
 gxml/Text.vala              |    6 ++++--
 test/CharacterDataTest.vala |   15 ++++++++++++++-
 test/TextTest.vala          |   17 +++++++----------
 5 files changed, 51 insertions(+), 26 deletions(-)
---
diff --git a/gxml/Attr.vala b/gxml/Attr.vala
index 6f7155b..f308258 100644
--- a/gxml/Attr.vala
+++ b/gxml/Attr.vala
@@ -114,7 +114,6 @@ namespace GXml {
                        }
                }
 
-               /* "raises [DomError] on setting/retrieval"?  */
                private string _node_value;
                /**
                 * The node_value for an attribute is a string
@@ -134,17 +133,13 @@ namespace GXml {
                                return this._node_value;
                        }
                        internal set {
-                               try {
-                                       // TODO: consider adding an empty () method to NodeList
-                                       foreach (DomNode child in this.child_nodes) {
-                                               // TODO: this doesn't clear the actual underlying attributes' 
values, is this what we want to do?  It works if we eventually sync up values
-                                               this.remove_child (child);
-                                       }
-                                       this.append_child (this.owner_document.create_text_node (value));
-                                       // TODO: may want to normalise
-                               } catch (DomError e) {
-                                       // TODO: handle
+                               // TODO: consider adding an empty () method to NodeList
+                               foreach (DomNode child in this.child_nodes) {
+                                       // TODO: this doesn't clear the actual underlying attributes' values, 
is this what we want to do?  It works if we eventually sync up values
+                                       this.remove_child (child);
                                }
+                               this.append_child (this.owner_document.create_text_node (value));
+                               // TODO: may want to normalise
                                // TODO: need to expand entity references too?
                        }
                }
diff --git a/gxml/CharacterData.vala b/gxml/CharacterData.vala
index 50b011a..f0d9fd2 100644
--- a/gxml/CharacterData.vala
+++ b/gxml/CharacterData.vala
@@ -78,15 +78,29 @@ namespace GXml {
                }
                /**
                 * Inserts arg into the character data at offset.
+
+                length == 5
+                0 1 2 3 4
+                f a n c y
+
                 */
                public void insert_data (ulong offset, string arg) /* throws DomError */ {
-                       // TODO: complain about boundaries
+                       if (offset < 0 || this.data.length <= offset) {
+                               GLib.warning ("INDEX_SIZE_ERR: insert_data called with offset %lu for data of 
length %lu", offset, this.data.length);
+                               return;
+                       }
+
                        this.data = this.data.substring (0, (long)offset).concat (arg, this.data.substring 
((long)offset));
                }
                /**
                 * Deletes a range of characters, count-characters long, starting from offset.
                 */
                public void delete_data (ulong offset, ulong count) /* throws DomError */ {
+                       if (offset < 0 || this.data.length <= offset || count < 0 || this.data.length < 
offset + count) {
+                               GLib.warning ("INDEX_SIZE_ERR: delete_data called with offset %lu and count 
%lu for data of length %lu", offset, count, this.data.length);
+                               return;
+                       }
+
                        this.data = this.data.substring (0, (long)offset).concat (this.data.substring 
((long)(offset + count)));
                }
                /**
@@ -94,7 +108,11 @@ namespace GXml {
                 * long, starting at offset, with arg.
                 */
                public void replace_data (ulong offset, ulong count, string arg) /* throws DomError */ {
-                       // TODO: bounds
+                       if (offset < 0 || this.data.length <= offset || count < 0) {
+                               GLib.warning ("INDEX_SIZE_ERR: replace_data called with offset %lu and count 
%lu for data of length %lu", offset, count, this.data.length);
+                               return;
+                       }
+
                        this.data = this.data.substring (0, (long)offset).concat (arg, this.data.substring 
((long)(offset + count)));
                }
        }
diff --git a/gxml/Text.vala b/gxml/Text.vala
index cff4128..c7e42e9 100644
--- a/gxml/Text.vala
+++ b/gxml/Text.vala
@@ -72,8 +72,10 @@ namespace GXml {
                public Text split_text (ulong offset) {
                        /* libxml2 doesn't handle this directly, in part because it doesn't
                           allow Text siblings.  Boo! */
-                       if (offset < 0 || offset > this.length) {
-                               this.owner_document.last_error = new DomError.INDEX_SIZE ("Offset '%u' is 
invalid for string of length '%u'", offset, this.length); // i18n
+                       if (offset < 0 || this.length < offset) {
+                               GLib.warning ("INDEX_SIZE_ERR: split_text called with offset '%lu' on string 
of length '%lu'", offset, this.length); // i18n
+                               //this.owner_document.last_error = new DomError.INDEX_SIZE ("Offset '%u' is 
invalid for string of length '%u'", offset, this.length); // i18n
+                               return null;
                        }
 
                        Text other = this.owner_document.create_text_node (this.data.substring 
((long)offset));
diff --git a/test/CharacterDataTest.vala b/test/CharacterDataTest.vala
index 6a54a52..4aeb073 100644
--- a/test/CharacterDataTest.vala
+++ b/test/CharacterDataTest.vala
@@ -63,7 +63,20 @@ class CharacterDataTest : GXmlTest  {
                                        Text txt = get_text_new_doc ("It is our choices that show what we 
are, far more than our abilities.", out doc);
                                        txt.insert_data (35, " truly");
                                        assert (txt.data == "It is our choices that show what we truly are, 
far more than our abilities.");
-                                       // TODO: test bounds
+
+                                       txt.insert_data (0, "Yes.  ");
+                                       assert (txt.data == "Yes.  It is our choices that show what we truly 
are, far more than our abilities.");
+
+                                       txt.insert_data (txt.data.length, "  Indeed.");
+                                       assert (txt.data == "Yes.  It is our choices that show what we truly 
are, far more than our abilities.  Indeed.");
+
+                                       // should fail
+                                       txt.insert_data (txt.data.length + 1, "  Perhaps.");
+                                       assert (txt.data == "Yes.  It is our choices that show what we truly 
are, far more than our abilities.  Indeed.");
+
+                                       // should fail
+                                       txt.insert_data (-1, "  No.");
+                                       assert (txt.data == "Yes.  It is our choices that show what we truly 
are, far more than our abilities.  Indeed.");
                                } catch (GXml.DomError e) {
                                        Test.message ("%s", e.message);
                                        assert_not_reached ();
diff --git a/test/TextTest.vala b/test/TextTest.vala
index fd58668..b6a4a88 100644
--- a/test/TextTest.vala
+++ b/test/TextTest.vala
@@ -32,18 +32,15 @@ class TextTest : GXmlTest {
                                        assert (txt1.node_value == "");
                                        assert (txt2.node_value == "Const");
 
-                                       try {
-                                               txt2.split_text (-1);
-                                               assert_not_reached ();
-                                       } catch (DomError.INDEX_SIZE e) {
-                                       }
-                                       try {
-                                               txt2.split_text (10);
-                                               assert_not_reached ();
-                                       } catch (DomError.INDEX_SIZE e) {
-                                       }
+                                       txt1 = txt2.split_text (-2);
+                                       assert (txt1 == null);
                                        assert (txt2.node_value == "Const");
 
+                                       txt1 = txt2.split_text (10);
+                                       assert (txt1 == null);
+                                       assert (txt2.node_value == "Const");
+
+
 
                                } catch (GXml.DomError e) {
                                        Test.message ("%s", e.message);


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