Skip to content

Commit

Permalink
feat: allow the baseIRI to be provided (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr authored May 7, 2023
1 parent 0623d58 commit 7a9c85b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
58 changes: 55 additions & 3 deletions __tests__/Conformance-test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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');

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();

Expand All @@ -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();

Expand All @@ -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: <http://example.org/>
shape <#MyShape> -> </MyClass> {
}
`, { 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: <http://example.org/>
shape <#MyShape> -> </MyClass> {
}
`, { 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'),
)
])
});
});
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> };
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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#',
Expand All @@ -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) };
Expand Down

0 comments on commit 7a9c85b

Please sign in to comment.