[gxml] * expose a constructor to create a GXml Document from a libxml2 Xml.Node*. Even if the internals ch



commit 2c13baf4f68becb9e88c3841fd7a947d8d292675
Author: Richard Schwarting <aquarichy gmail com>
Date:   Mon Aug 8 16:31:58 2011 +0200

    * expose a constructor to create a GXml Document from a libxml2 Xml.Node*.  Even if the internals change, it can still be useful to convert between the two.

 gxml/Document.vala |   34 +++++++++++++++++++++++++---------
 1 files changed, 25 insertions(+), 9 deletions(-)
---
diff --git a/gxml/Document.vala b/gxml/Document.vala
index 93429e1..dc60856 100644
--- a/gxml/Document.vala
+++ b/gxml/Document.vala
@@ -51,6 +51,9 @@ 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);
 
+		/* TODO: for future reference, find out if internals
+		   are only accessible by children when they're compiled
+		   together */
 		internal Xml.Doc *xmldoc;
 
 		/** Private methods */
@@ -173,17 +176,22 @@ namespace GXml.Dom {
 		}
 
 		/** Constructor */
+		/**
+		 * Creates a Document based on a libxml2 Xml.Doc* object.
+		 */
+		public Document.for_libxml2 (Xml.Doc *doc, bool require_root = true) throws DomError {
+			/* All other constructors should call this one,
+			   passing it a Xml.Doc* object */
 
-		/* All other constructors should call this one,
-		   passing it a Xml.Doc* object */
-		private Document (Xml.Doc *doc) throws DomError {
 			Xml.Node *root;
 
-			root = doc->get_root_element ();
 			if (doc == null)
 				throw new DomError.INVALID_DOC ("Failed to parse document.");
-			if (root == null)
-				throw new DomError.INVALID_ROOT ("Could not obtain root for document.");
+			if (require_root) {
+				root = doc->get_root_element ();
+				if (root == null)
+					throw new DomError.INVALID_ROOT ("Could not obtain root for document.");
+			}
 
 			// TODO: consider passing root as a base node?
 			base.for_document ();
@@ -206,7 +214,7 @@ namespace GXml.Dom {
 		public Document.for_path (string file_path) throws DomError {
 			Xml.Doc *doc = Xml.Parser.parse_file (file_path); // consider using read_file
 			// TODO: might want to check that the file_path exists
-			this (doc);
+			this.for_libxml2 (doc);
 		}
 
 		// TODO: can we make this private?
@@ -314,7 +322,7 @@ namespace GXml.Dom {
 			reader.expand ();
 			Xml.Doc *doc = reader.current_doc ();
 
-			this (doc);
+			this.for_libxml2 (doc);
 		}
 		/**
 		 * Creates a Document from data found in memory.
@@ -323,9 +331,17 @@ namespace GXml.Dom {
 		 */
 		public Document.from_string (string memory) throws DomError {
 			Xml.Doc *doc = Xml.Parser.parse_memory (memory, (int)memory.length);
-			this (doc);
+			this.for_libxml2 (doc);
 		}
 		/**
+		 * Creates an empty document. 
+		 */
+		public Document () {
+			Xml.Doc *doc = new Xml.Doc ();
+			this.for_libxml2 (doc, false);
+		}
+
+		/**
 		 * Saves a Document to the file at path file_path
 		 */
 		// TODO: is this a simple Unix file path, or does libxml2 do networks, too?



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