@lblod/graph-rdfa-processor 0.13.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,603 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.RDFaPredicate = exports.RDFaSubject = exports.RDFaGraph = void 0;
7
+
8
+ var _node = _interopRequireDefault(require("./node"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
13
+
14
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
+
16
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
17
+
18
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
19
+
20
+ var RDFaGraph =
21
+ /*#__PURE__*/
22
+ function () {
23
+ function RDFaGraph() {
24
+ _classCallCheck(this, RDFaGraph);
25
+
26
+ var dataContext = this;
27
+ this.curieParser = {
28
+ trim: function trim(str) {
29
+ return str.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
30
+ },
31
+ parse: function parse(value, resolve) {
32
+ value = this.trim(value);
33
+
34
+ if (value.charAt(0) == "[" && value.charAt(value.length - 1) == "]") {
35
+ value = value.substring(1, value.length - 1);
36
+ }
37
+
38
+ var colon = value.indexOf(":");
39
+
40
+ if (colon >= 0) {
41
+ var prefix = value.substring(0, colon);
42
+
43
+ if (prefix == "") {
44
+ // default prefix
45
+ var uri = dataContext.prefixes[""];
46
+ return uri ? uri + value.substring(colon + 1) : null;
47
+ } else if (prefix == "_") {
48
+ // blank node
49
+ return "_:" + value.substring(colon + 1);
50
+ } else if (DocumentData.NCNAME.test(prefix)) {
51
+ var uri = dataContext.prefixes[prefix];
52
+
53
+ if (uri) {
54
+ return uri + value.substring(colon + 1);
55
+ }
56
+ }
57
+ }
58
+
59
+ return resolve ? dataContext.baseURI.resolve(value) : value;
60
+ }
61
+ };
62
+ this.base = null;
63
+
64
+ this.toString = function (requestOptions) {
65
+ var options = requestOptions && requestOptions.shorten ? {
66
+ graph: this,
67
+ shorten: true,
68
+ prefixesUsed: {}
69
+ } : null;
70
+
71
+ if (requestOptions && requestOptions.blankNodePrefix) {
72
+ options.filterBlankNode = function (id) {
73
+ return "_:" + requestOptions.blankNodePrefix + id.substring(2);
74
+ };
75
+ }
76
+
77
+ if (requestOptions && requestOptions.numericalBlankNodePrefix) {
78
+ var onlyNumbers = /^[0-9]+$/;
79
+
80
+ options.filterBlankNode = function (id) {
81
+ var label = id.substring(2);
82
+ return onlyNumbers.test(label) ? "_:" + requestOptions.numericalBlankNodePrefix + label : id;
83
+ };
84
+ }
85
+
86
+ var s = "";
87
+
88
+ for (var subject in this.subjects) {
89
+ var snode = this.subjects[subject];
90
+ s += snode.toString(options);
91
+ s += "\n";
92
+ }
93
+
94
+ var prolog = requestOptions && requestOptions.baseURI ? "@base <" + baseURI + "> .\n" : "";
95
+
96
+ if (options && options.shorten) {
97
+ for (var prefix in options.prefixesUsed) {
98
+ prolog += "@prefix " + prefix + ": <" + this.prefixes[prefix] + "> .\n";
99
+ }
100
+ }
101
+
102
+ return prolog.length == 0 ? s : prolog + "\n" + s;
103
+ };
104
+
105
+ this.blankNodeCounter = 0;
106
+
107
+ this.clear = function () {
108
+ this.subjects = {};
109
+ this.prefixes = {};
110
+ this.terms = {};
111
+ this.blankNodeCounter = 0;
112
+ };
113
+
114
+ this.clear();
115
+ this.prefixes[""] = "http://www.w3.org/1999/xhtml/vocab#"; // w3c
116
+
117
+ this.prefixes["grddl"] = "http://www.w3.org/2003/g/data-view#";
118
+ this.prefixes["ma"] = "http://www.w3.org/ns/ma-ont#";
119
+ this.prefixes["owl"] = "http://www.w3.org/2002/07/owl#";
120
+ this.prefixes["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
121
+ this.prefixes["rdfa"] = "http://www.w3.org/ns/rdfa#";
122
+ this.prefixes["rdfs"] = "http://www.w3.org/2000/01/rdf-schema#";
123
+ this.prefixes["rif"] = "http://www.w3.org/2007/rif#";
124
+ this.prefixes["skos"] = "http://www.w3.org/2004/02/skos/core#";
125
+ this.prefixes["skosxl"] = "http://www.w3.org/2008/05/skos-xl#";
126
+ this.prefixes["wdr"] = "http://www.w3.org/2007/05/powder#";
127
+ this.prefixes["void"] = "http://rdfs.org/ns/void#";
128
+ this.prefixes["wdrs"] = "http://www.w3.org/2007/05/powder-s#";
129
+ this.prefixes["xhv"] = "http://www.w3.org/1999/xhtml/vocab#";
130
+ this.prefixes["xml"] = "http://www.w3.org/XML/1998/namespace";
131
+ this.prefixes["xsd"] = "http://www.w3.org/2001/XMLSchema#"; // non-rec w3c
132
+
133
+ this.prefixes["sd"] = "http://www.w3.org/ns/sparql-service-description#";
134
+ this.prefixes["org"] = "http://www.w3.org/ns/org#";
135
+ this.prefixes["gldp"] = "http://www.w3.org/ns/people#";
136
+ this.prefixes["cnt"] = "http://www.w3.org/2008/content#";
137
+ this.prefixes["dcat"] = "http://www.w3.org/ns/dcat#";
138
+ this.prefixes["earl"] = "http://www.w3.org/ns/earl#";
139
+ this.prefixes["ht"] = "http://www.w3.org/2006/http#";
140
+ this.prefixes["ptr"] = "http://www.w3.org/2009/pointers#"; // widely used
141
+
142
+ this.prefixes["cc"] = "http://creativecommons.org/ns#";
143
+ this.prefixes["ctag"] = "http://commontag.org/ns#";
144
+ this.prefixes["dc"] = "http://purl.org/dc/terms/";
145
+ this.prefixes["dcterms"] = "http://purl.org/dc/terms/";
146
+ this.prefixes["foaf"] = "http://xmlns.com/foaf/0.1/";
147
+ this.prefixes["gr"] = "http://purl.org/goodrelations/v1#";
148
+ this.prefixes["ical"] = "http://www.w3.org/2002/12/cal/icaltzd#";
149
+ this.prefixes["og"] = "http://ogp.me/ns#";
150
+ this.prefixes["rev"] = "http://purl.org/stuff/rev#";
151
+ this.prefixes["sioc"] = "http://rdfs.org/sioc/ns#";
152
+ this.prefixes["v"] = "http://rdf.data-vocabulary.org/#";
153
+ this.prefixes["vcard"] = "http://www.w3.org/2006/vcard/ns#";
154
+ this.prefixes["schema"] = "http://schema.org/"; // terms
155
+
156
+ this.terms["describedby"] = "http://www.w3.org/2007/05/powder-s#describedby";
157
+ this.terms["license"] = "http://www.w3.org/1999/xhtml/vocab#license";
158
+ this.terms["role"] = "http://www.w3.org/1999/xhtml/vocab#role";
159
+ Object.defineProperty(this, "tripleCount", {
160
+ enumerable: true,
161
+ configurable: false,
162
+ get: function get() {
163
+ var count = 0;
164
+
165
+ for (var s in this.subjects) {
166
+ var snode = this.subjects[s];
167
+
168
+ for (var p in snode.predicates) {
169
+ count += snode.predicates[p].objects.length;
170
+ }
171
+ }
172
+
173
+ return count;
174
+ }
175
+ });
176
+ }
177
+
178
+ _createClass(RDFaGraph, [{
179
+ key: "newBlankNode",
180
+ value: function newBlankNode() {
181
+ this.blankNodeCounter++;
182
+ return "_:" + this.blankNodeCounter;
183
+ }
184
+ }, {
185
+ key: "expand",
186
+ value: function expand(curie) {
187
+ return this.curieParser.parse(curie, true);
188
+ }
189
+ }, {
190
+ key: "shorten",
191
+ value: function shorten(uri, prefixesUsed) {
192
+ for (prefix in this.prefixes) {
193
+ var mapped = this.prefixes[prefix];
194
+
195
+ if (uri.indexOf(mapped) == 0) {
196
+ if (prefixesUsed) {
197
+ prefixesUsed[prefix] = mapped;
198
+ }
199
+
200
+ return prefix + ":" + uri.substring(mapped.length);
201
+ }
202
+ }
203
+
204
+ return null;
205
+ }
206
+ }, {
207
+ key: "add",
208
+ value: function add(subject, predicate, object, options) {
209
+ if (!subject || !predicate || !object) {
210
+ return;
211
+ }
212
+
213
+ subject = this.expand(subject);
214
+ predicate = this.expand(predicate);
215
+ var snode = this.subjects[subject];
216
+
217
+ if (!snode) {
218
+ snode = new RDFaSubject(this, subject);
219
+ this.subjects[subject] = snode;
220
+ }
221
+
222
+ if (options && options.origin) {
223
+ snode.origins.push(options.origin);
224
+ }
225
+
226
+ if (predicate == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
227
+ snode.types.push(object);
228
+ }
229
+
230
+ var pnode = snode.predicates[predicate];
231
+
232
+ if (!pnode) {
233
+ pnode = new RDFaPredicate(predicate);
234
+ snode.predicates[predicate] = pnode;
235
+ }
236
+
237
+ if (typeof object == "string") {
238
+ pnode.objects.push({
239
+ type: RDFaProcessor.PlainLiteralURI,
240
+ value: object
241
+ });
242
+ } else {
243
+ pnode.objects.push({
244
+ type: object.type ? this.expand(object.type) : RDFaProcessor.PlainLiteralURI,
245
+ value: object.value ? object.value : "",
246
+ origin: object.origin,
247
+ language: object.language
248
+ });
249
+ }
250
+ }
251
+ }, {
252
+ key: "addCollection",
253
+ value: function addCollection(subject, predicate, objectList, options) {
254
+ if (!subject || !predicate || !objectList) {
255
+ return;
256
+ }
257
+
258
+ var lastSubject = subject;
259
+ var lastPredicate = predicate;
260
+
261
+ for (var i = 0; i < objectList.length; i++) {
262
+ var object = {
263
+ type: options && options.type ? options.type : "rdf:PlainLiteral"
264
+ };
265
+
266
+ if (options && options.language) {
267
+ object.language = options.language;
268
+ }
269
+
270
+ if (options && options.datatype) {
271
+ object.datatype = options.datatype;
272
+ }
273
+
274
+ if (_typeof(objectList[i]) == "object") {
275
+ object.value = objectList[i].value ? objectList[i].value : "";
276
+
277
+ if (objectList[i].type) {
278
+ object.type = objectList[i].type;
279
+ }
280
+
281
+ if (objectList[i].language) {
282
+ object.language = objectList[i].language;
283
+ }
284
+
285
+ if (objectList[i].datatype) {
286
+ object.datatype = objectList[i].datatype;
287
+ }
288
+ } else {
289
+ object.value = objectList[i];
290
+ }
291
+
292
+ var bnode = this.newBlankNode();
293
+ this.add(lastSubject, lastPredicate, {
294
+ type: "rdf:object",
295
+ value: bnode
296
+ });
297
+ this.add(bnode, "rdf:first", object);
298
+ lastSubject = bnode;
299
+ lastPredicate = "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest";
300
+ }
301
+
302
+ this.add(lastSubject, lastPredicate, {
303
+ type: "rdf:object",
304
+ value: "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"
305
+ });
306
+ }
307
+ }, {
308
+ key: "remove",
309
+ value: function remove(subject, predicate) {
310
+ if (!subject) {
311
+ this.subjects = {};
312
+ return;
313
+ }
314
+
315
+ subject = this.expand(subject);
316
+ var snode = this.subjects[snode];
317
+
318
+ if (!snode) {
319
+ return;
320
+ }
321
+
322
+ if (!predicate) {
323
+ delete this.subjects[subject];
324
+ return;
325
+ }
326
+
327
+ predicate = this.expand(predicate);
328
+ delete snode.predicates[predicate];
329
+ }
330
+ }]);
331
+
332
+ return RDFaGraph;
333
+ }();
334
+
335
+ exports.RDFaGraph = RDFaGraph;
336
+
337
+ var RDFaSubject =
338
+ /*#__PURE__*/
339
+ function () {
340
+ function RDFaSubject(graph, subject) {
341
+ _classCallCheck(this, RDFaSubject);
342
+
343
+ this.graph = graph; // TODO: subject or id?
344
+
345
+ this.subject = subject;
346
+ this.id = subject;
347
+ this.predicates = {};
348
+ this.origins = [];
349
+ this.types = [];
350
+ }
351
+
352
+ _createClass(RDFaSubject, [{
353
+ key: "toString",
354
+ value: function toString(options) {
355
+ var s = null;
356
+
357
+ if (this.subject.substring(0, 2) == "_:") {
358
+ if (options && options.filterBlankNode) {
359
+ s = options.filterBlankNode(this.subject);
360
+ } else {
361
+ s = this.subject;
362
+ }
363
+ } else if (options && options.shorten) {
364
+ s = this.graph.shorten(this.subject, options.prefixesUsed);
365
+
366
+ if (!s) {
367
+ s = "<" + this.subject + ">";
368
+ }
369
+ } else {
370
+ s = "<" + this.subject + ">";
371
+ }
372
+
373
+ var first = true;
374
+
375
+ for (var predicate in this.predicates) {
376
+ if (!first) {
377
+ s += ";\n";
378
+ } else {
379
+ first = false;
380
+ }
381
+
382
+ s += " " + this.predicates[predicate].toString(options);
383
+ }
384
+
385
+ s += " .";
386
+ return s;
387
+ }
388
+ }, {
389
+ key: "toObject",
390
+ value: function toObject() {
391
+ var o = {
392
+ subject: this.subject,
393
+ predicates: {}
394
+ };
395
+
396
+ for (var predicate in this.predicates) {
397
+ var pnode = this.predicates[predicate];
398
+ var p = {
399
+ predicate: predicate,
400
+ objects: []
401
+ };
402
+ o.predicates[predicate] = p;
403
+
404
+ for (var i = 0; i < pnode.objects.length; i++) {
405
+ var object = pnode.objects[i];
406
+
407
+ if (object.type == RDFaProcessor.XMLLiteralURI) {
408
+ var serializer = new XMLSerializer();
409
+ var value = "";
410
+
411
+ for (var x = 0; x < object.value.length; x++) {
412
+ if (object.value[x].nodeType == _node.default.ELEMENT_NODE) {
413
+ value += serializer.serializeToString(object.value[x]);
414
+ } else if (object.value[x].nodeType == _node.default.TEXT_NODE) {
415
+ value += object.value[x].nodeValue;
416
+ }
417
+ }
418
+
419
+ p.objects.push({
420
+ type: object.type,
421
+ value: value,
422
+ language: object.language
423
+ });
424
+ } else if (object.type == RDFaProcessor.HTMLLiteralURI) {
425
+ var value = object.value.length == 0 ? "" : object.value[0].parentNode.innerHTML;
426
+ p.objects.push({
427
+ type: object.type,
428
+ value: value,
429
+ language: object.language
430
+ });
431
+ } else {
432
+ p.objects.push({
433
+ type: object.type,
434
+ value: object.value,
435
+ language: object.language
436
+ });
437
+ }
438
+ }
439
+ }
440
+
441
+ return o;
442
+ }
443
+ }, {
444
+ key: "getValues",
445
+ value: function getValues() {
446
+ var values = [];
447
+
448
+ for (var i = 0; i < arguments.length; i++) {
449
+ var property = this.graph.curieParser.parse(arguments[i], true);
450
+ var pnode = this.predicates[property];
451
+
452
+ if (pnode) {
453
+ for (var j = 0; j < pnode.objects.length; j++) {
454
+ values.push(pnode.objects[j].value);
455
+ }
456
+ }
457
+ }
458
+
459
+ return values;
460
+ }
461
+ }]);
462
+
463
+ return RDFaSubject;
464
+ }();
465
+
466
+ exports.RDFaSubject = RDFaSubject;
467
+
468
+ var RDFaPredicate =
469
+ /*#__PURE__*/
470
+ function () {
471
+ function RDFaPredicate(predicate) {
472
+ _classCallCheck(this, RDFaPredicate);
473
+
474
+ this.id = predicate;
475
+ this.predicate = predicate;
476
+ this.objects = [];
477
+ }
478
+
479
+ _createClass(RDFaPredicate, [{
480
+ key: "toString",
481
+ value: function toString(options) {
482
+ var s = null;
483
+
484
+ if (options && options.shorten && options.graph) {
485
+ s = options.graph.shorten(this.predicate, options.prefixesUsed);
486
+
487
+ if (!s) {
488
+ s = "<" + this.predicate + ">";
489
+ }
490
+ } else {
491
+ s = "<" + this.predicate + ">";
492
+ }
493
+
494
+ s += " ";
495
+
496
+ for (var i = 0; i < this.objects.length; i++) {
497
+ if (i > 0) {
498
+ s += ", ";
499
+ }
500
+
501
+ if (this.objects[i].type == "http://www.w3.org/1999/02/22-rdf-syntax-ns#object") {
502
+ if (this.objects[i].value.substring(0, 2) == "_:") {
503
+ if (options && options.filterBlankNode) {
504
+ s += options.filterBlankNode(this.objects[i].value);
505
+ } else {
506
+ s += this.objects[i].value;
507
+ }
508
+ } else if (options && options.shorten && options.graph) {
509
+ u = options.graph.shorten(this.objects[i].value, options.prefixesUsed);
510
+
511
+ if (u) {
512
+ s += u;
513
+ } else {
514
+ s += "<" + this.objects[i].value + ">";
515
+ }
516
+ } else {
517
+ s += "<" + this.objects[i].value + ">";
518
+ }
519
+ } else if (this.objects[i].type == "http://www.w3.org/2001/XMLSchema#integer" || this.objects[i].type == "http://www.w3.org/2001/XMLSchema#decimal" || this.objects[i].type == "http://www.w3.org/2001/XMLSchema#double" || this.objects[i].type == "http://www.w3.org/2001/XMLSchema#boolean") {
520
+ s += '"' + this.objects[i].value + '"' + "^^<" + this.objects[i].type + ">";
521
+ } else if (this.objects[i].type == "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral") {
522
+ var serializer = new XMLSerializer();
523
+ var value = "";
524
+
525
+ for (var x = 0; x < this.objects[i].value.length; x++) {
526
+ if (this.objects[i].value[x].nodeType == _node.default.ELEMENT_NODE) {
527
+ var prefixMap = RDFaPredicate.getPrefixMap(this.objects[i].value[x]);
528
+ var prefixes = [];
529
+
530
+ for (var prefix in prefixMap) {
531
+ prefixes.push(prefix);
532
+ }
533
+
534
+ prefixes.sort();
535
+ var e = this.objects[i].value[x].cloneNode(true);
536
+
537
+ for (var p = 0; p < prefixes.length; p++) {
538
+ e.setAttributeNS("http://www.w3.org/2000/xmlns/", prefixes[p].length == 0 ? "xmlns" : "xmlns:" + prefixes[p], prefixMap[prefixes[p]]);
539
+ }
540
+
541
+ value += serializer.serializeToString(e);
542
+ } else if (this.objects[i].value[x].nodeType == _node.default.TEXT_NODE) {
543
+ value += this.objects[i].value[x].nodeValue;
544
+ }
545
+ }
546
+
547
+ s += '"""' + value.replace(/([^\\]|^)(\\)(?!\\)/g, "$1\\\\").replace(/"""/g, '\\"\\"\\"') + '"""^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>';
548
+ } else if (this.objects[i].type == "http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML") {
549
+ // We can use innerHTML as a shortcut from the parentNode if the list is not empty
550
+ if (this.objects[i].value.length == 0) {
551
+ s += '""""""^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML>';
552
+ } else {
553
+ s += '"""' + this.objects[i].value[0].parentNode.innerHTML.replace(/([^\\]|^)(\\)(?!\\)/g, "$1\\\\").replace(/"""/g, '\\"\\"\\"') + '"""^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML>';
554
+ }
555
+ } else {
556
+ var l = this.objects[i].value.toString();
557
+
558
+ if (l.indexOf("\n") >= 0 || l.indexOf("\r") >= 0) {
559
+ s += '"""' + l.replace(/([^\\]|^)(\\)(?!\\)/g, "$1\\\\").replace(/"/g, '\\"') + '"""';
560
+ } else {
561
+ s += '"' + l.replace(/([^\\]|^)(\\)(?!\\)/g, "$1\\\\").replace(/"/g, '\\"') + '"';
562
+ }
563
+
564
+ if (this.objects[i].type != "http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral") {
565
+ s += "^^<" + this.objects[i].type + ">";
566
+ } else if (this.objects[i].language) {
567
+ s += "@" + this.objects[i].language;
568
+ }
569
+ }
570
+ }
571
+
572
+ return s;
573
+ }
574
+ }]);
575
+
576
+ return RDFaPredicate;
577
+ }();
578
+
579
+ exports.RDFaPredicate = RDFaPredicate;
580
+
581
+ RDFaPredicate.getPrefixMap = function (e) {
582
+ var prefixMap = {};
583
+
584
+ while (e.attributes) {
585
+ for (var i = 0; i < e.attributes.length; i++) {
586
+ if (e.attributes[i].namespaceURI == "http://www.w3.org/2000/xmlns/") {
587
+ var prefix = e.attributes[i].localName;
588
+
589
+ if (e.attributes[i].localName == "xmlns") {
590
+ prefix = "";
591
+ }
592
+
593
+ if (!(prefix in prefixMap)) {
594
+ prefixMap[prefix] = e.attributes[i].nodeValue;
595
+ }
596
+ }
597
+ }
598
+
599
+ e = e.parentNode;
600
+ }
601
+
602
+ return prefixMap;
603
+ };