From 235cf24518ca40bec59b497a416d9831db12eaa3 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 8 Jan 2015 21:30:31 +0100 Subject: further extended example to include annotations (emphasized and strong). The DemoOutput for that is still missing, though. convenience build functions have also been implemented in Document.cpp. --- src/core/model/Document.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) (limited to 'src/core/model/Document.cpp') diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp index 073f728..c653fe3 100644 --- a/src/core/model/Document.cpp +++ b/src/core/model/Document.cpp @@ -100,7 +100,7 @@ static Rooted resolveDescriptor( } // Otherwise take the first valid result. for (auto &r : resolved) { - if(r->isa(typeOf())){ + if (r->isa(typeOf())) { return r.cast(); } } @@ -155,6 +155,9 @@ Rooted StructuredEntity::buildEntity( return {nullptr}; } // append the new entity to the right field. + if (!parent->hasField(fieldName)) { + return {nullptr}; + } NodeVector &field = parent->getField(fieldName); field.push_back(entity); @@ -178,13 +181,60 @@ Rooted DocumentPrimitive::buildEntity( return {nullptr}; } // append the new entity to the right field. + if (!parent->hasField(fieldName)) { + return {nullptr}; + } NodeVector &field = parent->getField(fieldName); field.push_back(entity); - // and return it. return entity; } +Rooted AnnotationEntity::buildAnchor( + Handle parent, std::string id, const std::string &fieldName) +{ + // If the parent is not set, we can not build the anchor. + if (parent == nullptr) { + return {nullptr}; + } + // Then construct the Anchor itself + Rooted anchor{ + new AnnotationEntity::Anchor(parent->getManager(), parent, id)}; + // append the new entity to the right field. + if (!parent->hasField(fieldName)) { + return {nullptr}; + } + NodeVector &field = parent->getField(fieldName); + field.push_back(anchor); + // and return it. + return anchor; +} + +Rooted AnnotationEntity::buildEntity( + Handle parent, std::vector> domains, + const std::string &className, Handle start, + Handle end, Variant attributes, std::string name) +{ + // If the parent is not set, we can not build the AnnotationEntity. + if (parent == nullptr) { + return {nullptr}; + } + // If we can not find the correct descriptor, we can not build the entity + // either. + Rooted descriptor = resolveDescriptor(domains, className); + if (descriptor == nullptr) { + return {nullptr}; + } + // Then construct the AnnotationEntity itself + Rooted anno{ + new AnnotationEntity(parent->getManager(), parent, descriptor, + attributes, start, end, name)}; + // append the new entity to the document + parent->getAnnotations().push_back(anno); + // and return it. + return anno; +} + /* Type registrations */ } -- cgit v1.2.3