graphdb
Version:
Javascript client library supporting GraphDB and RDF4J REST API.
128 lines (120 loc) • 4.82 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var ContentParser = require('../parser/content-parser');
var ConsoleLogger = require('../logging/console-logger');
/**
* Implementation of registry holding {@link ContentParser} instances and
* providing interface for registration and access.
* If this registry is not provided with a list with parsers then it is
* initialized empty. Otherwise provided parsers are validated if they are
* compatible with the {@link ContentParser} interface and an error is
* thrown if there are invalid parsers.
*
* @class
* @author Mihail Radkov
* @author Svilen Velikov
*/
var ParserRegistry = /*#__PURE__*/function () {
/**
* @param {ContentParser[]} [parsers] initialized list with valid parser
* instances.
*/
function ParserRegistry() {
var parsers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
_classCallCheck(this, ParserRegistry);
this.parsers = {};
this.initLogger();
this.validateParsers(parsers);
this.init(parsers);
}
/**
* Initializes a console logger.
*/
return _createClass(ParserRegistry, [{
key: "initLogger",
value: function initLogger() {
this.logger = new ConsoleLogger({
name: 'ParserRegistry'
});
}
/**
* Register provided {@link ContentParser} under given key as returned by
* <code>parser.getSupportedType()</code>.
* If the type of the provided parser is already registered, then this method
* will override the registered parser with the provided instance.
*
* @param {ContentParser} parser implementation wrapper.
*/
}, {
key: "register",
value: function register(parser) {
ParserRegistry.validateParser(parser);
var supportedType = parser.getSupportedType();
if (this.parsers[supportedType]) {
this.logger.warn({
parserType: supportedType
}, 'Overriding registered parser');
}
this.parsers[parser.getSupportedType()] = parser;
}
/**
* Getter for parser of given type.
*
* @param {string} type of the parser for get.
* @return {ContentParser} if parser of requested type is found or
* <code>null</code> otherwise.
*/
}, {
key: "get",
value: function get(type) {
return this.parsers[type];
}
/**
* Maps provided parsers by their supported content type.
*
* @private
* @param {ContentParser[]} parsers provided with the constructor.
*/
}, {
key: "init",
value: function init(parsers) {
var _this = this;
parsers.forEach(function (parser) {
_this.parsers[parser.getSupportedType()] = parser;
});
}
/**
* @private
* @param {ContentParser[]} parsers
*/
}, {
key: "validateParsers",
value: function validateParsers(parsers) {
parsers.forEach(function (parser) {
ParserRegistry.validateParser(parser);
});
}
/**
* @private
* @param {ContentParser} parser to be validated
* @throws {Error} if the provided parser is not implementing
* {@link ContentParser} or has not supported type
*/
}], [{
key: "validateParser",
value: function validateParser(parser) {
if (!parser || !(parser instanceof ContentParser)) {
throw new Error('Parser is not provided or does not implement' + ' ContentParser!');
}
if (!parser.getSupportedType()) {
throw new Error('Parser type is mandatory parameter!');
}
}
}]);
}();
module.exports = ParserRegistry;