rdflib 2.2.21 → 2.2.22-0de51c50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rdflib.min.js +1 -1
- package/dist/rdflib.min.js.LICENSE.txt +13 -1
- package/dist/rdflib.min.js.map +1 -1
- package/esm/blank-node.js +3 -27
- package/esm/collection.js +7 -29
- package/esm/default-graph.js +1 -15
- package/esm/empty.js +0 -13
- package/esm/factories/canonical-data-factory.js +5 -40
- package/esm/factories/extended-term-factory.js +0 -11
- package/esm/factories/factory-types.js +2 -1
- package/esm/factories/rdflib-data-factory.js +0 -8
- package/esm/fetcher.js +257 -481
- package/esm/formula.js +43 -142
- package/esm/index.js +19 -36
- package/esm/jsonldparser.js +20 -29
- package/esm/jsonparser.js +0 -7
- package/esm/lists.js +18 -33
- package/esm/literal.js +10 -42
- package/esm/n3parser.js +38 -351
- package/esm/named-node.js +8 -27
- package/esm/namespace.js +0 -1
- package/esm/node-internal.js +11 -24
- package/esm/node.js +1 -6
- package/esm/parse.js +10 -17
- package/esm/patch-parser.js +9 -29
- package/esm/query-to-sparql.js +0 -18
- package/esm/query.js +50 -133
- package/esm/rdfaparser.js +72 -210
- package/esm/rdfxmlparser.js +31 -128
- package/esm/serialize.js +8 -33
- package/esm/serializer.js +122 -286
- package/esm/sparql-to-query.js +44 -134
- package/esm/statement.js +8 -19
- package/esm/store.js +109 -280
- package/esm/types.js +3 -2
- package/esm/update-manager.js +110 -246
- package/esm/updates-via.js +0 -27
- package/esm/uri.js +6 -50
- package/esm/utils/default-graph-uri.js +1 -0
- package/esm/utils/termValue.js +0 -1
- package/esm/utils/terms.js +15 -16
- package/esm/utils-js.js +16 -57
- package/esm/utils.js +4 -15
- package/esm/variable.js +0 -20
- package/lib/blank-node.js +3 -28
- package/lib/class-order.js +0 -1
- package/lib/collection.js +7 -34
- package/lib/default-graph.js +0 -20
- package/lib/empty.js +0 -18
- package/lib/factories/canonical-data-factory.js +4 -51
- package/lib/factories/extended-term-factory.js +0 -18
- package/lib/factories/factory-types.d.ts +6 -6
- package/lib/factories/factory-types.js +0 -3
- package/lib/factories/rdflib-data-factory.js +0 -12
- package/lib/fetcher.d.ts +6 -6
- package/lib/fetcher.js +258 -470
- package/lib/formula.d.ts +1 -1
- package/lib/formula.js +43 -151
- package/lib/index.d.ts +1 -2
- package/lib/index.js +14 -82
- package/lib/jsonldparser.js +19 -33
- package/lib/jsonparser.js +0 -9
- package/lib/lists.js +14 -39
- package/lib/literal.js +10 -49
- package/lib/log.d.ts +0 -6
- package/lib/log.js +0 -1
- package/lib/n3parser.d.ts +1 -0
- package/lib/n3parser.js +39 -359
- package/lib/named-node.js +7 -35
- package/lib/namespace.js +0 -3
- package/lib/node-internal.js +11 -21
- package/lib/node.js +1 -11
- package/lib/parse.d.ts +1 -1
- package/lib/parse.js +8 -27
- package/lib/patch-parser.js +10 -33
- package/lib/query-to-sparql.js +0 -23
- package/lib/query.js +46 -152
- package/lib/rdfaparser.js +72 -220
- package/lib/rdfxmlparser.js +31 -132
- package/lib/serialize.d.ts +1 -1
- package/lib/serialize.js +8 -43
- package/lib/serializer.d.ts +1 -0
- package/lib/serializer.js +121 -297
- package/lib/sparql-to-query.js +42 -167
- package/lib/statement.js +8 -24
- package/lib/store.d.ts +2 -2
- package/lib/store.js +102 -282
- package/lib/tf-types.d.ts +4 -4
- package/lib/types.d.ts +8 -8
- package/lib/types.js +3 -3
- package/lib/update-manager.d.ts +2 -2
- package/lib/update-manager.js +110 -254
- package/lib/updates-via.js +0 -30
- package/lib/uri.js +5 -58
- package/lib/utils/default-graph-uri.js +1 -3
- package/lib/utils/termValue.js +0 -2
- package/lib/utils/terms.js +15 -34
- package/lib/utils-js.js +16 -80
- package/lib/utils.js +4 -21
- package/lib/variable.js +0 -25
- package/lib/xsd-internal.js +0 -3
- package/lib/xsd.js +0 -4
- package/package.json +40 -39
- package/src/fetcher.ts +2 -2
- package/src/index.ts +0 -2
- package/src/jsonldparser.js +13 -4
- package/src/n3parser.js +12 -4
- package/src/serialize.ts +4 -11
- package/src/serializer.js +24 -0
- package/src/update-manager.ts +18 -7
- package/changes.txt +0 -59
- package/esm/convert.js +0 -61
- package/lib/convert.d.ts +0 -2
- package/lib/convert.js +0 -77
- package/src/convert.js +0 -70
package/lib/serializer.js
CHANGED
|
@@ -1,39 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
6
|
-
|
|
7
5
|
Object.defineProperty(exports, "__esModule", {
|
|
8
6
|
value: true
|
|
9
7
|
});
|
|
10
8
|
exports.Serializer = void 0;
|
|
11
9
|
exports.default = createSerializer;
|
|
12
|
-
|
|
13
10
|
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
14
|
-
|
|
15
11
|
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
16
|
-
|
|
17
12
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
18
|
-
|
|
19
13
|
var _namedNode = _interopRequireDefault(require("./named-node"));
|
|
20
|
-
|
|
21
14
|
var _blankNode = _interopRequireDefault(require("./blank-node"));
|
|
22
|
-
|
|
23
15
|
var Uri = _interopRequireWildcard(require("./uri"));
|
|
24
|
-
|
|
25
16
|
var Util = _interopRequireWildcard(require("./utils-js"));
|
|
26
|
-
|
|
27
17
|
var _canonicalDataFactory = _interopRequireDefault(require("./factories/canonical-data-factory"));
|
|
28
|
-
|
|
29
18
|
var _xsd = require("./xsd");
|
|
30
|
-
|
|
31
19
|
var _solidNamespace = _interopRequireDefault(require("solid-namespace"));
|
|
32
|
-
|
|
20
|
+
var ttl2jsonld = _interopRequireWildcard(require("@frogcat/ttl2jsonld"));
|
|
33
21
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
34
|
-
|
|
35
22
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
36
|
-
|
|
37
23
|
/* Serialization of RDF Graphs
|
|
38
24
|
**
|
|
39
25
|
** Tim Berners-Lee 2006
|
|
@@ -41,12 +27,13 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
41
27
|
** This is or was https://github.com/linkeddata/rdflib.js/blob/main/src/serializer.js
|
|
42
28
|
** Licence: MIT
|
|
43
29
|
*/
|
|
30
|
+
|
|
31
|
+
// import * as jsonld from 'jsonld'
|
|
32
|
+
|
|
44
33
|
function createSerializer(store) {
|
|
45
34
|
return new Serializer(store);
|
|
46
35
|
}
|
|
47
|
-
|
|
48
36
|
;
|
|
49
|
-
|
|
50
37
|
var Serializer = /*#__PURE__*/function () {
|
|
51
38
|
function Serializer(store) {
|
|
52
39
|
(0, _classCallCheck2.default)(this, Serializer);
|
|
@@ -58,36 +45,26 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
58
45
|
this.flags = '';
|
|
59
46
|
this.base = null;
|
|
60
47
|
this.prefixes = []; // suggested prefixes
|
|
61
|
-
|
|
62
48
|
this.namespaces = []; // complementary
|
|
63
|
-
|
|
64
49
|
var nsKeys = Object.keys((0, _solidNamespace.default)());
|
|
65
|
-
|
|
66
50
|
for (var i in nsKeys) {
|
|
67
51
|
var uri = (0, _solidNamespace.default)()[nsKeys[i]]('');
|
|
68
52
|
var prefix = nsKeys[i];
|
|
69
53
|
this.prefixes[uri] = prefix;
|
|
70
54
|
this.namespaces[prefix] = uri;
|
|
71
55
|
}
|
|
72
|
-
|
|
73
56
|
this.suggestPrefix('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); // XML code assumes this!
|
|
74
|
-
|
|
75
57
|
this.suggestPrefix('xml', 'reserved:reservedForFutureUse'); // XML reserves xml: in the spec.
|
|
76
58
|
|
|
77
59
|
this.namespacesUsed = []; // Count actually used and so needed in @prefixes
|
|
78
|
-
|
|
79
60
|
this.keywords = ['a']; // The only one we generate at the moment
|
|
80
|
-
|
|
81
61
|
this.prefixchars = 'abcdefghijklmnopqustuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
82
62
|
this.incoming = null; // Array not calculated yet
|
|
83
|
-
|
|
84
63
|
this.formulas = []; // remembering original formulae from hashes
|
|
85
|
-
|
|
86
64
|
this.store = store;
|
|
87
65
|
this.rdfFactory = store.rdfFactory || _canonicalDataFactory.default;
|
|
88
66
|
this.xsd = (0, _xsd.createXSD)(this.rdfFactory);
|
|
89
67
|
}
|
|
90
|
-
|
|
91
68
|
(0, _createClass2.default)(Serializer, [{
|
|
92
69
|
key: "setBase",
|
|
93
70
|
value: function setBase(base) {
|
|
@@ -104,7 +81,6 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
104
81
|
key: "toStr",
|
|
105
82
|
value: function toStr(x) {
|
|
106
83
|
var s = x.toNT();
|
|
107
|
-
|
|
108
84
|
if (x.termType === 'Graph') {
|
|
109
85
|
this.formulas[s] = x; // remember as reverse does not work
|
|
110
86
|
}
|
|
@@ -119,124 +95,109 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
119
95
|
if (!x) console.log('No formula object for ' + s);
|
|
120
96
|
return x;
|
|
121
97
|
}
|
|
122
|
-
|
|
123
98
|
return this.store.fromNT(s);
|
|
124
99
|
}
|
|
100
|
+
|
|
125
101
|
/**
|
|
126
102
|
* Defines a set of [prefix, namespace] pairs to be used by this Serializer instance.
|
|
127
103
|
* Overrides previous prefixes if any
|
|
128
104
|
* @param namespaces
|
|
129
105
|
* @return {Serializer}
|
|
130
106
|
*/
|
|
131
|
-
|
|
132
107
|
}, {
|
|
133
108
|
key: "setNamespaces",
|
|
134
109
|
value: function setNamespaces(namespaces) {
|
|
135
110
|
for (var px in namespaces) {
|
|
136
111
|
this.setPrefix(px, namespaces[px]);
|
|
137
112
|
}
|
|
138
|
-
|
|
139
113
|
return this;
|
|
140
114
|
}
|
|
115
|
+
|
|
141
116
|
/**
|
|
142
117
|
* Defines a namespace prefix, overriding any existing prefix for that URI
|
|
143
118
|
* @param prefix
|
|
144
119
|
* @param uri
|
|
145
120
|
*/
|
|
146
|
-
|
|
147
121
|
}, {
|
|
148
122
|
key: "setPrefix",
|
|
149
123
|
value: function setPrefix(prefix, uri) {
|
|
150
124
|
if (prefix.slice(0, 7) === 'default') return; // Try to weed these out
|
|
151
|
-
|
|
152
125
|
if (prefix.slice(0, 2) === 'ns') return; // From others inferior algos
|
|
153
|
-
|
|
154
126
|
if (!prefix || !uri) return; // empty strings not suitable
|
|
127
|
+
|
|
155
128
|
// remove any existing prefix targeting this uri
|
|
156
129
|
// for (let existingPrefix in this.namespaces) {
|
|
157
130
|
// if (this.namespaces[existingPrefix] == uri)
|
|
158
131
|
// delete this.namespaces[existingPrefix];
|
|
159
132
|
// }
|
|
160
|
-
// remove any existing mapping for this prefix
|
|
161
133
|
|
|
134
|
+
// remove any existing mapping for this prefix
|
|
162
135
|
for (var existingNs in this.prefixes) {
|
|
163
136
|
if (this.prefixes[existingNs] == prefix) delete this.prefixes[existingNs];
|
|
164
137
|
}
|
|
165
|
-
|
|
166
138
|
this.prefixes[uri] = prefix;
|
|
167
139
|
this.namespaces[prefix] = uri;
|
|
168
140
|
}
|
|
141
|
+
|
|
169
142
|
/* Accumulate Namespaces
|
|
170
143
|
**
|
|
171
144
|
** These are only hints. If two overlap, only one gets used
|
|
172
145
|
** There is therefore no guarantee in general.
|
|
173
146
|
*/
|
|
174
|
-
|
|
175
147
|
}, {
|
|
176
148
|
key: "suggestPrefix",
|
|
177
149
|
value: function suggestPrefix(prefix, uri) {
|
|
178
150
|
if (prefix.slice(0, 7) === 'default') return; // Try to weed these out
|
|
179
|
-
|
|
180
151
|
if (prefix.slice(0, 2) === 'ns') return; // From others inferior algos
|
|
181
|
-
|
|
182
152
|
if (!prefix || !uri) return; // empty strings not suitable
|
|
183
|
-
|
|
184
153
|
if (prefix in this.namespaces || uri in this.prefixes) return; // already used
|
|
185
|
-
|
|
186
154
|
this.prefixes[uri] = prefix;
|
|
187
155
|
this.namespaces[prefix] = uri;
|
|
188
|
-
}
|
|
156
|
+
}
|
|
189
157
|
|
|
158
|
+
// Takes a namespace -> prefix map
|
|
190
159
|
}, {
|
|
191
160
|
key: "suggestNamespaces",
|
|
192
161
|
value: function suggestNamespaces(namespaces) {
|
|
193
162
|
for (var px in namespaces) {
|
|
194
163
|
this.suggestPrefix(px, namespaces[px]);
|
|
195
164
|
}
|
|
196
|
-
|
|
197
165
|
return this;
|
|
198
166
|
}
|
|
199
167
|
}, {
|
|
200
168
|
key: "checkIntegrity",
|
|
201
169
|
value: function checkIntegrity() {
|
|
202
170
|
var p, ns;
|
|
203
|
-
|
|
204
171
|
for (p in this.namespaces) {
|
|
205
172
|
if (this.prefixes[this.namespaces[p]] !== p) {
|
|
206
173
|
throw new Error('Serializer integity error 1: ' + p + ', ' + this.namespaces[p] + ', ' + this.prefixes[this.namespaces[p]] + '!');
|
|
207
174
|
}
|
|
208
175
|
}
|
|
209
|
-
|
|
210
176
|
for (ns in this.prefixes) {
|
|
211
177
|
if (this.namespaces[this.prefixes[ns]] !== ns) {
|
|
212
178
|
throw new Error('Serializer integity error 2: ' + ns + ', ' + this.prefixs[ns] + ', ' + this.namespaces[this.prefixes[ns]] + '!');
|
|
213
179
|
}
|
|
214
180
|
}
|
|
215
|
-
}
|
|
181
|
+
}
|
|
216
182
|
|
|
183
|
+
// Make up an unused prefix for a random namespace
|
|
217
184
|
}, {
|
|
218
185
|
key: "makeUpPrefix",
|
|
219
186
|
value: function makeUpPrefix(uri) {
|
|
220
187
|
var p = uri;
|
|
221
|
-
|
|
222
188
|
function canUseMethod(pp) {
|
|
223
189
|
if (!this.validPrefix.test(pp)) return false; // bad format
|
|
224
|
-
|
|
225
190
|
if (pp === 'ns') return false; // boring
|
|
226
|
-
|
|
227
191
|
if (pp in this.namespaces) return false; // already used
|
|
228
|
-
|
|
229
192
|
this.prefixes[uri] = pp;
|
|
230
193
|
this.namespaces[pp] = uri;
|
|
231
194
|
return pp;
|
|
232
195
|
}
|
|
233
|
-
|
|
234
196
|
var canUse = canUseMethod.bind(this);
|
|
235
197
|
if ('#/'.indexOf(p[p.length - 1]) >= 0) p = p.slice(0, -1);
|
|
236
198
|
var slash = p.lastIndexOf('/');
|
|
237
199
|
if (slash >= 0) p = p.slice(slash + 1);
|
|
238
200
|
var i = 0;
|
|
239
|
-
|
|
240
201
|
while (i < p.length) {
|
|
241
202
|
if (this.prefixchars.indexOf(p[i])) {
|
|
242
203
|
i++;
|
|
@@ -244,23 +205,18 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
244
205
|
break;
|
|
245
206
|
}
|
|
246
207
|
}
|
|
247
|
-
|
|
248
208
|
p = p.slice(0, i);
|
|
249
209
|
if (p.length < 6 && canUse(p)) return p; // exact is best
|
|
250
|
-
|
|
251
210
|
if (canUse(p.slice(0, 3))) return p.slice(0, 3);
|
|
252
211
|
if (canUse(p.slice(0, 2))) return p.slice(0, 2);
|
|
253
212
|
if (canUse(p.slice(0, 4))) return p.slice(0, 4);
|
|
254
213
|
if (canUse(p.slice(0, 1))) return p.slice(0, 1);
|
|
255
214
|
if (canUse(p.slice(0, 5))) return p.slice(0, 5);
|
|
256
|
-
|
|
257
215
|
if (!this.validPrefix.test(p)) {
|
|
258
216
|
p = 'n'; // Otherwise the loop below may never termimnate
|
|
259
217
|
}
|
|
260
218
|
|
|
261
|
-
for (var j = 0;; j++)
|
|
262
|
-
if (canUse(p.slice(0, 3) + j)) return p.slice(0, 3) + j;
|
|
263
|
-
}
|
|
219
|
+
for (var j = 0;; j++) if (canUse(p.slice(0, 3) + j)) return p.slice(0, 3) + j;
|
|
264
220
|
}
|
|
265
221
|
}, {
|
|
266
222
|
key: "rootSubjects",
|
|
@@ -268,6 +224,7 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
268
224
|
var incoming = {};
|
|
269
225
|
var subjects = {};
|
|
270
226
|
var allBnodes = {};
|
|
227
|
+
|
|
271
228
|
/* This scan is to find out which nodes will have to be the roots of trees
|
|
272
229
|
** in the serialized form. This will be any symbols, and any bnodes
|
|
273
230
|
** which hve more or less than one incoming arc, and any bnodes which have
|
|
@@ -275,10 +232,8 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
275
232
|
** This should be kept linear time with repect to the number of statements.
|
|
276
233
|
** Note it does not use any indexing of the store.
|
|
277
234
|
*/
|
|
278
|
-
|
|
279
235
|
for (var i = 0; i < sts.length; i++) {
|
|
280
236
|
var st = sts[i];
|
|
281
|
-
|
|
282
237
|
var checkMentions = function checkMentions(x) {
|
|
283
238
|
if (!incoming.hasOwnProperty(x)) incoming[x] = [];
|
|
284
239
|
incoming[x].push(st.subject); // List of things which will cause this to be printed
|
|
@@ -294,43 +249,39 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
294
249
|
});
|
|
295
250
|
}
|
|
296
251
|
});
|
|
252
|
+
|
|
297
253
|
checkMentions(sts[i].object);
|
|
298
254
|
var ss = subjects[this.toStr(st.subject)]; // Statements with this as subject
|
|
299
|
-
|
|
300
255
|
if (!ss) ss = [];
|
|
301
256
|
ss.push(st);
|
|
302
257
|
subjects[this.toStr(st.subject)] = ss; // Make hash. @@ too slow for formula?
|
|
303
258
|
}
|
|
304
259
|
|
|
305
260
|
var roots = [];
|
|
306
|
-
|
|
307
261
|
for (var xNT in subjects) {
|
|
308
262
|
if (!subjects.hasOwnProperty(xNT)) continue;
|
|
309
263
|
var y = this.fromStr(xNT);
|
|
310
|
-
|
|
311
264
|
if (y.termType !== 'BlankNode' || !incoming[y] || incoming[y].length !== 1) {
|
|
312
265
|
roots.push(y);
|
|
313
266
|
continue;
|
|
314
267
|
}
|
|
315
268
|
}
|
|
316
|
-
|
|
317
269
|
this.incoming = incoming; // Keep for serializing @@ Bug for nested formulas
|
|
318
|
-
// Now do the scan using existing roots
|
|
319
270
|
|
|
271
|
+
// Now do the scan using existing roots
|
|
320
272
|
var rootsHash = {};
|
|
321
|
-
|
|
322
273
|
for (var k = 0; k < roots.length; k++) {
|
|
323
274
|
rootsHash[roots[k].toNT()] = true;
|
|
324
275
|
}
|
|
325
|
-
|
|
326
276
|
return {
|
|
327
277
|
'roots': roots,
|
|
328
278
|
'subjects': subjects,
|
|
329
279
|
'rootsHash': rootsHash,
|
|
330
280
|
'incoming': incoming
|
|
331
281
|
};
|
|
332
|
-
}
|
|
282
|
+
}
|
|
333
283
|
|
|
284
|
+
// //////////////////////////////////////////////////////
|
|
334
285
|
}, {
|
|
335
286
|
key: "toN3",
|
|
336
287
|
value: function toN3(f) {
|
|
@@ -347,7 +298,6 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
347
298
|
} else {
|
|
348
299
|
uri = hexify(uri);
|
|
349
300
|
}
|
|
350
|
-
|
|
351
301
|
return '<' + uri + '>';
|
|
352
302
|
}
|
|
353
303
|
}, {
|
|
@@ -360,41 +310,33 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
360
310
|
var self = this;
|
|
361
311
|
var kb = this.store;
|
|
362
312
|
var factory = this.rdfFactory;
|
|
363
|
-
|
|
364
313
|
var termToNT = function termToNT(x) {
|
|
365
314
|
if (x.termType !== 'Collection') {
|
|
366
315
|
return self.atomicTermToN3(x);
|
|
367
316
|
}
|
|
368
|
-
|
|
369
317
|
var list = x.elements;
|
|
370
318
|
var rest = kb.sym(rdfns + 'nill');
|
|
371
|
-
|
|
372
319
|
for (var i = list.length - 1; i >= 0; i--) {
|
|
373
320
|
var bnode = factory.blankNode();
|
|
374
321
|
str += termToNT(bnode) + ' ' + termToNT(kb.sym(rdfns + 'first')) + ' ' + termToNT(list[i]) + '.\n';
|
|
375
322
|
str += termToNT(bnode) + ' ' + termToNT(kb.sym(rdfns + 'rest')) + ' ' + termToNT(rest) + '.\n';
|
|
376
323
|
rest = bnode;
|
|
377
324
|
}
|
|
378
|
-
|
|
379
325
|
return self.atomicTermToN3(rest);
|
|
380
326
|
};
|
|
381
|
-
|
|
382
327
|
for (var i = 0; i < sorted.length; i++) {
|
|
383
328
|
var st = sorted[i];
|
|
384
329
|
var s = '';
|
|
385
330
|
s += termToNT(st.subject) + ' ';
|
|
386
331
|
s += termToNT(st.predicate) + ' ';
|
|
387
332
|
s += termToNT(st.object) + ' ';
|
|
388
|
-
|
|
389
333
|
if (this.flags.indexOf('q') >= 0) {
|
|
390
334
|
// Do quads not nrtiples
|
|
391
335
|
s += termToNT(st.why) + ' ';
|
|
392
336
|
}
|
|
393
|
-
|
|
394
337
|
s += '.\n';
|
|
395
338
|
str += s;
|
|
396
339
|
}
|
|
397
|
-
|
|
398
340
|
return str;
|
|
399
341
|
}
|
|
400
342
|
}, {
|
|
@@ -402,59 +344,46 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
402
344
|
value: function statementsToN3(sts) {
|
|
403
345
|
var indent = 4;
|
|
404
346
|
var width = 80;
|
|
405
|
-
var kb = this.store;
|
|
406
|
-
|
|
347
|
+
var kb = this.store;
|
|
348
|
+
// A URI Map alows us to put the type statemnts at the top.
|
|
407
349
|
var uriMap = {
|
|
408
350
|
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': 'aaa:00'
|
|
409
351
|
};
|
|
410
|
-
|
|
411
352
|
var SPO = function SPO(x, y) {
|
|
412
353
|
// Do limited canonicalization of bnodes
|
|
413
354
|
return Util.heavyCompareSPO(x, y, kb, uriMap);
|
|
414
355
|
};
|
|
415
|
-
|
|
416
356
|
sts.sort(SPO);
|
|
417
|
-
|
|
418
357
|
if (this.base && !this.defaultNamespace) {
|
|
419
358
|
this.defaultNamespace = this.base + '#';
|
|
420
359
|
}
|
|
421
|
-
|
|
422
360
|
var predMap = {};
|
|
423
|
-
|
|
424
361
|
if (this.flags.indexOf('s') < 0) {
|
|
425
362
|
predMap['http://www.w3.org/2002/07/owl#sameAs'] = '=';
|
|
426
363
|
}
|
|
427
|
-
|
|
428
364
|
if (this.flags.indexOf('t') < 0) {
|
|
429
365
|
predMap['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'] = 'a';
|
|
430
366
|
}
|
|
431
|
-
|
|
432
367
|
if (this.flags.indexOf('i') < 0) {
|
|
433
368
|
predMap['http://www.w3.org/2000/10/swap/log#implies'] = '=>';
|
|
434
|
-
}
|
|
435
|
-
|
|
369
|
+
}
|
|
370
|
+
// //////////////////////// Arrange the bits of text
|
|
436
371
|
|
|
437
372
|
var spaces = function spaces(n) {
|
|
438
373
|
var s = '';
|
|
439
|
-
|
|
440
|
-
for (var i = 0; i < n; i++) {
|
|
441
|
-
s += ' ';
|
|
442
|
-
}
|
|
443
|
-
|
|
374
|
+
for (var i = 0; i < n; i++) s += ' ';
|
|
444
375
|
return s;
|
|
445
376
|
};
|
|
446
|
-
|
|
447
377
|
var treeToLine = function treeToLine(tree) {
|
|
448
378
|
var str = '';
|
|
449
|
-
|
|
450
379
|
for (var i = 0; i < tree.length; i++) {
|
|
451
380
|
var branch = tree[i];
|
|
452
|
-
var s2 = typeof branch === 'string' ? branch : treeToLine(branch);
|
|
453
|
-
|
|
381
|
+
var s2 = typeof branch === 'string' ? branch : treeToLine(branch);
|
|
382
|
+
// Note the space before the dot in case statement ends with 123 or colon. which is in fact allowed but be conservative.
|
|
454
383
|
if (i !== 0) {
|
|
455
384
|
var ch = str.slice(-1) || ' ';
|
|
456
|
-
|
|
457
|
-
|
|
385
|
+
if (s2 === ',' || s2 === ';') {
|
|
386
|
+
// no gap
|
|
458
387
|
} else if (s2 === '.' && !'0123456789.:'.includes(ch)) {// no gap except after number and colon
|
|
459
388
|
// no gap
|
|
460
389
|
} else {
|
|
@@ -464,69 +393,55 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
464
393
|
|
|
465
394
|
str += s2;
|
|
466
395
|
}
|
|
467
|
-
|
|
468
396
|
return str;
|
|
469
|
-
};
|
|
470
|
-
|
|
397
|
+
};
|
|
471
398
|
|
|
399
|
+
// Convert a nested tree of lists and strings to a string
|
|
472
400
|
var treeToString = function treeToString(tree, level) {
|
|
473
401
|
var str = '';
|
|
474
402
|
var lastLength = 100000;
|
|
475
403
|
if (level === undefined) level = -1;
|
|
476
|
-
|
|
477
404
|
for (var i = 0; i < tree.length; i++) {
|
|
478
405
|
var branch = tree[i];
|
|
479
|
-
|
|
480
406
|
if (typeof branch !== 'string') {
|
|
481
407
|
var substr = treeToString(branch, level + 1);
|
|
482
|
-
|
|
483
408
|
if (substr.length < 10 * (width - indent * level) && substr.indexOf('"""') < 0) {
|
|
484
409
|
// Don't mess up multiline strings
|
|
485
410
|
var line = treeToLine(branch);
|
|
486
|
-
|
|
487
411
|
if (line.length < width - indent * level) {
|
|
488
412
|
branch = line; // Note! treat as string below
|
|
489
|
-
|
|
490
413
|
substr = '';
|
|
491
414
|
}
|
|
492
415
|
}
|
|
493
|
-
|
|
494
416
|
if (substr) lastLength = 10000;
|
|
495
417
|
str += substr;
|
|
496
418
|
}
|
|
497
|
-
|
|
498
419
|
if (typeof branch === 'string') {
|
|
499
420
|
if (branch.length === 1 && str.slice(-1) === '\n') {
|
|
500
421
|
if (',.;'.indexOf(branch) >= 0) {
|
|
501
|
-
str = str.slice(0, -1);
|
|
502
|
-
|
|
422
|
+
str = str.slice(0, -1);
|
|
423
|
+
// be conservative and ensure a whitespace between some chars and a final dot, as in treeToLine above
|
|
503
424
|
if (branch == '.' && '0123456789.:'.includes(str.charAt(str.length - 1))) {
|
|
504
425
|
str += ' ';
|
|
505
426
|
lastLength += 1;
|
|
506
427
|
}
|
|
507
|
-
|
|
508
428
|
str += branch + '\n'; // slip punct'n on end
|
|
509
|
-
|
|
510
429
|
lastLength += 1;
|
|
511
430
|
continue;
|
|
512
431
|
}
|
|
513
432
|
}
|
|
514
|
-
|
|
515
|
-
|
|
433
|
+
if (lastLength < indent * level + 4 ||
|
|
434
|
+
// if new line not necessary
|
|
516
435
|
lastLength + branch.length + 1 < width && ';.'.indexOf(str[str.length - 2]) < 0) {
|
|
517
436
|
// or the string fits on last line
|
|
518
437
|
str = str.slice(0, -1) + ' ' + branch + '\n'; // then continue on this line
|
|
519
|
-
|
|
520
438
|
lastLength += branch.length + 1;
|
|
521
439
|
} else {
|
|
522
440
|
var _line = spaces(indent * level) + branch;
|
|
523
|
-
|
|
524
441
|
str += _line + '\n';
|
|
525
442
|
lastLength = _line.length;
|
|
526
|
-
|
|
527
443
|
if (level < 0) {
|
|
528
444
|
str += '\n'; // extra blank line
|
|
529
|
-
|
|
530
445
|
lastLength = 100000; // don't touch
|
|
531
446
|
}
|
|
532
447
|
}
|
|
@@ -534,48 +449,41 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
534
449
|
}
|
|
535
450
|
|
|
536
451
|
return str;
|
|
537
|
-
};
|
|
538
|
-
// Convert a set of statements into a nested tree of lists and strings
|
|
539
|
-
|
|
452
|
+
};
|
|
540
453
|
|
|
454
|
+
// //////////////////////////////////////////// Structure for N3
|
|
455
|
+
// Convert a set of statements into a nested tree of lists and strings
|
|
541
456
|
function statementListToTreeMethod(statements) {
|
|
542
457
|
var stats = this.rootSubjects(statements);
|
|
543
458
|
var roots = stats.roots;
|
|
544
459
|
var results = [];
|
|
545
|
-
|
|
546
460
|
for (var i = 0; i < roots.length; i++) {
|
|
547
461
|
var root = roots[i];
|
|
548
462
|
results.push(subjectTree(root, stats));
|
|
549
463
|
}
|
|
550
|
-
|
|
551
464
|
return results;
|
|
552
465
|
}
|
|
466
|
+
var statementListToTree = statementListToTreeMethod.bind(this);
|
|
553
467
|
|
|
554
|
-
|
|
555
|
-
|
|
468
|
+
// The tree for a subject
|
|
556
469
|
function subjectTree(subject, stats) {
|
|
557
470
|
if (subject.termType === 'BlankNode' && !stats.incoming[subject]) {
|
|
558
471
|
return objectTree(subject, stats, true).concat(['.']); // Anonymous bnode subject
|
|
559
472
|
}
|
|
560
473
|
|
|
561
474
|
return [termToN3(subject, stats)].concat([propertyTree(subject, stats)]).concat(['.']);
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
|
|
475
|
+
}
|
|
476
|
+
// The property tree for a single subject or anonymous node
|
|
565
477
|
function propertyTreeMethod(subject, stats) {
|
|
566
478
|
var results = [];
|
|
567
479
|
var lastPred = null;
|
|
568
480
|
var sts = stats.subjects[this.toStr(subject)] || []; // relevant statements
|
|
569
|
-
|
|
570
481
|
if (typeof sts === 'undefined') {
|
|
571
482
|
throw new Error('Cant find statements for ' + subject);
|
|
572
483
|
}
|
|
573
|
-
|
|
574
484
|
var objects = [];
|
|
575
|
-
|
|
576
485
|
for (var i = 0; i < sts.length; i++) {
|
|
577
486
|
var st = sts[i];
|
|
578
|
-
|
|
579
487
|
if (st.predicate.uri === lastPred) {
|
|
580
488
|
objects.push(',');
|
|
581
489
|
} else {
|
|
@@ -583,20 +491,15 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
583
491
|
results = results.concat([objects]).concat([';']);
|
|
584
492
|
objects = [];
|
|
585
493
|
}
|
|
586
|
-
|
|
587
494
|
results.push(predMap[st.predicate.uri] ? predMap[st.predicate.uri] : termToN3(st.predicate, stats));
|
|
588
495
|
}
|
|
589
|
-
|
|
590
496
|
lastPred = st.predicate.uri;
|
|
591
497
|
objects.push(objectTree(st.object, stats));
|
|
592
498
|
}
|
|
593
|
-
|
|
594
499
|
results = results.concat([objects]);
|
|
595
500
|
return results;
|
|
596
501
|
}
|
|
597
|
-
|
|
598
502
|
var propertyTree = propertyTreeMethod.bind(this);
|
|
599
|
-
|
|
600
503
|
function objectTreeMethod(obj, stats, force) {
|
|
601
504
|
if (obj.termType === 'BlankNode' && (force || stats.rootsHash[obj.toNT()] === undefined)) {
|
|
602
505
|
// if not a root
|
|
@@ -606,63 +509,50 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
606
509
|
return '[]';
|
|
607
510
|
}
|
|
608
511
|
}
|
|
609
|
-
|
|
610
512
|
return termToN3(obj, stats);
|
|
611
513
|
}
|
|
612
|
-
|
|
613
514
|
var objectTree = objectTreeMethod.bind(this);
|
|
614
|
-
|
|
615
515
|
function termToN3Method(expr, stats) {
|
|
616
516
|
//
|
|
617
517
|
var i, res;
|
|
618
|
-
|
|
619
518
|
switch (expr.termType) {
|
|
620
519
|
case 'Graph':
|
|
621
520
|
res = ['{'];
|
|
622
521
|
res = res.concat(statementListToTree(expr.statements));
|
|
623
522
|
return res.concat(['}']);
|
|
624
|
-
|
|
625
523
|
case 'Collection':
|
|
626
524
|
res = ['('];
|
|
627
|
-
|
|
628
525
|
for (i = 0; i < expr.elements.length; i++) {
|
|
629
526
|
res.push([objectTree(expr.elements[i], stats)]);
|
|
630
527
|
}
|
|
631
|
-
|
|
632
528
|
res.push(')');
|
|
633
529
|
return res;
|
|
634
|
-
|
|
635
530
|
default:
|
|
636
531
|
return this.atomicTermToN3(expr);
|
|
637
532
|
}
|
|
638
533
|
}
|
|
639
|
-
|
|
640
534
|
Serializer.prototype.termToN3 = termToN3;
|
|
641
535
|
var termToN3 = termToN3Method.bind(this);
|
|
642
|
-
|
|
643
536
|
function prefixDirectivesMethod() {
|
|
644
537
|
var str = '';
|
|
645
|
-
|
|
646
538
|
if (this.defaultNamespace) {
|
|
647
539
|
str += '@prefix : ' + this.explicitURI(this.defaultNamespace) + '.\n';
|
|
648
540
|
}
|
|
649
|
-
|
|
650
541
|
for (var ns in this.prefixes) {
|
|
651
542
|
if (!this.prefixes.hasOwnProperty(ns)) continue;
|
|
652
543
|
if (!this.namespacesUsed[ns]) continue;
|
|
653
544
|
str += '@prefix ' + this.prefixes[ns] + ': ' + this.explicitURI(ns) + '.\n';
|
|
654
545
|
}
|
|
655
|
-
|
|
656
546
|
return str + '\n';
|
|
657
547
|
}
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
548
|
+
var prefixDirectives = prefixDirectivesMethod.bind(this);
|
|
549
|
+
// Body of statementsToN3:
|
|
661
550
|
var tree = statementListToTree(sts);
|
|
662
551
|
return prefixDirectives() + treeToString(tree);
|
|
663
|
-
}
|
|
664
|
-
//
|
|
552
|
+
}
|
|
553
|
+
// //////////////////////////////////////////// Atomic Terms
|
|
665
554
|
|
|
555
|
+
// Deal with term level things and nesting with no bnode structure
|
|
666
556
|
}, {
|
|
667
557
|
key: "atomicTermToN3",
|
|
668
558
|
value: function atomicTermToN3(expr, stats) {
|
|
@@ -670,26 +560,21 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
670
560
|
case 'BlankNode':
|
|
671
561
|
case 'Variable':
|
|
672
562
|
return expr.toNT();
|
|
673
|
-
|
|
674
563
|
case 'Literal':
|
|
675
564
|
var val = expr.value;
|
|
676
|
-
|
|
677
565
|
if (typeof val !== 'string') {
|
|
678
566
|
throw new TypeError('Value of RDF literal node must be a string');
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
|
|
567
|
+
}
|
|
568
|
+
// var val = expr.value.toString() // should be a string already
|
|
682
569
|
if (expr.datatype && this.flags.indexOf('x') < 0) {
|
|
683
570
|
// Supress native numbers
|
|
684
571
|
switch (expr.datatype.uri) {
|
|
685
572
|
case 'http://www.w3.org/2001/XMLSchema#integer':
|
|
686
573
|
return val;
|
|
687
|
-
|
|
688
574
|
case 'http://www.w3.org/2001/XMLSchema#decimal':
|
|
689
575
|
// In Turtle, must have dot
|
|
690
576
|
if (val.indexOf('.') < 0) val += '.0';
|
|
691
577
|
return val;
|
|
692
|
-
|
|
693
578
|
case 'http://www.w3.org/2001/XMLSchema#double':
|
|
694
579
|
{
|
|
695
580
|
// Must force use of 'e'
|
|
@@ -698,33 +583,27 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
698
583
|
if (!eNotation) val += 'e0';
|
|
699
584
|
return val;
|
|
700
585
|
}
|
|
701
|
-
|
|
702
586
|
case 'http://www.w3.org/2001/XMLSchema#boolean':
|
|
703
587
|
return expr.value === '1' ? 'true' : 'false';
|
|
704
588
|
}
|
|
705
589
|
}
|
|
706
|
-
|
|
707
590
|
var str = this.stringToN3(expr.value);
|
|
708
|
-
|
|
709
591
|
if (expr.language) {
|
|
710
592
|
str += '@' + expr.language;
|
|
711
593
|
} else if (!expr.datatype.equals(this.xsd.string)) {
|
|
712
594
|
str += '^^' + this.atomicTermToN3(expr.datatype, stats);
|
|
713
595
|
}
|
|
714
|
-
|
|
715
596
|
return str;
|
|
716
|
-
|
|
717
597
|
case 'NamedNode':
|
|
718
598
|
return this.symbolToN3(expr);
|
|
719
|
-
|
|
720
599
|
case 'DefaultGraph':
|
|
721
600
|
return '';
|
|
722
|
-
|
|
723
601
|
default:
|
|
724
602
|
throw new Error('Internal: atomicTermToN3 cannot handle ' + expr + ' of termType: ' + expr.termType);
|
|
725
603
|
}
|
|
726
|
-
}
|
|
604
|
+
}
|
|
727
605
|
|
|
606
|
+
// stringToN3: String escaping for N3
|
|
728
607
|
}, {
|
|
729
608
|
key: "stringToN3",
|
|
730
609
|
value: function stringToN3(str, flags) {
|
|
@@ -733,10 +612,12 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
733
612
|
var i, j, k;
|
|
734
613
|
var delim;
|
|
735
614
|
var forbidden;
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
str.slice(-1) !== '"' &&
|
|
739
|
-
|
|
615
|
+
if (str.length > 20 &&
|
|
616
|
+
// Long enough to make sense
|
|
617
|
+
str.slice(-1) !== '"' &&
|
|
618
|
+
// corner case'
|
|
619
|
+
flags.indexOf('n') < 0 && (
|
|
620
|
+
// Force single line
|
|
740
621
|
str.indexOf('\n') > 0 || str.indexOf('"') > 0)) {
|
|
741
622
|
delim = '"""';
|
|
742
623
|
forbidden = this.forbidden3;
|
|
@@ -744,7 +625,6 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
744
625
|
delim = '"';
|
|
745
626
|
forbidden = this.forbidden1;
|
|
746
627
|
}
|
|
747
|
-
|
|
748
628
|
for (i = 0; i < str.length;) {
|
|
749
629
|
forbidden.lastIndex = 0;
|
|
750
630
|
var m = forbidden.exec(str.slice(i));
|
|
@@ -752,12 +632,10 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
752
632
|
j = i + forbidden.lastIndex - 1;
|
|
753
633
|
res += str.slice(i, j);
|
|
754
634
|
var ch = str[j];
|
|
755
|
-
|
|
756
635
|
if (ch === '"' && delim === '"""' && str.slice(j, j + 3) !== '"""') {
|
|
757
636
|
res += ch;
|
|
758
637
|
} else {
|
|
759
638
|
k = '\b\f\r\t\v\n\\"'.indexOf(ch); // No escaping of bell (7)?
|
|
760
|
-
|
|
761
639
|
if (k >= 0) {
|
|
762
640
|
res += '\\' + 'bfrtvn\\"'[k];
|
|
763
641
|
} else {
|
|
@@ -770,28 +648,24 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
770
648
|
}
|
|
771
649
|
}
|
|
772
650
|
}
|
|
773
|
-
|
|
774
651
|
i = j + 1;
|
|
775
652
|
}
|
|
776
|
-
|
|
777
653
|
return delim + res + str.slice(i) + delim;
|
|
778
|
-
}
|
|
779
|
-
|
|
654
|
+
}
|
|
655
|
+
// A single symbol, either in <> or namespace notation
|
|
780
656
|
}, {
|
|
781
657
|
key: "symbolToN3",
|
|
782
658
|
value: function symbolToN3(x) {
|
|
783
659
|
// c.f. symbolString() in notation3.py
|
|
784
660
|
var uri = x.uri;
|
|
785
661
|
var j = uri.indexOf('#');
|
|
786
|
-
|
|
787
662
|
if (j < 0 && this.flags.indexOf('/') < 0) {
|
|
788
663
|
j = uri.lastIndexOf('/');
|
|
789
664
|
}
|
|
790
|
-
|
|
791
|
-
|
|
665
|
+
if (j >= 0 && this.flags.indexOf('p') < 0 && (
|
|
666
|
+
// Can split at namespace but only if http[s]: URI or file: or ws[s] (why not others?)
|
|
792
667
|
uri.indexOf('http') === 0 || uri.indexOf('ws') === 0 || uri.indexOf('file') === 0)) {
|
|
793
668
|
var canSplit = true;
|
|
794
|
-
|
|
795
669
|
for (var k = j + 1; k < uri.length; k++) {
|
|
796
670
|
if (this._notNameChars.indexOf(uri[k]) >= 0) {
|
|
797
671
|
canSplit = false;
|
|
@@ -807,61 +681,61 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
807
681
|
}
|
|
808
682
|
}
|
|
809
683
|
*/
|
|
810
|
-
|
|
811
|
-
|
|
812
684
|
if (canSplit) {
|
|
813
685
|
var localid = uri.slice(j + 1);
|
|
814
686
|
var namesp = uri.slice(0, j + 1);
|
|
815
|
-
|
|
816
687
|
if (this.defaultNamespace && this.defaultNamespace === namesp && this.flags.indexOf('d') < 0) {
|
|
817
688
|
// d -> suppress default
|
|
818
689
|
if (this.flags.indexOf('k') >= 0 && this.keyords.indexOf(localid) < 0) {
|
|
819
690
|
return localid;
|
|
820
691
|
}
|
|
821
|
-
|
|
822
692
|
return ':' + localid;
|
|
823
|
-
}
|
|
824
|
-
|
|
825
|
-
|
|
693
|
+
}
|
|
694
|
+
// this.checkIntegrity() // @@@ Remove when not testing
|
|
826
695
|
var prefix = this.prefixes[namesp];
|
|
827
696
|
if (!prefix) prefix = this.makeUpPrefix(namesp);
|
|
828
|
-
|
|
829
697
|
if (prefix) {
|
|
830
698
|
this.namespacesUsed[namesp] = true;
|
|
831
699
|
return prefix + ':' + localid;
|
|
832
|
-
}
|
|
833
|
-
|
|
700
|
+
}
|
|
701
|
+
// Fall though if can't do qname
|
|
834
702
|
}
|
|
835
703
|
}
|
|
836
704
|
|
|
837
705
|
return this.explicitURI(uri);
|
|
838
|
-
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
// /////////////////////////// Quad store serialization
|
|
709
|
+
|
|
839
710
|
// @para. write - a function taking a single string to be output
|
|
840
711
|
//
|
|
841
|
-
|
|
842
712
|
}, {
|
|
843
713
|
key: "writeStore",
|
|
844
714
|
value: function writeStore(write) {
|
|
845
715
|
var kb = this.store;
|
|
846
716
|
var fetcher = kb.fetcher;
|
|
847
|
-
var session = fetcher && fetcher.appNode;
|
|
717
|
+
var session = fetcher && fetcher.appNode;
|
|
848
718
|
|
|
849
|
-
|
|
719
|
+
// The core data
|
|
850
720
|
|
|
721
|
+
var sources = this.store.index[3];
|
|
851
722
|
for (var s in sources) {
|
|
852
723
|
// -> assume we can use -> as short for log:semantics
|
|
853
724
|
var source = kb.fromNT(s);
|
|
854
725
|
if (session && source.equals(session)) continue;
|
|
855
726
|
write('\n' + this.atomicTermToN3(source) + ' ' + this.atomicTermToN3(kb.sym('http://www.w3.org/2000/10/swap/log#semantics')) + ' { ' + this.statementsToN3(kb.statementsMatching(undefined, undefined, undefined, source)) + ' }.\n');
|
|
856
|
-
}
|
|
727
|
+
}
|
|
857
728
|
|
|
729
|
+
// The metadata from HTTP interactions:
|
|
858
730
|
|
|
859
731
|
kb.statementsMatching(undefined, kb.sym('http://www.w3.org/2007/ont/link#requestedURI')).map(function (st) {
|
|
860
732
|
write('\n<' + st.object.value + '> log:metadata {\n');
|
|
861
733
|
var sts = kb.statementsMatching(undefined, undefined, undefined, st.subject);
|
|
862
734
|
write(this.statementsToN3(this.statementsToN3(sts)));
|
|
863
735
|
write('}.\n');
|
|
864
|
-
});
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
// Inferences we have made ourselves not attributable to anyone else
|
|
865
739
|
|
|
866
740
|
var metaSources = [];
|
|
867
741
|
if (session) metaSources.push(session);
|
|
@@ -870,69 +744,56 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
870
744
|
metadata = metadata.concat(kb.statementsMatching(undefined, undefined, undefined, source));
|
|
871
745
|
});
|
|
872
746
|
write(this.statementsToN3(metadata));
|
|
873
|
-
}
|
|
747
|
+
}
|
|
874
748
|
|
|
749
|
+
// ////////////////////////////////////////////// XML serialization
|
|
875
750
|
}, {
|
|
876
751
|
key: "statementsToXML",
|
|
877
752
|
value: function statementsToXML(sts) {
|
|
878
753
|
var indent = 4;
|
|
879
754
|
var width = 80;
|
|
880
755
|
var namespaceCounts = []; // which have been used
|
|
881
|
-
|
|
882
756
|
namespaceCounts['http://www.w3.org/1999/02/22-rdf-syntax-ns#'] = true;
|
|
883
757
|
var liPrefix = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_'; // prefix for ordered list items
|
|
758
|
+
|
|
884
759
|
// //////////////////////// Arrange the bits of XML text
|
|
885
760
|
|
|
886
761
|
var spaces = function spaces(n) {
|
|
887
762
|
var s = '';
|
|
888
|
-
|
|
889
|
-
for (var i = 0; i < n; i++) {
|
|
890
|
-
s += ' ';
|
|
891
|
-
}
|
|
892
|
-
|
|
763
|
+
for (var i = 0; i < n; i++) s += ' ';
|
|
893
764
|
return s;
|
|
894
765
|
};
|
|
895
|
-
|
|
896
766
|
var XMLtreeToLine = function XMLtreeToLine(tree) {
|
|
897
767
|
var str = '';
|
|
898
|
-
|
|
899
768
|
for (var i = 0; i < tree.length; i++) {
|
|
900
769
|
var branch = tree[i];
|
|
901
770
|
var s2 = typeof branch === 'string' ? branch : XMLtreeToLine(branch);
|
|
902
771
|
str += s2;
|
|
903
772
|
}
|
|
904
|
-
|
|
905
773
|
return str;
|
|
906
|
-
};
|
|
907
|
-
|
|
774
|
+
};
|
|
908
775
|
|
|
776
|
+
// Convert a nested tree of lists and strings to a string
|
|
909
777
|
var XMLtreeToString = function XMLtreeToString(tree, level) {
|
|
910
778
|
var str = '';
|
|
911
779
|
var line;
|
|
912
780
|
var lastLength = 100000;
|
|
913
781
|
if (!level) level = 0;
|
|
914
|
-
|
|
915
782
|
for (var i = 0; i < tree.length; i++) {
|
|
916
783
|
var branch = tree[i];
|
|
917
|
-
|
|
918
784
|
if (typeof branch !== 'string') {
|
|
919
785
|
var substr = XMLtreeToString(branch, level + 1);
|
|
920
|
-
|
|
921
786
|
if (substr.length < 10 * (width - indent * level) && substr.indexOf('"""') < 0) {
|
|
922
787
|
// Don't mess up multiline strings
|
|
923
788
|
line = XMLtreeToLine(branch);
|
|
924
|
-
|
|
925
789
|
if (line.length < width - indent * level) {
|
|
926
790
|
branch = ' ' + line; // @@ Hack: treat as string below
|
|
927
|
-
|
|
928
791
|
substr = '';
|
|
929
792
|
}
|
|
930
793
|
}
|
|
931
|
-
|
|
932
794
|
if (substr) lastLength = 10000;
|
|
933
795
|
str += substr;
|
|
934
796
|
}
|
|
935
|
-
|
|
936
797
|
if (typeof branch === 'string') {
|
|
937
798
|
if (lastLength < indent * level + 4) {
|
|
938
799
|
// continue
|
|
@@ -946,36 +807,28 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
946
807
|
} else {// not string
|
|
947
808
|
}
|
|
948
809
|
}
|
|
949
|
-
|
|
950
810
|
return str;
|
|
951
811
|
};
|
|
952
|
-
|
|
953
812
|
function statementListToXMLTreeMethod(statements) {
|
|
954
813
|
this.suggestPrefix('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
|
|
955
814
|
var stats = this.rootSubjects(statements);
|
|
956
815
|
var roots = stats.roots;
|
|
957
816
|
var results = [];
|
|
958
|
-
|
|
959
817
|
for (var i = 0; i < roots.length; i++) {
|
|
960
818
|
var root = roots[i];
|
|
961
819
|
results.push(subjectXMLTree(root, stats));
|
|
962
820
|
}
|
|
963
|
-
|
|
964
821
|
return results;
|
|
965
822
|
}
|
|
966
|
-
|
|
967
823
|
var statementListToXMLTree = statementListToXMLTreeMethod.bind(this);
|
|
968
|
-
|
|
969
824
|
function escapeForXML(str) {
|
|
970
825
|
if (typeof str === 'undefined') return '@@@undefined@@@@';
|
|
971
826
|
return str.replace(/[&<"]/g, function (m) {
|
|
972
827
|
switch (m[0]) {
|
|
973
828
|
case '&':
|
|
974
829
|
return '&';
|
|
975
|
-
|
|
976
830
|
case '<':
|
|
977
831
|
return '<';
|
|
978
|
-
|
|
979
832
|
case '"':
|
|
980
833
|
return '"';
|
|
981
834
|
// '
|
|
@@ -986,73 +839,63 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
986
839
|
function relURIMethod(term) {
|
|
987
840
|
return escapeForXML(this.base ? Util.uri.refTo(this.base, term.uri) : term.uri);
|
|
988
841
|
}
|
|
842
|
+
var relURI = relURIMethod.bind(this);
|
|
989
843
|
|
|
990
|
-
|
|
991
|
-
|
|
844
|
+
// The tree for a subject
|
|
992
845
|
function subjectXMLTreeMethod(subject, stats) {
|
|
993
846
|
var results = [];
|
|
994
847
|
var type, t, st, pred;
|
|
995
848
|
var sts = stats.subjects[this.toStr(subject)]; // relevant statements
|
|
996
|
-
|
|
997
849
|
if (typeof sts === 'undefined') {
|
|
998
850
|
// empty bnode
|
|
999
851
|
return propertyXMLTree(subject, stats);
|
|
1000
|
-
}
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
// Sort only on the predicate, leave the order at object
|
|
1001
855
|
// level undisturbed. This leaves multilingual content in
|
|
1002
856
|
// the order of entry (for partner literals), which helps
|
|
1003
857
|
// readability.
|
|
1004
858
|
//
|
|
1005
859
|
// For the predicate sort, we attempt to split the uri
|
|
1006
860
|
// as a hint to the sequence
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
861
|
sts.sort(function (a, b) {
|
|
1010
862
|
var ap = a.predicate.uri;
|
|
1011
863
|
var bp = b.predicate.uri;
|
|
1012
|
-
|
|
1013
864
|
if (ap.substring(0, liPrefix.length) === liPrefix || bp.substring(0, liPrefix.length) === liPrefix) {
|
|
1014
865
|
// we're only interested in sorting list items
|
|
1015
866
|
return ap.localeCompare(bp);
|
|
1016
867
|
}
|
|
1017
|
-
|
|
1018
868
|
var as = ap.substring(liPrefix.length);
|
|
1019
869
|
var bs = bp.substring(liPrefix.length);
|
|
1020
870
|
var an = parseInt(as, 10);
|
|
1021
871
|
var bn = parseInt(bs, 10);
|
|
1022
|
-
|
|
1023
872
|
if (isNaN(an) || isNaN(bn) || an !== as || bn !== bs) {
|
|
1024
873
|
// we only care about integers
|
|
1025
874
|
return ap.localeCompare(bp);
|
|
1026
875
|
}
|
|
1027
|
-
|
|
1028
876
|
return an - bn;
|
|
1029
877
|
});
|
|
1030
|
-
|
|
1031
878
|
for (var i = 0; i < sts.length; i++) {
|
|
1032
|
-
st = sts[i];
|
|
1033
|
-
|
|
879
|
+
st = sts[i];
|
|
880
|
+
// look for a type
|
|
1034
881
|
if (st.predicate.uri === 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' && !type && st.object.termType === 'NamedNode') {
|
|
1035
882
|
type = st.object;
|
|
1036
883
|
continue; // don't include it as a child element
|
|
1037
|
-
}
|
|
1038
|
-
|
|
884
|
+
}
|
|
1039
885
|
|
|
886
|
+
// see whether predicate can be replaced with "li"
|
|
1040
887
|
pred = st.predicate;
|
|
1041
|
-
|
|
1042
888
|
if (pred.uri.substr(0, liPrefix.length) === liPrefix) {
|
|
1043
|
-
var number = pred.uri.substr(liPrefix.length);
|
|
1044
|
-
|
|
889
|
+
var number = pred.uri.substr(liPrefix.length);
|
|
890
|
+
// make sure these are actually numeric list items
|
|
1045
891
|
var intNumber = parseInt(number, 10);
|
|
1046
|
-
|
|
1047
892
|
if (number === intNumber.toString()) {
|
|
1048
893
|
// was numeric; don't need to worry about ordering since we've already
|
|
1049
894
|
// sorted the statements
|
|
1050
895
|
pred = this.rdfFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#li');
|
|
1051
896
|
}
|
|
1052
897
|
}
|
|
1053
|
-
|
|
1054
898
|
t = qname(pred);
|
|
1055
|
-
|
|
1056
899
|
switch (st.object.termType) {
|
|
1057
900
|
case 'BlankNode':
|
|
1058
901
|
if (stats.incoming[st.object].length === 1) {
|
|
@@ -1061,30 +904,23 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
1061
904
|
} else {
|
|
1062
905
|
results = results.concat(['<' + t + ' rdf:nodeID="' + st.object.toNT().slice(2) + '"/>']);
|
|
1063
906
|
}
|
|
1064
|
-
|
|
1065
907
|
break;
|
|
1066
|
-
|
|
1067
908
|
case 'NamedNode':
|
|
1068
909
|
results = results.concat(['<' + t + ' rdf:resource="' + relURI(st.object) + '"/>']);
|
|
1069
910
|
break;
|
|
1070
|
-
|
|
1071
911
|
case 'Literal':
|
|
1072
912
|
results = results.concat(['<' + t + (st.object.datatype.equals(this.xsd.string) ? '' : ' rdf:datatype="' + escapeForXML(st.object.datatype.uri) + '"') + (st.object.language ? ' xml:lang="' + st.object.language + '"' : '') + '>' + escapeForXML(st.object.value) + '</' + t + '>']);
|
|
1073
913
|
break;
|
|
1074
|
-
|
|
1075
914
|
case 'Collection':
|
|
1076
915
|
results = results.concat(['<' + t + ' rdf:parseType="Collection">', collectionXMLTree(st.object, stats), '</' + t + '>']);
|
|
1077
916
|
break;
|
|
1078
|
-
|
|
1079
917
|
default:
|
|
1080
918
|
throw new Error("Can't serialize object of type " + st.object.termType + ' into XML');
|
|
1081
919
|
} // switch
|
|
1082
|
-
|
|
1083
920
|
}
|
|
1084
921
|
|
|
1085
922
|
var tag = type ? qname(type) : 'rdf:Description';
|
|
1086
923
|
var attrs = '';
|
|
1087
|
-
|
|
1088
924
|
if (subject.termType === 'BlankNode') {
|
|
1089
925
|
if (!stats.incoming[subject] || stats.incoming[subject].length !== 1) {
|
|
1090
926
|
// not an anonymous bnode
|
|
@@ -1093,34 +929,25 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
1093
929
|
} else {
|
|
1094
930
|
attrs = ' rdf:about="' + relURI(subject) + '"';
|
|
1095
931
|
}
|
|
1096
|
-
|
|
1097
932
|
return ['<' + tag + attrs + '>'].concat([results]).concat(['</' + tag + '>']);
|
|
1098
933
|
}
|
|
1099
|
-
|
|
1100
934
|
var subjectXMLTree = subjectXMLTreeMethod.bind(this);
|
|
1101
|
-
|
|
1102
935
|
function collectionXMLTree(subject, stats) {
|
|
1103
936
|
var res = [];
|
|
1104
|
-
|
|
1105
937
|
for (var i = 0; i < subject.elements.length; i++) {
|
|
1106
938
|
res.push(subjectXMLTree(subject.elements[i], stats));
|
|
1107
939
|
}
|
|
1108
|
-
|
|
1109
940
|
return res;
|
|
1110
|
-
}
|
|
1111
|
-
|
|
941
|
+
}
|
|
1112
942
|
|
|
943
|
+
// The property tree for a single subject or anonymos node
|
|
1113
944
|
function propertyXMLTreeMethod(subject, stats) {
|
|
1114
945
|
var results = [];
|
|
1115
946
|
var sts = stats.subjects[this.toStr(subject)]; // relevant statements
|
|
1116
|
-
|
|
1117
947
|
if (!sts) return results; // No relevant statements
|
|
1118
|
-
|
|
1119
948
|
sts.sort();
|
|
1120
|
-
|
|
1121
949
|
for (var i = 0; i < sts.length; i++) {
|
|
1122
950
|
var st = sts[i];
|
|
1123
|
-
|
|
1124
951
|
switch (st.object.termType) {
|
|
1125
952
|
case 'BlankNode':
|
|
1126
953
|
if (stats.rootsHash[st.object.toNT()]) {
|
|
@@ -1129,103 +956,101 @@ var Serializer = /*#__PURE__*/function () {
|
|
|
1129
956
|
} else {
|
|
1130
957
|
results = results.concat(['<' + qname(st.predicate) + ' rdf:parseType="Resource">', propertyXMLTree(st.object, stats), '</' + qname(st.predicate) + '>']);
|
|
1131
958
|
}
|
|
1132
|
-
|
|
1133
959
|
break;
|
|
1134
|
-
|
|
1135
960
|
case 'NamedNode':
|
|
1136
961
|
results = results.concat(['<' + qname(st.predicate) + ' rdf:resource="' + relURI(st.object) + '"/>']);
|
|
1137
962
|
break;
|
|
1138
|
-
|
|
1139
963
|
case 'Literal':
|
|
1140
964
|
results = results.concat(['<' + qname(st.predicate) + (st.object.datatype.equals(this.xsd.string) ? '' : ' rdf:datatype="' + escapeForXML(st.object.datatype.value) + '"') + (st.object.language ? ' xml:lang="' + st.object.language + '"' : '') + '>' + escapeForXML(st.object.value) + '</' + qname(st.predicate) + '>']);
|
|
1141
965
|
break;
|
|
1142
|
-
|
|
1143
966
|
case 'Collection':
|
|
1144
967
|
results = results.concat(['<' + qname(st.predicate) + ' rdf:parseType="Collection">', collectionXMLTree(st.object, stats), '</' + qname(st.predicate) + '>']);
|
|
1145
968
|
break;
|
|
1146
|
-
|
|
1147
969
|
default:
|
|
1148
970
|
throw new Error("Can't serialize object of type " + st.object.termType + ' into XML');
|
|
1149
971
|
} // switch
|
|
1150
|
-
|
|
1151
972
|
}
|
|
1152
973
|
|
|
1153
974
|
return results;
|
|
1154
975
|
}
|
|
1155
|
-
|
|
1156
976
|
var propertyXMLTree = propertyXMLTreeMethod.bind(this);
|
|
1157
|
-
|
|
1158
977
|
function qnameMethod(term) {
|
|
1159
978
|
var uri = term.uri;
|
|
1160
979
|
var j = uri.indexOf('#');
|
|
1161
|
-
|
|
1162
980
|
if (j < 0 && this.flags.indexOf('/') < 0) {
|
|
1163
981
|
j = uri.lastIndexOf('/');
|
|
1164
982
|
}
|
|
1165
|
-
|
|
1166
983
|
if (j < 0) throw new Error('Cannot make qname out of <' + uri + '>');
|
|
1167
|
-
|
|
1168
984
|
for (var k = j + 1; k < uri.length; k++) {
|
|
1169
985
|
if (this._notNameChars.indexOf(uri[k]) >= 0) {
|
|
1170
986
|
throw new Error('Invalid character "' + uri[k] + '" cannot be in XML qname for URI: ' + uri);
|
|
1171
987
|
}
|
|
1172
988
|
}
|
|
1173
|
-
|
|
1174
989
|
var localid = uri.slice(j + 1);
|
|
1175
990
|
var namesp = uri.slice(0, j + 1);
|
|
1176
|
-
|
|
1177
991
|
if (this.defaultNamespace && this.defaultNamespace === namesp && this.flags.indexOf('d') < 0) {
|
|
1178
992
|
// d -> suppress default
|
|
1179
993
|
return localid;
|
|
1180
994
|
}
|
|
1181
|
-
|
|
1182
995
|
var prefix = this.prefixes[namesp];
|
|
1183
996
|
if (!prefix) prefix = this.makeUpPrefix(namesp);
|
|
1184
997
|
namespaceCounts[namesp] = true;
|
|
1185
998
|
return prefix + ':' + localid;
|
|
1186
999
|
}
|
|
1000
|
+
var qname = qnameMethod.bind(this);
|
|
1187
1001
|
|
|
1188
|
-
|
|
1002
|
+
// Body of toXML:
|
|
1189
1003
|
|
|
1190
1004
|
var tree = statementListToXMLTree(sts);
|
|
1191
1005
|
var str = '<rdf:RDF';
|
|
1192
|
-
|
|
1193
1006
|
if (this.defaultNamespace) {
|
|
1194
1007
|
str += ' xmlns="' + escapeForXML(this.defaultNamespace) + '"';
|
|
1195
1008
|
}
|
|
1196
|
-
|
|
1197
1009
|
for (var ns in namespaceCounts) {
|
|
1198
|
-
if (!namespaceCounts.hasOwnProperty(ns)) continue;
|
|
1199
|
-
|
|
1010
|
+
if (!namespaceCounts.hasOwnProperty(ns)) continue;
|
|
1011
|
+
// Rel uris in xml ns is not strictly allowed in the XMLNS spec but needed in practice often
|
|
1200
1012
|
var ns2 = this.base && this.flags.includes('z') ? Util.uri.refTo(this.base, ns) : ns;
|
|
1201
1013
|
str += '\n xmlns:' + this.prefixes[ns] + '="' + escapeForXML(ns2) + '"';
|
|
1202
1014
|
}
|
|
1203
|
-
|
|
1204
1015
|
str += '>';
|
|
1205
1016
|
var tree2 = [str, tree, '</rdf:RDF>']; // @@ namespace declrations
|
|
1206
|
-
|
|
1207
1017
|
return XMLtreeToString(tree2, -1);
|
|
1208
1018
|
} // End @@ body
|
|
1209
|
-
|
|
1019
|
+
}, {
|
|
1020
|
+
key: "statementsToJsonld",
|
|
1021
|
+
value: function statementsToJsonld(sts) {
|
|
1022
|
+
// ttl2jsonld creates context keys for all ttl prefix
|
|
1023
|
+
// context keys must be full IRI
|
|
1024
|
+
function findId(itemObj) {
|
|
1025
|
+
if (itemObj['@id']) {
|
|
1026
|
+
var item = itemObj['@id'].split(':');
|
|
1027
|
+
if (keys[item[0]]) itemObj['@id'] = jsonldObj['@context'][item[0]] + item[1];
|
|
1028
|
+
}
|
|
1029
|
+
var itemValues = Object.values(itemObj);
|
|
1030
|
+
for (var i in itemValues) {
|
|
1031
|
+
if (typeof itemValues[i] !== 'string') {
|
|
1032
|
+
// @list contains array
|
|
1033
|
+
findId(itemValues[i]);
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
var turtleDoc = this.statementsToN3(sts);
|
|
1038
|
+
var jsonldObj = ttl2jsonld.parse(turtleDoc);
|
|
1039
|
+
return JSON.stringify(jsonldObj, null, 2);
|
|
1040
|
+
}
|
|
1210
1041
|
}]);
|
|
1211
1042
|
return Serializer;
|
|
1212
1043
|
}(); // String escaping utilities
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
1044
|
exports.Serializer = Serializer;
|
|
1216
|
-
|
|
1217
1045
|
function hexify(str) {
|
|
1218
1046
|
// also used in parser
|
|
1219
1047
|
return encodeURI(str);
|
|
1220
1048
|
}
|
|
1221
|
-
|
|
1222
1049
|
function backslashUify(str) {
|
|
1223
1050
|
var res = '';
|
|
1224
1051
|
var k;
|
|
1225
|
-
|
|
1226
1052
|
for (var i = 0; i < str.length; i++) {
|
|
1227
1053
|
k = str.charCodeAt(i);
|
|
1228
|
-
|
|
1229
1054
|
if (k > 65535) {
|
|
1230
1055
|
res += "\\U" + ('00000000' + k.toString(16)).slice(-8); // convert to upper?
|
|
1231
1056
|
} else if (k > 126) {
|
|
@@ -1234,6 +1059,5 @@ function backslashUify(str) {
|
|
|
1234
1059
|
res += str[i];
|
|
1235
1060
|
}
|
|
1236
1061
|
}
|
|
1237
|
-
|
|
1238
1062
|
return res;
|
|
1239
1063
|
}
|