summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/model/Typesystem.cpp8
-rw-r--r--src/core/model/Typesystem.hpp26
-rw-r--r--src/core/parser/stack/OntologyHandler.cpp2
-rw-r--r--src/plugins/xml/XmlOutput.cpp45
-rw-r--r--testdata/integration/ontology_serialization/attributes_descriptor.in.osml10
-rw-r--r--testdata/integration/ontology_serialization/attributes_descriptor.out.osxml18
6 files changed, 89 insertions, 20 deletions
diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp
index df2b9fb..688cb60 100644
--- a/src/core/model/Typesystem.cpp
+++ b/src/core/model/Typesystem.cpp
@@ -618,6 +618,14 @@ bool StructType::cast(Variant &data, Logger &logger) const
return buildFromArrayOrMap(data, logger, NullMagicCallback, true);
}
+NodeVector<Attribute> StructType::getOwnAttributes() const
+{
+ NodeVector<Attribute> res;
+ res.insert(res.end(), attributes.begin() + attributeStart,
+ attributes.end());
+ return res;
+}
+
ssize_t StructType::indexOf(const std::string &name) const
{
size_t res;
diff --git a/src/core/model/Typesystem.hpp b/src/core/model/Typesystem.hpp
index 559a74c..193726d 100644
--- a/src/core/model/Typesystem.hpp
+++ b/src/core/model/Typesystem.hpp
@@ -64,20 +64,20 @@ public:
*/
enum class MagicCallbackResult {
/**
- * A magic value with the given name could not be resolved.
- */
+ * A magic value with the given name could not be resolved.
+ */
NOT_FOUND,
/**
- * A magic value with the given name could be resolved, but is of the
- * wrong type.
- */
+ * A magic value with the given name could be resolved, but is of the
+ * wrong type.
+ */
FOUND_INVALID,
/**
- * A magic value with the given name could be resolved and is of the
- * correct type.
- */
+ * A magic value with the given name could be resolved and is of the
+ * correct type.
+ */
FOUND_VALID
};
@@ -953,7 +953,15 @@ public:
*
* @return a const reference pointing at the attribute list.
*/
- const NodeVector<Attribute> &getAttributes() const { return attributes; };
+ const NodeVector<Attribute> &getAttributes() const { return attributes; }
+
+ /**
+ * Returns a vector of all attributes that belong to this StructType itself,
+ * excluding the attributes of the parent structure.
+ *
+ * @return a vector of all attributes that belong to this StructType itself.
+ */
+ NodeVector<Attribute> getOwnAttributes() const;
/**
* Returns the index of the given attribute in a data array representing
diff --git a/src/core/parser/stack/OntologyHandler.cpp b/src/core/parser/stack/OntologyHandler.cpp
index 0a33c97..0cd8140 100644
--- a/src/core/parser/stack/OntologyHandler.cpp
+++ b/src/core/parser/stack/OntologyHandler.cpp
@@ -108,7 +108,7 @@ void OntologyAnnotationHandler::end() { scope().pop(logger()); }
bool OntologyAttributesHandler::startCommand(Variant::mapType &args)
{
- // Fetch the current typesystem and create the struct node
+ // Fetch the current descriptor and add the attributes descriptor
Rooted<Descriptor> parent = scope().selectOrThrow<Descriptor>();
Rooted<StructType> attrDesc = parent->getAttributesDescriptor();
diff --git a/src/plugins/xml/XmlOutput.cpp b/src/plugins/xml/XmlOutput.cpp
index 8c45a52..cdca730 100644
--- a/src/plugins/xml/XmlOutput.cpp
+++ b/src/plugins/xml/XmlOutput.cpp
@@ -51,7 +51,8 @@ struct TransformParams {
};
/*
- * These are method declarations to allow for cross-references of methods.
+ * These are forward method declarations to allow for cross-references of
+ * methods.
*/
/*
@@ -69,6 +70,18 @@ static Rooted<Element> transformOntology(Handle<Element> parent,
static std::string getTypeRef(Handle<Typesystem> referencing,
Handle<Type> referenced);
+static Rooted<Element> transformStructTypeEntry(Handle<Element> parent,
+ const std::string &tagName,
+ Handle<StructType> t,
+ Handle<Attribute> a,
+ TransformParams &P);
+
+static Rooted<Element> transformStructType(Handle<Element> parent,
+ const std::string &structTagName,
+ const std::string &fieldTagName,
+ Handle<StructType> t,
+ TransformParams &P);
+
static Rooted<Element> transformTypesystem(Handle<Element> parent,
Handle<Typesystem> t,
TransformParams &P);
@@ -301,7 +314,12 @@ static void transformDescriptor(Handle<Element> elem, Handle<Element> syntax,
{
// add name.
addNameAttribute(d, elem->getAttributes());
- // TODO: transform the attributes descriptor.
+ // transform the attributes descriptor.
+ Rooted<Element> attributes = transformStructType(
+ elem, "attributes", "attribute", d->getAttributesDescriptor(), P);
+ // remove the parent entry if it is there.
+ attributes->getAttributes().erase("parent");
+ elem->addChild(attributes);
// transform the syntactic sugar description.
{
Rooted<Element> open =
@@ -434,12 +452,13 @@ std::string getTypeRef(Handle<Typesystem> referencing, Handle<Type> referenced)
}
Rooted<Element> transformStructTypeEntry(Handle<Element> parent,
+ const std::string &tagName,
Handle<StructType> t,
Handle<Attribute> a,
TransformParams &P)
{
// create an xml element for the attribute.
- Rooted<Element> attribute{new Element(P.mgr, parent, "field")};
+ Rooted<Element> attribute{new Element(P.mgr, parent, tagName)};
addNameAttribute(a, attribute->getAttributes());
// add the type reference
{
@@ -451,22 +470,27 @@ Rooted<Element> transformStructTypeEntry(Handle<Element> parent,
attribute->getAttributes().emplace("default",
toString(a->getDefaultValue(), P));
}
- // set the optional flag.
- attribute->getAttributes().emplace("optional",
- getStringForBool(a->isOptional()));
return attribute;
}
Rooted<Element> transformStructType(Handle<Element> parent,
+ const std::string &structTagName,
+ const std::string &fieldTagName,
Handle<StructType> t, TransformParams &P)
{
// create an xml element for the struct type itself.
- Rooted<Element> structType{new Element(P.mgr, parent, "struct")};
+ Rooted<Element> structType{new Element(P.mgr, parent, structTagName)};
addNameAttribute(t, structType->getAttributes());
+ // transformt the parent reference.
+ if (t->getParentStructure() != nullptr) {
+ std::string typeRef =
+ getTypeRef(t->getTypesystem(), t->getParentStructure());
+ structType->getAttributes().emplace("parent", typeRef);
+ }
// transform all attributes.
- for (auto &a : t->getAttributes()) {
+ for (auto &a : t->getOwnAttributes()) {
Rooted<Element> attribute =
- transformStructTypeEntry(structType, t, a, P);
+ transformStructTypeEntry(structType, fieldTagName, t, a, P);
structType->addChild(attribute);
}
return structType;
@@ -504,7 +528,8 @@ Rooted<Element> transformTypesystem(Handle<Element> parent,
for (auto tp : t->getTypes()) {
Rooted<Element> type;
if (tp->isa(&RttiTypes::StructType)) {
- type = transformStructType(typesystem, tp.cast<StructType>(), P);
+ type = transformStructType(typesystem, "struct", "field",
+ tp.cast<StructType>(), P);
} else {
P.logger.warning(std::string("Type ") + tp->getName() +
" can not be serialized, because it is neither a "
diff --git a/testdata/integration/ontology_serialization/attributes_descriptor.in.osml b/testdata/integration/ontology_serialization/attributes_descriptor.in.osml
new file mode 100644
index 0000000..8c1957a
--- /dev/null
+++ b/testdata/integration/ontology_serialization/attributes_descriptor.in.osml
@@ -0,0 +1,10 @@
+\document
+ \ontology#test
+ \struct#a[root=true]
+ \attributes
+ \attribute[name="myAttr", type=int, default=4]
+ \struct#b[isa=a]
+ \attributes
+ \attribute[name="myOtherAttr", type=string, default="bla"]
+
+\a[myAttr=4]
diff --git a/testdata/integration/ontology_serialization/attributes_descriptor.out.osxml b/testdata/integration/ontology_serialization/attributes_descriptor.out.osxml
new file mode 100644
index 0000000..00f21f5
--- /dev/null
+++ b/testdata/integration/ontology_serialization/attributes_descriptor.out.osxml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<document>
+ <ontology name="test">
+ <struct cardinality="{*}" name="a" root="true" transparent="false">
+ <syntax/>
+ <attributes>
+ <attribute default="4" name="myAttr" type="int"/>
+ </attributes>
+ </struct>
+ <struct cardinality="{*}" isa="a" name="b" root="false" transparent="false">
+ <syntax/>
+ <attributes>
+ <attribute default="bla" name="myOtherAttr" type="string"/>
+ </attributes>
+ </struct>
+ </ontology>
+ <test:a myAttr="4"/>
+</document>