summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-04-16 02:03:13 +0200
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2016-04-25 22:26:13 +0200
commit29fa2cbd1351d2bf3fdb7720da8762baf4b2cfa8 (patch)
treeab9cecdb5db34f825b354df731a6f97490e531b6
parent5be7bf9153341fdf52c13aeb2d88d5e082a7af3b (diff)
Prepend the descriptor path to the ids to make it simpler to distinguish the ids from mere names
-rw-r--r--src/transformations/uniqueid/UniqueIdTransformation.cpp40
-rw-r--r--testdata/integration/basic_functionality/reference.out.osxml4
-rw-r--r--testdata/integration/basic_functionality/reference_inherited.out.osxml4
-rw-r--r--testdata/integration/basic_functionality/reference_scope.out.osxml10
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 <set>
#include <string>
+#include <core/common/Utils.hpp>
#include <core/common/Variant.hpp>
#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> 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> 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> descriptor;
+ if (node->isa(&RttiTypes::StructuredEntity)) {
+ descriptor = node.cast<StructuredEntity>()->getDescriptor();
+ } else if (node->isa(&RttiTypes::AnnotationEntity)) {
+ descriptor = node.cast<AnnotationEntity>()->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<Document> doc)
{
// Push the document root element onto the queue
@@ -136,13 +167,8 @@ void UniqueIdTransformationImpl::transform(Handle<Document> doc)
// Generate ids for all referenced elements that do not yet have ids
std::map<std::string, size_t> seqNos;
for (Rooted<Node> 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 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document xmlns:test="test">
<test:test>
- <test:b>foo</test:b>
- <test:a id="foo" name="foo"/>
+ <test:b>test_a_foo</test:b>
+ <test:a id="test_a_foo" name="foo"/>
</test:test>
</document>
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 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document xmlns:test="test">
<test:test>
- <test:b>foo</test:b>
- <test:d id="foo" name="foo"/>
+ <test:b>test_d_foo</test:b>
+ <test:d id="test_d_foo" name="foo"/>
</test:test>
</document>
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 @@
<document xmlns:test="test">
<test:test>
<test:c>
- <test:b>foo</test:b>
- <test:b>foo2</test:b>
- <test:a id="foo" name="foo"/>
+ <test:b>test_a_foo</test:b>
+ <test:b>test_a_foo2</test:b>
+ <test:a id="test_a_foo" name="foo"/>
</test:c>
<test:c>
- <test:b>foo</test:b>
+ <test:b>test_a_foo</test:b>
<test:a name="foo"/>
</test:c>
<test:c>
<test:a name="foo"/>
<test:a name="foo2"/>
</test:c>
- <test:a id="foo2" name="foo2"/>
+ <test:a id="test_a_foo2" name="foo2"/>
</test:test>
</document>