rdflib 2.2.35 → 2.2.36

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