summaryrefslogtreecommitdiff
path: root/src/core/Registry.cpp
blob: 88babb7adbc05261dcc17c28a21bc9470e0d0e05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    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>

#include "Registry.hpp"

namespace ousia {

/* Class Registry */

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)
{
	// 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");
}

std::string Registry::getMimetypeForExtension(
    const std::string &extension) const
{
	// 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 std::string{};
}

void Registry::registerResourceLocator(ResourceLocator *locator)
{
	locators.push_back(locator);
}

bool Registry::locateResource(Resource &resource, const std::string &path,
                              ResourceType type,
                              const Resource &relativeTo) const
{
	// 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) {
		if (locator->locate(resource, path, type, relativeTo)) {
			return true;
		}
	}

	return false;
}
}