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