summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/XML.cpp29
-rw-r--r--src/core/XML.hpp7
-rw-r--r--src/core/model/Domain.hpp13
3 files changed, 30 insertions, 19 deletions
diff --git a/src/core/XML.cpp b/src/core/XML.cpp
index 038cb86..7f03b35 100644
--- a/src/core/XML.cpp
+++ b/src/core/XML.cpp
@@ -4,12 +4,16 @@
namespace ousia {
namespace xml {
-void Node::serialize(std::ostream& out){
+void Node::serialize(std::ostream &out, const std::string &doctype)
+{
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
+ if (doctype != "") {
+ out << doctype << "\n";
+ }
doSerialize(out, 0);
}
-void Element::doSerialize(std::ostream& out, unsigned int tabdepth)
+void Element::doSerialize(std::ostream &out, unsigned int tabdepth)
{
for (unsigned int t = 0; t < tabdepth; t++) {
out << '\t';
@@ -18,17 +22,22 @@ void Element::doSerialize(std::ostream& out, unsigned int tabdepth)
for (auto &a : attributes) {
out << ' ' << a.first << "=\"" << a.second << '\"';
}
- out << ">\n";
- for (auto &n : children) {
- n->doSerialize(out, tabdepth + 1);
- }
- for (unsigned int t = 0; t < tabdepth; t++) {
- out << '\t';
+ // if we have no children, we close the tag immediately.
+ if (children.size() == 0) {
+ out << "/>\n";
+ } else {
+ out << ">\n";
+ for (auto &n : children) {
+ n->doSerialize(out, tabdepth + 1);
+ }
+ for (unsigned int t = 0; t < tabdepth; t++) {
+ out << '\t';
+ }
+ out << "</" << name << ">\n";
}
- out << "</" << name << ">\n";
}
-void Text::doSerialize(std::ostream& out, unsigned int tabdepth)
+void Text::doSerialize(std::ostream &out, unsigned int tabdepth)
{
for (unsigned int t = 0; t < tabdepth; t++) {
out << '\t';
diff --git a/src/core/XML.hpp b/src/core/XML.hpp
index 9ca124a..51ef6fd 100644
--- a/src/core/XML.hpp
+++ b/src/core/XML.hpp
@@ -64,9 +64,12 @@ public:
/**
* This method writes an XML prolog and the XML representing the current
* node, including all children, to the given output stream.
- * @param out is the output stream the serialized data shall be written to.
+ * @param out is the output stream the serialized data shall be
+ * written to.
+ * @param doctype enables you to add a prefix after the XML prolog
+ * specifying the doctype.
*/
- void serialize(std::ostream &out);
+ void serialize(std::ostream &out, const std::string & doctype = "");
/**
* This method just writes the XML representation of this node to the
* output stream, without the XML prolog.
diff --git a/src/core/model/Domain.hpp b/src/core/model/Domain.hpp
index 96e13c7..8d5de0c 100644
--- a/src/core/model/Domain.hpp
+++ b/src/core/model/Domain.hpp
@@ -261,11 +261,6 @@ protected:
VisitorSet &visited) override;
public:
- /**
- * Note, that this flag will always be set to "false" for non-primitive
- * FieldDescriptors, because in that case the cardinalities regulate
- * whether children have to be inserted or not.
- */
const bool optional;
// TODO: What about the name of default fields?
@@ -305,15 +300,19 @@ public:
* TREE for the main or default structure or SUBTREE
* for supporting structures.
* @param name is the name of this field.
+ * @param optional should be set to 'false' is this field needs to be
+ * filled in order for an instance of the parent
+ * Descriptor to be valid.
*/
FieldDescriptor(Manager &mgr, Handle<Descriptor> parent,
FieldType fieldType = FieldType::TREE,
- std::string name = "")
+ std::string name = "",
+ bool optional = false)
: Node(mgr, std::move(name), parent),
children(this),
fieldType(fieldType),
// TODO: What would be a wise initialization of the primitiveType?
- optional(false)
+ optional(optional)
{
}