From 7a9c85b1afa01a5d76707ca05be7779a1b1135bf Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Sun, 7 May 2023 22:42:54 +0200 Subject: [PATCH] feat: allow the baseIRI to be provided (#33) --- __tests__/Conformance-test.js | 58 +++++++++++++++++++++++++++++++++-- lib/index.d.ts | 1 + lib/index.js | 6 ++-- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/__tests__/Conformance-test.js b/__tests__/Conformance-test.js index 66252c3..53451b9 100644 --- a/__tests__/Conformance-test.js +++ b/__tests__/Conformance-test.js @@ -1,6 +1,7 @@ const fs = require('fs'); const path = require('path'); const Parser = require('../lib').Parser; +const parse = require('../lib').parse; const N3 = require('n3'); require('jest-rdf'); @@ -8,7 +9,7 @@ describe('Testing each conformance file', () => { it.each( fs.readdirSync(path.join(__dirname, 'valid')).filter(str => str.endsWith('.shaclc')) )('testing %s correctly parses', (file) => { - + const shaclc = fs.readFileSync(path.join(__dirname, 'valid', file)).toString(); const ttl = fs.readFileSync(path.join(__dirname, 'valid', file.replace('.shaclc', '.ttl'))).toString(); @@ -20,12 +21,11 @@ describe('Testing each conformance file', () => { }); }); - describe('Testing each extended conformance file', () => { it.each( fs.readdirSync(path.join(__dirname, 'extended')).filter(str => str.endsWith('.shaclc')) )('testing %s correctly parses when extended is enabled', (file) => { - + const shaclc = fs.readFileSync(path.join(__dirname, 'extended', file)).toString(); const ttl = fs.readFileSync(path.join(__dirname, 'extended', file.replace('.shaclc', '.ttl'))).toString(); @@ -43,3 +43,55 @@ describe('Testing each extended conformance file', () => { expect(() => (new Parser()).parse(shaclc)).toThrowError(); }); }); + +describe('Testing relative IRIs', () => { + it('should use the provided baseIRI to resolve relative IRIs', () => { + expect(parse(` + PREFIX ex: + + shape <#MyShape> -> { + } + `, { baseIRI: 'http://www.jeswr.org/' })).toBeRdfIsomorphic([ + N3.DataFactory.quad( + N3.DataFactory.namedNode('http://www.jeswr.org/#MyShape'), + N3.DataFactory.namedNode('http://www.w3.org/ns/shacl#targetClass'), + N3.DataFactory.namedNode('http://www.jeswr.org/MyClass'), + ), + N3.DataFactory.quad( + N3.DataFactory.namedNode('http://www.jeswr.org/#MyShape'), + N3.DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), + N3.DataFactory.namedNode('http://www.w3.org/ns/shacl#NodeShape'), + ), + N3.DataFactory.quad( + N3.DataFactory.namedNode('http://www.jeswr.org/'), + N3.DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), + N3.DataFactory.namedNode('http://www.w3.org/2002/07/owl#Ontology'), + ) + ]) + }); + + it('should use the provided baseIRI to resolve relative IRIs', () => { + expect(parse(` + PREFIX ex: + + shape <#MyShape> -> { + } + `, { baseIRI: 'http://www.jeswr.org/humanShape' })).toBeRdfIsomorphic([ + N3.DataFactory.quad( + N3.DataFactory.namedNode('http://www.jeswr.org/humanShape#MyShape'), + N3.DataFactory.namedNode('http://www.w3.org/ns/shacl#targetClass'), + N3.DataFactory.namedNode('http://www.jeswr.org/MyClass'), + ), + N3.DataFactory.quad( + N3.DataFactory.namedNode('http://www.jeswr.org/humanShape#MyShape'), + N3.DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), + N3.DataFactory.namedNode('http://www.w3.org/ns/shacl#NodeShape'), + ), + N3.DataFactory.quad( + N3.DataFactory.namedNode('http://www.jeswr.org/humanShape'), + N3.DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), + N3.DataFactory.namedNode('http://www.w3.org/2002/07/owl#Ontology'), + ) + ]) + }); +}); diff --git a/lib/index.d.ts b/lib/index.d.ts index 6c327ef..00d64b2 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -2,6 +2,7 @@ import { Quad } from '@rdfjs/types'; export interface ParseOptions { extendedSyntax?: boolean; + baseIRI?: string; } export declare function parse(str: string, options?: ParseOptions): Quad[] & { prefixes: Record }; diff --git a/lib/index.js b/lib/index.js index ff24009..260c8ed 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,11 +5,11 @@ class Parser { constructor() { } - parse(str, { extendedSyntax } = {}) { + parse(str, { extendedSyntax, baseIRI } = {}) { this._parser = new ShaclcParser(); this._parser.Parser.factory = N3.DataFactory; - this._parser.Parser.base = N3.DataFactory.namedNode('urn:x-base:default'); + this._parser.Parser.base = N3.DataFactory.namedNode(baseIRI || 'urn:x-base:default'); this._parser.Parser.extended = extendedSyntax === true; this._parser.Parser.prefixes = { rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', @@ -21,7 +21,7 @@ class Parser { this._parser.Parser.currentPropertyNode = undefined; this._parser.Parser.nodeShapeStack = []; this._parser.Parser.tempCurrentNodeShape = undefined; - this._parser.Parser.n3Parser = new N3.Parser({ baseIRI: 'urn:x-base:default' }); + this._parser.Parser.n3Parser = new N3.Parser({ baseIRI: baseIRI || 'urn:x-base:default' }); const arr = [] this._parser.Parser.onQuad = (quad) => { arr.push(quad) };