Commit 361c7e8f authored by Daniel Espinosa Ortiz's avatar Daniel Espinosa Ortiz
Browse files

StreamReader: implemented number of doc children

Fix issue #38
parent 422890f9
Pipeline #184151 passed with stages
in 9 minutes and 37 seconds
......@@ -131,44 +131,49 @@ public class GXml.StreamReader : GLib.Object {
parse_doc_nodes ();
}
public void parse_doc_nodes () throws GLib.Error
{
try {
read_byte ();
} catch {
return;
}
while (true) {
if (cur_char () == '<') {
try {
public void parse_doc_nodes () throws GLib.Error
{
try {
read_byte ();
} catch {
break;
}
if (is_space (cur_char ())) {
throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: unexpected space character before node's name"));
}
if (cur_char () == '?') {
if (start) {
parse_xml_dec ();
start = false;
read_text_node ();
continue;
} else {
parse_pi_dec ();
read_text_node ();
continue;
}
} else if (cur_char () == '!') {
parse_comment_dec ();
read_text_node ();
continue;
}
break;
}
break;
} catch {
return;
}
while (true) {
if (cur_char () != '<') {
throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: expected '<' character"));
}
try {
read_byte ();
} catch {
return;
}
if (is_space (cur_char ())) {
throw new StreamReaderError.INVALID_DOCUMENT_ERROR (_("Invalid document: unexpected space character before node's name"));
}
if (cur_char () != '?' && cur_char () != '!') {
return;
}
if (cur_char () == '?') {
if (start) {
parse_xml_dec ();
start = false;
read_text_node ();
message ("Stoped at: %c", cur_char ());
continue;
} else {
parse_pi_dec ();
read_text_node ();
message ("Stoped at: %c", cur_char ());
continue;
}
} else if (cur_char () == '!') {
parse_comment_dec ();
read_text_node ();
message ("Stoped at: %c", cur_char ());
continue;
}
}
}
}
private GXml.Element read_root_element () throws GLib.Error {
return read_element (true);
......@@ -412,11 +417,6 @@ public class GXml.StreamReader : GLib.Object {
var t = document.create_text_node (text.str);
document.append_child (t);
try {
read_byte ();
} catch {
return;
}
}
private bool is_space (char c) {
return c == 0x20 || c == 0x9 || c == 0xA || c == ' ' || c == '\t' || c == '\n';
......
......@@ -399,6 +399,53 @@ class GXmlTest {
loop.quit ();
return Source.REMOVE;
});
loop.run ();
});
Test.add_func ("/gxml/stream-reader/mix", () => {
var loop = new GLib.MainLoop (null);
Idle.add (()=>{
string str = """<?xml version="1.0"?>
<?test-instruction CONTENT IN PI?>
<!--This is a comment-->
<BookStore>
</BookStore>
""";
message ("Stream with Comments and PI");
var doc = new Library ();
try {
doc.read (str);
bool found1 = false;
bool found2 = false;
for (int i = 0; i < doc.child_nodes.length; i++) {
var n = doc.child_nodes.item (i);
if (n is DomProcessingInstruction) {
found1 = true;
message ("Text: '%s'", ((DomProcessingInstruction) n).target);
assert ("test-instruction" == ((DomProcessingInstruction) n).target);
assert (" CONTENT IN PI" == ((DomProcessingInstruction) n).data);
}
if (n is DomComment) {
found2 = true;
message ("Text: '%s'", ((DomComment) n).data);
assert ("This is a comment" == ((DomComment) n).data);
}
if (n is DomElement) {
message ("Element: %s", n.node_name);
}
}
assert (found1);
assert (found2);
assert (doc.store != null);
message (doc.write_string ());
assert (doc.document_element != null);
message ("Is BookStore?");
assert (doc.document_element is BookStore);
} catch (GLib.Error e) {
warning ("Error while reading stream: %s", e.message);
}
loop.quit ();
return Source.REMOVE;
});
loop.run ();
});
Test.run ();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment