rdflib 2.2.10 → 2.2.12-5f141ca2

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 (190) hide show
  1. package/.babelrc +0 -0
  2. package/README.md +1 -0
  3. package/changes.txt +0 -0
  4. package/dist/rdflib.min.js +62 -0
  5. package/dist/rdflib.min.js.map +1 -0
  6. package/esm/blank-node.js +137 -0
  7. package/esm/class-order.js +12 -0
  8. package/esm/collection.js +173 -0
  9. package/esm/convert.js +61 -0
  10. package/esm/default-graph.js +57 -0
  11. package/esm/empty.js +46 -0
  12. package/esm/factories/canonical-data-factory.js +204 -0
  13. package/esm/factories/extended-term-factory.js +55 -0
  14. package/esm/factories/factory-types.js +18 -0
  15. package/esm/factories/rdflib-data-factory.js +55 -0
  16. package/esm/fetcher.js +2180 -0
  17. package/esm/formula.js +987 -0
  18. package/esm/index.js +64 -0
  19. package/esm/jsonldparser.js +122 -0
  20. package/esm/jsonparser.js +60 -0
  21. package/esm/literal.js +236 -0
  22. package/esm/log.js +13 -0
  23. package/esm/n3parser.js +1854 -0
  24. package/esm/named-node.js +154 -0
  25. package/esm/namespace.js +17 -0
  26. package/esm/node-internal.js +151 -0
  27. package/esm/node.js +46 -0
  28. package/esm/parse.js +138 -0
  29. package/esm/patch-parser.js +110 -0
  30. package/esm/query-to-sparql.js +83 -0
  31. package/esm/query.js +620 -0
  32. package/esm/rdfaparser.js +1197 -0
  33. package/esm/rdfxmlparser.js +588 -0
  34. package/esm/serialize.js +95 -0
  35. package/esm/serializer.js +1162 -0
  36. package/esm/sparql-to-query.js +566 -0
  37. package/esm/statement.js +128 -0
  38. package/esm/store.js +1306 -0
  39. package/esm/tf-types.js +1 -0
  40. package/esm/types.js +25 -0
  41. package/esm/update-manager.js +1252 -0
  42. package/esm/updates-via.js +197 -0
  43. package/esm/uri.js +245 -0
  44. package/esm/utils/default-graph-uri.js +4 -0
  45. package/esm/utils/termValue.js +8 -0
  46. package/esm/utils/terms.js +72 -0
  47. package/esm/utils-js.js +348 -0
  48. package/esm/utils.js +103 -0
  49. package/esm/variable.js +101 -0
  50. package/esm/xsd-internal.js +10 -0
  51. package/esm/xsd.js +15 -0
  52. package/lib/blank-node.d.ts +44 -0
  53. package/lib/blank-node.js +0 -0
  54. package/lib/class-order.d.ts +7 -0
  55. package/lib/class-order.js +0 -0
  56. package/lib/collection.d.ts +65 -0
  57. package/lib/collection.js +0 -0
  58. package/lib/convert.d.ts +2 -0
  59. package/lib/convert.js +0 -0
  60. package/lib/default-graph.d.ts +13 -0
  61. package/lib/default-graph.js +0 -0
  62. package/lib/empty.d.ts +11 -0
  63. package/lib/empty.js +0 -0
  64. package/lib/factories/canonical-data-factory.d.ts +6 -0
  65. package/lib/factories/canonical-data-factory.js +0 -0
  66. package/lib/factories/extended-term-factory.d.ts +13 -0
  67. package/lib/factories/extended-term-factory.js +0 -0
  68. package/lib/factories/factory-types.d.ts +67 -0
  69. package/lib/factories/factory-types.js +0 -0
  70. package/lib/factories/rdflib-data-factory.d.ts +4 -0
  71. package/lib/factories/rdflib-data-factory.js +0 -0
  72. package/lib/fetcher.d.ts +443 -0
  73. package/lib/fetcher.js +3 -2
  74. package/lib/formula.d.ts +327 -0
  75. package/lib/formula.js +0 -0
  76. package/lib/index.d.ts +40 -0
  77. package/lib/index.js +0 -0
  78. package/lib/jsonldparser.d.ts +13 -0
  79. package/lib/jsonldparser.js +0 -0
  80. package/lib/jsonparser.d.ts +4 -0
  81. package/lib/jsonparser.js +0 -0
  82. package/lib/literal.d.ts +67 -0
  83. package/lib/literal.js +0 -0
  84. package/lib/log.d.ts +15 -0
  85. package/lib/log.js +0 -0
  86. package/lib/n3parser.d.ts +62 -0
  87. package/lib/n3parser.js +1334 -1289
  88. package/lib/named-node.d.ts +44 -0
  89. package/lib/named-node.js +0 -0
  90. package/lib/namespace.d.ts +7 -0
  91. package/lib/namespace.js +0 -0
  92. package/lib/node-internal.d.ts +63 -0
  93. package/lib/node-internal.js +0 -0
  94. package/lib/node.d.ts +2 -0
  95. package/lib/node.js +0 -0
  96. package/lib/parse.d.ts +16 -0
  97. package/lib/parse.js +0 -0
  98. package/lib/patch-parser.d.ts +3 -0
  99. package/lib/patch-parser.js +0 -0
  100. package/lib/query-to-sparql.d.ts +1 -0
  101. package/lib/query-to-sparql.js +0 -0
  102. package/lib/query.d.ts +27 -0
  103. package/lib/query.js +0 -0
  104. package/lib/rdfaparser.d.ts +78 -0
  105. package/lib/rdfaparser.js +0 -0
  106. package/lib/rdfxmlparser.d.ts +60 -0
  107. package/lib/rdfxmlparser.js +430 -413
  108. package/lib/serialize.d.ts +23 -0
  109. package/lib/serialize.js +1 -1
  110. package/lib/serializer.d.ts +54 -0
  111. package/lib/serializer.js +851 -824
  112. package/lib/sparql-to-query.d.ts +6 -0
  113. package/lib/sparql-to-query.js +0 -0
  114. package/lib/statement.d.ts +55 -0
  115. package/lib/statement.js +0 -0
  116. package/lib/store.d.ts +302 -0
  117. package/lib/store.js +0 -0
  118. package/lib/tf-types.d.ts +151 -0
  119. package/lib/tf-types.js +0 -0
  120. package/lib/types.d.ts +68 -0
  121. package/lib/types.js +0 -0
  122. package/lib/update-manager.d.ts +198 -0
  123. package/lib/update-manager.js +0 -0
  124. package/lib/updates-via.d.ts +26 -0
  125. package/lib/updates-via.js +0 -0
  126. package/lib/uri.d.ts +33 -0
  127. package/lib/uri.js +0 -0
  128. package/lib/utils/default-graph-uri.d.ts +3 -0
  129. package/lib/utils/default-graph-uri.js +0 -0
  130. package/lib/utils/termValue.d.ts +3 -0
  131. package/lib/utils/termValue.js +0 -0
  132. package/lib/utils/terms.d.ts +33 -0
  133. package/lib/utils/terms.js +0 -0
  134. package/lib/utils-js.d.ts +50 -0
  135. package/lib/utils-js.js +0 -0
  136. package/lib/utils.d.ts +20 -0
  137. package/lib/utils.js +0 -0
  138. package/lib/variable.d.ts +29 -0
  139. package/lib/variable.js +0 -0
  140. package/lib/xsd-internal.d.ts +11 -0
  141. package/lib/xsd-internal.js +0 -0
  142. package/lib/xsd.d.ts +19 -0
  143. package/lib/xsd.js +8 -14
  144. package/package.json +3 -3
  145. package/src/blank-node.ts +0 -0
  146. package/src/class-order.ts +0 -0
  147. package/src/collection.ts +0 -0
  148. package/src/convert.js +0 -0
  149. package/src/default-graph.ts +0 -0
  150. package/src/empty.ts +0 -0
  151. package/src/factories/canonical-data-factory.ts +0 -0
  152. package/src/factories/extended-term-factory.ts +0 -0
  153. package/src/factories/factory-types.ts +0 -0
  154. package/src/factories/rdflib-data-factory.ts +0 -0
  155. package/src/fetcher.ts +6 -3
  156. package/src/formula.ts +0 -0
  157. package/src/index.ts +0 -0
  158. package/src/jsonldparser.js +0 -0
  159. package/src/jsonparser.js +0 -0
  160. package/src/literal.ts +0 -0
  161. package/src/log.js +0 -0
  162. package/src/n3parser.js +1114 -1110
  163. package/src/named-node.ts +0 -0
  164. package/src/namespace.ts +0 -0
  165. package/src/node-internal.ts +0 -0
  166. package/src/node.ts +0 -0
  167. package/src/parse.ts +0 -0
  168. package/src/patch-parser.js +0 -0
  169. package/src/query-to-sparql.js +0 -0
  170. package/src/query.js +0 -0
  171. package/src/rdfaparser.js +0 -0
  172. package/src/rdfxmlparser.js +22 -21
  173. package/src/serialize.ts +3 -3
  174. package/src/serializer.js +74 -62
  175. package/src/sparql-to-query.js +0 -0
  176. package/src/statement.ts +0 -0
  177. package/src/store.ts +0 -0
  178. package/src/tf-types.ts +0 -0
  179. package/src/types.ts +0 -0
  180. package/src/update-manager.ts +0 -0
  181. package/src/updates-via.js +0 -0
  182. package/src/uri.ts +0 -0
  183. package/src/utils/default-graph-uri.ts +0 -0
  184. package/src/utils/termValue.ts +0 -0
  185. package/src/utils/terms.ts +0 -0
  186. package/src/utils-js.js +0 -0
  187. package/src/utils.ts +0 -0
  188. package/src/variable.ts +0 -0
  189. package/src/xsd-internal.js +0 -0
  190. package/src/xsd.js +16 -14
