Skip to content

Commit

Permalink
Improve hack to allow blank node predicates in Turtle
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Jun 20, 2019
1 parent 247ec8d commit 34b3ef4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
18 changes: 15 additions & 3 deletions lib/GeneralizedN3StreamParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import {Parser} from "n3";
import {Transform} from "stream";

// Temporarily set format to text/n3 to allow blank node predicates (needed by JSON-LD tests)
const readPredicateOld = Parser.prototype._readPredicate;
// tslint:disable-next-line:only-arrow-functions
Parser.prototype._readPredicate = function(token: any) {
if (this.allowBlankNodePredicates) {
this._n3Mode = true;
}
const ret = readPredicateOld.call(this, token);
if (this.allowBlankNodePredicates) {
this._n3Mode = false;
}
return ret;
};

export class GeneralizedN3StreamParser extends Transform {

constructor(options: any) {
Expand All @@ -10,9 +24,7 @@ export class GeneralizedN3StreamParser extends Transform {

// Set up parser
const parser: any = new Parser(options);

// Set to format to text/n3 to allow blank node predicates (needed by JSON-LD tests)
parser._n3Mode = true;
parser.allowBlankNodePredicates = true;

let onData: any;
let onEnd: any;
Expand Down
23 changes: 16 additions & 7 deletions test/Util-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {literal, namedNode, quad} from "@rdfjs/data-model";
import {blankNode, literal, namedNode, quad} from "@rdfjs/data-model";
import {existsSync, mkdirSync, readFileSync} from "fs";
import "isomorphic-fetch";
import "jest-rdf";
Expand Down Expand Up @@ -174,21 +174,30 @@ describe('Util', () => {
]);
});

it('should parse text/turtle streams with blank node predicates', async () => {
expect(await arrayifyStream(Util.parseRdfRaw('text/turtle', 'http://example.org/',
streamifyString('<a> _:b <c>.'))))
.toEqualRdfQuadArray([
quad(namedNode('http://example.org/a'), blankNode('b'),
namedNode('http://example.org/c')),
]);
});

it('should parse application/n-triples streams', async () => {
expect(await arrayifyStream(Util.parseRdfRaw('application/n-triples', 'http://example.org/',
streamifyString('<a> <b> <c>.'))))
streamifyString('<http://ex.org/a> <http://ex.org/b> <http://ex.org/c>.'))))
.toEqualRdfQuadArray([
quad(namedNode('http://example.org/a'), namedNode('http://example.org/b'),
namedNode('http://example.org/c')),
quad(namedNode('http://ex.org/a'), namedNode('http://ex.org/b'),
namedNode('http://ex.org/c')),
]);
});

it('should parse application/n-quads streams', async () => {
expect(await arrayifyStream(Util.parseRdfRaw('application/n-quads', 'http://example.org/',
streamifyString('<a> <b> <c> <d>.'))))
streamifyString('<http://ex.org/a> <http://ex.org/b> <http://ex.org/c> <http://ex.org/d>.'))))
.toEqualRdfQuadArray([
quad(namedNode('http://example.org/a'), namedNode('http://example.org/b'),
namedNode('http://example.org/c'), namedNode('http://example.org/d')),
quad(namedNode('http://ex.org/a'), namedNode('http://ex.org/b'),
namedNode('http://ex.org/c'), namedNode('http://ex.org/d')),
]);
});

Expand Down

0 comments on commit 34b3ef4

Please sign in to comment.