rdflib 2.2.22-2f49e8ef → 2.2.22-53d65d90

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