@@ -0,0 +1,154 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
4
+ import _inherits from "@babel/runtime/helpers/inherits";
5
+ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
8
+
9
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
10
+
11
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
12
+
13
+ import ClassOrder from './class-order';
14
+ import Node from './node-internal';
15
+ import { NamedNodeTermType } from './types';
16
+ import { termValue } from './utils/termValue';
17
+ import { isTerm } from './utils/terms';
18
+ /**
19
+ * A named (IRI) RDF node
20
+ */
21
+
22
+ var NamedNode = /*#__PURE__*/function (_Node) {
23
+ _inherits(NamedNode, _Node);
24
+
25
+ var _super = _createSuper(NamedNode);
26
+
27
+ /**
28
+ * Create a named (IRI) RDF Node
29
+ * @constructor
30
+ * @param iri - The IRI for this node
31
+ */
32
+ function NamedNode(iri) {
33
+ var _this;
34
+
35
+ _classCallCheck(this, NamedNode);
36
+
37
+ _this = _super.call(this, termValue(iri));
38
+
39
+ _defineProperty(_assertThisInitialized(_this), "termType", NamedNodeTermType);
40
+
41
+ _defineProperty(_assertThisInitialized(_this), "classOrder", ClassOrder.NamedNode);
42
+
43
+ if (!_this.value) {
44
+ throw new Error('Missing IRI for NamedNode');
45
+ }
46
+
47
+ if (!_this.value.includes(':')) {
48
+ throw new Error('NamedNode IRI "' + iri + '" must be absolute.');
49
+ }
50
+
51
+ if (_this.value.includes(' ')) {
52
+ var message = 'Error: NamedNode IRI "' + iri + '" must not contain unencoded spaces.';
53
+ throw new Error(message);
54
+ }
55
+
56
+ return _this;
57
+ }
58
+ /**
59
+ * Returns an $rdf node for the containing directory, ending in slash.
60
+ */
61
+
62
+
63
+ _createClass(NamedNode, [{
64
+ key: "dir",
65
+ value: function dir() {
66
+ var str = this.value.split('#')[0];
67
+ var p = str.slice(0, -1).lastIndexOf('/');
68
+ var q = str.indexOf('//');
69
+ if (q >= 0 && p < q + 2 || p < 0) return null;
70
+ return new NamedNode(str.slice(0, p + 1));
71
+ }
72
+ /**
73
+ * Returns an NN for the whole web site, ending in slash.
74
+ * Contrast with the "origin" which does NOT have a trailing slash
75
+ */
76
+
77
+ }, {
78
+ key: "site",
79
+ value: function site() {
80
+ var str = this.value.split('#')[0];
81
+ var p = str.indexOf('//');
82
+ if (p < 0) throw new Error('This URI does not have a web site part (origin)');
83
+ var q = str.indexOf('/', p + 2);
84
+
85
+ if (q < 0) {
86
+ return new NamedNode(str.slice(0) + '/'); // Add slash to a bare origin
87
+ } else {
88
+ return new NamedNode(str.slice(0, q + 1));
89
+ }
90
+ }
91
+ /**
92
+ * Creates the fetchable named node for the document.
93
+ * Removes everything from the # anchor tag.
94
+ */
95
+
96
+ }, {
97
+ key: "doc",
98
+ value: function doc() {
99
+ if (this.value.indexOf('#') < 0) {
100
+ return this;
101
+ } else {
102
+ return new NamedNode(this.value.split('#')[0]);
103
+ }
104
+ }
105
+ /**
106
+ * Returns the URI including <brackets>
107
+ */
108
+
109
+ }, {
110
+ key: "toString",
111
+ value: function toString() {
112
+ return '<' + this.value + '>';
113
+ }
114
+ /** The local identifier with the document */
115
+
116
+ }, {
117
+ key: "id",
118
+ value: function id() {
119
+ return this.value.split('#')[1];
120
+ }
121
+ /** Alias for value, favored by Tim */
122
+
123
+ }, {
124
+ key: "uri",
125
+ get: function get() {
126
+ return this.value;
127
+ },
128
+ set: function set(uri) {
129
+ this.value = uri;
130
+ }
131
+ /**
132
+ * Creates a named node from the specified input value
133
+ * @param value - An input value
134
+ */
135
+
136
+ }], [{
137
+ key: "fromValue",
138
+ value: function fromValue(value) {
139
+ if (typeof value === 'undefined' || value === null) {
140
+ return value;
141
+ }
142
+
143
+ if (isTerm(value)) {
144
+ return value;
145
+ }
146
+
147
+ return new NamedNode(value);
148
+ }
149
+ }]);
150
+
151
+ return NamedNode;
152
+ }(Node);
153
+
154
+ export { NamedNode as default };
@@ -0,0 +1,17 @@
1
+ import RDFlibNamedNode from './named-node';
2
+
3
+ /**
4
+ * Gets a namespace for the specified namespace's URI
5
+ * @param nsuri - The URI for the namespace
6
+ * @param [factory] - The factory for creating named nodes with
7
+ */
8
+ export default function Namespace(nsuri, factory) {
9
+ var dataFactory = factory || {
10
+ namedNode: function namedNode(value) {
11
+ return new RDFlibNamedNode(value);
12
+ }
13
+ };
14
+ return function (ln) {
15
+ return dataFactory.namedNode(nsuri + (ln || ''));
16
+ };
17
+ }
@@ -0,0 +1,151 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
+
5
+ /**
6
+ * The superclass of all RDF Statement objects, that is
7
+ * NamedNode, Literal, BlankNode, etc.
8
+ * Should not be instantiated directly.
9
+ * Also called Term.
10
+ * @link https://rdf.js.org/data-model-spec/#term-interface
11
+ * @class Node
12
+ */
13
+ var Node = /*#__PURE__*/function () {
14
+ // Specified in './node.ts' to prevent circular dependency
15
+ // Specified in './node.ts' to prevent circular dependency
16
+
17
+ /** The type of node */
18
+
19
+ /** The class order for this node */
20
+
21
+ /** The node's value */
22
+ function Node(value) {
23
+ _classCallCheck(this, Node);
24
+
25
+ _defineProperty(this, "termType", void 0);
26
+
27
+ _defineProperty(this, "classOrder", void 0);
28
+
29
+ _defineProperty(this, "value", void 0);
30
+
31
+ this.value = value;
32
+ }
33
+ /**
34
+ * Creates the substituted node for this one, according to the specified bindings
35
+ * @param bindings - Bindings of identifiers to nodes
36
+ */
37
+
38
+
39
+ _createClass(Node, [{
40
+ key: "substitute",
41
+ value: function substitute(bindings) {
42
+ console.log('@@@ node substitute' + this);
43
+ return this;
44
+ }
45
+ /**
46
+ * Compares this node with another
47
+ * @see {equals} to check if two nodes are equal
48
+ * @param other - The other node
49
+ */
50
+
51
+ }, {
52
+ key: "compareTerm",
53
+ value: function compareTerm(other) {
54
+ if (this.classOrder < other.classOrder) {
55
+ return -1;
56
+ }
57
+
58
+ if (this.classOrder > other.classOrder) {
59
+ return +1;
60
+ }
61
+
62
+ if (this.value < other.value) {
63
+ return -1;
64
+ }
65
+
66
+ if (this.value > other.value) {
67
+ return +1;
68
+ }
69
+
70
+ return 0;
71
+ }
72
+ /**
73
+ * Compares whether the two nodes are equal
74
+ * @param other The other node
75
+ */
76
+
77
+ }, {
78
+ key: "equals",
79
+ value: function equals(other) {
80
+ if (!other) {
81
+ return false;
82
+ }
83
+
84
+ return this.termType === other.termType && this.value === other.value;
85
+ }
86
+ /**
87
+ * Creates a hash for this node
88
+ * @deprecated use {rdfFactory.id} instead if possible
89
+ */
90
+
91
+ }, {
92
+ key: "hashString",
93
+ value: function hashString() {
94
+ return this.toCanonical();
95
+ }
96
+ /**
97
+ * Compares whether this node is the same as the other one
98
+ * @param other - Another node
99
+ */
100
+
101
+ }, {
102
+ key: "sameTerm",
103
+ value: function sameTerm(other) {
104
+ return this.equals(other);
105
+ }
106
+ /**
107
+ * Creates a canonical string representation of this node
108
+ */
109
+
110
+ }, {
111
+ key: "toCanonical",
112
+ value: function toCanonical() {
113
+ return this.toNT();
114
+ }
115
+ /**
116
+ * Creates a n-triples string representation of this node
117
+ */
118
+
119
+ }, {
120
+ key: "toNT",
121
+ value: function toNT() {
122
+ return this.toString();
123
+ }
124
+ /**
125
+ * Creates a n-quads string representation of this node
126
+ */
127
+
128
+ }, {
129
+ key: "toNQ",
130
+ value: function toNQ() {
131
+ return this.toNT();
132
+ }
133
+ /**
134
+ * Creates a string representation of this node
135
+ */
136
+
137
+ }, {
138
+ key: "toString",
139
+ value: function toString() {
140
+ throw new Error('Node.toString() is abstract - see the subclasses instead');
141
+ }
142
+ }]);
143
+
144
+ return Node;
145
+ }();
146
+
147
+ _defineProperty(Node, "fromValue", void 0);
148
+
149
+ _defineProperty(Node, "toJS", void 0);
150
+
151
+ export { Node as default };
package/esm/node.js ADDED
@@ -0,0 +1,46 @@
1
+ // This file attaches all functionality to Node
2
+ // that would otherwise require circular dependencies.
3
+ import { fromValue } from './collection';
4
+ import Node from './node-internal';
5
+ import Namespace from './namespace';
6
+ import { isCollection, isLiteral } from './utils/terms';
7
+
8
+ /**
9
+ * Creates an RDF Node from a native javascript value.
10
+ * RDF Nodes are returned unchanged, undefined returned as itself.
11
+ * @method fromValue
12
+ * @static
13
+ * @param value {Node|Date|String|Number|Boolean|Undefined}
14
+ * @return {Node|Collection}
15
+ */
16
+ Node.fromValue = fromValue;
17
+ export default Node;
18
+ var ns = {
19
+ xsd: Namespace('http://www.w3.org/2001/XMLSchema#')
20
+ };
21
+ /**
22
+ * Gets the javascript object equivalent to a node
23
+ * @param term The RDF node
24
+ */
25
+
26
+ Node.toJS = function (term) {
27
+ if (isCollection(term)) {
28
+ return term.elements.map(Node.toJS); // Array node (not standard RDFJS)
29
+ }
30
+
31
+ if (!isLiteral(term)) return term;
32
+
33
+ if (term.datatype.equals(ns.xsd('boolean'))) {
34
+ return term.value === '1' || term.value === 'true';
35
+ }
36
+
37
+ if (term.datatype.equals(ns.xsd('dateTime')) || term.datatype.equals(ns.xsd('date'))) {
38
+ return new Date(term.value);
39
+ }
40
+
41
+ if (term.datatype.equals(ns.xsd('integer')) || term.datatype.equals(ns.xsd('float')) || term.datatype.equals(ns.xsd('decimal'))) {
42
+ return Number(term.value);
43
+ }
44
+
45
+ return term.value;
46
+ };
package/esm/parse.js ADDED
@@ -0,0 +1,138 @@
1
+ import DataFactory from './factories/extended-term-factory';
2
+ import jsonldParser from './jsonldparser'; // @ts-ignore is this injected?
3
+
4
+ import { Parser as N3jsParser } from 'n3'; // @@ Goal: remove this dependency
5
+
6
+ import N3Parser from './n3parser';
7
+ import { parseRDFaDOM } from './rdfaparser';
8
+ import RDFParser from './rdfxmlparser';
9
+ import sparqlUpdateParser from './patch-parser';
10
+ import * as Util from './utils-js';
11
+ import { TurtleContentType, N3ContentType, RDFXMLContentType, XHTMLContentType, HTMLContentType, SPARQLUpdateContentType, SPARQLUpdateSingleMatchContentType, JSONLDContentType, NQuadsContentType, NQuadsAltContentType } from './types';
12
+
13
+ /**
14
+ * Parse a string and put the result into the graph kb.
15
+ * Normal method is sync.
16
+ * Unfortunately jsdonld is currently written to need to be called async.
17
+ * Hence the mess below with executeCallback.
18
+ * @param str - The input string to parse
19
+ * @param kb - The store to use
20
+ * @param base - The base URI to use
21
+ * @param contentType - The MIME content type string for the input - defaults to text/turtle
22
+ * @param [callback] - The callback to call when the data has been loaded
23
+ */
24
+ export default function parse(str, kb, base) {
25
+ var contentType = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'text/turtle';
26
+ var callback = arguments.length > 4 ? arguments[4] : undefined;
27
+ contentType = contentType || TurtleContentType;
28
+ contentType = contentType.split(';')[0];
29
+
30
+ try {
31
+ if (contentType === N3ContentType || contentType === TurtleContentType) {
32
+ var p = N3Parser(kb, kb, base, base, null, null, '', null);
33
+ p.loadBuf(str);
34
+ executeCallback();
35
+ } else if (contentType === RDFXMLContentType) {
36
+ var parser = new RDFParser(kb);
37
+ parser.parse(Util.parseXML(str), base, kb.sym(base));
38
+ executeCallback();
39
+ } else if (contentType === XHTMLContentType) {
40
+ parseRDFaDOM(Util.parseXML(str, {
41
+ contentType: XHTMLContentType
42
+ }), kb, base);
43
+ executeCallback();
44
+ } else if (contentType === HTMLContentType) {
45
+ parseRDFaDOM(Util.parseXML(str, {
46
+ contentType: HTMLContentType
47
+ }), kb, base);
48
+ executeCallback();
49
+ } else if (contentType === SPARQLUpdateContentType || contentType === SPARQLUpdateSingleMatchContentType) {
50
+ // @@ we handle a subset
51
+ sparqlUpdateParser(str, kb, base);
52
+ executeCallback();
53
+ } else if (contentType === JSONLDContentType) {
54
+ jsonldParser(str, kb, base, executeCallback);
55
+ } else if (contentType === NQuadsContentType || contentType === NQuadsAltContentType) {
56
+ var n3Parser = new N3jsParser({
57
+ factory: DataFactory
58
+ });
59
+ nquadCallback(null, str);
60
+ } else if (contentType === undefined) {
61
+ throw new Error("contentType is undefined");
62
+ } else {
63
+ throw new Error("Don't know how to parse " + contentType + ' yet');
64
+ }
65
+ } catch (e) {
66
+ // @ts-ignore
67
+ executeErrorCallback(e);
68
+ }
69
+
70
+ parse.handled = {
71
+ 'text/n3': true,
72
+ 'text/turtle': true,
73
+ 'application/rdf+xml': true,
74
+ 'application/xhtml+xml': true,
75
+ 'text/html': true,
76
+ 'application/sparql-update': true,
77
+ 'application/sparql-update-single-match': true,
78
+ 'application/ld+json': true,
79
+ 'application/nquads': true,
80
+ 'application/n-quads': true
81
+ };
82
+
83
+ function executeCallback() {
84
+ if (callback) {
85
+ callback(null, kb);
86
+ } else {
87
+ return;
88
+ }
89
+ }
90
+
91
+ function executeErrorCallback(e) {
92
+ if ( // TODO: Always true, what is the right behavior
93
+ contentType !== JSONLDContentType || // @ts-ignore always true?
94
+ contentType !== NQuadsContentType || // @ts-ignore always true?
95
+ contentType !== NQuadsAltContentType) {
96
+ if (callback) {
97
+ callback(e, kb);
98
+ } else {
99
+ var e2 = new Error('' + e + ' while trying to parse <' + base + '> as ' + contentType); //@ts-ignore .cause is not a default error property
100
+
101
+ e2.cause = e;
102
+ throw e2;
103
+ }
104
+ }
105
+ }
106
+ /*
107
+ function setJsonLdBase (doc, base) {
108
+ if (doc instanceof Array) {
109
+ return
110
+ }
111
+ if (!('@context' in doc)) {
112
+ doc['@context'] = {}
113
+ }
114
+ doc['@context']['@base'] = base
115
+ }
116
+ */
117
+
118
+
119
+ function nquadCallback(err, nquads) {
120
+ if (err) {
121
+ callback(err, kb);
122
+ }
123
+
124
+ try {
125
+ n3Parser.parse(nquads, tripleCallback);
126
+ } catch (err) {
127
+ callback(err, kb);
128
+ }
129
+ }
130
+
131
+ function tripleCallback(err, triple) {
132
+ if (triple) {
133
+ kb.add(triple.subject, triple.predicate, triple.object, triple.graph);
134
+ } else {
135
+ callback(err, kb);
136
+ }
137
+ }
138
+ }
@@ -0,0 +1,110 @@
1
+ // Parse a simple SPARL-Update subset syntax for patches.
2
+ //
3
+ // This parses
4
+ // WHERE {xxx} DELETE {yyy} INSERT DATA {zzz}
5
+ // (not necessarily in that order)
6
+ // as though it were the n3
7
+ // <#query> patch:where {xxx}; patch:delete {yyy}; patch:insert {zzz}.
8
+ import N3Parser from './n3parser';
9
+ import Namespace from './namespace';
10
+ export default function sparqlUpdateParser(str, kb, base) {
11
+ var i, j, k;
12
+ var keywords = ['INSERT', 'DELETE', 'WHERE'];
13
+ var SQNS = Namespace('http://www.w3.org/ns/pim/patch#');
14
+ var p = N3Parser(kb, kb, base, base, null, null, '', null);
15
+ var clauses = {};
16
+
17
+ var badSyntax = function badSyntax(uri, lines, str, i, why) {
18
+ return 'Line ' + (lines + 1) + ' of <' + uri + '>: Bad syntax:\n ' + why + '\n at: "' + str.slice(i, i + 30) + '"';
19
+ }; // var check = function (next, last, message) {
20
+ // if (next < 0) {
21
+ // throw badSyntax(p._thisDoc, p.lines, str, j, last, message)
22
+ // }
23
+ // return next
24
+ // }
25
+
26
+
27
+ i = 0;
28
+ var query = kb.sym(base + '#query'); // Invent a URI for the query
29
+
30
+ clauses['query'] = query; // A way of accessing it in its N3 model.
31
+
32
+ while (true) {
33
+ // console.log("A Now at i = " + i)
34
+ j = p.skipSpace(str, i);
35
+
36
+ if (j < 0) {
37
+ return clauses;
38
+ } // console.log("B After space at j= " + j)
39
+
40
+
41
+ if (str[j] === ';') {
42
+ i = p.skipSpace(str, j + 1);
43
+
44
+ if (i < 0) {
45
+ return clauses; // Allow end in a
46
+ }
47
+
48
+ j = i;
49
+ }
50
+
51
+ var found = false;
52
+
53
+ for (k = 0; k < keywords.length; k++) {
54
+ var key = keywords[k];
55
+
56
+ if (str.slice(j, j + key.length) === key) {
57
+ i = p.skipSpace(str, j + key.length);
58
+
59
+ if (i < 0) {
60
+ throw badSyntax(p._thisDoc, p.lines, str, j + key.length, 'found EOF, needed {...} after ' + key);
61
+ }
62
+
63
+ if ((key === 'INSERT' || key === 'DELETE') && str.slice(i, i + 4) === 'DATA') {
64
+ // Some wanted 'DATA'. Whatever
65
+ j = p.skipSpace(str, i + 4);
66
+
67
+ if (j < 0) {
68
+ throw badSyntax(p._thisDoc, p.lines, str, i + 4, 'needed {...} after INSERT DATA ' + key);
69
+ }
70
+
71
+ i = j;
72
+ }
73
+
74
+ var res2 = [];
75
+ j = p.node(str, i, res2); // Parse all the complexity of the clause
76
+
77
+ if (j < 0) {
78
+ throw badSyntax(p._thisDoc, p.lines, str, i, 'bad syntax or EOF in {...} after ' + key);
79
+ }
80
+
81
+ clauses[key.toLowerCase()] = res2[0];
82
+ kb.add(query, SQNS(key.toLowerCase()), res2[0]); // , kb.sym(base)
83
+ // key is the keyword and res2 has the contents
84
+
85
+ found = true;
86
+ i = j;
87
+ }
88
+ }
89
+
90
+ if (!found && str.slice(j, j + 7) === '@prefix') {
91
+ i = p.directive(str, j);
92
+
93
+ if (i < 0) {
94
+ throw badSyntax(p._thisDoc, p.lines, str, i, 'bad syntax or EOF after @prefix ');
95
+ } // console.log("P before dot i= " + i)
96
+
97
+
98
+ i = p.checkDot(str, i); // console.log("Q after dot i= " + i)
99
+
100
+ found = true;
101
+ }
102
+
103
+ if (!found) {
104
+ // console.log("Bad syntax " + j)
105
+ throw badSyntax(p._thisDoc, p.lines, str, j, "Unknown syntax at start of statememt: '" + str.slice(j).slice(0, 20) + "'");
106
+ }
107
+ } // while
108
+ // return clauses
109
+
110
+ }
@@ -0,0 +1,83 @@
1
+ import log from './log';
2
+ export default function queryToSPARQL(query) {
3
+ var indent = 0;
4
+
5
+ function getSelect(query) {
6
+ var str = addIndent() + 'SELECT ';
7
+
8
+ for (var i = 0; i < query.vars.length; i++) {
9
+ str += query.vars[i] + ' ';
10
+ }
11
+
12
+ str += '\n';
13
+ return str;
14
+ }
15
+
16
+ function getPattern(pat) {
17
+ var str = '';
18
+ var st = pat.statements;
19
+
20
+ for (var x in st) {
21
+ log.debug('Found statement: ' + st);
22
+ str += addIndent() + st[x] + '\n';
23
+ }
24
+
25
+ return str;
26
+ }
27
+
28
+ function getConstraints(pat) {
29
+ var str = '';
30
+
31
+ for (var v in pat.constraints) {
32
+ var foo = pat.constraints[v];
33
+ str += addIndent() + 'FILTER ( ' + foo.describe(v) + ' ) ' + '\n';
34
+ }
35
+
36
+ return str;
37
+ }
38
+
39
+ function getOptionals(pat) {
40
+ var str = '';
41
+
42
+ for (var x = 0; x < pat.optional.length; x++) {
43
+ // alert(pat.optional.termType)
44
+ log.debug('Found optional query');
45
+ str += addIndent() + 'OPTIONAL { ' + '\n';
46
+ indent++;
47
+ str += getPattern(pat.optional[x]);
48
+ str += getConstraints(pat.optional[x]);
49
+ str += getOptionals(pat.optional[x]);
50
+ indent--;
51
+ str += addIndent() + '}' + '\n';
52
+ }
53
+
54
+ return str;
55
+ }
56
+
57
+ function getWhere(pat) {
58
+ var str = addIndent() + 'WHERE \n' + '{ \n';
59
+ indent++;
60
+ str += getPattern(pat);
61
+ str += getConstraints(pat);
62
+ str += getOptionals(pat);
63
+ indent--;
64
+ str += '}';
65
+ return str;
66
+ }
67
+
68
+ function addIndent() {
69
+ var str = '';
70
+
71
+ for (var i = 0; i < indent; i++) {
72
+ str += ' ';
73
+ }
74
+
75
+ return str;
76
+ }
77
+
78
+ function getSPARQL(query) {
79
+ return getSelect(query) + getWhere(query.pat);
80
+ }
81
+
82
+ return getSPARQL(query);
83
+ }