From 6decad0b8e7e369bd8215f31a45dd3eae982d2a9 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 21 Jan 2015 01:17:49 +0100 Subject: Some further refactoring -- renamed Scope to ParserScope, got rid of parser namespace, added new functionality to RegistryClass, wrote documentation, added function for extracting file extensions to Utils --- src/core/Registry.hpp | 115 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 102 insertions(+), 13 deletions(-) (limited to 'src/core/Registry.hpp') diff --git a/src/core/Registry.hpp b/src/core/Registry.hpp index 40eede1..965f336 100644 --- a/src/core/Registry.hpp +++ b/src/core/Registry.hpp @@ -16,37 +16,126 @@ along with this program. If not, see . */ +/** + * @file Registry.hpp + * + * Class used for registering plugin classes. The Registry is one of the central + * classes needed for parsing and transforming an Ousía document. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + #ifndef _OUSIA_REGISTRY_HPP_ #define _OUSIA_REGISTRY_HPP_ #include +#include #include +#include #include namespace ousia { -// TODO: Add support for ScriptEngine type - -namespace parser { +// Forward declarations class Parser; -} class ResourceLocator; +/** + * The Registry class is the central class which is used to store references to + * all available plugins. + */ class Registry { private: - std::vector parsers; - std::map parserMimetypes; - + /** + * Mapping between parser mimetypes and pairs of parsers and their supported + * Rtti types. + */ + std::map> parsers; + + /** + * Map from file extensions to registered mimetypes. + */ + std::map extensions; + + /** + * List containing all registered ResourceLocator instances. + */ std::vector locators; public: - void registerParser(parser::Parser &parser); - - parser::Parser *getParserForMimetype(const std::string &mimetype) const; - - void registerResourceLocator(ResourceLocator &locator); - + /** + * Registers a new parser instance for the given set of mimetypes. Throws + * an exception if a parser is already registered for one of the mimetypes. + * + * @param mimetypes is a set of mimetypes for which the Parser should be + * registered. + * @param types is a set of node the parser is known to return. This + * information is needed in order to prevent inclusion of the wrong Node + * types + * @param parser is the parser instance that is registered for the given + * mimetypes. + */ + void registerParser(const std::set &mimetypes, + const RttiSet &types, Parser *parser); + + /** + * Returns a pointer pointing at a Parser that was registered for handling + * the given mimetype. + * + * @param mimetype is the mimetype for which a Parser instance should be + * looked up. + * @return a pair containing a pointer at the parser and the RttiTypes + * supported by the parser. The pointer is set to a nullptr if no such + * parser could be found. + */ + const std::pair &getParserForMimetype( + const std::string &mimetype) const; + + /** + * Registers a file extension with a certain mimetype. Throws an exception + * if a mimetype is already registered for this extension. + * + * @param extension is the file extension for which the mimetype should be + * registered. The extension has to be provided without a leading dot. The + * extensions are handled case insensitive. + * @param mimetype is the mimetype that should be registered for the + * extension. + */ + void registerExtension(const std::string &extension, + const std::string &mimetype); + + /** + * Returns the mimetype for the given extension. + * + * @param extension is the file extension for which the mimetype should be + * looked up. The extension has to be provided without a leading dot. The + * extensions are handled case insensitive. + * @return the registered mimetype or an empty string of none was found. + */ + std::string getMimetypeForExtension(const std::string &extension) const; + + /** + * Registers a ResourceLocator instance that should be used for locating + * resources. Two registered ResourceLocator should not be capable of + * accessing Resources at the same location. If this happens, the resource + * locator that was registered first has precedence. + * + * @param locator is the ResourceLocator instance that should be registered. + */ + void registerResourceLocator(ResourceLocator *locator); + + /** + * Locates a resource using the registered ResourceLocator instances. + * + * @param resource is the resource descriptor to which the result will be + * written. + * @param path is the path under which the resource should be looked up. + * @param type is the ResourceType. Specifying a resource type may help to + * locate the resource. + * @param relativeTo is another resource relative to which the resource may + * be looked up. + */ bool locateResource(Resource &resource, const std::string &path, ResourceType type = ResourceType::UNKNOWN, const Resource &relativeTo = NullResource) const; -- cgit v1.2.3 From d41716e4cc2be229fee82d99dc84e253d7949d47 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Thu, 22 Jan 2015 02:43:00 +0100 Subject: Added function which registers mimetypes for some default extensions (to be extended) --- src/core/Registry.cpp | 9 +++++++++ src/core/Registry.hpp | 5 +++++ 2 files changed, 14 insertions(+) (limited to 'src/core/Registry.hpp') diff --git a/src/core/Registry.cpp b/src/core/Registry.cpp index c42a97a..88babb7 100644 --- a/src/core/Registry.cpp +++ b/src/core/Registry.cpp @@ -74,6 +74,15 @@ void Registry::registerExtension(const std::string &extension, 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 { diff --git a/src/core/Registry.hpp b/src/core/Registry.hpp index 965f336..f932480 100644 --- a/src/core/Registry.hpp +++ b/src/core/Registry.hpp @@ -105,6 +105,11 @@ public: void registerExtension(const std::string &extension, const std::string &mimetype); + /** + * Registers mimetypes for some default extensions. + */ + void registerDefaultExtensions(); + /** * Returns the mimetype for the given extension. * -- cgit v1.2.3 From ba52f3f4823faf9c73e9445770a44887f3c2b389 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Fri, 23 Jan 2015 00:31:07 +0100 Subject: Added mimetype by filename lookup to registry --- src/core/Registry.cpp | 12 ++++++++++++ src/core/Registry.hpp | 9 +++++++++ 2 files changed, 21 insertions(+) (limited to 'src/core/Registry.hpp') diff --git a/src/core/Registry.cpp b/src/core/Registry.cpp index 88babb7..044668c 100644 --- a/src/core/Registry.cpp +++ b/src/core/Registry.cpp @@ -97,6 +97,18 @@ std::string Registry::getMimetypeForExtension( return std::string{}; } +std::string Registry::getMimetypeForFilename(const std::string &filename) const +{ + // Fetch the file extension + std::string ext = Utils::extractFileExtension(path); + if (ext.empty()) { + return std::string{}; + } + + // Fetch the mimetype for the extension + return ctx.registry.getMimetypeForExtension(ext); +} + void Registry::registerResourceLocator(ResourceLocator *locator) { locators.push_back(locator); diff --git a/src/core/Registry.hpp b/src/core/Registry.hpp index f932480..4b4cb65 100644 --- a/src/core/Registry.hpp +++ b/src/core/Registry.hpp @@ -120,6 +120,15 @@ public: */ std::string getMimetypeForExtension(const std::string &extension) const; + /** + * Tries to deduce the mimetype from the given filename. + * + * @param filename is the filename from which the mimetype should be + * deduced. + * @return the mimetype or an empty string if no filename could be deduced. + */ + std::string getMimetypeForFilename(const std::string &filename) const; + /** * Registers a ResourceLocator instance that should be used for locating * resources. Two registered ResourceLocator should not be capable of -- cgit v1.2.3