UNPKG

jsonld-streaming-parser

Version:
78 lines 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntryHandlerKeywordType = void 0; const jsonld_context_parser_1 = require("jsonld-context-parser"); const Util_1 = require("../../Util"); const EntryHandlerPredicate_1 = require("../EntryHandlerPredicate"); const EntryHandlerKeyword_1 = require("./EntryHandlerKeyword"); /** * Handles @graph entries. */ class EntryHandlerKeywordType extends EntryHandlerKeyword_1.EntryHandlerKeyword { constructor() { super('@type'); } isStackProcessor() { return false; } async handle(parsingContext, util, key, keys, value, depth) { const keyOriginal = keys[depth]; // The current identifier identifies an rdf:type predicate. // But we only emit it once the node closes, // as it's possible that the @type is used to identify the datatype of a literal, which we ignore here. const context = await parsingContext.getContext(keys); const predicate = util.rdfType; const parentKey = await util.unaliasKeywordParent(keys, depth); const reverse = Util_1.Util.isPropertyReverse(context, keyOriginal, parentKey); const isEmbedded = Util_1.Util.isPropertyInEmbeddedNode(parentKey); util.validateReverseInEmbeddedNode(key, reverse, isEmbedded); const isAnnotation = Util_1.Util.isPropertyInAnnotationObject(parentKey); // Handle multiple values if the value is an array const elements = Array.isArray(value) ? value : [value]; for (const element of elements) { if (typeof element !== 'string') { parsingContext.emitError(new jsonld_context_parser_1.ErrorCoded(`Found illegal @type '${element}'`, jsonld_context_parser_1.ERROR_CODES.INVALID_TYPE_VALUE)); } const type = util.createVocabOrBaseTerm(context, element); if (type) { await EntryHandlerPredicate_1.EntryHandlerPredicate.handlePredicateObject(parsingContext, util, keys, depth, predicate, type, reverse, isEmbedded, isAnnotation); } } // Collect type-scoped contexts if they exist let scopedContext = Promise.resolve(context); let hasTypedScopedContext = false; for (const element of elements.sort()) { // Spec requires lexicographical ordering const typeContext = Util_1.Util.getContextValue(context, '@context', element, null); if (typeContext) { hasTypedScopedContext = true; scopedContext = scopedContext.then((c) => parsingContext.parseContext(typeContext, c.getContextRaw())); } } // Error if an out-of-order type-scoped context was found when support is not enabled. if (parsingContext.streamingProfile && (hasTypedScopedContext || !parsingContext.streamingProfileAllowOutOfOrderPlainType) && (parsingContext.processingStack[depth] || parsingContext.idStack[depth])) { parsingContext.emitError(new jsonld_context_parser_1.ErrorCoded('Found an out-of-order type-scoped context, while streaming is enabled.' + '(disable `streamingProfile`)', jsonld_context_parser_1.ERROR_CODES.INVALID_STREAMING_KEY_ORDER)); } // If at least least one type-scoped context applies, set them in the tree. if (hasTypedScopedContext) { // Do not propagate by default scopedContext = scopedContext.then((c) => { // Set the original context at this depth as a fallback // This is needed when a context was already defined at the given depth, // and this context needs to remain accessible from child nodes when propagation is disabled. if (c.getContextRaw()['@propagate'] !== true) { return new jsonld_context_parser_1.JsonLdContextNormalized(Object.assign(Object.assign({}, c.getContextRaw()), { '@propagate': false, '@__propagateFallback': context.getContextRaw() })); } return c; }); // Set the new context in the context tree parsingContext.contextTree.setContext(keys.slice(0, keys.length - 1), scopedContext); } // Flag that type has been processed at this depth parsingContext.processingType[depth] = true; } } exports.EntryHandlerKeywordType = EntryHandlerKeywordType; //# sourceMappingURL=EntryHandlerKeywordType.js.map