rdflib 2.2.17 → 2.2.18-12f6a201

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.
package/lib/serializer.js CHANGED
@@ -122,6 +122,50 @@ var Serializer = /*#__PURE__*/function () {
122
122
 
123
123
  return this.store.fromNT(s);
124
124
  }
125
+ /**
126
+ * Defines a set of [prefix, namespace] pairs to be uised by this Serializer instance.
127
+ * Overrides previous prefixes if any
128
+ * @param namespaces
129
+ * @return {Serializer}
130
+ */
131
+
132
+ }, {
133
+ key: "setNamespaces",
134
+ value: function setNamespaces(namespaces) {
135
+ for (var px in namespaces) {
136
+ this.setPrefix(px, namespaces[px]);
137
+ }
138
+
139
+ return this;
140
+ }
141
+ /**
142
+ * Defines a namespace prefix, overriding any existing prefix for that URI
143
+ * @param prefix
144
+ * @param uri
145
+ */
146
+
147
+ }, {
148
+ key: "setPrefix",
149
+ value: function setPrefix(prefix, uri) {
150
+ if (prefix.slice(0, 7) === 'default') return; // Try to weed these out
151
+
152
+ if (prefix.slice(0, 2) === 'ns') return; // From others inferior algos
153
+
154
+ if (!prefix || !uri) return; // empty strings not suitable
155
+ // remove any existing prefix targeting this uri
156
+ // for (let existingPrefix in this.namespaces) {
157
+ // if (this.namespaces[existingPrefix] == uri)
158
+ // delete this.namespaces[existingPrefix];
159
+ // }
160
+ // remove any existing mapping for this prefix
161
+
162
+ for (var existingNs in this.prefixes) {
163
+ if (this.prefixes[existingNs] == prefix) delete this.prefixes[existingNs];
164
+ }
165
+
166
+ this.prefixes[uri] = prefix;
167
+ this.namespaces[prefix] = uri;
168
+ }
125
169
  /* Accumulate Namespaces
126
170
  **
127
171
  ** These are only hints. If two overlap, only one gets used
@@ -405,13 +449,13 @@ var Serializer = /*#__PURE__*/function () {
405
449
 
406
450
  for (var i = 0; i < tree.length; i++) {
407
451
  var branch = tree[i];
408
- var s2 = typeof branch === 'string' ? branch : treeToLine(branch); // Note the space before the dot in case statement ends 123. which is in fact allowed but be conservative.
452
+ var s2 = typeof branch === 'string' ? branch : treeToLine(branch); // Note the space before the dot in case statement ends with 123 or colon. which is in fact allowed but be conservative.
409
453
 
410
454
  if (i !== 0) {
411
455
  var ch = str.slice(-1) || ' ';
412
456
 
413
457
  if (s2 === ',' || s2 === ';') {// no gap
414
- } else if (s2 === '.' && !'0123456789.'.includes(ch)) {// no gap except after number
458
+ } else if (s2 === '.' && !'0123456789.:'.includes(ch)) {// no gap except after number and colon
415
459
  // no gap
416
460
  } else {
417
461
  str += ' '; // separate from previous token
@@ -454,7 +498,14 @@ var Serializer = /*#__PURE__*/function () {
454
498
  if (typeof branch === 'string') {
455
499
  if (branch.length === 1 && str.slice(-1) === '\n') {
456
500
  if (',.;'.indexOf(branch) >= 0) {
457
- str = str.slice(0, -1) + branch + '\n'; // slip punct'n on end
501
+ str = str.slice(0, -1); // be conservative and ensure a whitespace between some chars and a final dot, as in treeToLine above
502
+
503
+ if (branch == '.' && '0123456789.:'.includes(str.charAt(str.length - 1))) {
504
+ str += ' ';
505
+ lastLength += 1;
506
+ }
507
+
508
+ str += branch + '\n'; // slip punct'n on end
458
509
 
459
510
  lastLength += 1;
460
511
  continue;
package/lib/store.d.ts CHANGED
@@ -299,4 +299,5 @@ export default class IndexedFormula extends Formula {
299
299
  * @param term
300
300
  */
301
301
  uris(term: Quad_Subject): string[];
302
+ serialize(base: any, contentType: any, provenance: any, options?: any): string | undefined;
302
303
  }
package/lib/store.js CHANGED
@@ -51,12 +51,18 @@ var _namedNode = _interopRequireDefault(require("./named-node"));
51
51
 
52
52
  var _index = require("./index");
53
53
 
54
+ var _serialize2 = _interopRequireDefault(require("./serialize"));
55
+
54
56
  var _blankNode = _interopRequireDefault(require("./blank-node"));
55
57
 
56
58
  var _defaultGraph = _interopRequireDefault(require("./default-graph"));
57
59
 
58
60
  var _literal = _interopRequireDefault(require("./literal"));
59
61
 
62
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
63
+
64
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
65
+
60
66
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
61
67
 
62
68
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
@@ -1159,6 +1165,11 @@ var IndexedFormula = /*#__PURE__*/function (_Formula) {
1159
1165
 
1160
1166
  if (prefix.slice(0, 2) === 'ns' || prefix.slice(0, 7) === 'default') {
1161
1167
  return;
1168
+ } // remove any prefix that currently targets nsuri
1169
+
1170
+
1171
+ for (var existingPrefix in this.namespaces) {
1172
+ if (this.namespaces[existingPrefix] == nsuri) delete this.namespaces[existingPrefix];
1162
1173
  }
1163
1174
 
1164
1175
  this.namespaces[prefix] = nsuri;
@@ -1291,6 +1302,20 @@ var IndexedFormula = /*#__PURE__*/function (_Formula) {
1291
1302
 
1292
1303
  return res;
1293
1304
  }
1305
+ }, {
1306
+ key: "serialize",
1307
+ value: function serialize(base, contentType, provenance, options) {
1308
+ var _options;
1309
+
1310
+ // override Formula.serialize to force the serializer namespace prefixes
1311
+ // to those of this IndexedFormula
1312
+ // if namespaces are explicitly passed in options, let them override the existing namespaces in this formula
1313
+ var namespaces = (_options = options) !== null && _options !== void 0 && _options.namespaces ? _objectSpread(_objectSpread({}, this.namespaces), options.namespaces) : _objectSpread({}, this.namespaces);
1314
+ options = _objectSpread(_objectSpread({}, options || {}), {}, {
1315
+ namespaces: namespaces
1316
+ });
1317
+ return (0, _serialize2.default)(provenance, this, base, contentType, undefined, options);
1318
+ }
1294
1319
  }], [{
1295
1320
  key: "defaultGraphURI",
1296
1321
  get: function get() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rdflib",
3
3
  "description": "an RDF library for node.js. Suitable for client and server side.",
4
- "version": "2.2.17",
4
+ "version": "2.2.18-12f6a201",
5
5
  "engines": {
6
6
  "node": ">=6.0"
7
7
  },
package/src/formula.ts CHANGED
@@ -5,7 +5,7 @@ import log from './log'
5
5
  import RDFlibNamedNode from './named-node'
6
6
  import Namespace from './namespace'
7
7
  import Node from './node-internal'
8
- import Serializer from './serialize'
8
+ import serialize from './serialize'
9
9
  import Statement from './statement'
10
10
  import {
11
11
  Bindings,
@@ -721,33 +721,11 @@ export default class Formula extends Node {
721
721
  * @param base - The base string
722
722
  * @param contentType - The content type of the syntax to use
723
723
  * @param provenance - The provenance URI
724
+ * @param options - options to pass to the serializer, as defined in serialize method
724
725
  */
725
- serialize (base, contentType, provenance) {
726
- let documentString
727
- let sts
728
- let sz
729
- sz = Serializer(this)
730
- sz.suggestNamespaces(this.ns)
731
- sz.setBase(base)
732
- if (provenance) {
733
- sts = this.statementsMatching(void 0, void 0, void 0, provenance)
734
- } else {
735
- sts = this.statements
736
- }
737
- switch (
738
- contentType != null ? contentType : 'text/n3') {
739
- case 'application/rdf+xml':
740
- documentString = sz.statementsToXML(sts)
741
- break
742
- case 'text/n3':
743
- case 'text/turtle':
744
- documentString = sz.statementsToN3(sts)
745
- break
746
- default:
747
- throw new Error('serialize: Content-type ' + contentType +
748
- ' not supported.')
749
- }
750
- return documentString
726
+ serialize (base, contentType, provenance, options?) {
727
+ // delegate the graph serialization to the implementation in ./serialize
728
+ return serialize(provenance, this, base, contentType, undefined, options);
751
729
  }
752
730
 
753
731
  /**
package/src/index.ts CHANGED
@@ -120,6 +120,9 @@ export {
120
120
  export { termValue } from './utils/termValue'
121
121
 
122
122
  export class ConnectedStore extends Store {
123
+
124
+ fetcher: Fetcher;
125
+
123
126
  constructor (features) {
124
127
  super(features)
125
128
  this.fetcher = new Fetcher(this, {})
@@ -127,6 +130,9 @@ export class ConnectedStore extends Store {
127
130
  }
128
131
 
129
132
  export class LiveStore extends ConnectedStore {
133
+
134
+ updater: UpdateManager;
135
+
130
136
  constructor (features) {
131
137
  super(features)
132
138
  this.updater = new UpdateManager(this)
@@ -73,23 +73,8 @@ export default function jsonldParser (str, kb, base, callback) {
73
73
  return jsonld
74
74
  .flatten(JSON.parse(str), null, { base: baseString })
75
75
  .then((flattened) => flattened.reduce((store, flatResource) => {
76
- const id = flatResource['@id']
77
- ? kb.rdfFactory.namedNode(flatResource['@id'])
78
- : kb.rdfFactory.blankNode()
79
-
80
- for (const property of Object.keys(flatResource)) {
81
- if (property === '@id') {
82
- continue
83
- }
84
- const value = flatResource[property]
85
- if (Array.isArray(value)) {
86
- for (let i = 0; i < value.length; i++) {
87
- kb.addStatement(createStatement(kb, id, property, value[i], base))
88
- }
89
- } else {
90
- kb.addStatement(createStatement(kb, id, property, value, base))
91
- }
92
- }
76
+
77
+ kb = processResource(kb, base, flatResource)
93
78
 
94
79
  return kb
95
80
  }, kb))
@@ -97,6 +82,41 @@ export default function jsonldParser (str, kb, base, callback) {
97
82
  .catch(callback)
98
83
  }
99
84
 
85
+
86
+ function processResource(kb, base, flatResource) {
87
+ const id = flatResource['@id']
88
+ ? kb.rdfFactory.namedNode(flatResource['@id'])
89
+ : kb.rdfFactory.blankNode()
90
+
91
+ for (const property of Object.keys(flatResource)) {
92
+ if (property === '@id') {
93
+ continue
94
+ } else if (property == '@graph') {
95
+ // the JSON-LD flattened structure may contain nested graphs
96
+ // the id value for this object is the new base (named graph id) for all nested flat resources
97
+ const graphId = id
98
+ // this is an array of resources
99
+ const nestedFlatResources = flatResource[property]
100
+
101
+ // recursively process all flat resources in the array, but with the graphId as base.
102
+ for (let i = 0; i < nestedFlatResources.length; i++ ) {
103
+ kb = processResource(kb, graphId, nestedFlatResources[i])
104
+ }
105
+ }
106
+
107
+ const value = flatResource[property]
108
+ if (Array.isArray(value)) {
109
+ for (let i = 0; i < value.length; i++) {
110
+ kb.addStatement(createStatement(kb, id, property, value[i], base))
111
+ }
112
+ } else {
113
+ kb.addStatement(createStatement(kb, id, property, value, base))
114
+ }
115
+ }
116
+
117
+ return kb
118
+ }
119
+
100
120
  /**
101
121
  * Create statement quad depending on @type being a type node
102
122
  * @param kb
@@ -107,6 +127,7 @@ export default function jsonldParser (str, kb, base, callback) {
107
127
  */
108
128
  function createStatement(kb, id, property, value, base) {
109
129
  let predicate, object
130
+
110
131
  if (property === "@type") {
111
132
  predicate = kb.rdfFactory.namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
112
133
  object = kb.rdfFactory.namedNode(value)
@@ -198,7 +198,7 @@ export default class RDFParser {
198
198
 
199
199
  /**
200
200
  * Build our initial scope frame and parse the DOM into triples
201
- * @param {DOMTree} document The DOM to parse
201
+ * @param {HTMLDocument} document The DOM to parse
202
202
  * @param {String} base The base URL to use
203
203
  * @param {Object} why The context to which this resource belongs
204
204
  */
package/src/serialize.ts CHANGED
@@ -21,9 +21,9 @@ import { BlankNode, NamedNode } from './tf-types'
21
21
  */
22
22
  export default function serialize (
23
23
  /** The graph or nodes that should be serialized */
24
- target: Formula | NamedNode | BlankNode,
24
+ target: Formula | NamedNode | BlankNode | null,
25
25
  /** The store */
26
- kb?: IndexedFormula,
26
+ kb: Formula,
27
27
  base?: unknown,
28
28
  /**
29
29
  * The mime type.
@@ -36,10 +36,14 @@ export default function serialize (
36
36
  * A string of letters, each of which set an options
37
37
  * e.g. `deinprstux`
38
38
  */
39
- flags: string
39
+ flags?: string,
40
+ /**
41
+ * A set of [prefix, uri] pairs that define namespace prefixes
42
+ */
43
+ namespaces?: Record<string, string>
40
44
  }
41
45
  ): string | undefined {
42
- base = base || target.value
46
+ base = base || target?.value
43
47
  const opts = options || {}
44
48
  contentType = contentType || TurtleContentType // text/n3 if complex?
45
49
  var documentString: string | undefined = undefined
@@ -48,7 +52,17 @@ export default function serialize (
48
52
  if ((opts as any).flags) sz.setFlags((opts as any).flags)
49
53
  var newSts = kb!.statementsMatching(undefined, undefined, undefined, target as NamedNode)
50
54
  var n3String: string
51
- sz.suggestNamespaces(kb!.namespaces)
55
+
56
+ // If an IndexedFormula, use the namespaces from the given graph as suggestions
57
+ if ('namespaces' in kb) {
58
+ sz.suggestNamespaces( (kb as IndexedFormula).namespaces);
59
+ }
60
+
61
+ // use the provided options.namespaces are mandatory prefixes
62
+ if (opts.namespaces) {
63
+ sz.setNamespaces( opts.namespaces);
64
+ }
65
+
52
66
  sz.setBase(base)
53
67
  switch (contentType) {
54
68
  case RDFXMLContentType:
package/src/serializer.js CHANGED
@@ -73,6 +73,46 @@ export class Serializer {
73
73
  return this.store.fromNT(s)
74
74
  }
75
75
 
76
+ /**
77
+ * Defines a set of [prefix, namespace] pairs to be uised by this Serializer instance.
78
+ * Overrides previous prefixes if any
79
+ * @param namespaces
80
+ * @return {Serializer}
81
+ */
82
+ setNamespaces(namespaces) {
83
+ for (var px in namespaces) {
84
+ this.setPrefix(px, namespaces[px])
85
+ }
86
+ return this
87
+ }
88
+
89
+ /**
90
+ * Defines a namespace prefix, overriding any existing prefix for that URI
91
+ * @param prefix
92
+ * @param uri
93
+ */
94
+ setPrefix(prefix, uri) {
95
+ if (prefix.slice(0, 7) === 'default') return // Try to weed these out
96
+ if (prefix.slice(0, 2) === 'ns') return // From others inferior algos
97
+ if (!prefix || !uri) return // empty strings not suitable
98
+
99
+ // remove any existing prefix targeting this uri
100
+ // for (let existingPrefix in this.namespaces) {
101
+ // if (this.namespaces[existingPrefix] == uri)
102
+ // delete this.namespaces[existingPrefix];
103
+ // }
104
+
105
+ // remove any existing mapping for this prefix
106
+ for (let existingNs in this.prefixes) {
107
+ if (this.prefixes[existingNs] == prefix)
108
+ delete this.prefixes[existingNs];
109
+ }
110
+
111
+ this.prefixes[uri] = prefix
112
+ this.namespaces[prefix] = uri
113
+ }
114
+
115
+
76
116
  /* Accumulate Namespaces
77
117
  **
78
118
  ** These are only hints. If two overlap, only one gets used
@@ -300,12 +340,12 @@ export class Serializer {
300
340
  for (var i = 0; i < tree.length; i++) {
301
341
  var branch = tree[i]
302
342
  var s2 = (typeof branch === 'string') ? branch : treeToLine(branch)
303
- // Note the space before the dot in case statement ends 123. which is in fact allowed but be conservative.
343
+ // Note the space before the dot in case statement ends with 123 or colon. which is in fact allowed but be conservative.
304
344
  if (i !== 0) {
305
345
  var ch = str.slice(-1) || ' '
306
346
  if (s2 === ',' || s2 === ';') {
307
347
  // no gap
308
- } else if (s2 === '.' && !('0123456789.'.includes(ch))) { // no gap except after number
348
+ } else if (s2 === '.' && !('0123456789.:'.includes(ch))) { // no gap except after number and colon
309
349
  // no gap
310
350
  } else {
311
351
  str += ' ' // separate from previous token
@@ -340,7 +380,13 @@ export class Serializer {
340
380
  if (typeof branch === 'string') {
341
381
  if (branch.length === 1 && str.slice(-1) === '\n') {
342
382
  if (',.;'.indexOf(branch) >= 0) {
343
- str = str.slice(0, -1) + branch + '\n' // slip punct'n on end
383
+ str = str.slice(0, -1)
384
+ // be conservative and ensure a whitespace between some chars and a final dot, as in treeToLine above
385
+ if (branch == '.' && '0123456789.:'.includes(str.charAt(str.length-1))) {
386
+ str += ' '
387
+ lastLength += 1
388
+ }
389
+ str += branch + '\n' // slip punct'n on end
344
390
  lastLength += 1
345
391
  continue
346
392
  }
package/src/store.ts CHANGED
@@ -52,6 +52,7 @@ import {
52
52
  Term,
53
53
  } from './tf-types'
54
54
  import { namedNode } from './index'
55
+ import serialize from "./serialize";
55
56
  import BlankNode from './blank-node'
56
57
  import DefaultGraph from './default-graph'
57
58
  import Empty from './empty'
@@ -1046,6 +1047,13 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
1046
1047
  if (prefix.slice(0, 2) === 'ns' || prefix.slice(0, 7) === 'default') {
1047
1048
  return
1048
1049
  }
1050
+
1051
+ // remove any prefix that currently targets nsuri
1052
+ for (let existingPrefix in this.namespaces) {
1053
+ if (this.namespaces[existingPrefix] == nsuri)
1054
+ delete this.namespaces[existingPrefix];
1055
+ }
1056
+
1049
1057
  this.namespaces[prefix] = nsuri
1050
1058
  }
1051
1059
 
@@ -1154,5 +1162,20 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
1154
1162
  }
1155
1163
  return res
1156
1164
  }
1165
+
1166
+ serialize (base, contentType, provenance, options?) {
1167
+
1168
+ // override Formula.serialize to force the serializer namespace prefixes
1169
+ // to those of this IndexedFormula
1170
+
1171
+ // if namespaces are explicitly passed in options, let them override the existing namespaces in this formula
1172
+ const namespaces = options?.namespaces ? {...this.namespaces, ...options.namespaces} : {...this.namespaces};
1173
+
1174
+ options = {
1175
+ ...(options || {}),
1176
+ namespaces
1177
+ }
1178
+ return serialize(provenance, this, base, contentType, undefined, options);
1179
+ }
1157
1180
  }
1158
1181
  IndexedFormula.handleRDFType = handleRDFType