summaryrefslogtreecommitdiff
path: root/src/core
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 /src/core
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.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/XML.cpp53
-rw-r--r--src/core/XML.hpp16
2 files changed, 49 insertions, 20 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;
};
}