From 29fa2cbd1351d2bf3fdb7720da8762baf4b2cfa8 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Thu, 16 Apr 2015 02:03:13 +0200 Subject: Prepend the descriptor path to the ids to make it simpler to distinguish the ids from mere names --- .../uniqueid/UniqueIdTransformation.cpp | 40 ++++++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'src/transformations/uniqueid/UniqueIdTransformation.cpp') diff --git a/src/transformations/uniqueid/UniqueIdTransformation.cpp b/src/transformations/uniqueid/UniqueIdTransformation.cpp index 6065e9d..c901540 100644 --- a/src/transformations/uniqueid/UniqueIdTransformation.cpp +++ b/src/transformations/uniqueid/UniqueIdTransformation.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include "UniqueIdTransformation.hpp" @@ -63,6 +64,13 @@ private: */ void processVariant(const Variant &data); + /** + * Used to build the id prefix. + * + * @return a string containing the prefix. + */ + std::string buildId(Handle node); + public: /** * Applys the transformation to the given document. @@ -117,6 +125,29 @@ void UniqueIdTransformationImpl::processFields(const DocumentEntity *entity) } } +std::string UniqueIdTransformationImpl::buildId(Handle node) +{ + // Fetch the name of the node -- if non is set, use the type name + std::string name = node->getName().empty() ? node->type()->name : node->getName(); + + // Try to fetch a Descriptor instance for the node + Rooted descriptor; + if (node->isa(&RttiTypes::StructuredEntity)) { + descriptor = node.cast()->getDescriptor(); + } else if (node->isa(&RttiTypes::AnnotationEntity)) { + descriptor = node.cast()->getDescriptor(); + } + + // If a Descriptor instance is found, preprend the path to the Descriptor + // declaration + if (descriptor != nullptr) { + return Utils::join(descriptor->path(), "_") + "_" + name; + } + + // Otherwise just return the name + return name; +} + void UniqueIdTransformationImpl::transform(Handle doc) { // Push the document root element onto the queue @@ -136,13 +167,8 @@ void UniqueIdTransformationImpl::transform(Handle doc) // Generate ids for all referenced elements that do not yet have ids std::map seqNos; for (Rooted node : nodesWithoutId) { - // Generate a first id -- use the node name if it is available, - // otherwise use the internal type name and append the internal unique - // id. - std::string id = - node->getName().empty() - ? node->type()->name + "_" + std::to_string(node->getUid()) - : node->getName(); + // Generate a first id + std::string id = buildId(node); // If the id name is not unique, append a sequence number if (ids.count(id) != 0) { -- cgit v1.2.3