From f37d3cd42eb18433445c2e259cd71a1b2bd67be0 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Fri, 9 Jan 2015 17:00:53 +0100 Subject: added non-pretty output of XML serialization, changed DemoOutput accordingly and changed DemoOutputTest to have some kind of automatic inspection instead of visual inspection. --- src/core/XML.cpp | 53 ++++++++++++++++++++++++++++------------- src/core/XML.hpp | 16 +++++++++---- src/plugins/html/DemoOutput.cpp | 6 ++--- src/plugins/html/DemoOutput.hpp | 11 +++++---- 4 files changed, 59 insertions(+), 27 deletions(-) (limited to 'src') 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 << ""; + if (pretty) { + out << '\n'; } - out << "\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 = ""); + const std::string &doctype = "", + 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 &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 doc, - std::ostream &out) + std::ostream &out, bool pretty) { Manager &mgr = doc->getManager(); // Create an XML object tree for the document first. Rooted html{new xml::Element{ - mgr, {nullptr}, "html", {{"xlmns", "http://www.w3.org/1999/xhtml"}}}}; + mgr, {nullptr}, "html"}}; // add the head Element Rooted head{new xml::Element{mgr, html, "head"}}; html->addChild(head); @@ -78,7 +78,7 @@ void DemoHTMLTransformer::writeHTML(Handle doc, body->addChild(book); // After the content has been transformed, we serialize it. - html->serialize(out, ""); + html->serialize(out, "", 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 doc, std::ostream &out); + void writeHTML(Handle doc, std::ostream &out, + bool pretty = true); }; } } -- cgit v1.2.3