1 | import { isNamedNode, isNamedNodeLike, NamedNode } from "./named-node.js";
|
2 | import { isBlankNodeLike, BlankNode, isBlankNode } from "./blank-node.js";
|
3 | import { isLiteral, isLiteralLike, Literal } from "./literal.js";
|
4 | import { isVariable, isVariableLike, Variable } from "./variable.js";
|
5 | import { isDefaultGraphLike, DefaultGraph, isDefaultGraph } from "./default-graph.js";
|
6 | import { isQuadLike, Quad } from "./quad.js";
|
7 | import { isTermLike } from "./term.js";
|
8 | import UUID from "pure-uuid";
|
9 | export class DataFactory {
|
10 | constructor() {
|
11 | this.blankNode = (value) => {
|
12 | return new BlankNode((value || `blank-${new UUID(4).format()}`).replace(/^_:/, ""));
|
13 | };
|
14 | this.literal = (value, languageOrDataType) => {
|
15 | const getLiteral = (value, languageOrDataType) => {
|
16 | if (typeof languageOrDataType === "string") {
|
17 | return new Literal(value, languageOrDataType, this.namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"));
|
18 | }
|
19 | else if (isNamedNodeLike(languageOrDataType)) {
|
20 | return new Literal(value, "", this.fromTerm(languageOrDataType));
|
21 | }
|
22 | else {
|
23 | return new Literal(value, "", this.namedNode("http://www.w3.org/2001/XMLSchema#string"));
|
24 | }
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 | const firstIndex = value.indexOf('"'), lastIndex = value.lastIndexOf('"');
|
30 | if (firstIndex === -1 || lastIndex === firstIndex) {
|
31 | return getLiteral(value, languageOrDataType);
|
32 | }
|
33 | const newValue = value.substr(firstIndex + 1, lastIndex - firstIndex - 1), rest = value.substr(lastIndex + 1);
|
34 | if (rest.startsWith("^^")) {
|
35 | return getLiteral(newValue, this.namedNode(rest.substr(2)));
|
36 | }
|
37 | if (rest.startsWith("@")) {
|
38 | const language = rest.substr(1);
|
39 |
|
40 | return getLiteral(newValue, language === "" ? undefined : language);
|
41 | }
|
42 | if (rest.startsWith("<") && rest.endsWith(">")) {
|
43 | return getLiteral(newValue, rest.substr(1, rest.length - 2));
|
44 | }
|
45 | return getLiteral(value, languageOrDataType);
|
46 | };
|
47 | this.variable = (value) => {
|
48 | return new Variable(value.replace(/^\?/, ""));
|
49 | };
|
50 | this.defaultGraph = () => {
|
51 | return new DefaultGraph();
|
52 | };
|
53 | this.quad = (subject, predicate, object, graph) => {
|
54 | return new Quad(this.fromTerm(subject), this.fromTerm(predicate), this.fromTerm(object), this.fromTerm(graph));
|
55 | };
|
56 | this.fromTerm = (term) => {
|
57 | if (!isTermLike(term)) {
|
58 | throw new Error("Provided value is not a Term");
|
59 | }
|
60 | if (isNamedNode(term)) {
|
61 | return term;
|
62 | }
|
63 | else if (isNamedNodeLike(term)) {
|
64 | return this.namedNode(term.value);
|
65 | }
|
66 | else if (isBlankNode(term)) {
|
67 | return term;
|
68 | }
|
69 | else if (isBlankNodeLike(term)) {
|
70 | return this.blankNode(term.value);
|
71 | }
|
72 | else if (isLiteral(term)) {
|
73 | return term;
|
74 | }
|
75 | else if (isLiteralLike(term)) {
|
76 |
|
77 |
|
78 | return new Literal(term.value, term.language, this.namedNode(term.datatype.value));
|
79 | }
|
80 | else if (isVariable(term)) {
|
81 | return term;
|
82 | }
|
83 | else if (isVariableLike(term)) {
|
84 | return this.variable(term.value);
|
85 | }
|
86 | else if (isDefaultGraph(term)) {
|
87 | return term;
|
88 | }
|
89 | else if (isDefaultGraphLike(term)) {
|
90 | return this.defaultGraph();
|
91 | }
|
92 |
|
93 | throw new Error("Invalid term type, expected one of NamedNode | BlankNode | Literal | Variable | DefaultGraph");
|
94 | };
|
95 | this.fromQuad = (quad) => {
|
96 | if (!isQuadLike(quad)) {
|
97 | throw new Error("Provided value is not a Quad");
|
98 | }
|
99 | return this.quad(quad.subject, quad.predicate, quad.object, quad.graph);
|
100 | };
|
101 | this.namedNode = this.namedNode.bind(this);
|
102 | }
|
103 | namedNode(value) {
|
104 | return new NamedNode(value);
|
105 | }
|
106 | }
|
107 | export const DefaultDataFactory = new DataFactory();
|
108 |
|
\ | No newline at end of file |