Skip to content
Snippets Groups Projects
Commit b1007d94 authored by Daniel Espinosa's avatar Daniel Espinosa
Browse files

StreamReader: prepraration for serialization tests

parent d88f2f5a
Branches
Tags
No related merge requests found
...@@ -62,9 +62,16 @@ public class GXml.StreamReader : GLib.Object { ...@@ -62,9 +62,16 @@ public class GXml.StreamReader : GLib.Object {
private inline uint8 cur_byte () { private inline uint8 cur_byte () {
return buf[0]; return buf[0];
} }
public DomDocument read () throws GLib.Error { public DomDocument read () throws GLib.Error {
_document = new Document (); _document = new Document ();
internal_read ();
return document;
}
public void read_document (DomDocument doc) throws GLib.Error {
_document = doc;
internal_read ();
}
private void internal_read () throws GLib.Error {
read_byte (); read_byte ();
if (cur_char () != '<') { if (cur_char () != '<') {
throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: should start with '<'")); throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: should start with '<'"));
...@@ -85,7 +92,6 @@ public class GXml.StreamReader : GLib.Object { ...@@ -85,7 +92,6 @@ public class GXml.StreamReader : GLib.Object {
} }
var re = read_root_element (); var re = read_root_element ();
document.append_child (re); document.append_child (re);
return document;
} }
public GXml.Element read_root_element () throws GLib.Error { public GXml.Element read_root_element () throws GLib.Error {
return read_element (true); return read_element (true);
......
...@@ -21,6 +21,115 @@ ...@@ -21,6 +21,115 @@
*/ */
using GXml; using GXml;
class ContentNode : GXml.Element {
string _val = null;
public string val {
get {
_val = text_content;
return _val;
}
set {
text_content = _val;
}
}
}
class Name : ContentNode {
construct {
try {
initialize ("Name");
} catch (GLib.Error e) {
warning ("Error: %s", e.message);
}
}
}
class Email : ContentNode {
construct {
try {
initialize ("Email");
} catch (GLib.Error e) {
warning ("Error: %s", e.message);
}
}
}
class Author : GXml.Element {
public Name name { get; set; }
public Email email { get; set; }
construct {
try {
initialize ("Author");
} catch (GLib.Error e) {
warning ("Error: %s", e.message);
}
}
public class Collection : GXml.ArrayList {
construct {
try {
initialize (typeof (Author));
} catch (GLib.Error e) {
warning ("Error: %s", e.message);
}
}
}
}
class Authors : GXml.Element {
public Author.Collection authors { get; set; }
construct {
try {
initialize ("Authors");
set_instance_property ("authors");
} catch (GLib.Error e) {
warning ("Error: %s", e.message);
}
}
}
class Book : GXml.Element {
[Description(nick="::year")]
public int year { get; set; }
[Description(nick="::ISBN")]
public string ISBN { get; set; }
construct {
try {
initialize ("Book");
} catch (GLib.Error e) {
warning ("Error: %s", e.message);
}
}
public class Collection : GXml.ArrayList {
construct {
try {
initialize (typeof (Book));
} catch (GLib.Error e) {
warning ("Error: %s", e.message);
}
}
}
}
class BookStore : GXml.Element {
public Book.Collection books { get; set; }
construct {
try {
initialize ("BookStore");
set_instance_property ("books");
} catch (GLib.Error e) {
warning ("Error: %s", e.message);
}
}
}
class Library : GXml.Document {
[Description(nick="::ROOT")]
public BookStore store { get; set; }
public void read (string str) throws GLib.Error {
var istream = new MemoryInputStream.from_data (str.data, null);
var sr = new StreamReader (istream);
sr.read_document (this);
}
}
class GXmlTest { class GXmlTest {
public static int main (string[] args) { public static int main (string[] args) {
Test.init (ref args); Test.init (ref args);
...@@ -106,7 +215,58 @@ class GXmlTest { ...@@ -106,7 +215,58 @@ class GXmlTest {
(doc.document_element as GXml.Element).parse_buffer.end (res); (doc.document_element as GXml.Element).parse_buffer.end (res);
message (doc.write_string ()); message (doc.write_string ());
assert ((doc.document_element as GXml.Element).read_buffer == null); assert ((doc.document_element as GXml.Element).read_buffer == null);
//assert ((doc.document_element.child_nodes.item (0) as GXml.Element).read_buffer == null); loop.quit ();
} catch (GLib.Error e) {
warning ("Error: %s", e.message);
}
});
} catch (GLib.Error e) {
warning ("Error while reading stream: %s", e.message);
}
return Source.REMOVE;
});
loop.run ();
});
Test.add_func ("/gxml/stream-reader/serialization", () => {
var loop = new GLib.MainLoop (null);
Idle.add (()=>{
string str = """<?xml version="1.0"?>
<BookStore>
<book year="2014" isbn="ISBN83763550019---11">
<Authors>
<Author>
<Name>Fred</Name>
<Email>fweasley@hogwarts.co.uk</Email>
</Author>
<Author>
<Name>George</Name>
<Email>gweasley@hogwarts.co.uk</Email>
</Author>
</Authors>
<name>Book1</name>
</book>
<book year="2014" isbn="ISBN83763550019---11">
<Authors>
<Author>
<Name>Fred</Name>
<Email>fweasley@hogwarts.co.uk</Email>
</Author>
<Author>
<Name>George</Name>
<Email>gweasley@hogwarts.co.uk</Email>
</Author>
</Authors>
<name>Book1</name>
</book>
</BookStore>""";
var doc = new Library ();
try {
doc.read (str);
(doc.document_element as GXml.Element).parse_buffer.begin ((obj, res)=>{
try {
(doc.document_element as GXml.Element).parse_buffer.end (res);
message (doc.write_string ());
assert ((doc.document_element as GXml.Element).read_buffer == null);
loop.quit (); loop.quit ();
} catch (GLib.Error e) { } catch (GLib.Error e) {
warning ("Error: %s", e.message); warning ("Error: %s", e.message);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment