[gxml] XParser: can create InputStream
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] XParser: can create InputStream
- Date: Fri, 5 May 2017 16:44:48 +0000 (UTC)
commit 98955256b87756ea20b690f660d407e962b922c4
Author: Daniel Espinosa <esodan gmail com>
Date: Fri May 5 11:29:09 2017 -0500
XParser: can create InputStream
XParser can create an InputStream to be used with OutputStream
to get parsing.
Added methods for GomDocument and GomElement to use it.
gxml/GomDocument.vala | 8 ++++++++
gxml/GomElement.vala | 7 +++++++
gxml/XParser.vala | 25 +++++++++++++++++++++++++
test/GomElementTest.vala | 44 ++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 84 insertions(+), 0 deletions(-)
---
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index 196655f..963a108 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -120,6 +120,14 @@ public class GXml.GomDocument : GomNode,
parser.write_stream (stream, null);
}
/**
+ * Creates an {@link GLib.InputStream} to write a string representation
+ * in XML of {@link GomDocument}
+ */
+ public InputStream create_stream () throws GLib.Error {
+ var parser = new XParser (this);
+ return parser.create_stream (null);
+ }
+ /**
* Reads a file contents and parse it to document.
*/
public void read_from_file (GLib.File file) throws GLib.Error {
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index af60b17..fb24d1e 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -86,6 +86,13 @@ public class GXml.GomElement : GomNode,
public void write_stream (GLib.OutputStream stream) throws GLib.Error {
(this.owner_document as GomDocument).write_stream (stream);
}
+ /**
+ * Creates an {@link GLib.InputStream} to write a string representation
+ * in XML of {@link GomElement} using node's {@link GomDocument}
+ */
+ public InputStream create_stream () throws GLib.Error {
+ return (this.owner_document as GomDocument).create_stream ();
+ }
// DomNode overrides
public new string? lookup_prefix (string? nspace) {
if (_namespace_uri == nspace)
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 2a0fc4f..5c3a2af 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -83,6 +83,31 @@ public class GXml.XParser : Object, GXml.Parser {
tw = null;
}
+ /**
+ * Creates an {@link GLib.InputStream} to write a string representation
+ * in XML
+ */
+ public InputStream
+ create_stream (GLib.Cancellable? cancellable = null) throws GLib.Error {
+ var buf = new Xml.Buffer ();
+ tw = new TextWriter.memory (buf);
+ if (_node is DomDocument) tw.start_document ();
+ tw.set_indent (indent);
+ // Root
+ if (_node is DomDocument) {
+ if ((_node as DomDocument).document_element == null)
+ tw.end_document ();
+ }
+ start_node (_node);
+ tw.end_element ();
+ tw.end_document ();
+ tw.flush ();
+ var s = new GLib.StringBuilder ();
+ s.append (buf.content ());
+ tw = null;
+ return new GLib.MemoryInputStream.from_data ((uint8[]) s.str.dup (), null);
+ }
+
public string write_string () throws GLib.Error {
return dump ();
}
diff --git a/test/GomElementTest.vala b/test/GomElementTest.vala
index e017882..687f6b2 100644
--- a/test/GomElementTest.vala
+++ b/test/GomElementTest.vala
@@ -221,5 +221,49 @@ class GomElementTest : GXmlTest {
assert_not_reached ();
}
});
+ Test.add_func ("/gxml/gom-element/write/string", () => {
+ try {
+ var n = new GomElement ();
+ n.initialize ("Node");
+ n.set_attribute ("name","value");
+ string str = n.write_string ();
+ assert ("<Node" in str);
+ assert ("<Node name=\"value\"/>" in str);
+ } catch (GLib.Error e) {
+ GLib.message ("Error: "+e.message);
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/gom-element/write/stream", () => {
+ try {
+ var n = new GomElement ();
+ n.initialize ("Node");
+ n.set_attribute ("name","value");
+ var ostream = new MemoryOutputStream.resizable ();
+ n.write_stream (ostream);
+ string str = (string) ostream.data;
+ assert ("<Node" in str);
+ assert ("<Node name=\"value\"/>" in str);
+ } catch (GLib.Error e) {
+ GLib.message ("Error: "+e.message);
+ assert_not_reached ();
+ }
+ });
+ Test.add_func ("/gxml/gom-element/write/input_stream", () => {
+ try {
+ var n = new GomElement ();
+ n.initialize ("Node");
+ n.set_attribute ("name","value");
+ var ostream = new MemoryOutputStream.resizable ();
+ var istream = n.create_stream ();
+ ostream.splice (istream, GLib.OutputStreamSpliceFlags.NONE);
+ string str = (string) ostream.data;
+ assert ("<Node" in str);
+ assert ("<Node name=\"value\"/>" in str);
+ } catch (GLib.Error e) {
+ GLib.message ("Error: "+e.message);
+ assert_not_reached ();
+ }
+ });
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]