-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSONLDParser.js
219 lines (191 loc) · 6.59 KB
/
JSONLDParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* Created by joachimvh on 30/06/2015.
*/
var _ = require('lodash');
var Util = require('./Util');
var format = require('util').format;
var N3Lexer = require('./N3Lexer');
var N3Parser = require('./N3Parser');
function JSONLDParser () {}
// used to make sure URIs are still valid after simplification
JSONLDParser.suffixTest = new RegExp('^' + N3Lexer._suffix.source + '$');
JSONLDParser.prototype.toN3 = function (jsonld)
{
var idMap = {};
var context = _.cloneDeep(jsonld['@context']) || {};
context['_'] = '_';
// TODO: what if there is also a base prefix in deeper contexts?
if (context[N3Parser.BASE])
{
context[''] = context[N3Parser.BASE];
delete context[N3Parser.BASE];
}
var n3 = this._toN3(jsonld, context, idMap);
// remove root graph
if (('@graph' in jsonld) && !_.startsWith(n3.statement, '{} .'))
n3.statement = n3.statement.substring(2, n3.statement.length-2);
var result;
if (n3.triples.length > 0)
result = this.tripleListToN3(n3.triples, idMap);
else
result = n3.statement;
// TODO: only supporting prefixes of root graph for now
for (var key in context)
{
if (key === '_')
continue;
result = format('PREFIX %s: <%s>\n%s', key, context[key], result);
}
return result + ' .';
};
JSONLDParser.prototype._toN3 = function (jsonld, context, idMap)
{
var subject, n3s, roots = [], key;
// strings need escaping, tick marks, etc. Will be handled later in the @value block
if (_.isString(jsonld))
jsonld = { '@value': jsonld };
if (Util.isLiteral(jsonld))
return { statement: jsonld, triples: [] };
if ('@list' in jsonld)
{
n3s = _.map(jsonld['@list'], function (thingy) { return this._toN3(thingy, context, idMap); }.bind(this));
subject = format('( %s )', _.map(n3s, 'statement').join(' '));
roots.push.apply(roots, _.flatten(_.filter(_.map(n3s, 'triples'))));
if (subject === '( )')
subject = '()';
}
if ('@graph' in jsonld)
{
var newIDMap = {};
_.assign(newIDMap, idMap);
n3s = _.map(jsonld['@graph'], function (thingy) { return this._toN3(thingy, context, newIDMap); }.bind(this));
// results with no triples are subject triples
subject = format('{ %s }', this.tripleListToN3(_.flatten(_.map(n3s,
function (n3)
{
if (n3.triples.length > 0)
return n3.triples;
if (n3.statement in newIDMap)
return [];
return { subject: n3.statement };
})),
newIDMap));
if (subject === '{ }')
subject = '{}';
}
if ('@value' in jsonld)
{
subject = JSON.stringify(jsonld['@value']);
// won't have triple quote strings since JSON converts newlines to \n
if (jsonld['@language'])
subject = format('%s@%s', subject, jsonld['@language']);
if (jsonld['@type'])
subject = format('%s^^%s', subject, this._parseID(jsonld['@type'], context));
}
// TODO: @forAll, @forSome
if ('@id' in jsonld)
{
var id = this._parseID(jsonld['@id'], context);
if (subject !== undefined)
idMap[id] = subject;
subject = id;
}
if ('@context' in jsonld)
{
context = _.cloneDeep(context);
_.assign(context, jsonld['@context']);
}
var triples = [];
for (key in jsonld)
{
if (key[0] === '@' && (key !== '@type' || '@value' in jsonld)) // @type has already been handled if there is a @value
continue;
var objects = jsonld[key];
if (!_.isArray(objects))
objects = [objects];
// interpret @type values as @ids
if (key === '@type')
{
key = 'a';
objects = _.map(objects, function (type) { return { '@id': type }});
}
else if (key === 'log:implies')
key = '=>';
else
key = this._parseID(key, context);
objects = _.map(objects, function (thingy) { return this._toN3(thingy, context, idMap); }.bind(this));
for (var i = 0; i < objects.length; ++i)
{
roots.push.apply(roots, objects[i].triples);
triples.push({subject: subject, predicate: key, object: objects[i].statement });
}
}
// blank nodes []
if (subject === undefined)
return { statement: triples.length === 0 ? '[]' : this.tripleListToN3(triples, idMap), triples: roots };
else
return { statement: subject, triples: triples.concat(roots) };
};
JSONLDParser.prototype.tripleListToN3 = function (triples, idMap)
{
if (triples.length === 0)
return '';
// convert list to tree
var tree = {}, s, p, o;
for (var i = 0; i < triples.length; ++i)
{
s = parseID(triples[i].subject);
if (!(s in tree))
tree[s] = {};
if (!('predicate' in triples[i]))
continue;
p = parseID(triples[i].predicate);
o = parseID(triples[i].object);
if (!(p in tree[s]))
tree[s][p] = [];
tree[s][p].push(o);
}
var subjects = [];
for (s in tree)
{
var predicates = [];
for (p in tree[s])
predicates.push(format('%s %s', p, tree[s][p].join(' , ')));
if (predicates.length === 0)
subjects.push(s);
else if (s === 'undefined') // undefined gets translated to a string by being an object key
subjects.push(format('[ %s ]', predicates.join(' ; ')));
else
subjects.push(format('%s %s', s, predicates.join(' ;\n')));
}
return subjects.join(' .\n');
function parseID(id) { return idMap[id] || id; }
};
JSONLDParser.prototype._parseID = function (id, context)
{
var prefix, suffix;
if (id[0] === '?' || Util.isNonStringLiteral(id))
return id + '';
var colonIdx = id.indexOf(':');
if (colonIdx >= 0)
{
prefix = id.substring(0, colonIdx);
suffix = id.substring(colonIdx+1);
if (prefix === N3Parser.BASE)
prefix = '';
if ((context[prefix] || prefix === '_') && suffix.substr(0, 2) !== '//')
return format('%s:%s', prefix, suffix);
}
for (prefix in context)
{
var namespace = context[prefix];
if (_.startsWith(id, namespace))
{
suffix = id.substring(namespace.length);
if (JSONLDParser.suffixTest.test(suffix))
return format('%s:%s', prefix, suffix);
}
}
return format('<%s>', id);
};
module.exports = JSONLDParser;