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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/*
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/Logger.hpp>
#include <core/common/Rtti.hpp>
#include <core/common/Utils.hpp>
#include <core/model/Node.hpp>
#include <core/parser/ParserContext.hpp>
#include <core/Registry.hpp>
#include "Resource.hpp"
#include "ResourceManager.hpp"
namespace ousia {
/* Deduction of the ResourceType */
namespace RttiTypes {
extern const Rtti Document;
extern const Rtti Domain;
extern const Rtti Node;
extern const Rtti Typesystem;
}
/**
* Map mapping from relations (the "rel" attribute in includes) to the
* corresponding ResourceType.
*/
static const std::unordered_map<std::string, ResourceType> RelResourceTypeMap{
{"document", ResourceType::DOCUMENT},
{"domain", ResourceType::DOMAIN_DESC},
{"typesystem", ResourceType::TYPESYSTEM}};
/**
* Map mapping from Rtti pointers to the corresponding ResourceType.
*/
static const std::unordered_map<Rtti *, ResourceType> RttiResourceTypeMap{
{&RttiTypes::Document, ResourceType::DOCUMENT},
{&RttiTypes::Domain, ResourceType::DOMAIN_DESC},
{&RttiTypes::Typesystem, ResourceType::TYPESYSTEM}};
static ResourceType relToResourceType(const std::string &rel, Logger &logger)
{
std::string s = Utils::toLowercase(rel);
if (!s.empty()) {
auto it = RelResourceTypeMap.find(s);
if (it != RelResourceTypeMap.end()) {
return it->second;
} else {
logger.error(std::string("Unknown relation \"") + rel +
std::string("\""));
}
}
return ResourceType::UNKNOWN;
}
static ResourceType supportedTypesToResourceType(const RttiSet &supportedTypes)
{
if (supportedTypes.size() == 1U) {
auto it = RttiResourceTypeMap.find(supportedTypes[0]);
if (it != RelResourceTypeMap.end()) {
return it->second;
}
}
return ResourceType::UNKNOWN;
}
static ResourceType deduceResourceType(const std::string &rel,
const RttiSet &supportedTypes,
Logger &logger)
{
ResourceType res;
// Try to deduce the ResourceType from the "rel" attribute
res = relToResourceType(rel, logger);
// If this did not work, try to deduce the ResourceType from the
// supportedTypes supplied by the Parser instance.
if (res == ResourceType::UNKNOWN) {
res = supportedTypesToResourceType(supportedTypes);
}
if (res == ResourceType::UNKNOWN) {
logger.note(
"Ambigous resource type, consider specifying the \"rel\" "
"attribute");
}
return res;
}
/* Functions for reducing the set of supported types */
/**
* Map mapping from relations (the "rel" attribute in includes) to the
* corresponding RttiType
*/
static const std::unordered_map<std::string, Rtti *> RelRttiTypeMap{
{"document", &RttiTypes::DOCUMENT},
{"domain", &RttiTypes::DOMAIN},
{"typesystem", &RttiTypes::TYPESYSTEM}};
static Rtti *relToRttiType(const std::string &rel)
{
std::string s = Utils::toLowercase(rel);
if (!s.empty()) {
auto it = RelRttiTypeMap.find(s);
if (it != RelRttiTypeMap.end()) {
return it->second;
}
}
return &ResourceType::Node;
}
static RttiType shrinkSupportedTypes(const RttiSet &supportedTypes,
const std::string &rel)
{
RttiSet types;
RttiType *type = relToRttiType(rel);
for (RttiType *supportedType : supportedTypes) {
if (supportedType->isa(type)) {
types.insert(supportedType);
}
}
return types;
}
/* Class ResourceManager */
Rooted<Node> ResourceManager::link(ParserContext &ctx, Resource &resource,
const std::string &mimetype,
const RttiSet &supportedTypes)
{
}
Rooted<Node> ResourceManager::link(ParserContext &ctx, const std::string &path,
const std::string &mimetype,
const std::string &rel,
const RttiSet &supportedTypes,
const Resource &relativeTo)
{
// Try to deduce the ResourceType
ResourceType resourceType =
deduceResourceType(rel, supportedTypes, ctx.logger);
// Lookup the resource for given path and resource type
Resource resource;
if (!ctx.registry.locateResource(resource, path, resourceType,
relativeTo)) {
ctx.logger.error("File \"" + path + "\" not found.");
return nullptr;
}
// Try to shrink the set of supportedTypes
RttiSet types = shrinkSupportedTypes(supportedTypes, rel);
// Check whether the resource has already been parsed
Rooted<Node> node = nullptr;
auto it = locations.find(res.getLocation());
if (it != locations.end()) {
node =
}
= link(ctx, resource, mimetype, types);
// Try to deduce the mimetype
std::string mime = mimetype;
if (mime.empty()) {
// Fetch the file extension
std::string ext = Utils::extractFileExtension(path);
if (ext.empty()) {
ctx.logger.error(
std::string("Specified filename \"") + path +
std::string(
"\" has no extension and no mimetype (\"type\" "
"attribute) was given instead."));
return nullptr;
}
// Fetch the mimetype for the extension
mime = ctx.registry.getMimetypeForExtension(ext);
if (mime.empty()) {
ctx.logger.error(std::string("Unknown file extension \"") + ext +
std::string("\""));
return nullptr;
}
}
// Fetch a parser for the mimetype
const std::pair<Parser *, RttiSet> parser =
ctx.registry.getParserForMimetype(mime);
// Make sure a parser was found
if (!parser->first) {
ctx.logger.error(std::string("Cannot parse files of type \"") + mime +
std::string("\""));
}
// Make sure the parser returns one of the supported types
}
Rooted<Node> ResourceManager::link(ParserContext &ctx, const std::string &path,
const std::string &mimetype,
const std::string &rel,
const RttiSet &supportedTypes,
SourceId relativeTo)
{
// Fetch the resource corresponding to the source id, make sure it is valid
const Resource &relativeResource = getResource(relativeTo);
if (!relativeResource.isValid()) {
ctx.logger.fatalError("Internal error: Invalid SourceId supplied.");
return nullptr;
}
// Continue with the usual include routine
return include(ctx, path, mimetype, rel, supportedTypes, relativeResource);
}
const Resource &getResource(SourceId sourceId) const
{
if (sourceId < resources.size()) {
return resources[sourceId];
}
return NullResource;
}
SourceContext ResourceManager::buildContext(const SourceLocation &location)
{
SourceContext res;
// TODO
return res;
}
};
}
#endif /* _OUSIA_RESOURCE_MANAGER_HPP_ */
|