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 ++++++++++++++++++---- .../basic_functionality/reference.out.osxml | 4 +-- .../reference_inherited.out.osxml | 4 +-- .../basic_functionality/reference_scope.out.osxml | 10 +++--- 4 files changed, 42 insertions(+), 16 deletions(-) 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) { diff --git a/testdata/integration/basic_functionality/reference.out.osxml b/testdata/integration/basic_functionality/reference.out.osxml index e8f1a5c..abb80ab 100644 --- a/testdata/integration/basic_functionality/reference.out.osxml +++ b/testdata/integration/basic_functionality/reference.out.osxml @@ -1,7 +1,7 @@ - foo - + test_a_foo + diff --git a/testdata/integration/basic_functionality/reference_inherited.out.osxml b/testdata/integration/basic_functionality/reference_inherited.out.osxml index f2ffe40..5a57978 100644 --- a/testdata/integration/basic_functionality/reference_inherited.out.osxml +++ b/testdata/integration/basic_functionality/reference_inherited.out.osxml @@ -1,7 +1,7 @@ - foo - + test_d_foo + diff --git a/testdata/integration/basic_functionality/reference_scope.out.osxml b/testdata/integration/basic_functionality/reference_scope.out.osxml index 3684558..4d61ed0 100644 --- a/testdata/integration/basic_functionality/reference_scope.out.osxml +++ b/testdata/integration/basic_functionality/reference_scope.out.osxml @@ -2,18 +2,18 @@ - foo - foo2 - + test_a_foo + test_a_foo2 + - foo + test_a_foo - + -- cgit v1.2.3