summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Paassen <bpaassen@techfak.uni-bielefeld.de>2015-01-09 17:00:53 +0100
committerBenjamin Paassen <bpaassen@techfak.uni-bielefeld.de>2015-01-09 17:00:53 +0100
commitf37d3cd42eb18433445c2e259cd71a1b2bd67be0 (patch)
treec4371b1b4e3f578a6d20dc76dd74fb85d6fb901a
parent58fed7b74357b82ba55558f91ae13123dc2380eb (diff)
added non-pretty output of XML serialization, changed DemoOutput accordingly and changed DemoOutputTest to have some kind of automatic inspection instead of visual inspection.
-rw-r--r--src/core/XML.cpp53
-rw-r--r--src/core/XML.hpp16
-rw-r--r--src/plugins/html/DemoOutput.cpp6
-rw-r--r--src/plugins/html/DemoOutput.hpp11
-rw-r--r--test/core/model/TestAdvanced.hpp2
-rw-r--r--test/plugins/html/DemoOutputTest.cpp23
6 files changed, 79 insertions, 32 deletions
diff --git a/src/core/XML.cpp b/src/core/XML.cpp
index 9cfb5dc..b7d1511 100644
--- a/src/core/XML.cpp
+++ b/src/core/XML.cpp
@@ -4,18 +4,23 @@
namespace ousia {
namespace xml {
-void Node::serialize(std::ostream &out, const std::string &doctype)
+void Node::serialize(std::ostream &out, const std::string &doctype, bool pretty)
{
if (doctype != "") {
- out << doctype << "\n";
+ out << doctype;
+ if (pretty) {
+ out << '\n';
+ }
}
- doSerialize(out, 0);
+ doSerialize(out, 0, pretty);
}
-void Element::doSerialize(std::ostream &out, unsigned int tabdepth)
+void Element::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty)
{
- for (unsigned int t = 0; t < tabdepth; t++) {
- out << '\t';
+ if (pretty) {
+ for (unsigned int t = 0; t < tabdepth; t++) {
+ out << '\t';
+ }
}
out << '<' << name;
for (auto &a : attributes) {
@@ -23,25 +28,41 @@ void Element::doSerialize(std::ostream &out, unsigned int tabdepth)
}
// if we have no children, we close the tag immediately.
if (children.size() == 0) {
- out << "/>\n";
+ out << "/>";
+ if (pretty) {
+ out << '\n';
+ }
} else {
- out << ">\n";
+ out << ">";
+ if (pretty) {
+ out << '\n';
+ }
for (auto &n : children) {
- n->doSerialize(out, tabdepth + 1);
+ n->doSerialize(out, tabdepth + 1, pretty);
}
- for (unsigned int t = 0; t < tabdepth; t++) {
- out << '\t';
+ if (pretty) {
+ for (unsigned int t = 0; t < tabdepth; t++) {
+ out << '\t';
+ }
+ }
+ out << "</" << name << ">";
+ if (pretty) {
+ out << '\n';
}
- out << "</" << name << ">\n";
}
}
-void Text::doSerialize(std::ostream &out, unsigned int tabdepth)
+void Text::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty)
{
- for (unsigned int t = 0; t < tabdepth; t++) {
- out << '\t';
+ if (pretty) {
+ for (unsigned int t = 0; t < tabdepth; t++) {
+ out << '\t';
+ }
+ }
+ out << text;
+ if (pretty) {
+ out << '\n';
}
- out << text << '\n';
}
}
diff --git a/src/core/XML.hpp b/src/core/XML.hpp
index e55ecba..b05d4c6 100644
--- a/src/core/XML.hpp
+++ b/src/core/XML.hpp
@@ -73,9 +73,12 @@ public:
* @param out is the output stream the serialized data shall be
* written to.
* @param doctype enables you to add a prefix specifying the doctype.
+ * @param pretty is a flag that manipulates whether newlines and tabs are
+ * used.
*/
void serialize(std::ostream &out,
- const std::string &doctype = "<?xml version=\"1.0\"?>");
+ const std::string &doctype = "<?xml version=\"1.0\"?>",
+ bool pretty = true);
/**
* This method just writes the XML representation of this node to the
* output stream.
@@ -83,8 +86,11 @@ public:
* @param out the output stream the serialized data shall be written
* to.
* @param tabdepth the current tabdepth for prettier output.
+ * @param pretty is a flag that manipulates whether newlines and tabs are
+ * used.
*/
- virtual void doSerialize(std::ostream &out, unsigned int tabdepth) = 0;
+ virtual void doSerialize(std::ostream &out, unsigned int tabdepth,
+ bool pretty) = 0;
/**
* @return the parent XML element of this node.
@@ -130,7 +136,8 @@ public:
* * The end tag of this element.
*
*/
- void doSerialize(std::ostream &out, unsigned int tabdepth) override;
+ void doSerialize(std::ostream &out, unsigned int tabdepth,
+ bool pretty) override;
const ManagedVector<Node> &getChildren() const { return children; }
@@ -155,7 +162,8 @@ public:
* This just writes the text to the output.
*
*/
- void doSerialize(std::ostream &out, unsigned int tabdepth) override;
+ void doSerialize(std::ostream &out, unsigned int tabdepth,
+ bool pretty) override;
};
}
diff --git a/src/plugins/html/DemoOutput.cpp b/src/plugins/html/DemoOutput.cpp
index 69f2756..4cadf12 100644
--- a/src/plugins/html/DemoOutput.cpp
+++ b/src/plugins/html/DemoOutput.cpp
@@ -29,12 +29,12 @@ namespace ousia {
namespace html {
void DemoHTMLTransformer::writeHTML(Handle<model::Document> doc,
- std::ostream &out)
+ std::ostream &out, bool pretty)
{
Manager &mgr = doc->getManager();
// Create an XML object tree for the document first.
Rooted<xml::Element> html{new xml::Element{
- mgr, {nullptr}, "html", {{"xlmns", "http://www.w3.org/1999/xhtml"}}}};
+ mgr, {nullptr}, "html"}};
// add the head Element
Rooted<xml::Element> head{new xml::Element{mgr, html, "head"}};
html->addChild(head);
@@ -78,7 +78,7 @@ void DemoHTMLTransformer::writeHTML(Handle<model::Document> doc,
body->addChild(book);
// After the content has been transformed, we serialize it.
- html->serialize(out, "<!DOCTYPE html>");
+ html->serialize(out, "<!DOCTYPE html>", pretty);
}
/**
diff --git a/src/plugins/html/DemoOutput.hpp b/src/plugins/html/DemoOutput.hpp
index 07d758b..b755aac 100644
--- a/src/plugins/html/DemoOutput.hpp
+++ b/src/plugins/html/DemoOutput.hpp
@@ -83,11 +83,14 @@ public:
* Therefore this is not an adequate model of our algorithms but only a
* Demo.
*
- * @param doc is a Document using concepts of the book, headings, emphasis
- * and lists domains but no other.
- * @param out is the output stream the data shall be written to.
+ * @param doc is a Document using concepts of the book, headings,
+ * emphasis and lists domains but no other.
+ * @param out is the output stream the data shall be written to.
+ * @param pretty is a flag that manipulates whether newlines and tabs are
+ * used.
*/
- void writeHTML(Handle<model::Document> doc, std::ostream &out);
+ void writeHTML(Handle<model::Document> doc, std::ostream &out,
+ bool pretty = true);
};
}
}
diff --git a/test/core/model/TestAdvanced.hpp b/test/core/model/TestAdvanced.hpp
index a1510e7..769a0df 100644
--- a/test/core/model/TestAdvanced.hpp
+++ b/test/core/model/TestAdvanced.hpp
@@ -254,7 +254,7 @@ static Rooted<Document> constructAdvancedDocument(Manager &mgr, Logger &logger,
{
if (!addAnnotation(logger, doc, p,
"Aufklärung ist der Ausgang des Menschen aus "
- "seiner selbstverschuldeten Unmündigkeit",
+ "seiner selbstverschuldeten Unmündigkeit!",
"strong")) {
return {nullptr};
}
diff --git a/test/plugins/html/DemoOutputTest.cpp b/test/plugins/html/DemoOutputTest.cpp
index 5a34112..5b2758d 100644
--- a/test/plugins/html/DemoOutputTest.cpp
+++ b/test/plugins/html/DemoOutputTest.cpp
@@ -19,6 +19,7 @@
#include <gtest/gtest.h>
#include <iostream>
+#include <sstream>
#include <plugins/html/DemoOutput.hpp>
@@ -56,9 +57,17 @@ TEST(DemoHTMLTransformer, writeHTML)
mgr.exportGraphviz("bookDocument.dot");
#endif
- // TODO: change this. Don't use printouts
+ // we can only do a rough check here.
DemoHTMLTransformer transformer;
- transformer.writeHTML(doc, std::cout);
+ std::stringstream out;
+ transformer.writeHTML(doc, out);
+ const std::string res = out.str();
+ ASSERT_FALSE(res == "");
+ ASSERT_TRUE(res.find("Was ist Aufklärung?") != std::string::npos);
+ ASSERT_TRUE(res.find(
+ "Aufklärung ist der Ausgang des Menschen aus seiner "
+ "selbstverschuldeten Unmündigkeit!") != std::string::npos);
+ ASSERT_TRUE(res.find("Sapere aude!") != std::string::npos);
}
TEST(DemoHTMLTransformer, AnnotationProcessing)
@@ -100,9 +109,15 @@ TEST(DemoHTMLTransformer, AnnotationProcessing)
buildAnnotationEntity(doc, logger, {"emphasized"}, em_start, em_end);
buildAnnotationEntity(doc, logger, {"strong"}, strong_start, strong_end);
- // TODO: change this. Don't use printouts
+ // Check serialization.
DemoHTMLTransformer transformer;
- transformer.writeHTML(doc, std::cout);
+ std::stringstream out;
+ transformer.writeHTML(doc, out, false);
+ const std::string res = out.str();
+ // In HTML the overlapping structure must be serialized as follows:
+ ASSERT_TRUE(
+ res.find("<em>bla<strong>blub</strong></em><strong>bla</strong>") !=
+ std::string::npos);
}
}
}