diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-20 01:24:17 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-20 01:24:17 +0100 |
commit | 57a1ea659ae125934c541951113c0c3a38b10971 (patch) | |
tree | 8bf6f93a1c4b56cd48777b2f2f80c77466ef61ee /src/core/Registry.cpp | |
parent | 20d3e71f26ca884271ed5d372a8459394554c147 (diff) | |
parent | 533e1a93c8f636b78f5687dd9c343a81563081a1 (diff) |
Merge branch 'master' of somweyr.de:ousia
Diffstat (limited to 'src/core/Registry.cpp')
-rw-r--r-- | src/core/Registry.cpp | 51 |
1 files changed, 35 insertions, 16 deletions
diff --git a/src/core/Registry.cpp b/src/core/Registry.cpp index 74d1cf8..9427aed 100644 --- a/src/core/Registry.cpp +++ b/src/core/Registry.cpp @@ -16,8 +16,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <core/common/Logger.hpp> #include <core/parser/Parser.hpp> +#include <core/resource/Resource.hpp> +#include <core/resource/ResourceLocator.hpp> + +#include "Registry.hpp" namespace ousia { @@ -25,13 +28,13 @@ using namespace parser; /* Class Registry */ -void Registry::registerParser(parser::Parser *parser) +void Registry::registerParser(parser::Parser &parser) { - parsers.push_back(parser); - for (const auto &mime : parser->mimetypes()) { + parsers.push_back(&parser); + for (const auto &mime : parser.mimetypes()) { //TODO: This does not allow for multiple parsers with the same mimetype. // Is that how its supposed to be? - parserMimetypes.insert(std::make_pair(mime, parser)); + parserMimetypes.insert(std::make_pair(mime, &parser)); } } @@ -44,24 +47,40 @@ Parser *Registry::getParserForMimetype(const std::string &mimetype) const return nullptr; } -void Registry::registerResourceLocator(ResourceLocator *locator) +void Registry::registerResourceLocator(ResourceLocator &locator) { - locators.push_back(locator); + locators.push_back(&locator); } -ResourceLocator::Location Registry::locateResource( - const std::string &path, const std::string &relativeTo, - ResourceLocator::Type type) const +bool Registry::locateResource(Resource &resource, const std::string &path, + ResourceType type, + const Resource &relativeTo) const { - ResourceLocator::Location *last; + // Try the locator of the given "relativeTo" resource first + if (relativeTo.isValid()) { + if (relativeTo.getLocator().locate(resource, path, type, relativeTo)) { + return true; + } + } + + // Iterate over all registered locators and try to resolve the given path for (auto &locator : locators) { - ResourceLocator::Location loc = locator->locate(path, relativeTo, type); - if (loc.found) { - return loc; + if (locator->locate(resource, path, type, relativeTo)) { + return true; } - last = &loc; } - return *last; + + // If this did not work out, retry but use the UNKNOWN type. + if (type != ResourceType::UNKNOWN) { + for (auto &locator : locators) { + if (locator->locate(resource, path, ResourceType::UNKNOWN, + relativeTo)) { + return true; + } + } + } + + return false; } } |