summaryrefslogtreecommitdiff
path: root/src/core/resource
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-27 01:39:19 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-27 01:39:19 +0100
commit84f0004cdf45f6fbad6461676897aa27f03cbb93 (patch)
treeacb15536b40d7389c3c65df1567084bb57da416e /src/core/resource
parent495e2de57e587450e9532c7fe4ae0c2bfb196e6c (diff)
Removed dependency between "Project" model class and parser/resources. This reduces coupling and was stupid beforehand.
Diffstat (limited to 'src/core/resource')
-rw-r--r--src/core/resource/ResourceManager.cpp59
-rw-r--r--src/core/resource/ResourceManager.hpp56
2 files changed, 67 insertions, 48 deletions
diff --git a/src/core/resource/ResourceManager.cpp b/src/core/resource/ResourceManager.cpp
index c7a4104..d149e49 100644
--- a/src/core/resource/ResourceManager.cpp
+++ b/src/core/resource/ResourceManager.cpp
@@ -70,14 +70,23 @@ void ResourceManager::purgeResource(SourceId sourceId)
contextReaders.erase(sourceId);
}
-Rooted<Node> ResourceManager::parse(Registry &registry, ParserContext &ctx,
- const ResourceRequest &req, ParseMode mode)
+Rooted<Node> ResourceManager::parse(ParserContext &ctx, const std::string &path,
+ const std::string &mimetype,
+ const std::string &rel,
+ const RttiSet &supportedTypes,
+ ParseMode mode)
{
+ // Some references used for convenience
+ Registry &registry = ctx.getRegistry();
+ Logger &logger = ctx.getLogger();
+ Resource relativeTo = getResource(ctx.getSourceId());
+
// Locate the resource relative to the old resource, abort if this did not
// work
+ ResourceRequest req{path, mimetype, rel, supportedTypes};
Resource resource;
- if (!req.locate(registry, ctx.getLogger(), resource,
- getResource(ctx.getSourceId()))) {
+ if (!req.deduce(registry, logger) ||
+ !req.locate(registry, logger, resource, relativeTo)) {
return nullptr;
}
@@ -87,17 +96,18 @@ Rooted<Node> ResourceManager::parse(Registry &registry, ParserContext &ctx,
// We can now try to parse the given file
Rooted<Node> node;
try {
- // Set the current source id in the logger instance
{
- GuardedLogger logger(ctx.getLogger(), SourceLocation{sourceId});
+ // Set the current source id in the logger instance. Note that this
+ // modifies the logger instance -- the GuardedLogger is just used to
+ // make sure the default location is popped from the stack again.
+ GuardedLogger guardedLogger(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, distinguish the LINK and the
- // INCLUDE
- // mode
+ // INCLUDE mode
switch (mode) {
case ParseMode::LINK: {
ParserScope scope; // New empty parser scope instance
@@ -105,11 +115,11 @@ Rooted<Node> ResourceManager::parse(Registry &registry, ParserContext &ctx,
node = req.getParser()->parse(reader, childCtx);
// Perform all deferred resolutions
- scope.performDeferredResolution(ctx.getLogger());
+ scope.performDeferredResolution(logger);
// Validate the parsed node
if (node != nullptr) {
- node->validate(ctx.getLogger());
+ node->validate(logger);
}
break;
}
@@ -121,13 +131,14 @@ Rooted<Node> ResourceManager::parse(Registry &registry, ParserContext &ctx,
}
}
if (node == nullptr) {
- throw LoggableException{"Requested file \"" + resource.getLocation() +
+ throw LoggableException{"Requested file \"" +
+ resource.getLocation() +
"\" cannot be parsed."};
}
}
catch (LoggableException ex) {
// Log the exception and return nullptr
- ctx.getLogger().log(ex);
+ logger.log(ex);
return nullptr;
}
@@ -140,30 +151,21 @@ Rooted<Node> ResourceManager::parse(Registry &registry, ParserContext &ctx,
return node;
}
-Rooted<Node> ResourceManager::link(Registry &registry, ParserContext &ctx,
- const std::string &path,
+Rooted<Node> ResourceManager::link(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;
+ return parse(ctx, path, mimetype, rel, supportedTypes, ParseMode::LINK);
}
-Rooted<Node> ResourceManager::include(Registry &registry, ParserContext &ctx,
+Rooted<Node> ResourceManager::include(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;
+ return parse(ctx, path, mimetype, rel, supportedTypes, ParseMode::INCLUDE);
}
SourceContext ResourceManager::readContext(const SourceLocation &location,
@@ -236,5 +238,12 @@ Rooted<Node> ResourceManager::getNode(Manager &mgr, const Resource &resource)
{
return getNode(mgr, getSourceId(resource));
}
+
+SourceContextCallback ResourceManager::getSourceContextCallback()
+{
+ return [this](const SourceLocation &location) {
+ return this->readContext(location);
+ };
+}
}
diff --git a/src/core/resource/ResourceManager.hpp b/src/core/resource/ResourceManager.hpp
index cdd0e34..e98e8f4 100644
--- a/src/core/resource/ResourceManager.hpp
+++ b/src/core/resource/ResourceManager.hpp
@@ -42,7 +42,6 @@
namespace ousia {
// Forward declarations
-class Registry;
class Node;
class Parser;
class ParserContext;
@@ -119,16 +118,23 @@ private:
* "link" or the "include" mode. In the latter case the ParserScope instance
* inside the ParserContext is exchanged with an empty one.
*
- * @param registry is the registry that should be used to locate the
- * resource.
* @param ctx is the context that should be passed to the parser.
- * @param req is a ResourceRequest instance that contains all information
- * about the requested resource.
+ * @param mimetype is the mimetype of the resource that should be parsed
+ * (may be empty, in which case the mimetype is deduced from the file
+ * extension)
+ * @param rel is a "relation string" supplied by the user which specifies
+ * the relationship of the specified resource. May be empty, in which case
+ * the relation is deduced from the supported types and the types of the
+ * parser for the given mimetype.
+ * @param supportedTypes contains the types of the returned Node the caller
+ * can deal with. Note that only the types the parser claims to return are
+ * checked, not the actual result.
* @param mode describes whether the file should be included or linked.
* @return the parsed node or nullptr if something goes wrong.
*/
- Rooted<Node> parse(Registry &registry, ParserContext &ctx,
- const ResourceRequest &req, ParseMode mode);
+ Rooted<Node> parse(ParserContext &ctx, const std::string &path,
+ const std::string &mimetype, const std::string &rel,
+ const RttiSet &supportedTypes, ParseMode mode);
public:
/**
@@ -138,13 +144,11 @@ public:
* which allows the Node instance returned by the parser to be cached. Logs
* any problem in the logger instance of the given ParserContext.
*
- * @param registry is the registry instance that should be used to lookup
- * the parser instances and to locate the resources.
* @param ctx is the context from which the Logger instance will be looked
- * up. This context instance is not directly passed to the Parser, the
- * ParserScope instance is replaced with a new one. The sourceId specified
- * in the context instance will be used as relative location for looking up
- * the new resource.
+ * up. The sourceId specified in the context instance will be used as
+ * relative location for looking up the new resource. The registry specified
+ * in the ParserContext is used to lookup the parser instances and to
+ * locate the resources.
* @param mimetype is the mimetype of the resource that should be parsed
* (may be empty, in which case the mimetype is deduced from the file
* extension)
@@ -157,9 +161,8 @@ public:
* checked, not the actual result.
* @return the parsed node or nullptr if something goes wrong.
*/
- Rooted<Node> link(Registry &registry, ParserContext &ctx,
- const std::string &path, const std::string &mimetype,
- const std::string &rel,
+ Rooted<Node> link(ParserContext &ctx, const std::string &path,
+ const std::string &mimetype, const std::string &rel,
const RttiSet &supportedTypes);
/**
@@ -171,11 +174,11 @@ public:
* a new ParserScope and thus guarantees reproducible results. Logs any
* problem in the logger instance of the given ParserContext.
*
- * @param registry is the registry instance that should be used to lookup
- * the parser instances and to locate the resources.
* @param ctx is the context from which the Logger instance will be looked
* up. The sourceId specified in the context instance will be used as
- * relative location for looking up the new resource.
+ * relative location for looking up the new resource. The registry specified
+ * in the ParserContext is used to lookup the parser instances and to
+ * locate the resources.
* @param path is the requested path of the file that should be included.
* @param mimetype is the mimetype of the resource that should be parsed
* (may be empty, in which case the mimetype is deduced from the file
@@ -189,10 +192,8 @@ public:
* checked, not the actual result.
* @return the parsed node or nullptr if something goes wrong.
*/
- Rooted<Node> include(Registry &registry, ParserContext &ctx,
- const std::string &path,
- const std::string &mimetype,
- const std::string &rel,
+ Rooted<Node> include(ParserContext &ctx, const std::string &path,
+ const std::string &mimetype, const std::string &rel,
const RttiSet &supportedTypes);
/**
@@ -290,6 +291,15 @@ public:
* @return the Node instance corresponding to the given resource.
*/
Rooted<Node> getNode(Manager &mgr, const Resource &resource);
+
+ /**
+ * Returns a SourceContextCallback that can be passed to a logger instance.
+ * Remeber to reset the SourceContextCallback after the Project instance has
+ * been freed.
+ *
+ * @return a SourceContextCallback that is coupled to this Project instance.
+ */
+ SourceContextCallback getSourceContextCallback();
};
}