rdflib 2.2.10 → 2.2.12-5f141ca2

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