Source Resolver

Usage

 1#pragma once
 2
 3#include <erbsland/conf/SourceResolver.hpp>
 4#include <erbsland/conf/FileSourceResolver.hpp>
 5
 6using el::conf::SourceResolver;
 7using el::conf::SourceResolverPtr;
 8using el::conf::SourceResolverContext;
 9using el::conf::SourceList;
10using el::conf::SourceListPtr;
11using el::conf::FileSourceResolver;
12
13class MySourceResolver final : public SourceResolver {
14public:
15    MySourceResolver() : _fallbackResolver(FileSourceResolver::create()) {}
16    [[nodiscard]] static auto create() -> SourceResolverPtr {
17        return std::make_shared<MySourceResolver>();
18    }
19
20public:
21    auto resolve(const SourceResolverContext &context) -> SourceListPtr override {
22        if (!context.includeText.starts_with(u8"my:")) {
23            return _fallbackResolver->resolve(context);
24        }
25        auto result = std::make_shared<SourceList>();
26        // ...add my sources...
27        return result;
28    }
29
30private:
31    SourceResolverPtr _fallbackResolver;
32};

Interface

class SourceResolver

The interface for any source resolver implementation.

Subclassed by erbsland::conf::FileSourceResolver

Public Functions

virtual ~SourceResolver() = default

Default destructor.

virtual SourceListPtr resolve(const SourceResolverContext &context) = 0

Resolve Sources

This function is called when the parser encounters an include meta-command.

The raw and unprocessed text of the command and the source of the parsed document are given as arguments to this function. This function must either return a list of sources that match the expression or throw an Error exception. If an exception is thrown, the parsing will be stopped and the thrown exception will be passed to the caller of parse().

If a list is returned, the parser will parse the sources in the given order and include the parsed contents in the document. The returned sources should be in a closed state. The parser will open them in the sequence they are parsed.

Tested:

Tested via Parser class.

Parameters:

context – The resolve context.

Throws:

Error – Throw Error with ErrorCategory::Syntax if the include text does not match the required format.

Returns:

A list of sources (see SourceList) to include in the document.

struct SourceResolverContext

The context for resolving sources.

Tested:

Tested via Parser class.

Public Members

String includeText

The raw and unprocessed text from the include-command.

SourceIdentifierPtr sourceIdentifier

The source identifier of the document with the include-command.

using erbsland::conf::SourceResolverPtr = std::shared_ptr<SourceResolver>
class FileSourceResolver : public erbsland::conf::SourceResolver

A file source resolver.

The file source resolver supports the recommended format to include files. It works with relative and absolute paths and also has support for wildcards.

Here are a few examples:

&at;include "file:example.elcl"              # File in the same directory.
&at;include "file:sub/example.elcl"          # File in a subdirectory of the current configuration file.
&at;include "file:../example.elcl"           # File in the parent directory (if access rules allow it)
&at;include "file:/usr/local/example.elcl"   # Absolute path.

Public Types

enum Feature

Features of the file source resolver

Values:

enumerator RecursiveWildcard

Support for recursive wildcards.

enumerator FilenameWildcard

Support for filename wildcards.

enumerator AbsolutePaths

Support for absolute paths.

enumerator WindowsUNCPath

Support for Windows UNC paths.

enumerator FileProtocol

Support for the file: protocol prefix.

enumerator _featureCount

Public Functions

FileSourceResolver() = default

Default constructor.

~FileSourceResolver() override = default

Default destructor.

void enable(Feature feature)

Enable a feature

void disable(Feature feature)

Disable a feature

bool isEnabled(Feature feature) const

Test if a feature enabled.

virtual SourceListPtr resolve(const SourceResolverContext &context) override

Resolve Sources

This function is called when the parser encounters an include meta-command.

The raw and unprocessed text of the command and the source of the parsed document are given as arguments to this function. This function must either return a list of sources that match the expression or throw an Error exception. If an exception is thrown, the parsing will be stopped and the thrown exception will be passed to the caller of parse().

If a list is returned, the parser will parse the sources in the given order and include the parsed contents in the document. The returned sources should be in a closed state. The parser will open them in the sequence they are parsed.

Tested:

Tested via Parser class.

Parameters:

context – The resolve context.

Throws:

Error – Throw Error with ErrorCategory::Syntax if the include text does not match the required format.

Returns:

A list of sources (see SourceList) to include in the document.

Public Static Functions

static inline FileSourceResolverPtr create()

Create a new instance of the file source resolver.

using erbsland::conf::FileSourceResolverPtr = std::shared_ptr<FileSourceResolver>