From f98526eb42909efbc8b2b4f85dfbfd588e25f515 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 9 Apr 2015 15:05:50 +0200 Subject: First steps towards typesystem serialization. struct types can be successfully serialized. --- src/plugins/xml/XmlOutput.cpp | 123 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 113 insertions(+), 10 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/xml/XmlOutput.cpp b/src/plugins/xml/XmlOutput.cpp index 1bc1ab7..8c45a52 100644 --- a/src/plugins/xml/XmlOutput.cpp +++ b/src/plugins/xml/XmlOutput.cpp @@ -65,6 +65,10 @@ static Rooted transformOntology(Handle parent, /* * Typesystem transformation. */ + +static std::string getTypeRef(Handle referencing, + Handle referenced); + static Rooted transformTypesystem(Handle parent, Handle t, TransformParams &P); @@ -275,13 +279,19 @@ static Rooted transformFieldDescriptor(Handle parent, syntax->addChild(close); } } - // translate the child references. - for (auto s : fd->getChildren()) { - std::string ref = - getStructuredClassRef(fd->getParent().cast(), s); - Rooted childRef{ - new Element(P.mgr, fieldDescriptor, "childRef", {{"ref", ref}})}; - fieldDescriptor->addChild(childRef); + if (!fd->isPrimitive()) { + // translate the child references. + for (auto s : fd->getChildren()) { + std::string ref = + getStructuredClassRef(fd->getParent().cast(), s); + Rooted childRef{new Element(P.mgr, fieldDescriptor, + "childRef", {{"ref", ref}})}; + fieldDescriptor->addChild(childRef); + } + } else { + // translate the primitive type. + fieldDescriptor->getAttributes().emplace( + "type", getTypeRef(nullptr, fd->getPrimitiveType())); } return fieldDescriptor; } @@ -408,15 +418,108 @@ Rooted transformOntology(Handle parent, Handle o, /* * Typesystem transformation functions. */ + +std::string getTypeRef(Handle referencing, Handle referenced) +{ + std::string typeRef; + if (referencing != referenced->getTypesystem() && + !(referenced->getTypesystem()->isa(&RttiTypes::SystemTypesystem))) { + // qualify the name if that's necessary. + typeRef = referenced->getTypesystem()->getName() + "." + + referenced->getName(); + } else { + typeRef = referenced->getName(); + } + return typeRef; +} + +Rooted transformStructTypeEntry(Handle parent, + Handle t, + Handle a, + TransformParams &P) +{ + // create an xml element for the attribute. + Rooted attribute{new Element(P.mgr, parent, "field")}; + addNameAttribute(a, attribute->getAttributes()); + // add the type reference + { + std::string typeRef = getTypeRef(t->getTypesystem(), a->getType()); + attribute->getAttributes().emplace("type", typeRef); + } + // set the default value. + if (a->getDefaultValue() != nullptr) { + attribute->getAttributes().emplace("default", + toString(a->getDefaultValue(), P)); + } + // set the optional flag. + attribute->getAttributes().emplace("optional", + getStringForBool(a->isOptional())); + return attribute; +} + +Rooted transformStructType(Handle parent, + Handle t, TransformParams &P) +{ + // create an xml element for the struct type itself. + Rooted structType{new Element(P.mgr, parent, "struct")}; + addNameAttribute(t, structType->getAttributes()); + // transform all attributes. + for (auto &a : t->getAttributes()) { + Rooted attribute = + transformStructTypeEntry(structType, t, a, P); + structType->addChild(attribute); + } + return structType; +} Rooted transformTypesystem(Handle parent, Handle t, TransformParams &P) { - // TODO: implement. - return nullptr; + // do not transform the system typesystem. + if (t->isa(&RttiTypes::SystemTypesystem)) { + return nullptr; + } + // only transform this typesystem if it was not transformed already. + if (t->getLocation().getSourceId() != P.documentId) { + // also: store that we have serialized this ontology. + if (!P.serialized.insert(t->getLocation().getSourceId()).second) { + return nullptr; + } + } + + if (P.flat) { + // transform all referenced typesystems if we want a standalone version. + for (auto t2 : t->getTypesystemReferences()) { + Rooted refTypes = transformTypesystem(parent, t2, P); + if (refTypes != nullptr) { + parent->addChild(refTypes); + } + } + } + + // transform the typesystem itself. + // create an XML element for the ontology. + Rooted typesystem{new Element(P.mgr, parent, "typesystem")}; + addNameAttribute(t, typesystem->getAttributes()); + // transform all types + for (auto tp : t->getTypes()) { + Rooted type; + if (tp->isa(&RttiTypes::StructType)) { + type = transformStructType(typesystem, tp.cast(), P); + } else { + P.logger.warning(std::string("Type ") + tp->getName() + + " can not be serialized, because it is neither a " + "StructType nor an EnumType."); + } + if (type != nullptr) { + typesystem->addChild(type); + } + } + // return the transformed Ontology. + return typesystem; } /* - * Attributes transform functions. + * DocumentEntity attributes transform functions. */ std::map transformAttributes(const std::string &name, -- cgit v1.2.3