diff options
Diffstat (limited to 'src/core/resource/ResourceManager.cpp')
-rw-r--r-- | src/core/resource/ResourceManager.cpp | 234 |
1 files changed, 88 insertions, 146 deletions
diff --git a/src/core/resource/ResourceManager.cpp b/src/core/resource/ResourceManager.cpp index 184a16d..03fd65b 100644 --- a/src/core/resource/ResourceManager.cpp +++ b/src/core/resource/ResourceManager.cpp @@ -25,33 +25,17 @@ #include <core/common/SourceContextReader.hpp> #include <core/common/Utils.hpp> #include <core/model/Node.hpp> -#include <core/parser/ParserContext.hpp> +#include <core/model/Project.hpp> #include <core/parser/Parser.hpp> +#include <core/parser/ParserContext.hpp> +#include <core/parser/ParserScope.hpp> #include <core/Registry.hpp> #include "ResourceManager.hpp" -#include "ResourceUtils.hpp" +#include "ResourceRequest.hpp" namespace ousia { -/* Static helper functions */ - -static void logUnsopportedType(Logger &logger, Resource &resource, - const RttiSet &supportedTypes) -{ - // Build a list containing the expected type names - std::vector<std::string> expected; - for (const Rtti *supportedType : supportedTypes) { - expected.push_back(supportedType->name); - } - - // Log the actual error message - logger.error( - std::string("Expected the file \"") + resource.getLocation() + - std::string("\" to define one of the following internal types ") + - Utils::join(expected, ", ", "{", "}")); -} - /* Class ResourceManager */ SourceId ResourceManager::allocateSourceId(const Resource &resource) @@ -86,39 +70,14 @@ void ResourceManager::purgeResource(SourceId sourceId) contextReaders.erase(sourceId); } -Rooted<Node> ResourceManager::parse(ParserContext &ctx, Resource &resource, - const std::string &mimetype, - const RttiSet &supportedTypes) +Rooted<Node> ResourceManager::parse(Registry ®istry, ParserContext &ctx, + const ResourceRequest &req, ParseMode mode) { - // Try to deduce the mimetype of no mimetype was given - std::string mime = mimetype; - if (mime.empty()) { - mime = ctx.registry.getMimetypeForFilename(resource.getLocation()); - if (mime.empty()) { - ctx.logger.error(std::string("Filename \"") + - resource.getLocation() + - std::string( - "\" has an unknown file extension. Explicitly " - "specify a mimetype.")); - return nullptr; - } - } - - // Fetch a parser for the mimetype - const std::pair<Parser *, RttiSet> &parserDescr = - ctx.registry.getParserForMimetype(mime); - Parser *parser = parserDescr.first; - - // Make sure a parser was found - if (!parser) { - ctx.logger.error(std::string("Cannot parse files of type \"") + mime + - std::string("\"")); - return nullptr; - } - - // Make sure the parser returns at least one of the supported types - if (!Rtti::setIsOneOf(parserDescr.second, supportedTypes)) { - logUnsopportedType(ctx.logger, resource, supportedTypes); + // Locate the resource relative to the old resource, abort if this did not + // work + Resource resource; + if (!req.locate(registry, ctx.getLogger(), resource, + getResource(ctx.getSourceId()))) { return nullptr; } @@ -129,16 +88,31 @@ Rooted<Node> ResourceManager::parse(ParserContext &ctx, Resource &resource, Rooted<Node> node; try { // Set the current source id in the logger instance - GuardedLogger logger(ctx.logger, SourceLocation{sourceId}); - - // Fetch the input stream and create a char reader - std::unique_ptr<std::istream> is = resource.stream(); - CharReader reader(*is, sourceId); - - // Actually parse the input stream - node = parser->parse(reader, ctx); + { + GuardedLogger logger(ctx.getLogger(), SourceLocation{sourceId}); + + // Fetch the input stream and create a char reader + std::unique_ptr<std::istream> is = resource.stream(); + CharReader reader(*is, sourceId); + + // Actually parse the input stream, distinguish the LINK and the + // INCLUDE + // mode + switch (mode) { + case ParseMode::LINK: { + ParserScope scope; // New empty parser scope instance + ParserContext childCtx = ctx.clone(scope, sourceId); + node = req.getParser()->parse(reader, childCtx); + } + case ParseMode::INCLUDE: { + ParserContext childCtx = ctx.clone(sourceId); + node = req.getParser()->parse(reader, childCtx); + } + } + } if (node == nullptr) { - throw LoggableException{"Internal error: Parser returned null."}; + throw LoggableException{"File \"" + resource.getLocation() + + "\" cannot be parsed."}; } } catch (LoggableException ex) { @@ -146,17 +120,66 @@ Rooted<Node> ResourceManager::parse(ParserContext &ctx, Resource &resource, purgeResource(sourceId); // Log the exception and return nullptr - ctx.logger.log(ex); + ctx.getLogger().log(ex); return nullptr; } - // Store the parsed node along with the sourceId - storeNode(sourceId, node); + // Store the parsed node along with the sourceId, if we are in the LINK mode + if (mode == ParseMode::LINK) { + storeNode(sourceId, node); + } // Return the parsed node return node; } +Rooted<Node> ResourceManager::link(Registry ®istry, ParserContext &ctx, + const std::string &path, + const std::string &mimetype, + const std::string &rel, + const RttiSet &supportedTypes) +{ + ResourceRequest req{path, mimetype, rel, supportedTypes}; + if (req.deduce(registry, ctx.getLogger())) { + return parse(registry, ctx, req, ParseMode::LINK); + } + return nullptr; +} + +Rooted<Node> ResourceManager::include(Registry ®istry, ParserContext &ctx, + const std::string &path, + const std::string &mimetype, + const std::string &rel, + const RttiSet &supportedTypes) +{ + ResourceRequest req{path, mimetype, rel, supportedTypes}; + if (req.deduce(registry, ctx.getLogger())) { + return parse(registry, ctx, req, ParseMode::INCLUDE); + } + return nullptr; +} + +SourceContext ResourceManager::readContext(const SourceLocation &location, + size_t maxContextLength) +{ + const Resource &resource = getResource(location.getSourceId()); + if (resource.isValid()) { + // Fetch a char reader for the resource + std::unique_ptr<std::istream> is = resource.stream(); + CharReader reader{*is, location.getSourceId()}; + + // Return the context + return contextReaders[location.getSourceId()].readContext( + reader, location, maxContextLength, resource.getLocation()); + } + return SourceContext{}; +} + +SourceContext ResourceManager::readContext(const SourceLocation &location) +{ + return readContext(location, SourceContextReader::MAX_MAX_CONTEXT_LENGTH); +} + SourceId ResourceManager::getSourceId(const std::string &location) { auto it = locations.find(location); @@ -187,7 +210,7 @@ Rooted<Node> ResourceManager::getNode(Manager &mgr, SourceId sourceId) { auto it = nodes.find(sourceId); if (it != nodes.end()) { - Managed *managed = mgr.getManaged(sourceId); + Managed *managed = mgr.getManaged(it->second); if (managed != nullptr) { return dynamic_cast<Node *>(managed); } else { @@ -206,86 +229,5 @@ Rooted<Node> ResourceManager::getNode(Manager &mgr, const Resource &resource) { return getNode(mgr, getSourceId(resource)); } - -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 = - ResourceUtils::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 = ResourceUtils::limitRttiTypes(supportedTypes, rel); - - // Check whether the resource has already been parsed - Rooted<Node> node = getNode(ctx.manager, resource); - if (node == nullptr) { - // Node has not already been parsed, parse it now - node = parse(ctx, resource, mimetype, supportedTypes); - - // Abort if parsing failed - if (node == nullptr) { - return nullptr; - } - } - - // Make sure the node has one of the supported types - if (!node->type().isOneOf(supportedTypes)) { - logUnsopportedType(ctx.logger, resource, supportedTypes); - return nullptr; - } - - return node; -} - -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 link(ctx, path, mimetype, rel, supportedTypes, relativeResource); -} - -SourceContext ResourceManager::readContext(const SourceLocation &location, - size_t maxContextLength) -{ - const Resource &resource = getResource(location.getSourceId()); - if (resource.isValid()) { - // Fetch a char reader for the resource - std::unique_ptr<std::istream> is = resource.stream(); - CharReader reader{*is, location.getSourceId()}; - - // Return the context - return contextReaders[location.getSourceId()].readContext( - reader, location, maxContextLength, resource.getLocation()); - } - return SourceContext{}; -} - -SourceContext ResourceManager::readContext(const SourceLocation &location) -{ - return readContext(location, SourceContextReader::MAX_MAX_CONTEXT_LENGTH); -} - } |