summaryrefslogtreecommitdiff
path: root/src/core/Registry.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/Registry.cpp')
-rw-r--r--src/core/Registry.cpp90
1 files changed, 76 insertions, 14 deletions
diff --git a/src/core/Registry.cpp b/src/core/Registry.cpp
index 86665a2..4aad7db 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,92 @@
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
+{
+ 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)
{
- 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));
+ // 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;
+}
+
+void Registry::registerDefaultExtensions()
+{
+ registerExtension("oxd", "text/vnd.ousia.oxd");
+ registerExtension("oxm", "text/vnd.ousia.oxm");
+ registerExtension("opd", "text/vnd.ousia.opd");
+ registerExtension("oss", "text/vnd.ousia.oss");
+ registerExtension("js", "application/javascript");
}
-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{};
+}
+
+std::string Registry::getMimetypeForFilename(const std::string &filename) const
+{
+ // Fetch the file extension
+ std::string ext = Utils::extractFileExtension(filename);
+ if (ext.empty()) {
+ return std::string{};
+ }
+
+ // Fetch the mimetype for the extension
+ return getMimetypeForExtension(ext);
}
-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,