diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-16 17:33:56 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-16 17:33:56 +0100 |
commit | 821648607f5e0c6702eefbc206c0421d1a347bda (patch) | |
tree | d6a5241ba39d75d7aec1f7e8544e05a03142e47d /src/core/model/Document.cpp | |
parent | 9929838e62d9c17647d74be54af5853e8b613c4b (diff) |
first attempt on validation method for Document classes.
Diffstat (limited to 'src/core/model/Document.cpp')
-rw-r--r-- | src/core/model/Document.cpp | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp index 2f12acb..92791a4 100644 --- a/src/core/model/Document.cpp +++ b/src/core/model/Document.cpp @@ -92,7 +92,8 @@ int DocumentEntity::getFieldDescriptorIndex( } } -bool DocumentEntity::validate(Logger &logger) const +bool DocumentEntity::doValidate(Logger &logger, + std::set<ManagedUid> &visited) const { // TODO: check the validated form of Attributes // iterate over every field @@ -134,9 +135,16 @@ bool DocumentEntity::validate(Logger &logger) const // iterate over every actual child of this DocumentEntity for (auto &rc : fields[f]) { - if (!rc->isa(RttiTypes::StructuredEntity)) { + if (!rc->isa(RttiTypes::Anchor)) { + // Anchors are uninteresting and can be ignored. continue; } + if (!rc->isa(RttiTypes::DocumentPrimitive)) { + // For DocumentPrimitives we have to check the content type. + // TODO: Do that! + continue; + } + // otherwise this is a StructuredEntity Handle<StructuredEntity> c = rc.cast<StructuredEntity>(); ManagedUid id = c->getDescriptor()->getUid(); @@ -193,6 +201,23 @@ bool DocumentEntity::validate(Logger &logger) const } } } + + // go into recursion. + for (auto &f : fields) { + for (auto &n : f) { + if (!visited.insert(n->getUid()).second) { + logger.error("The given document contains a cycle!"); + return false; + } + if (n->isValidated()) { + continue; + } + if (!n->validate(logger, visited)) { + return false; + } + } + } + return true; } @@ -222,6 +247,17 @@ StructuredEntity::StructuredEntity(Manager &mgr, Handle<Document> doc, doc->setRoot(this); } +bool StructuredEntity::doValidate(Logger &logger, + std::set<ManagedUid> &visited) const +{ + // check if the parent is set. + if (getParent() == nullptr) { + return false; + } + // check the validity as a DocumentEntity. + return DocumentEntity::doValidate(logger, visited); +} + /* Class AnnotationEntity */ AnnotationEntity::AnnotationEntity(Manager &mgr, Handle<Document> parent, @@ -236,6 +272,34 @@ AnnotationEntity::AnnotationEntity(Manager &mgr, Handle<Document> parent, parent->annotations.push_back(this); } +bool AnnotationEntity::doValidate(Logger &logger, + std::set<ManagedUid> &visited) const +{ + // check if this AnnotationEntity is correctly registered at its document. + if (getParent() == nullptr || !getParent()->isa(RttiTypes::Document)) { + return false; + } + Handle<Document> doc = getParent().cast<Document>(); + bool found = false; + for (auto &a : doc->getAnnotations()) { + if (a == this) { + found = true; + break; + } + } + if (!found) { + logger.error("This annotation was not registered at the document."); + return false; + } + + // check the validity as a DocumentEntity. + if (!DocumentEntity::doValidate(logger, visited)) { + return false; + } + // TODO: then check if the anchors are in the correct document. + return true; +} + /* Class Document */ void Document::continueResolve(ResolutionState &state) |