rdflib 2.2.17 → 2.2.18-ce0e0a33

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/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