diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-02-23 00:50:47 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-02-23 00:50:47 +0100 |
commit | 8891dea26a1653a003b4171155e155d3aa6689ae (patch) | |
tree | ae03e305ca4dd5bb26f70763db8a5b5574ef6558 /src/plugins/xml/XmlOutput.cpp | |
parent | 2d4508837b7885c962f815c062f98803917eca71 (diff) |
Output element names in XML serialization
Diffstat (limited to 'src/plugins/xml/XmlOutput.cpp')
-rw-r--r-- | src/plugins/xml/XmlOutput.cpp | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/src/plugins/xml/XmlOutput.cpp b/src/plugins/xml/XmlOutput.cpp index 8af29fd..ea1bc30 100644 --- a/src/plugins/xml/XmlOutput.cpp +++ b/src/plugins/xml/XmlOutput.cpp @@ -101,8 +101,10 @@ static std::string toString(Variant v, bool pretty) } std::map<std::string, std::string> XmlTransformer::transformAttributes( - DocumentEntity *entity, Logger &logger, bool pretty) -{ // copy the attributes. + const std::string &name, DocumentEntity *entity, Logger &logger, + bool pretty) +{ + // copy the attributes. Variant attrs = entity->getAttributes(); // build them. entity->getDescriptor()->getAttributesDescriptor()->build(attrs, logger); @@ -112,6 +114,13 @@ std::map<std::string, std::string> XmlTransformer::transformAttributes( NodeVector<Attribute> as = entity->getDescriptor()->getAttributesDescriptor()->getAttributes(); std::map<std::string, std::string> xmlAttrs; + + // Write the element name if one was given + if (!name.empty()) { + xmlAttrs.emplace("name", name); + } + + // Write other user defined properties for (size_t a = 0; a < as.size(); a++) { xmlAttrs.emplace(as[a]->getName(), toString(attrArr[a], pretty)); } @@ -170,8 +179,9 @@ void XmlTransformer::transformChildren(DocumentEntity *parentEntity, assert(field[0]->isa(&RttiTypes::DocumentPrimitive)); Rooted<DocumentPrimitive> prim = field[0].cast<DocumentPrimitive>(); // transform the primitive content. - Rooted<Text> text = transformPrimitive(par, fieldDesc->getPrimitiveType(), prim, logger, pretty); - if(text != nullptr){ + Rooted<Text> text = transformPrimitive( + par, fieldDesc->getPrimitiveType(), prim, logger, pretty); + if (text != nullptr) { par->addChild(text); } } @@ -184,12 +194,12 @@ Rooted<Element> XmlTransformer::transformStructuredEntity( { Manager &mgr = parent->getManager(); // transform the attributes. - auto attrs = transformAttributes(s.get(), logger, pretty); + auto attrs = transformAttributes(s->getName(), s.get(), logger, pretty); addNameAttribute(s, attrs); // create the XML element itself. Rooted<Element> elem{ new Element{mgr, parent, s->getDescriptor()->getName(), - transformAttributes(s.get(), logger, pretty), + transformAttributes(s->getName(), s.get(), logger, pretty), s->getDescriptor()->getParent().cast<Domain>()->getName()}}; // then transform the children. transformChildren(s.get(), elem, logger, pretty); @@ -206,7 +216,7 @@ Rooted<Element> XmlTransformer::transformAnchor(Handle<Element> parent, // of the annotation here. // transform the attributes. auto attrs = - transformAttributes(a->getAnnotation().get(), logger, pretty); + transformAttributes("", a->getAnnotation().get(), logger, pretty); addNameAttribute(a->getAnnotation(), attrs); elem = Rooted<Element>{new Element( @@ -240,16 +250,17 @@ Rooted<Text> XmlTransformer::transformPrimitive(Handle<Element> parent, Manager &mgr = parent->getManager(); // transform the primitive content. Variant content = p->getContent(); - if(!type->build(content, logger)){ + if (!type->build(content, logger)) { return nullptr; } // special treatment for struct types because they get built as arrays, // which is not so nice for output purposes. - if(type->isa(&RttiTypes::StructType)){ + if (type->isa(&RttiTypes::StructType)) { Variant::mapType map; Variant::arrayType arr = content.asArray(); size_t a = 0; - for(Handle<Attribute> attr : type.cast<StructType>()->getAttributes()){ + for (Handle<Attribute> attr : + type.cast<StructType>()->getAttributes()) { map.emplace(attr->getName(), arr[a++]); } content = std::move(map); @@ -258,4 +269,4 @@ Rooted<Text> XmlTransformer::transformPrimitive(Handle<Element> parent, return text; } } -}
\ No newline at end of file +} |