rdflib 2.2.34 → 2.2.35-2bb9ed0b

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