@faubulous/mentor-rdf-serializers 1.9.3 → 1.10.0
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/quad-serializer-base.d.ts +19 -1
- package/dist/quad-serializer-base.d.ts.map +1 -1
- package/dist/quad-serializer-base.js +27 -59
- package/dist/quad-serializer-base.js.map +1 -1
- package/dist/term-serializer.d.ts +172 -0
- package/dist/term-serializer.d.ts.map +1 -0
- package/dist/term-serializer.js +208 -0
- package/dist/term-serializer.js.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export * from './quad-sorting-strategy';
|
|
|
8
8
|
export * from './quad-sorter';
|
|
9
9
|
export * from './serialization-result';
|
|
10
10
|
export * from './serialization-options';
|
|
11
|
+
export * from './term-serializer';
|
|
11
12
|
export * from './quad-context-serializer';
|
|
12
13
|
export * from './token-formatter-base';
|
|
13
14
|
export * from './token-formatter.interface';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,wBAAwB,CAAC;AACvC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,eAAe,CAAC;AAC9B,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,8BAA8B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,wBAAwB,CAAC;AACvC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,eAAe,CAAC;AAC9B,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,8BAA8B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export * from './quad-sorting-strategy';
|
|
|
8
8
|
export * from './quad-sorter';
|
|
9
9
|
export * from './serialization-result';
|
|
10
10
|
export * from './serialization-options';
|
|
11
|
+
export * from './term-serializer';
|
|
11
12
|
export * from './quad-context-serializer';
|
|
12
13
|
export * from './token-formatter-base';
|
|
13
14
|
export * from './token-formatter.interface';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,wBAAwB,CAAC;AACvC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,eAAe,CAAC;AAC9B,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,8BAA8B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,wBAAwB,CAAC;AACvC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,eAAe,CAAC;AAC9B,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,8BAA8B,CAAC"}
|
|
@@ -4,6 +4,7 @@ import { Rdf12Quad, Rdf12Term, TripleTerm } from './utilities/types';
|
|
|
4
4
|
import { IQuadSerializer } from './quad-serializer.interface';
|
|
5
5
|
import { SerializationResult } from './serialization-result';
|
|
6
6
|
import { SerializationOptions } from './serialization-options';
|
|
7
|
+
import { TermSerializerSettings } from './term-serializer';
|
|
7
8
|
/**
|
|
8
9
|
* Abstract base class for RDF serializers. Provides common functionality
|
|
9
10
|
* for serializing terms and literals.
|
|
@@ -25,6 +26,15 @@ export declare abstract class QuadSerializerBase implements IQuadSerializer {
|
|
|
25
26
|
* Whether this serializer supports RDF 1.2 features (triple terms, reifiers, annotations).
|
|
26
27
|
*/
|
|
27
28
|
protected supportsRdf12: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* The settings the terms of the current serialization run are written with, resolved from
|
|
31
|
+
* the capabilities of this serializer and the options the hooks are called with.
|
|
32
|
+
*/
|
|
33
|
+
private _termSettings?;
|
|
34
|
+
/**
|
|
35
|
+
* The options the term settings were resolved from.
|
|
36
|
+
*/
|
|
37
|
+
private _termOptions?;
|
|
28
38
|
/**
|
|
29
39
|
* Serializes a single quad to a string.
|
|
30
40
|
*/
|
|
@@ -44,11 +54,19 @@ export declare abstract class QuadSerializerBase implements IQuadSerializer {
|
|
|
44
54
|
/**
|
|
45
55
|
* Serializes a blank node.
|
|
46
56
|
*/
|
|
47
|
-
protected serializeBlankNode(node: BlankNode,
|
|
57
|
+
protected serializeBlankNode(node: BlankNode, options: Required<SerializationOptions>): string;
|
|
48
58
|
/**
|
|
49
59
|
* Serializes a literal value.
|
|
50
60
|
*/
|
|
51
61
|
protected serializeLiteral(literal: Literal, options: Required<SerializationOptions>): string;
|
|
62
|
+
/**
|
|
63
|
+
* Gets the settings the terms are written with for a set of options. Some serializers merge
|
|
64
|
+
* the options anew for every quad, so a run is told apart by the options object the hooks
|
|
65
|
+
* are called with; resolving the settings again is cheap.
|
|
66
|
+
* @param options The merged options of the run.
|
|
67
|
+
* @returns The term settings.
|
|
68
|
+
*/
|
|
69
|
+
protected termSettingsFor(options: Required<SerializationOptions>): TermSerializerSettings;
|
|
52
70
|
/**
|
|
53
71
|
* Serializes a SPARQL variable.
|
|
54
72
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quad-serializer-base.d.ts","sourceRoot":"","sources":["../src/quad-serializer-base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACnF,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"quad-serializer-base.d.ts","sourceRoot":"","sources":["../src/quad-serializer-base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAC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AAI,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAC,GAAG,MAAM;IAI9F;;OAEG;IACH,SAAS,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAC,GAAG,MAAM;IAI7F;;;;;;OAMG;IACH,SAAS,CAAC,eAAe,CAAC,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAC,GAAG,sBAAsB;IAY1F;;OAEG;IACH,SAAS,CAAC,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAC,oBAAoB,CAAC,GAAG,MAAM;IAIjG;;OAEG;IACH,SAAS,CAAC,mBAAmB,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAC,GAAG,MAAM;IAYtG;;OAEG;IACH,SAAS,CAAC,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,SAAS,EAAE,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAC,GAAG,MAAM;IAmBhG;;OAEG;IACH,SAAS,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,oBAAoB,GAAG,QAAQ,CAAC,oBAAoB,CAAC;CAGvF;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,CAAC,EAAE,oBAAoB,GAAG,QAAQ,CAAC,oBAAoB,CAAC,CAS3F"}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import { RDF, XSD } from './ontologies';
|
|
2
1
|
import { DEFAULT_OPTIONS } from './serialization-options';
|
|
3
|
-
import {
|
|
4
|
-
import { isInteger, isDecimal, isDouble, needsLongString } from "./utilities/literals";
|
|
5
|
-
import { normalizeBlankNodeId } from './utilities/blank-nodes';
|
|
6
|
-
import { findPrefix } from "./utilities/prefixes";
|
|
2
|
+
import { TermSerializer } from './term-serializer';
|
|
7
3
|
/**
|
|
8
4
|
* Abstract base class for RDF serializers. Provides common functionality
|
|
9
5
|
* for serializing terms and literals.
|
|
@@ -21,6 +17,15 @@ export class QuadSerializerBase {
|
|
|
21
17
|
* Whether this serializer supports RDF 1.2 features (triple terms, reifiers, annotations).
|
|
22
18
|
*/
|
|
23
19
|
supportsRdf12 = false;
|
|
20
|
+
/**
|
|
21
|
+
* The settings the terms of the current serialization run are written with, resolved from
|
|
22
|
+
* the capabilities of this serializer and the options the hooks are called with.
|
|
23
|
+
*/
|
|
24
|
+
_termSettings;
|
|
25
|
+
/**
|
|
26
|
+
* The options the term settings were resolved from.
|
|
27
|
+
*/
|
|
28
|
+
_termOptions;
|
|
24
29
|
/**
|
|
25
30
|
* Serializes quads with formatting, returning detailed result.
|
|
26
31
|
*/
|
|
@@ -33,70 +38,33 @@ export class QuadSerializerBase {
|
|
|
33
38
|
* Serializes a named node (IRI).
|
|
34
39
|
*/
|
|
35
40
|
serializeNamedNode(node, options) {
|
|
36
|
-
|
|
37
|
-
if (this.supportsRdfTypeShorthand && options.useRdfTypeShorthand && node.value === RDF.type) {
|
|
38
|
-
return 'a';
|
|
39
|
-
}
|
|
40
|
-
if (this.supportsPrefixes && Object.keys(options.prefixes).length > 0) {
|
|
41
|
-
const prefixMatch = findPrefix(node.value, options.prefixes);
|
|
42
|
-
if (prefixMatch) {
|
|
43
|
-
const escapedLocalName = escapeLocalName(prefixMatch.localName);
|
|
44
|
-
return `${prefixMatch.prefix}:${escapedLocalName}`;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return `<${escapeIri(node.value)}>`;
|
|
41
|
+
return TermSerializer.serializeNamedNode(node, this.termSettingsFor(options), true);
|
|
48
42
|
}
|
|
49
43
|
/**
|
|
50
44
|
* Serializes a blank node.
|
|
51
45
|
*/
|
|
52
|
-
serializeBlankNode(node,
|
|
53
|
-
return
|
|
46
|
+
serializeBlankNode(node, options) {
|
|
47
|
+
return TermSerializer.serializeBlankNode(node, this.termSettingsFor(options));
|
|
54
48
|
}
|
|
55
49
|
/**
|
|
56
50
|
* Serializes a literal value.
|
|
57
51
|
*/
|
|
58
52
|
serializeLiteral(literal, options) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
if (this.supportsPrefixes) {
|
|
75
|
-
if (datatype === XSD.integer && isInteger(value)) {
|
|
76
|
-
return value;
|
|
77
|
-
}
|
|
78
|
-
if (datatype === XSD.decimal && isDecimal(value)) {
|
|
79
|
-
return value;
|
|
80
|
-
}
|
|
81
|
-
if (datatype === XSD.double && isDouble(value)) {
|
|
82
|
-
return value;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
// Check if we need long string quoting
|
|
86
|
-
const useLongString = this.supportsPrefixes && needsLongString(value);
|
|
87
|
-
const quote = useLongString ? '"""' : '"';
|
|
88
|
-
const escapedValue = escapeString(value, useLongString);
|
|
89
|
-
// xsd:string is the default, no need to serialize it
|
|
90
|
-
if (!datatype || datatype === XSD.string) {
|
|
91
|
-
return `${quote}${escapedValue}${quote}`;
|
|
53
|
+
return TermSerializer.serializeLiteral(literal, this.termSettingsFor(options));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Gets the settings the terms are written with for a set of options. Some serializers merge
|
|
57
|
+
* the options anew for every quad, so a run is told apart by the options object the hooks
|
|
58
|
+
* are called with; resolving the settings again is cheap.
|
|
59
|
+
* @param options The merged options of the run.
|
|
60
|
+
* @returns The term settings.
|
|
61
|
+
*/
|
|
62
|
+
termSettingsFor(options) {
|
|
63
|
+
if (options !== this._termOptions || !this._termSettings) {
|
|
64
|
+
this._termSettings = TermSerializer.settingsOf({ supportsPrefixes: this.supportsPrefixes, supportsRdfTypeShorthand: this.supportsRdfTypeShorthand }, { prefixes: options.prefixes, useRdfTypeShorthand: options.useRdfTypeShorthand });
|
|
65
|
+
this._termOptions = options;
|
|
92
66
|
}
|
|
93
|
-
|
|
94
|
-
const datatypeStr = this.serializeNamedNode({
|
|
95
|
-
termType: 'NamedNode',
|
|
96
|
-
value: datatype,
|
|
97
|
-
equals: () => false
|
|
98
|
-
}, options);
|
|
99
|
-
return `${quote}${escapedValue}${quote}^^${datatypeStr}`;
|
|
67
|
+
return this._termSettings;
|
|
100
68
|
}
|
|
101
69
|
/**
|
|
102
70
|
* Serializes a SPARQL variable.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quad-serializer-base.js","sourceRoot":"","sources":["../src/quad-serializer-base.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"quad-serializer-base.js","sourceRoot":"","sources":["../src/quad-serializer-base.ts"],"names":[],"mappings":"AAKA,OAAO,EAAwB,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAChF,OAAO,EAAE,cAAc,EAA0B,MAAM,mBAAmB,CAAC;AAE3E;;;GAGG;AACH,MAAM,OAAgB,kBAAkB;IAMpC;;OAEG;IACO,gBAAgB,GAAY,KAAK,CAAC;IAE5C;;OAEG;IACO,wBAAwB,GAAY,KAAK,CAAC;IAEpD;;OAEG;IACO,aAAa,GAAY,KAAK,CAAC;IAEzC;;;OAGG;IACK,aAAa,CAA0B;IAE/C;;OAEG;IACK,YAAY,CAAkC;IAYtD;;OAEG;IACH,MAAM,CAAC,KAAiC,EAAE,OAA8B;QACpE,OAAO;YACH,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC;SACzC,CAAC;IACN,CAAC;IAED;;OAEG;IACO,kBAAkB,CAAC,IAAe,EAAE,OAAuC;QACjF,OAAO,cAAc,CAAC,kBAAkB,CAAC,IAAI,EAAE,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,CAAC;IACxF,CAAC;IAED;;OAEG;IACO,kBAAkB,CAAC,IAAe,EAAE,OAAuC;QACjF,O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lankNode, Literal, NamedNode, Term } from '@rdfjs/types';
|
|
3
|
+
/**
|
|
4
|
+
* What a syntax allows a term serializer to abbreviate.
|
|
5
|
+
*/
|
|
6
|
+
export interface TermSerializerCapabilities {
|
|
7
|
+
/**
|
|
8
|
+
* Whether IRIs may be written as prefixed names and numbers, booleans and multi-line
|
|
9
|
+
* strings in their bare or long forms.
|
|
10
|
+
*/
|
|
11
|
+
supportsPrefixes: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Whether `rdf:type` may be written as `a` in predicate position.
|
|
14
|
+
*/
|
|
15
|
+
supportsRdfTypeShorthand: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Options of a term serializer.
|
|
19
|
+
*/
|
|
20
|
+
export interface TermSerializerOptions {
|
|
21
|
+
/**
|
|
22
|
+
* Prefix mappings for compact IRI serialization, prefix name to namespace IRI.
|
|
23
|
+
*/
|
|
24
|
+
prefixes?: Record<string, string>;
|
|
25
|
+
/**
|
|
26
|
+
* Whether to write `rdf:type` as `a` in predicate position. Default: true.
|
|
27
|
+
*/
|
|
28
|
+
useRdfTypeShorthand?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* The labels to write for blank nodes, by identifier, such as the labels a document already
|
|
31
|
+
* uses. A blank node without one is written as `_:` with its normalized identifier.
|
|
32
|
+
*/
|
|
33
|
+
blankNodeLabels?: Record<string, string>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Everything the serialization of a term depends on: the capabilities of the syntax and the
|
|
37
|
+
* options of the run, resolved to plain values.
|
|
38
|
+
*/
|
|
39
|
+
export interface TermSerializerSettings {
|
|
40
|
+
/**
|
|
41
|
+
* Whether IRIs may be written as prefixed names and literals in their bare forms.
|
|
42
|
+
*/
|
|
43
|
+
supportsPrefixes: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Whether `rdf:type` may be written as `a` in predicate position.
|
|
46
|
+
*/
|
|
47
|
+
supportsRdfTypeShorthand: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Prefix name to namespace IRI.
|
|
50
|
+
*/
|
|
51
|
+
prefixes: Record<string, string>;
|
|
52
|
+
/**
|
|
53
|
+
* Whether to write `rdf:type` as `a` in predicate position.
|
|
54
|
+
*/
|
|
55
|
+
useRdfTypeShorthand: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* The labels to write for blank nodes, by identifier, if any.
|
|
58
|
+
*/
|
|
59
|
+
blankNodeLabels?: Record<string, string>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Serializes single RDF terms the way a document of a syntax writes them: prefixed names where
|
|
63
|
+
* a prefix is declared, `a` for `rdf:type` in predicate position, bare numbers and booleans, and
|
|
64
|
+
* long strings for multi-line text where the syntax allows it. The quad serializers build on the
|
|
65
|
+
* static methods, which take the settings of a run; the instance methods serve code that writes
|
|
66
|
+
* terms into a document, such as an editor.
|
|
67
|
+
*/
|
|
68
|
+
export declare class TermSerializer {
|
|
69
|
+
private readonly _capabilities;
|
|
70
|
+
private _settings;
|
|
71
|
+
/**
|
|
72
|
+
* Create a term serializer.
|
|
73
|
+
* @param capabilities What the target syntax allows.
|
|
74
|
+
* @param options The serialization options.
|
|
75
|
+
*/
|
|
76
|
+
constructor(capabilities: TermSerializerCapabilities, options?: TermSerializerOptions);
|
|
77
|
+
/**
|
|
78
|
+
* Create a term serializer for a syntax.
|
|
79
|
+
* @param syntax The syntax the terms are written in.
|
|
80
|
+
* @param options The serialization options.
|
|
81
|
+
* @returns A term serializer with the capabilities of the syntax.
|
|
82
|
+
*/
|
|
83
|
+
static forSyntax(syntax: RdfSyntax, options?: TermSerializerOptions): TermSerializer;
|
|
84
|
+
/**
|
|
85
|
+
* Resolve capabilities and options to the settings of a run.
|
|
86
|
+
* @param capabilities What the target syntax allows.
|
|
87
|
+
* @param options The serialization options.
|
|
88
|
+
* @returns The settings.
|
|
89
|
+
*/
|
|
90
|
+
static settingsOf(capabilities: TermSerializerCapabilities, options?: TermSerializerOptions): TermSerializerSettings;
|
|
91
|
+
/**
|
|
92
|
+
* Serialize a named node as a prefixed name when a prefix matches, or as an IRI reference.
|
|
93
|
+
* @param node The named node.
|
|
94
|
+
* @param settings The settings of the run.
|
|
95
|
+
* @param allowTypeShorthand Whether `rdf:type` may be written as `a`, as in predicate position.
|
|
96
|
+
* @returns The serialized node.
|
|
97
|
+
*/
|
|
98
|
+
static serializeNamedNode(node: NamedNode, settings: TermSerializerSettings, allowTypeShorthand?: boolean): string;
|
|
99
|
+
/**
|
|
100
|
+
* Serialize a blank node with its configured label, or as `_:` with its normalized identifier.
|
|
101
|
+
* @param node The blank node.
|
|
102
|
+
* @param settings The settings of the run.
|
|
103
|
+
* @returns The serialized node.
|
|
104
|
+
*/
|
|
105
|
+
static serializeBlankNode(node: BlankNode, settings: TermSerializerSettings): string;
|
|
106
|
+
/**
|
|
107
|
+
* Serialize a literal: bare where the syntax allows it and the lexical form is valid for the
|
|
108
|
+
* datatype, quoted with its language tag or datatype otherwise.
|
|
109
|
+
* @param literal The literal.
|
|
110
|
+
* @param settings The settings of the run.
|
|
111
|
+
* @returns The serialized literal.
|
|
112
|
+
*/
|
|
113
|
+
static serializeLiteral(literal: Literal, settings: TermSerializerSettings): string;
|
|
114
|
+
/**
|
|
115
|
+
* Write a number or boolean without quotes when the lexical form is valid for the datatype.
|
|
116
|
+
* @param value The lexical value.
|
|
117
|
+
* @param datatype The datatype IRI, if any.
|
|
118
|
+
* @returns The bare form, or `undefined` if the literal has to be quoted.
|
|
119
|
+
*/
|
|
120
|
+
private static _serializeBareLiteral;
|
|
121
|
+
/**
|
|
122
|
+
* Change the options the terms are written with, keeping the capabilities of the syntax.
|
|
123
|
+
* @param options The serialization options.
|
|
124
|
+
* @returns This serializer.
|
|
125
|
+
*/
|
|
126
|
+
configure(options: TermSerializerOptions): this;
|
|
127
|
+
/**
|
|
128
|
+
* Whether the syntax allows prefixed names and bare literals.
|
|
129
|
+
*/
|
|
130
|
+
get abbreviates(): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Serialize a term in subject position.
|
|
133
|
+
* @param term The subject term.
|
|
134
|
+
* @returns The serialized term.
|
|
135
|
+
* @throws An error if the term cannot be a subject.
|
|
136
|
+
*/
|
|
137
|
+
serializeSubject(term: Term): string;
|
|
138
|
+
/**
|
|
139
|
+
* Serialize a term in predicate position, where `rdf:type` may be written as `a`.
|
|
140
|
+
* @param term The predicate term.
|
|
141
|
+
* @returns The serialized term.
|
|
142
|
+
*/
|
|
143
|
+
serializePredicate(term: NamedNode): string;
|
|
144
|
+
/**
|
|
145
|
+
* Serialize a term in object position.
|
|
146
|
+
* @param term The object term.
|
|
147
|
+
* @returns The serialized term.
|
|
148
|
+
* @throws An error if the term type cannot be written.
|
|
149
|
+
*/
|
|
150
|
+
serializeObject(term: Term): string;
|
|
151
|
+
/**
|
|
152
|
+
* Serialize a named node as a prefixed name when a prefix matches, or as an IRI reference.
|
|
153
|
+
* @param node The named node.
|
|
154
|
+
* @param allowTypeShorthand Whether `rdf:type` may be written as `a`, as in predicate position.
|
|
155
|
+
* @returns The serialized node.
|
|
156
|
+
*/
|
|
157
|
+
serializeNamedNode(node: NamedNode, allowTypeShorthand?: boolean): string;
|
|
158
|
+
/**
|
|
159
|
+
* Serialize a blank node with its configured label, or as `_:` with its normalized identifier.
|
|
160
|
+
* @param node The blank node.
|
|
161
|
+
* @returns The serialized node.
|
|
162
|
+
*/
|
|
163
|
+
serializeBlankNode(node: BlankNode): string;
|
|
164
|
+
/**
|
|
165
|
+
* Serialize a literal: bare where the syntax allows it and the lexical form is valid for the
|
|
166
|
+
* datatype, quoted with its language tag or datatype otherwise.
|
|
167
|
+
* @param literal The literal.
|
|
168
|
+
* @returns The serialized literal.
|
|
169
|
+
*/
|
|
170
|
+
serializeLiteral(literal: Literal): string;
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=term-serializer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"term-serializer.d.ts","sourceRoot":"","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C,YAAY,EAAE,0BAA0B,EAAE,OAAO,GAAE,qBAA0B,GAAG,sBAAsB;IAUxH;;;;;;OAMG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,sBAAsB,EAAE,kBAAkB,GAAE,OAAe,GAAG,MAAM;IAgBzH;;;;;OAKG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,sBAAsB,GAAG,MAAM;IAIpF;;;;;;OAMG;IACH,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,sBAAsB,GAAG,MAAM;IA4BnF;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,qBAAqB;IAoBpC;;;;OAIG;IACH,SAAS,CAAC,OAAO,EAAE,qBAAqB,GAAG,IAAI;IAM/C;;OAEG;IACH,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED;;;;;OAKG;IACH,gBAAgB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAQpC;;;;OAIG;IACH,kBAAkB,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM;IAI3C;;;;;OAKG;IACH,eAAe,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAanC;;;;;OAKG;IACH,kBAAkB,CAAC,IAAI,EAAE,SAAS,EAAE,kBAAkB,GAAE,OAAe,GAAG,MAAM;IAIhF;;;;OAIG;IACH,kBAAkB,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM;IAI3C;;;;;OAKG;IACH,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM;CAG7C"}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { RdfSyntax } from '@faubulous/mentor-rdf-parsers';
|
|
2
|
+
import { RDF, XSD } from './ontologies';
|
|
3
|
+
import { normalizeBlankNodeId } from './utilities/blank-nodes';
|
|
4
|
+
import { escapeIri, escapeLocalName, escapeString } from './utilities/escaping';
|
|
5
|
+
import { isDecimal, isDouble, isInteger, needsLongString } from './utilities/literals';
|
|
6
|
+
import { findPrefix } from './utilities/prefixes';
|
|
7
|
+
/**
|
|
8
|
+
* Serializes single RDF terms the way a document of a syntax writes them: prefixed names where
|
|
9
|
+
* a prefix is declared, `a` for `rdf:type` in predicate position, bare numbers and booleans, and
|
|
10
|
+
* long strings for multi-line text where the syntax allows it. The quad serializers build on the
|
|
11
|
+
* static methods, which take the settings of a run; the instance methods serve code that writes
|
|
12
|
+
* terms into a document, such as an editor.
|
|
13
|
+
*/
|
|
14
|
+
export class TermSerializer {
|
|
15
|
+
_capabilities;
|
|
16
|
+
_settings;
|
|
17
|
+
/**
|
|
18
|
+
* Create a term serializer.
|
|
19
|
+
* @param capabilities What the target syntax allows.
|
|
20
|
+
* @param options The serialization options.
|
|
21
|
+
*/
|
|
22
|
+
constructor(capabilities, options = {}) {
|
|
23
|
+
this._capabilities = capabilities;
|
|
24
|
+
this._settings = TermSerializer.settingsOf(capabilities, options);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create a term serializer for a syntax.
|
|
28
|
+
* @param syntax The syntax the terms are written in.
|
|
29
|
+
* @param options The serialization options.
|
|
30
|
+
* @returns A term serializer with the capabilities of the syntax.
|
|
31
|
+
*/
|
|
32
|
+
static forSyntax(syntax, options) {
|
|
33
|
+
const abbreviates = syntax === RdfSyntax.Turtle || syntax === RdfSyntax.TriG || syntax === RdfSyntax.N3;
|
|
34
|
+
return new TermSerializer({ supportsPrefixes: abbreviates, supportsRdfTypeShorthand: abbreviates }, options);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Resolve capabilities and options to the settings of a run.
|
|
38
|
+
* @param capabilities What the target syntax allows.
|
|
39
|
+
* @param options The serialization options.
|
|
40
|
+
* @returns The settings.
|
|
41
|
+
*/
|
|
42
|
+
static settingsOf(capabilities, options = {}) {
|
|
43
|
+
return {
|
|
44
|
+
supportsPrefixes: capabilities.supportsPrefixes,
|
|
45
|
+
supportsRdfTypeShorthand: capabilities.supportsRdfTypeShorthand,
|
|
46
|
+
prefixes: options.prefixes ?? {},
|
|
47
|
+
useRdfTypeShorthand: options.useRdfTypeShorthand ?? true,
|
|
48
|
+
blankNodeLabels: options.blankNodeLabels
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Serialize a named node as a prefixed name when a prefix matches, or as an IRI reference.
|
|
53
|
+
* @param node The named node.
|
|
54
|
+
* @param settings The settings of the run.
|
|
55
|
+
* @param allowTypeShorthand Whether `rdf:type` may be written as `a`, as in predicate position.
|
|
56
|
+
* @returns The serialized node.
|
|
57
|
+
*/
|
|
58
|
+
static serializeNamedNode(node, settings, allowTypeShorthand = false) {
|
|
59
|
+
if (allowTypeShorthand && settings.supportsRdfTypeShorthand && settings.useRdfTypeShorthand && node.value === RDF.type) {
|
|
60
|
+
return 'a';
|
|
61
|
+
}
|
|
62
|
+
if (settings.supportsPrefixes) {
|
|
63
|
+
const match = findPrefix(node.value, settings.prefixes);
|
|
64
|
+
if (match) {
|
|
65
|
+
return `${match.prefix}:${escapeLocalName(match.localName)}`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return `<${escapeIri(node.value)}>`;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Serialize a blank node with its configured label, or as `_:` with its normalized identifier.
|
|
72
|
+
* @param node The blank node.
|
|
73
|
+
* @param settings The settings of the run.
|
|
74
|
+
* @returns The serialized node.
|
|
75
|
+
*/
|
|
76
|
+
static serializeBlankNode(node, settings) {
|
|
77
|
+
return settings.blankNodeLabels?.[node.value] ?? `_:${normalizeBlankNodeId(node.value)}`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Serialize a literal: bare where the syntax allows it and the lexical form is valid for the
|
|
81
|
+
* datatype, quoted with its language tag or datatype otherwise.
|
|
82
|
+
* @param literal The literal.
|
|
83
|
+
* @param settings The settings of the run.
|
|
84
|
+
* @returns The serialized literal.
|
|
85
|
+
*/
|
|
86
|
+
static serializeLiteral(literal, settings) {
|
|
87
|
+
const value = literal.value;
|
|
88
|
+
const datatype = literal.datatype?.value;
|
|
89
|
+
const language = literal.language;
|
|
90
|
+
if (language) {
|
|
91
|
+
return `"${escapeString(value)}"@${language}`;
|
|
92
|
+
}
|
|
93
|
+
if (settings.supportsPrefixes) {
|
|
94
|
+
const bare = TermSerializer._serializeBareLiteral(value, datatype);
|
|
95
|
+
if (bare !== undefined) {
|
|
96
|
+
return bare;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const useLongString = settings.supportsPrefixes && needsLongString(value);
|
|
100
|
+
const quote = useLongString ? '"""' : '"';
|
|
101
|
+
const quoted = `${quote}${escapeString(value, useLongString)}${quote}`;
|
|
102
|
+
if (!datatype || datatype === XSD.string) {
|
|
103
|
+
return quoted;
|
|
104
|
+
}
|
|
105
|
+
return `${quoted}^^${TermSerializer.serializeNamedNode(literal.datatype, settings)}`;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Write a number or boolean without quotes when the lexical form is valid for the datatype.
|
|
109
|
+
* @param value The lexical value.
|
|
110
|
+
* @param datatype The datatype IRI, if any.
|
|
111
|
+
* @returns The bare form, or `undefined` if the literal has to be quoted.
|
|
112
|
+
*/
|
|
113
|
+
static _serializeBareLiteral(value, datatype) {
|
|
114
|
+
if (datatype === XSD.boolean && (value === 'true' || value === 'false' || value === '1' || value === '0')) {
|
|
115
|
+
return value === 'true' || value === '1' ? 'true' : 'false';
|
|
116
|
+
}
|
|
117
|
+
if (datatype === XSD.integer && isInteger(value)) {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
if (datatype === XSD.decimal && isDecimal(value)) {
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
if (datatype === XSD.double && isDouble(value)) {
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Change the options the terms are written with, keeping the capabilities of the syntax.
|
|
130
|
+
* @param options The serialization options.
|
|
131
|
+
* @returns This serializer.
|
|
132
|
+
*/
|
|
133
|
+
configure(options) {
|
|
134
|
+
this._settings = TermSerializer.settingsOf(this._capabilities, options);
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Whether the syntax allows prefixed names and bare literals.
|
|
139
|
+
*/
|
|
140
|
+
get abbreviates() {
|
|
141
|
+
return this._settings.supportsPrefixes;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Serialize a term in subject position.
|
|
145
|
+
* @param term The subject term.
|
|
146
|
+
* @returns The serialized term.
|
|
147
|
+
* @throws An error if the term cannot be a subject.
|
|
148
|
+
*/
|
|
149
|
+
serializeSubject(term) {
|
|
150
|
+
if (term.termType === 'Literal') {
|
|
151
|
+
throw new Error('A literal cannot be the subject of a statement.');
|
|
152
|
+
}
|
|
153
|
+
return this.serializeObject(term);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Serialize a term in predicate position, where `rdf:type` may be written as `a`.
|
|
157
|
+
* @param term The predicate term.
|
|
158
|
+
* @returns The serialized term.
|
|
159
|
+
*/
|
|
160
|
+
serializePredicate(term) {
|
|
161
|
+
return TermSerializer.serializeNamedNode(term, this._settings, true);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Serialize a term in object position.
|
|
165
|
+
* @param term The object term.
|
|
166
|
+
* @returns The serialized term.
|
|
167
|
+
* @throws An error if the term type cannot be written.
|
|
168
|
+
*/
|
|
169
|
+
serializeObject(term) {
|
|
170
|
+
switch (term.termType) {
|
|
171
|
+
case 'NamedNode':
|
|
172
|
+
return this.serializeNamedNode(term);
|
|
173
|
+
case 'BlankNode':
|
|
174
|
+
return this.serializeBlankNode(term);
|
|
175
|
+
case 'Literal':
|
|
176
|
+
return this.serializeLiteral(term);
|
|
177
|
+
default:
|
|
178
|
+
throw new Error(`A term of type ${term.termType} cannot be written as a single term.`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Serialize a named node as a prefixed name when a prefix matches, or as an IRI reference.
|
|
183
|
+
* @param node The named node.
|
|
184
|
+
* @param allowTypeShorthand Whether `rdf:type` may be written as `a`, as in predicate position.
|
|
185
|
+
* @returns The serialized node.
|
|
186
|
+
*/
|
|
187
|
+
serializeNamedNode(node, allowTypeShorthand = false) {
|
|
188
|
+
return TermSerializer.serializeNamedNode(node, this._settings, allowTypeShorthand);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Serialize a blank node with its configured label, or as `_:` with its normalized identifier.
|
|
192
|
+
* @param node The blank node.
|
|
193
|
+
* @returns The serialized node.
|
|
194
|
+
*/
|
|
195
|
+
serializeBlankNode(node) {
|
|
196
|
+
return TermSerializer.serializeBlankNode(node, this._settings);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Serialize a literal: bare where the syntax allows it and the lexical form is valid for the
|
|
200
|
+
* datatype, quoted with its language tag or datatype otherwise.
|
|
201
|
+
* @param literal The literal.
|
|
202
|
+
* @returns The serialized literal.
|
|
203
|
+
*/
|
|
204
|
+
serializeLiteral(literal) {
|
|
205
|
+
return TermSerializer.serializeLiteral(literal, this._settings);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
//# sourceMappingURL=term-serializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"term-serializer.js","sourceRoot":"","sources":["../src/term-serializer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAE1D,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAChF,OAAO,EAA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