[gxml] * make to_string return an XML representation of a node or document



commit 5901d3934205752eeafbfc51485e7f059709c0cd
Author: Richard Schwarting <aquarichy gmail com>
Date:   Thu Jul 21 20:46:46 2011 -0400

    * make to_string return an XML representation of a node or document

 gxml/BackedNode.vala |   11 +++++++++++
 gxml/Document.vala   |   15 +++++++++++++--
 gxml/NodeList.vala   |    2 +-
 gxml/XNode.vala      |    8 ++++++--
 4 files changed, 31 insertions(+), 5 deletions(-)
---
diff --git a/gxml/BackedNode.vala b/gxml/BackedNode.vala
index b683fc5..6993734 100644
--- a/gxml/BackedNode.vala
+++ b/gxml/BackedNode.vala
@@ -205,5 +205,16 @@ namespace GXml.Dom {
 		public override XNode? clone_nodes (bool deep) {
 			return this; // STUB
 		}
+
+		public override string to_string (bool format = false, int level = 0) {
+			Xml.Buffer *buffer;
+			string str;
+
+			buffer = new Xml.Buffer ();
+			buffer->node_dump (this.owner_document.xmldoc, this.node, level, format ? 1 : 0);
+			str = buffer->content ();
+
+			return str;
+		}
 	}
 }
diff --git a/gxml/Document.vala b/gxml/Document.vala
index a4cc5f5..0d972ba 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -46,7 +46,7 @@ namespace GXml.Dom {
 		// We don't want want to use XNode's Xml.Node or its dict
 		// internal HashTable<Xml.Attr*, Attr> attr_dict = new HashTable<Xml.Attr*, Attr> (null, null);
 
-		private Xml.Doc *xmldoc;
+		internal Xml.Doc *xmldoc;
 
 		/** Private methods */
 		// internal unowned Attr? lookup_attr (Xml.Attr *xmlattr) {
@@ -453,9 +453,20 @@ namespace GXml.Dom {
 			}
 		}
 		private void check_character_validity (string str) throws DomError {
-			if (false == false) { // TODO: define validity
+			if (true == false) { // TODO: define validity
 				throw new DomError.INVALID_CHARACTER ("'%s' contains invalid characters.".printf (str));
 			}
 		}
+		public override string to_string (bool format = false, int level = 0) {
+			Xml.Buffer *buffer;
+			string str;
+			int len;
+
+			buffer = new Xml.Buffer ();
+
+			this.xmldoc->dump_memory_format (out str, out len, format);
+
+			return str;
+		}
 	}
 }
diff --git a/gxml/NodeList.vala b/gxml/NodeList.vala
index 1144fb9..66e1ede 100644
--- a/gxml/NodeList.vala
+++ b/gxml/NodeList.vala
@@ -398,7 +398,7 @@ namespace GXml.Dom {
 		public string to_string () {
 			_str = "NodeList[";
 			foreach (XNode node in this) {
-				_str += "(" + node.to_string () + ")";
+				//_str += "(" + node.to_string () + ")";
 			}
 			_str += "]";
 
diff --git a/gxml/XNode.vala b/gxml/XNode.vala
index c013421..faa0eb4 100644
--- a/gxml/XNode.vala
+++ b/gxml/XNode.vala
@@ -200,9 +200,13 @@ namespace GXml.Dom {
 		/**
 		 * Provides a string representation of this node.
 		 *
-		 * #todo: actually create a good, XML-ish one, will require overrides
+		 * @param format false: no formatting, true: formatted, with indentation
+		 * @param level Indentation level
+		 *
+		 * @return XML string for node.
 		 */
-		public string to_string () {
+		// TODO: need to investigate how to activate format
+		public virtual string to_string (bool format = false, int level = 0) {
 			_str = "XNode(%d:%s)".printf (this.node_type, this.node_name);
 			return _str;
 		}



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