diff options
Diffstat (limited to 'src/core/Registry.cpp')
-rw-r--r-- | src/core/Registry.cpp | 69 |
1 files changed, 55 insertions, 14 deletions
diff --git a/src/core/Registry.cpp b/src/core/Registry.cpp index 86665a2..c42a97a 100644 --- a/src/core/Registry.cpp +++ b/src/core/Registry.cpp @@ -16,6 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <core/common/Exceptions.hpp> +#include <core/common/Utils.hpp> #include <core/parser/Parser.hpp> #include <core/resource/Resource.hpp> #include <core/resource/ResourceLocator.hpp> @@ -24,32 +26,71 @@ namespace ousia { -using namespace parser; - /* Class Registry */ -void Registry::registerParser(parser::Parser &parser) +void Registry::registerParser(const std::set<std::string> &mimetypes, + const RttiSet &types, Parser *parser) +{ + for (const std::string &mimetype : mimetypes) { + // Make sure no other parser was given for this mimetype + auto it = parsers.find(mimetype); + if (it != parsers.end()) { + throw OusiaException{std::string{"Parser for mimetype "} + + mimetype + + std::string{" already registered."}}; + } + + // Store a reference at the parser and a copy of the given RttiSet + parsers[mimetype] = std::pair<Parser *, RttiSet>{parser, types}; + } +} + +static const std::pair<Parser *, RttiSet> NullParser{nullptr, RttiSet{}}; + +const std::pair<Parser *, RttiSet> &Registry::getParserForMimetype( + const std::string &mimetype) const { - 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)); + const auto it = parsers.find(mimetype); + if (it != parsers.end()) { + return it->second; + } + return NullParser; +} + +void Registry::registerExtension(const std::string &extension, + const std::string &mimetype) +{ + // Always use extensions in lower case + std::string ext = Utils::toLower(extension); + + // Make sure the extension is unique + auto it = extensions.find(ext); + if (it != extensions.end()) { + throw OusiaException{std::string{"Extension "} + extension + + std::string{" already registered."}}; } + + // Register the mimetype + extensions[ext] = mimetype; } -Parser *Registry::getParserForMimetype(const std::string &mimetype) const +std::string Registry::getMimetypeForExtension( + const std::string &extension) const { - const auto it = parserMimetypes.find(mimetype); - if (it != parserMimetypes.end()) { + // Always use extensions in lower case + std::string ext = Utils::toLower(extension); + + // Try to find the extension + auto it = extensions.find(ext); + if (it != extensions.end()) { return it->second; } - return nullptr; + return std::string{}; } -void Registry::registerResourceLocator(ResourceLocator &locator) +void Registry::registerResourceLocator(ResourceLocator *locator) { - locators.push_back(&locator); + locators.push_back(locator); } bool Registry::locateResource(Resource &resource, const std::string &path, |