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
|
/*
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 <iostream>
#include <string>
#include <core/common/Logger.hpp>
#include <core/common/Rtti.hpp>
#include <core/common/Utils.hpp>
#include <core/resource/Resource.hpp>
#include <core/resource/Resource.hpp>
#include <core/Registry.hpp>
#include "ResourceRequest.hpp"
namespace ousia {
namespace RttiTypes {
extern const Rtti Document;
extern const Rtti Domain;
extern const Rtti Node;
extern const Rtti Typesystem;
}
/**
* Map mapping from Rtti pointers to the corresponding ResourceType.
*/
static const std::unordered_map<const Rtti *, ResourceType>
RTTI_RESOURCE_TYPE_MAP{{&RttiTypes::Document, ResourceType::DOCUMENT},
{&RttiTypes::Domain, ResourceType::DOMAIN_DESC},
{&RttiTypes::Typesystem, ResourceType::TYPESYSTEM}};
/**
* Function used internally to build a set with all currently supported
* ResourceType instances.
*
* @param supportedTypes are all supported types.
* @return a set containing all ResourceTypes that can be used for these
* RTTI descriptors.
*/
static const std::unordered_set<ResourceType, Utils::EnumHash>
supportedResourceTypes(const RttiSet &supportedTypes)
{
std::unordered_set<ResourceType, Utils::EnumHash> res;
for (const Rtti *supportedType : supportedTypes) {
auto it = RTTI_RESOURCE_TYPE_MAP.find(supportedType);
if (it != RTTI_RESOURCE_TYPE_MAP.end()) {
res.insert(it->second);
}
}
return res;
}
/**
* Converts a set of supported RTTI descriptors to a string describing the
* corresponding ResourceTypes.
*
* @param supportedTypes are all supported types.
* @return a string containing all corresponding resource types.
*/
static std::string supportedResourceTypesString(const RttiSet &supportedTypes)
{
return Utils::join(supportedResourceTypes(supportedTypes), "\", \"", "\"",
"\"");
}
/**
* Tries to deduce the resource type from the given set of supported types.
* Returns ResourceType::UNKNOWN if there are ambiguities.
*
* @param supportedTypes are all supported types.
* @return the deduced ResourceType or ResourceType::UNKNOWN if there was an
* ambiguity.
*/
static ResourceType deduceResourceType(const RttiSet &supportedTypes)
{
ResourceType resourceType = ResourceType::UNKNOWN;
for (const Rtti *supportedType : supportedTypes) {
auto it = RTTI_RESOURCE_TYPE_MAP.find(supportedType);
if (it != RTTI_RESOURCE_TYPE_MAP.end()) {
// Preven ambiguity
if (resourceType != ResourceType::UNKNOWN &&
resourceType != it->second) {
resourceType = ResourceType::UNKNOWN;
break;
}
resourceType = it->second;
}
}
return resourceType;
}
/**
* Function used to limit the supportedTypes to those that correspond to the
* ResourceType.
*
* @param resourceType is the type of the resource type that is going to be
* included.
* @param supportedTypes are all supported types.
* @return a restricted set of supportedTypes that correspond to the
* resourceType.
*/
static RttiSet limitSupportedTypes(ResourceType resourceType,
const RttiSet &supportedTypes)
{
// Calculate the expected types
RttiSet expectedTypes;
for (auto entry : RTTI_RESOURCE_TYPE_MAP) {
if (entry.second == resourceType) {
expectedTypes.insert(entry.first);
}
}
// Restrict the supported types to the expected types
return Rtti::setIntersection(supportedTypes, expectedTypes);
}
/* Class ResourceRequest */
ResourceRequest::ResourceRequest(const std::string &path,
const std::string &mimetype,
const std::string &rel,
const RttiSet &supportedTypes)
: path(path),
mimetype(mimetype),
rel(rel),
supportedTypes(supportedTypes),
resourceType(ResourceType::UNKNOWN),
parser(nullptr)
{
}
bool ResourceRequest::deduce(Registry ®istry, Logger &logger)
{
bool ok = true;
// Make sure the given file name is not empty
if (path.empty()) {
logger.error("Filename may not be empty");
return false;
}
// Try to deduce the mimetype if none was given
if (mimetype.empty()) {
mimetype = registry.getMimetypeForFilename(path);
if (mimetype.empty()) {
logger.error(std::string("Filename \"") + path +
std::string(
"\" has an unknown file extension. Explicitly "
"specify a mimetype."));
ok = false;
}
}
// Find a parser for the mimetype
if (!mimetype.empty()) {
auto parserDescr = registry.getParserForMimetype(mimetype);
parser = parserDescr.first;
parserTypes = parserDescr.second;
// Make sure a valid parser was returned, and if yes, whether the
// parser is allows to run here
if (!parser) {
logger.error(std::string("Cannot parse files of type \"") +
mimetype + std::string("\""));
ok = false;
} else if (!Rtti::setIsOneOf(supportedTypes, parserTypes)) {
logger.error(std::string("Resource of type \"") + mimetype +
std::string("\" cannot be included here!"));
ok = false;
}
}
// Try to deduce the ResourceType from the "rel" string
if (!rel.empty()) {
resourceType = Resource::getResourceTypeByName(rel);
if (resourceType == ResourceType::UNKNOWN) {
logger.error(std::string("Unknown relation \"") + rel +
std::string("\", expected one of ") +
supportedResourceTypesString(supportedTypes));
ok = false;
}
}
// Try to deduce the ResourceType from the supportedTypes
if (resourceType == ResourceType::UNKNOWN) {
resourceType = deduceResourceType(supportedTypes);
}
// Further limit the supportedTypes to those types that correspond to the
// specified resource type.
if (resourceType != ResourceType::UNKNOWN) {
supportedTypes = limitSupportedTypes(resourceType, supportedTypes);
if (supportedTypes.empty()) {
logger.error(std::string("Resource of type \"") + mimetype +
std::string("\" and relationship \"") +
Resource::getResourceTypeName(resourceType) +
std::string("\" cannot be included here"));
ok = false;
}
} else if (supportedTypes.size() != 1 ||
*supportedTypes.begin() != &RttiTypes::Node) {
logger.warning(std::string(
"Ambiguous resource relationship, consider "
"specifying one of ") +
supportedResourceTypesString(supportedTypes) +
std::string(" as \"rel\" attribute"));
}
return ok;
}
bool ResourceRequest::locate(Registry ®istry, Logger &logger,
Resource &resource,
const Resource &relativeTo) const
{
if (!registry.locateResource(resource, path, resourceType, relativeTo)) {
logger.error(std::string("File not found: ") + path);
return false;
}
return true;
}
}
|