summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Paassen <bpaassen@techfak.uni-bielefeld.de>2015-02-04 20:20:02 +0100
committerBenjamin Paassen <bpaassen@techfak.uni-bielefeld.de>2015-02-04 20:20:02 +0100
commitfce5cce5fdc7bd265eb023dfc0fc1accbe5a796e (patch)
treec745b76fc01ff7fa8c38c5810682c8878ade1fc1
parente76f58e912bd6661ba755d27da97bebf711f06ad (diff)
set non-empty default field name.
-rw-r--r--src/core/model/Document.cpp28
-rw-r--r--src/core/model/Document.hpp66
-rw-r--r--src/core/model/Domain.cpp2
-rw-r--r--src/core/model/Domain.hpp14
-rw-r--r--src/plugins/html/DemoOutput.cpp2
-rw-r--r--test/core/model/DocumentTest.cpp25
-rw-r--r--test/core/model/DomainTest.cpp2
-rw-r--r--test/core/model/TestDocumentBuilder.hpp19
8 files changed, 66 insertions, 92 deletions
diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp
index 8c87cfe..f27da1d 100644
--- a/src/core/model/Document.cpp
+++ b/src/core/model/Document.cpp
@@ -34,31 +34,15 @@ int DocumentEntity::getFieldDescriptorIndex(const std::string &fieldName,
const NodeVector<FieldDescriptor> &fds = descriptor->getFieldDescriptors();
unsigned int f = 0;
- // look if we have an empty name.
- if (fieldName == "") {
- // in that case we look for a default field.
- // First: Do we only have one field?
- if (fds.size() == 1) {
- // if so we return that one.
+ // otherwise we return the FieldDescriptor with the correct name (if
+ // such a descriptor exists).
+ for (auto &fd : fds) {
+ if (fd->getName() == fieldName) {
return f;
}
- // Second: Do we have a TREE field?
- for (auto &fd : fds) {
- if (fd->getFieldType() == FieldDescriptor::FieldType::TREE) {
- return f;
- }
- f++;
- }
- } else {
- // otherwise we return the FieldDescriptor with the correct name (if
- // such a descriptor exists).
- for (auto &fd : fds) {
- if (fd->getName() == fieldName) {
- return f;
- }
- f++;
- }
+ f++;
}
+
if (enforce) {
throw OusiaException(std::string("\"") + descriptor->getName() +
"\" has no field with name \"" + fieldName + "\"");
diff --git a/src/core/model/Document.hpp b/src/core/model/Document.hpp
index ad8cbca..35c6664 100644
--- a/src/core/model/Document.hpp
+++ b/src/core/model/Document.hpp
@@ -219,27 +219,20 @@ public:
/**
* This returns true if there is a FieldDescriptor in the Descriptor for
- * this DocumentEntity which has the given name. If an empty name is
- * given it is assumed that the 'default' FieldDescriptor is referenced,
- * where 'default' means either:
- * 1.) The only TREE typed FieldDescriptor (if present) or
- * 2.) the only FieldDescriptor (if only one is specified).
+ * this DocumentEntity which has the given name.
*
* @param fieldName is the name of a field as specified in the
* FieldDescriptor in the Domain description.
* @return true if this FieldDescriptor exists.
*/
- bool hasField(const std::string &fieldName = "") const
+ bool hasField(const std::string &fieldName = DEFAULT_FIELD_NAME) const
{
return getFieldDescriptorIndex(fieldName, false) != -1;
}
/**
* This returns the vector of entities containing all members of the field
- * with the given name. If an empty name is given it is assumed that the
- * 'default' FieldDescriptor is referenced, where 'default' means either:
- * 1.) The only TREE typed FieldDescriptor (if present) or
- * 2.) the only FieldDescriptor (if only one is specified).
+ * with the given name.
*
* If the name is unknown an exception is thrown.
*
@@ -248,7 +241,7 @@ public:
* @return a NodeVector of all StructuredEntities in that field.
*/
const NodeVector<StructureNode> &getField(
- const std::string &fieldName = "") const
+ const std::string &fieldName = DEFAULT_FIELD_NAME) const
{
return fields[getFieldDescriptorIndex(fieldName, true)];
}
@@ -272,11 +265,7 @@ public:
}
/**
- * This adds a StructureNode to the field with the given name. If an
- * empty name is given it is assumed that the 'default' FieldDescriptor is
- * referenced, where 'default' means either:
- * 1.) The only TREE typed FieldDescriptor (if present) or
- * 2.) the only FieldDescriptor (if only one is specified).
+ * This adds a StructureNode to the field with the given name.
*
* If the name is unknown an exception is thrown.
*
@@ -289,14 +278,10 @@ public:
* FieldDescriptor in the Domain description.
*/
void addStructureNode(Handle<StructureNode> s,
- const std::string &fieldName = "");
+ const std::string &fieldName = DEFAULT_FIELD_NAME);
/**
* This adds multiple StructureNodes to the field with the given name.
- * If an empty name is given it is assumed that the 'default'
- * FieldDescriptor is referenced, where 'default' means either:
- * 1.) The only TREE typed FieldDescriptor (if present) or
- * 2.) the only FieldDescriptor (if only one is specified).
*
* If the name is unknown an exception is thrown.
*
@@ -309,13 +294,9 @@ public:
* FieldDescriptor in the Domain description.
*/
void addStructureNodes(const std::vector<Handle<StructureNode>> &ss,
- const std::string &fieldName = "");
+ const std::string &fieldName = DEFAULT_FIELD_NAME);
/**
- * This removes a StructureNode from the field with the given name. If an
- * empty name is given it is assumed that the 'default' FieldDescriptor is
- * referenced, where 'default' means either:
- * 1.) The only TREE typed FieldDescriptor (if present) or
- * 2.) the only FieldDescriptor (if only one is specified).
+ * This removes a StructureNode from the field with the given name.
*
* If the name is unknown an exception is thrown.
*
@@ -327,8 +308,9 @@ public:
* @return true if this StructureNode was a child here and false if
* if was not found.
*/
- bool removeStructureNodeFromField(Handle<StructureNode> s,
- const std::string &fieldName = "");
+ bool removeStructureNodeFromField(
+ Handle<StructureNode> s,
+ const std::string &fieldName = DEFAULT_FIELD_NAME);
/**
* This adds a StructureNode to the field with the given FieldDescriptor.
@@ -409,7 +391,8 @@ public:
*/
Rooted<StructuredEntity> createChildStructuredEntity(
Handle<StructuredClass> descriptor, Variant attributes = {},
- const std::string &fieldName = "", std::string name = "");
+ const std::string &fieldName = DEFAULT_FIELD_NAME,
+ std::string name = "");
/*
* Creates a new DocumentPrimitive as child of this DocumentEntity.
*
@@ -423,7 +406,8 @@ public:
* @return the newly created DocumentPrimitive.
*/
Rooted<DocumentPrimitive> createChildDocumentPrimitive(
- Variant content = {}, const std::string &fieldName = "");
+ Variant content = {},
+ const std::string &fieldName = DEFAULT_FIELD_NAME);
/**
* Creates a new Anchor as child of this DocumentEntity.
@@ -434,8 +418,8 @@ public:
*
* @return the newly created Anchor.
*/
- Rooted<Anchor> createChildAnchor(std::string name,
- const std::string &fieldName = "");
+ Rooted<Anchor> createChildAnchor(
+ std::string name, const std::string &fieldName = DEFAULT_FIELD_NAME);
};
/**
@@ -488,14 +472,14 @@ public:
* @param attributes is a Map Variant containing attribute fillings for this
* StructuredEntity. It is empty per default.
* @param fieldName is the name of the field in the parent DocumentEntity
- * where this StructuredEntity shall be added. It is empty
- * per default, referring to the default field.
+ * where this StructuredEntity shall be added.
* @param name is some name for this StructuredEntity that may be used
* for later reference. It is empty per default.
*/
StructuredEntity(Manager &mgr, Handle<Node> parent,
Handle<StructuredClass> descriptor,
- Variant attributes = {}, const std::string &fieldName = "",
+ Variant attributes = {},
+ const std::string &fieldName = DEFAULT_FIELD_NAME,
std::string name = "")
: StructureNode(mgr, std::move(name), parent, fieldName),
DocumentEntity(this, descriptor, std::move(attributes))
@@ -560,11 +544,10 @@ public:
* specified at the parents Descriptor for the given
* fieldName.
* @param fieldName is the name of the field in the parent DocumentEntity
- * where this DocumentPrimitive shall be added. It is empty
- * per default, referring to the default field.
+ * where this DocumentPrimitive shall be added.
*/
DocumentPrimitive(Manager &mgr, Handle<Node> parent, Variant content = {},
- const std::string &fieldName = "")
+ const std::string &fieldName = DEFAULT_FIELD_NAME)
: StructureNode(mgr, "", parent, fieldName), content(content)
{
}
@@ -609,11 +592,10 @@ public:
* as child of the given parent.
* @param name is the Anchor id.
* @param fieldName is the name of the field in the parent DocumentEntity
- * where this Anchor shall be added. It is empty
- * per default, referring to the default field.
+ * where this Anchor shall be added.
*/
Anchor(Manager &mgr, std::string name, Handle<Node> parent,
- const std::string &fieldName = "")
+ const std::string &fieldName = DEFAULT_FIELD_NAME)
: StructureNode(mgr, std::move(name), parent, fieldName)
{
}
diff --git a/src/core/model/Domain.cpp b/src/core/model/Domain.cpp
index ef505dd..cc52ec7 100644
--- a/src/core/model/Domain.cpp
+++ b/src/core/model/Domain.cpp
@@ -66,7 +66,7 @@ bool FieldDescriptor::doValidate(Logger &logger) const
valid = false;
}
// check name
- if (!getName().empty()) {
+ if (getName() != DEFAULT_FIELD_NAME) {
valid = valid & validateName(logger);
}
// check consistency of FieldType with the rest of the FieldDescriptor.
diff --git a/src/core/model/Domain.hpp b/src/core/model/Domain.hpp
index 6648551..bef7919 100644
--- a/src/core/model/Domain.hpp
+++ b/src/core/model/Domain.hpp
@@ -217,6 +217,9 @@ class Descriptor;
class StructuredClass;
class Domain;
+
+static const std::string DEFAULT_FIELD_NAME = "$default";
+
/**
* As mentioned in the description above a FieldDescriptor specifies the
* StructuredClasses that are allowed as children of a StructuredClass or
@@ -289,7 +292,7 @@ public:
* Descriptor to be valid.
*/
FieldDescriptor(Manager &mgr, Handle<Descriptor> parent,
- Handle<Type> primitiveType, std::string name = "",
+ Handle<Type> primitiveType, std::string name = DEFAULT_FIELD_NAME,
bool optional = false);
/**
@@ -309,7 +312,7 @@ public:
*/
FieldDescriptor(Manager &mgr, Handle<Descriptor> parent = nullptr,
FieldType fieldType = FieldType::TREE,
- std::string name = "", bool optional = false);
+ std::string name = DEFAULT_FIELD_NAME, bool optional = false);
/**
* Returns a const reference to the NodeVector of StructuredClasses whose
@@ -559,7 +562,7 @@ public:
* @return the newly created FieldDescriptor.
*/
Rooted<FieldDescriptor> createPrimitiveFieldDescriptor(
- Handle<Type> primitiveType, std::string name = "",
+ Handle<Type> primitiveType, std::string name = DEFAULT_FIELD_NAME,
bool optional = false);
/**
@@ -578,7 +581,8 @@ public:
*/
Rooted<FieldDescriptor> createFieldDescriptor(
FieldDescriptor::FieldType fieldType = FieldDescriptor::FieldType::TREE,
- std::string name = "", bool optional = false);
+ std::string name = DEFAULT_FIELD_NAME,
+ bool optional = false);
/**
* This tries to construct the shortest possible path of this Descriptor
@@ -739,7 +743,7 @@ public:
* @param root specifies whether this StructuredClass is
* allowed to be at the root of a Document.
*/
- StructuredClass(Manager &mgr, std::string name = "",
+ StructuredClass(Manager &mgr, std::string name,
Handle<Domain> domain = nullptr,
Variant cardinality = AnyCardinality,
Handle<StructType> attributesDescriptor = nullptr,
diff --git a/src/plugins/html/DemoOutput.cpp b/src/plugins/html/DemoOutput.cpp
index 503c104..cefb3c9 100644
--- a/src/plugins/html/DemoOutput.cpp
+++ b/src/plugins/html/DemoOutput.cpp
@@ -318,7 +318,7 @@ Rooted<xml::Element> DemoHTMLTransformer::transformParagraph(
std::string childDescriptorName = t->getDescriptor()->getName();
if (childDescriptorName == "text") {
Handle<DocumentPrimitive> primitive =
- t->getField()[0].cast<DocumentPrimitive>();
+ t->getField("content")[0].cast<DocumentPrimitive>();
if (primitive.isNull()) {
throw OusiaException("Text field is not primitive!");
}
diff --git a/test/core/model/DocumentTest.cpp b/test/core/model/DocumentTest.cpp
index 7190437..5a2bcec 100644
--- a/test/core/model/DocumentTest.cpp
+++ b/test/core/model/DocumentTest.cpp
@@ -65,11 +65,13 @@ TEST(Document, construct)
foreword->getField()[0].cast<StructuredEntity>();
ASSERT_FALSE(text.isNull());
ASSERT_EQ("text", text->getDescriptor()->getName());
- ASSERT_TRUE(text->hasField());
- ASSERT_EQ(1U, text->getField().size());
- ASSERT_TRUE(text->getField()[0]->isa(typeOf<DocumentPrimitive>()));
- Variant content =
- text->getField()[0].cast<DocumentPrimitive>()->getContent();
+ ASSERT_TRUE(text->hasField("content"));
+ ASSERT_EQ(1U, text->getField("content").size());
+ ASSERT_TRUE(
+ text->getField("content")[0]->isa(typeOf<DocumentPrimitive>()));
+ Variant content = text->getField("content")[0]
+ .cast<DocumentPrimitive>()
+ ->getContent();
ASSERT_EQ("Some introductory text", content.asString());
}
}
@@ -97,12 +99,13 @@ TEST(Document, construct)
par->getField()[0].cast<StructuredEntity>();
ASSERT_FALSE(text.isNull());
ASSERT_EQ("text", text->getDescriptor()->getName());
- ASSERT_TRUE(text->hasField());
- ASSERT_EQ(1U, text->getField().size());
- ASSERT_TRUE(
- text->getField()[0]->isa(typeOf<DocumentPrimitive>()));
- Variant content =
- text->getField()[0].cast<DocumentPrimitive>()->getContent();
+ ASSERT_TRUE(text->hasField("content"));
+ ASSERT_EQ(1U, text->getField("content").size());
+ ASSERT_TRUE(text->getField("content")[0]->isa(
+ typeOf<DocumentPrimitive>()));
+ Variant content = text->getField("content")[0]
+ .cast<DocumentPrimitive>()
+ ->getContent();
ASSERT_EQ("Some actual text", content.asString());
}
}
diff --git a/test/core/model/DomainTest.cpp b/test/core/model/DomainTest.cpp
index 32cdd24..69c6694 100644
--- a/test/core/model/DomainTest.cpp
+++ b/test/core/model/DomainTest.cpp
@@ -213,7 +213,7 @@ TEST(Descriptor, pathToAdvanced)
ASSERT_TRUE(path[1]->isa(RttiTypes::StructuredClass));
ASSERT_EQ("B", path[1]->getName());
ASSERT_TRUE(path[2]->isa(RttiTypes::FieldDescriptor));
- ASSERT_EQ("", path[2]->getName());
+ ASSERT_EQ("$default", path[2]->getName());
}
TEST(StructuredClass, isSubclassOf)
diff --git a/test/core/model/TestDocumentBuilder.hpp b/test/core/model/TestDocumentBuilder.hpp
index 71b353d..05b27b7 100644
--- a/test/core/model/TestDocumentBuilder.hpp
+++ b/test/core/model/TestDocumentBuilder.hpp
@@ -133,8 +133,8 @@ Rooted<StructuredEntity> buildRootStructuredEntity(Handle<Document> document,
*/
Rooted<StructuredEntity> buildStructuredEntity(
Handle<Document> document, Logger &logger, Handle<StructuredEntity> parent,
- Path path, const std::string &fieldName = "", Variant attributes = {},
- std::string name = "")
+ Path path, const std::string &fieldName = DEFAULT_FIELD_NAME,
+ Variant attributes = {}, std::string name = "")
{
// If the input handles are not set, we can not build the entity.
if (parent == nullptr) {
@@ -152,7 +152,7 @@ Rooted<StructuredEntity> buildStructuredEntity(
if (descriptor == nullptr) {
return {nullptr};
}
- if(!descriptor->isa(RttiTypes::StructuredClass)){
+ if (!descriptor->isa(RttiTypes::StructuredClass)) {
return {nullptr};
logger.error("The descriptor was not an AnnotationClass!");
}
@@ -184,11 +184,12 @@ Rooted<StructuredEntity> buildStructuredEntity(
* input handle was empty or the given domains did not
* contain a AnnotationClass with the given name.
*/
-Rooted<AnnotationEntity> buildAnnotationEntity(
- Handle<Document> document, Logger &logger, const Path &path,
- Handle<Anchor> start,
- Handle<Anchor> end, Variant attributes = {},
- std::string name = "")
+Rooted<AnnotationEntity> buildAnnotationEntity(Handle<Document> document,
+ Logger &logger, const Path &path,
+ Handle<Anchor> start,
+ Handle<Anchor> end,
+ Variant attributes = {},
+ std::string name = "")
{
// If the input handles are not set, we can not build the entity.
if (document == nullptr) {
@@ -202,7 +203,7 @@ Rooted<AnnotationEntity> buildAnnotationEntity(
if (descriptor == nullptr) {
return {nullptr};
}
- if(!descriptor->isa(RttiTypes::AnnotationClass)){
+ if (!descriptor->isa(RttiTypes::AnnotationClass)) {
return {nullptr};
logger.error("The descriptor was not an AnnotationClass!");
}