Skip to content

Commit

Permalink
linkeddata#349 Use jsonld package without n3 package
Browse files Browse the repository at this point in the history
  • Loading branch information
Fletcher91 committed Oct 15, 2019
1 parent 33bacf9 commit e18a093
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 32 deletions.
18 changes: 12 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"dependencies": {
"@babel/runtime": "^7.5.5",
"async": "^3.1.x",
"jsonld": "^1.6.2",
"jsonld": "^1.8.0",
"n3": "^1.2.0",
"solid-auth-cli": "^1.0.8",
"solid-auth-client": "^2.3.0",
Expand Down
98 changes: 98 additions & 0 deletions src/jsonldparser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import jsonld from 'jsonld'

import { arrayToStatements } from './util'

/**
* Parses json-ld formatted JS objects to a rdf Term.
* @param kb - The DataFactory to use.
* @param obj - The json-ld object to process.
* @return {Literal|NamedNode|BlankNode|Collection}
*/
export function jsonldObjectToTerm (kb, obj) {
if (typeof obj === 'string') {
return kb.rdfFactory.literal(obj)
}

if (Object.prototype.hasOwnProperty.call(obj, '@list')) {
if (kb.rdfFactory.supports["COLLECTIONS"] === true) {
return listToCollection(kb, obj['@list'])
}

return listToStatements(kb, obj)
}

if (Object.prototype.hasOwnProperty.call(obj, '@id')) {
return kb.rdfFactory.namedNode(obj['@id'])
}

if (Object.prototype.hasOwnProperty.call(obj, '@language')) {
return kb.rdfFactory.literal(obj['@value'], obj['@language'])
}

if (Object.prototype.hasOwnProperty.call(obj, '@type')) {
return kb.rdfFactory.literal(obj['@value'], kb.rdfFactory.namedNode(obj['@type']))
}

if (Object.prototype.hasOwnProperty.call(obj, '@value')) {
return kb.rdfFactory.literal(obj['@value'])
}

return kb.rdfFactory.literal(obj)
}

/**
* Adds the statements in a json-ld list object to {kb}.
*/
function listToStatements (kb, obj) {
const listId = obj['@id'] ? kb.rdfFactory.namedNode(obj['@id']) : kb.rdfFactory.blankNode()

const items = obj['@list'].map((listItem => jsonldObjectToTerm(kb, listItem)))
const statements = arrayToStatements(kb.rdfFactory, listId, items)
kb.addAll(statements)

return listId
}

function listToCollection (kb, obj) {
if (!Array.isArray(obj)) {
throw new TypeError("Object must be an array")
}
return kb.rdfFactory.collection(obj.map((o) => jsonldObjectToTerm(kb, o)))
}

/**
* Takes a json-ld formatted string {str} and adds its statements to {kb}.
*
* Ensure that {kb.rdfFactory} is a DataFactory.
*/
export default function jsonldParser (str, kb, base, callback) {
const baseString = Object.prototype.hasOwnProperty.call(base, 'termType')
? base.value
: base

return jsonld
.flatten(JSON.parse(str), null, { base: baseString })
.then((flattened) => flattened.reduce((store, flatResource) => {
const id = flatResource['@id']
? kb.rdfFactory.namedNode(flatResource['@id'])
: kb.rdfFactory.blankNode()

for (const property of Object.keys(flatResource)) {
if (property === '@id') {
continue
}
const value = flatResource[property]
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
kb.addStatement(kb.rdfFactory.quad(id, kb.rdfFactory.namedNode(property), jsonldObjectToTerm(kb, value[i])))
}
} else {
kb.addStatement(kb.rdfFactory.quad(id, kb.rdfFactory.namedNode(property), jsonldObjectToTerm(kb, value)))
}
}

return kb
}, kb))
.then(callback)
.catch(callback)
}
25 changes: 5 additions & 20 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import BlankNode from './blank-node'
import DataFactory from './data-factory'
import jsonld from 'jsonld'
import Literal from './literal'
import jsonldParser from './jsonldparser'
import { Parser as N3jsParser } from 'n3' // @@ Goal: remove this dependency
import N3Parser from './n3parser'
import NamedNode from './named-node'
import { parseRDFaDOM } from './rdfaparser'
import RDFParser from './rdfxmlparser'
import sparqlUpdateParser from './patch-parser'
Expand Down Expand Up @@ -37,24 +34,12 @@ export default function parse (str, kb, base, contentType, callback) {
} else if (contentType === 'application/sparql-update') { // @@ we handle a subset
sparqlUpdateParser(str, kb, base)
executeCallback()
} else if (contentType === 'application/ld+json' ||
contentType === 'application/nquads' ||
} else if (contentType === 'application/ld+json') {
jsonldParser(str, kb, base, executeCallback)
} else if (contentType === 'application/nquads' ||
contentType === 'application/n-quads') {
var n3Parser = new N3jsParser({ factory: DataFactory })
var triples = []
if (contentType === 'application/ld+json') {
var jsonDocument
try {
jsonDocument = JSON.parse(str)
} catch (parseErr) {
return callback(parseErr, null)
}
jsonld.toRDF(jsonDocument,
{format: 'application/nquads', base},
nquadCallback)
} else {
nquadCallback(null, str)
}
nquadCallback(null, str)
} else {
throw new Error("Don't know how to parse " + contentType + ' yet')
}
Expand Down
34 changes: 34 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Utility functions for $rdf
* @module util
*/
import { jsonldObjectToTerm } from './jsonldparser'
import { docpart } from './uri'
import log from './log'
import * as uri from './uri'
Expand Down Expand Up @@ -69,6 +70,39 @@ export function AJAR_handleNewTerm (kb, p, requestedBy) {
return sf.fetch(docuri, { referringTerm: requestedBy })
}

const rdf = {
first: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first',
rest: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest',
nil: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'
}

/**
* Expands an array of Terms to a set of statements representing the rdf:list.
* @param rdfFactory - The factory to use
* @param {NamedNode|BlankNode} subject - The iri of the first list item.
* @param {Term[]} data - The terms to expand into the list.
* @return {Statement[]} - The {data} as a set of statements.
*/
export function arrayToStatements(rdfFactory, subject, data) {
const statements = []

data.reduce((id, listObj, i, listData) => {
statements.push(rdfFactory.quad(id, rdfFactory.namedNode(rdf.first), listData[i]))

let nextNode
if (i < listData.length - 1) {
nextNode = rdfFactory.blankNode()
statements.push(rdfFactory.quad(id, rdfFactory.namedNode(rdf.rest), nextNode))
} else {
statements.push(rdfFactory.quad(id, rdfFactory.namedNode(rdf.rest), rdfFactory.namedNode(rdf.nil)))
}

return nextNode
}, subject)

return statements
}

export function ArrayIndexOf (arr, item, i) {
i || (i = 0)
var length = arr.length
Expand Down
Loading

0 comments on commit e18a093

Please sign in to comment.