1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 * This module creates a new API for accessing and modifying RDF graphs. The
7 * goal is to be able to serialise the graph in a human readable form. Also
8 * if the graph was originally loaded from an RDF/XML the serialisation should
9 * closely match the original with any new data closely following the existing
10 * layout. The output should always be compatible with Mozilla's RDF parser.
12 * This is all achieved by using a DOM Document to hold the current state of the
13 * graph in XML form. This can be initially loaded and parsed from disk or
14 * a blank document used for an empty graph. As assertions are added to the
15 * graph, appropriate DOM nodes are added to the document to represent them
16 * along with any necessary whitespace to properly layout the XML.
18 * In general the order of adding assertions to the graph will impact the form
19 * the serialisation takes. If a resource is first added as the object of an
20 * assertion then it will eventually be serialised inside the assertion's
21 * property element. If a resource is first added as the subject of an assertion
22 * then it will be serialised at the top level of the XML.
25 const NS_XML = "http://www.w3.org/XML/1998/namespace";
26 const NS_XMLNS = "http://www.w3.org/2000/xmlns/";
27 const NS_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
28 const NS_NC = "http://home.netscape.com/NC-rdf#";
30 /* eslint prefer-template: 1 */
32 function raw(strings) {
33 return strings.raw[0].replace(/\s+/, "");
36 // Copied from http://www.w3.org/TR/2000/REC-xml-20001006#CharClasses
37 const XML_LETTER = raw`
38 \u0041-\u005A\u0061-\u007A\u00C0-\u00D6\u00D8-\u00F6
39 \u00F8-\u00FF\u0100-\u0131\u0134-\u013E\u0141-\u0148
40 \u014A-\u017E\u0180-\u01C3\u01CD-\u01F0\u01F4-\u01F5
41 \u01FA-\u0217\u0250-\u02A8\u02BB-\u02C1\u0386\u0388-\u038A
42 \u038C\u038E-\u03A1\u03A3-\u03CE\u03D0-\u03D6\u03DA\u03DC
43 \u03DE\u03E0\u03E2-\u03F3\u0401-\u040C\u040E-\u044F
44 \u0451-\u045C\u045E-\u0481\u0490-\u04C4\u04C7-\u04C8
45 \u04CB-\u04CC\u04D0-\u04EB\u04EE-\u04F5\u04F8-\u04F9
46 \u0531-\u0556\u0559\u0561-\u0586\u05D0-\u05EA\u05F0-\u05F2
47 \u0621-\u063A\u0641-\u064A\u0671-\u06B7\u06BA-\u06BE
48 \u06C0-\u06CE\u06D0-\u06D3\u06D5\u06E5-\u06E6\u0905-\u0939
49 \u093D\u0958-\u0961\u0985-\u098C\u098F-\u0990\u0993-\u09A8
50 \u09AA-\u09B0\u09B2\u09B6-\u09B9\u09DC-\u09DD\u09DF-\u09E1
51 \u09F0-\u09F1\u0A05-\u0A0A\u0A0F-\u0A10\u0A13-\u0A28
52 \u0A2A-\u0A30\u0A32-\u0A33\u0A35-\u0A36\u0A38-\u0A39
53 \u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8B\u0A8D
54 \u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2-\u0AB3
55 \u0AB5-\u0AB9\u0ABD\u0AE0\u0B05-\u0B0C\u0B0F-\u0B10
56 \u0B13-\u0B28\u0B2A-\u0B30\u0B32-\u0B33\u0B36-\u0B39
57 \u0B3D\u0B5C-\u0B5D\u0B5F-\u0B61\u0B85-\u0B8A\u0B8E-\u0B90
58 \u0B92-\u0B95\u0B99-\u0B9A\u0B9C\u0B9E-\u0B9F\u0BA3-\u0BA4
59 \u0BA8-\u0BAA\u0BAE-\u0BB5\u0BB7-\u0BB9\u0C05-\u0C0C
60 \u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39
61 \u0C60-\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8
62 \u0CAA-\u0CB3\u0CB5-\u0CB9\u0CDE\u0CE0-\u0CE1\u0D05-\u0D0C
63 \u0D0E-\u0D10\u0D12-\u0D28\u0D2A-\u0D39\u0D60-\u0D61
64 \u0E01-\u0E2E\u0E30\u0E32-\u0E33\u0E40-\u0E45\u0E81-\u0E82
65 \u0E84\u0E87-\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F
66 \u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA-\u0EAB\u0EAD-\u0EAE\u0EB0
67 \u0EB2-\u0EB3\u0EBD\u0EC0-\u0EC4\u0F40-\u0F47\u0F49-\u0F69
68 \u10A0-\u10C5\u10D0-\u10F6\u1100\u1102-\u1103\u1105-\u1107
69 \u1109\u110B-\u110C\u110E-\u1112\u113C\u113E\u1140\u114C
70 \u114E\u1150\u1154-\u1155\u1159\u115F-\u1161\u1163\u1165
71 \u1167\u1169\u116D-\u116E\u1172-\u1173\u1175\u119E\u11A8
72 \u11AB\u11AE-\u11AF\u11B7-\u11B8\u11BA\u11BC-\u11C2\u11EB
73 \u11F0\u11F9\u1E00-\u1E9B\u1EA0-\u1EF9\u1F00-\u1F15
74 \u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57
75 \u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC
76 \u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB
77 \u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2126\u212A-\u212B
78 \u212E\u2180-\u2182\u3041-\u3094\u30A1-\u30FA\u3105-\u312C
79 \uAC00-\uD7A3\u4E00-\u9FA5\u3007\u3021-\u3029
81 const XML_DIGIT = raw`
82 \u0030-\u0039\u0660-\u0669\u06F0-\u06F9\u0966-\u096F
83 \u09E6-\u09EF\u0A66-\u0A6F\u0AE6-\u0AEF\u0B66-\u0B6F
84 \u0BE7-\u0BEF\u0C66-\u0C6F\u0CE6-\u0CEF\u0D66-\u0D6F
85 \u0E50-\u0E59\u0ED0-\u0ED9\u0F20-\u0F29
87 const XML_COMBINING = raw`
88 \u0300-\u0345\u0360-\u0361\u0483-\u0486\u0591-\u05A1
89 \u05A3-\u05B9\u05BB-\u05BD\u05BF\u05C1-\u05C2\u05C4
90 \u064B-\u0652\u0670\u06D6-\u06DC\u06DD-\u06DF\u06E0-\u06E4
91 \u06E7-\u06E8\u06EA-\u06ED\u0901-\u0903\u093C\u093E-\u094C
92 \u094D\u0951-\u0954\u0962-\u0963\u0981-\u0983\u09BC\u09BE
93 \u09BF\u09C0-\u09C4\u09C7-\u09C8\u09CB-\u09CD\u09D7
94 \u09E2-\u09E3\u0A02\u0A3C\u0A3E\u0A3F\u0A40-\u0A42
95 \u0A47-\u0A48\u0A4B-\u0A4D\u0A70-\u0A71\u0A81-\u0A83
96 \u0ABC\u0ABE-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0B01-\u0B03
97 \u0B3C\u0B3E-\u0B43\u0B47-\u0B48\u0B4B-\u0B4D\u0B56-\u0B57
98 \u0B82-\u0B83\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7
99 \u0C01-\u0C03\u0C3E-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D
100 \u0C55-\u0C56\u0C82-\u0C83\u0CBE-\u0CC4\u0CC6-\u0CC8
101 \u0CCA-\u0CCD\u0CD5-\u0CD6\u0D02-\u0D03\u0D3E-\u0D43
102 \u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0E31\u0E34-\u0E3A
103 \u0E47-\u0E4E\u0EB1\u0EB4-\u0EB9\u0EBB-\u0EBC\u0EC8-\u0ECD
104 \u0F18-\u0F19\u0F35\u0F37\u0F39\u0F3E\u0F3F\u0F71-\u0F84
105 \u0F86-\u0F8B\u0F90-\u0F95\u0F97\u0F99-\u0FAD\u0FB1-\u0FB7
106 \u0FB9\u20D0-\u20DC\u20E1\u302A-\u302F\u3099\u309A
108 const XML_EXTENDER = raw`
109 \u00B7\u02D0\u02D1\u0387\u0640\u0E46\u0EC6\u3005
110 \u3031-\u3035\u309D-\u309E\u30FC-\u30FE
112 const XML_NCNAMECHAR = String.raw`${XML_LETTER}${XML_DIGIT}\.\-_${XML_COMBINING}${XML_EXTENDER}`;
113 const XML_NCNAME = new RegExp(`^[${XML_LETTER}_][${XML_NCNAMECHAR}]*$`);
115 const URI_SUFFIX = /[A-Za-z_][0-9A-Za-z\.\-_]*$/;
116 const INDENT = /\n([ \t]*)$/;
117 const RDF_LISTITEM = /^http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#_\d+$/;
119 const RDF_NODE_INVALID_TYPES =
120 ["RDF", "ID", "about", "bagID", "parseType", "resource", "nodeID",
121 "li", "aboutEach", "aboutEachPrefix"];
122 const RDF_PROPERTY_INVALID_TYPES =
123 ["Description", "RDF", "ID", "about", "bagID", "parseType", "resource",
124 "nodeID", "aboutEach", "aboutEachPrefix"];
127 * Whether to use properly namespaces attributes for rdf:about etc...
128 * When on this produces poor output in the event that the rdf namespace is the
129 * default namespace, and the parser recognises unnamespaced attributes and
130 * most of our rdf examples are unnamespaced so leaving off for the time being.
132 const USE_RDFNS_ATTR = false;
134 var EXPORTED_SYMBOLS = ["RDFLiteral", "RDFIntLiteral", "RDFDateLiteral",
135 "RDFBlankNode", "RDFResource", "RDFDataSource"];
137 const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
139 XPCOMUtils.defineLazyGlobalGetters(this, ["DOMParser", "Element", "XMLSerializer", "fetch"]);
141 ChromeUtils.defineModuleGetter(this, "OS",
142 "resource://gre/modules/osfile.jsm");
143 ChromeUtils.defineModuleGetter(this, "Services",
144 "resource://gre/modules/Services.jsm");
146 function isAttr(obj) {
147 return obj && typeof obj == "object" && ChromeUtils.getClassName(obj) == "Attr";
149 function isDocument(obj) {
150 return obj && typeof obj == "object" && obj.nodeType == Element.DOCUMENT_NODE;
152 function isElement(obj) {
153 return Element.isInstance(obj);
155 function isText(obj) {
156 return obj && typeof obj == "object" && ChromeUtils.getClassName(obj) == "Text";
160 * Logs an error message to the error console
162 function ERROR(str) {
166 function RDF_R(name) {
167 return NS_RDF + name;
170 function renameNode(domnode, namespaceURI, qname) {
171 if (isElement(domnode)) {
172 var newdomnode = domnode.ownerDocument.createElementNS(namespaceURI, qname);
173 if ("listCounter" in domnode)
174 newdomnode.listCounter = domnode.listCounter;
175 domnode.replaceWith(newdomnode);
176 while (domnode.firstChild)
177 newdomnode.appendChild(domnode.firstChild);
178 for (let attr of domnode.attributes) {
179 domnode.removeAttributeNode(attr);
180 newdomnode.setAttributeNode(attr);
183 } else if (isAttr(domnode)) {
184 if (domnode.ownerElement.hasAttribute(namespaceURI, qname))
185 throw new Error("attribute already exists");
186 var attr = domnode.ownerDocument.createAttributeNS(namespaceURI, qname);
187 attr.value = domnode.value;
188 domnode.ownerElement.setAttributeNode(attr);
189 domnode.ownerElement.removeAttributeNode(domnode);
192 throw new Error("cannot rename node of this type");
195 function predicateOrder(a, b) {
196 return a.getPredicate().localeCompare(b.getPredicate());
200 * Returns either an rdf namespaced attribute or an un-namespaced attribute
201 * value. Returns null if neither exists,
203 function getRDFAttribute(element, name) {
204 if (element.hasAttributeNS(NS_RDF, name))
205 return element.getAttributeNS(NS_RDF, name);
206 if (element.hasAttribute(name))
207 return element.getAttribute(name);
212 * Represents an assertion in the datasource
215 constructor(subject, predicate, object) {
216 if (!(subject instanceof RDFSubject))
217 throw new Error("subject must be an RDFSubject");
219 if (typeof(predicate) != "string")
220 throw new Error("predicate must be a string URI");
222 if (!(object instanceof RDFLiteral) && !(object instanceof RDFSubject))
223 throw new Error("object must be a concrete RDFNode");
225 if (object instanceof RDFSubject && object._ds != subject._ds)
226 throw new Error("object must be from the same datasource as subject");
228 // The subject on this assertion, an RDFSubject
229 this._subject = subject;
230 // The predicate, a string
231 this._predicate = predicate;
232 // The object, an RDFNode
233 this._object = object;
234 // The datasource this assertion exists in
235 this._ds = this._subject._ds;
236 // Marks that _DOMnode is the subject's element
237 this._isSubjectElement = false;
238 // The DOM node that represents this assertion. Could be a property element,
239 // a property attribute or the subject's element for rdf:type
240 this._DOMNode = null;
244 * Adds content to _DOMnode to store this assertion in the DOM document.
247 if (this._object instanceof RDFLiteral)
248 this._object._applyToDOMNode(this._ds, this._DOMnode);
250 this._object._addReferenceToElement(this._DOMnode);
254 * Returns the DOM Element linked to the subject that this assertion is
257 _getSubjectElement() {
258 if (isAttr(this._DOMnode))
259 return this._DOMnode.ownerElement;
260 if (this._isSubjectElement)
261 return this._DOMnode;
262 return this._DOMnode.parentNode;
266 return this._subject;
270 return this._predicate;
280 return (rdfnode.constructor === this.constructor &&
281 rdfnode._value == this._value);
286 * A simple literal value
288 class RDFLiteral extends RDFNode {
295 * This stores the value of the literal in the given DOM node
297 _applyToDOMNode(ds, domnode) {
298 if (isElement(domnode))
299 domnode.textContent = this._value;
300 else if (isAttr(domnode))
301 domnode.value = this._value;
303 throw new Error("cannot use this node for a literal");
312 * A literal that is integer typed.
314 class RDFIntLiteral extends RDFLiteral {
316 super(parseInt(value));
320 * This stores the value of the literal in the given DOM node
322 _applyToDOMNode(ds, domnode) {
323 if (!isElement(domnode))
324 throw new Error("cannot use this node for a literal");
326 RDFLiteral.prototype._applyToDOMNode.call(this, ds, domnode);
327 var prefix = ds._resolvePrefix(domnode, `${NS_NC}parseType`);
328 domnode.setAttributeNS(prefix.namespaceURI, prefix.qname, "Integer");
333 * A literal that represents a date.
335 class RDFDateLiteral extends RDFLiteral {
337 if (!(value instanceof Date))
338 throw new Error("RDFDateLiteral must be constructed with a Date object");
344 * This stores the value of the literal in the given DOM node
346 _applyToDOMNode(ds, domnode) {
347 if (!isElement(domnode))
348 throw new Error("cannot use this node for a literal");
350 domnode.textContent = this._value.getTime();
351 var prefix = ds._resolvePrefix(domnode, `${NS_NC}parseType`);
352 domnode.setAttributeNS(prefix.namespaceURI, prefix.qname, "Date");
357 * This is an RDF node that can be a subject so a resource or a blank node
359 class RDFSubject extends RDFNode {
362 // A lookup of the assertions with this as the subject. Keyed on predicate
363 this._assertions = {};
364 // A lookup of the assertions with this as the object. Keyed on predicate
365 this._backwards = {};
366 // The datasource this subject belongs to
368 // The DOM elements in the document that represent this subject. Array of Element
373 * Creates a new Element in the document for holding assertions about this
374 * subject. The URI controls what tagname to use.
376 _createElement(uri) {
377 // Seek an appropriate reference to this node to add this node under
379 for (var p in this._backwards) {
380 for (let back of this._backwards[p]) {
381 // Don't add under an rdf:type
382 if (back.getPredicate() == RDF_R("type"))
384 // The assertion already has a child node, probably one of ours
385 if (back._DOMnode.firstChild)
387 parent = back._DOMnode;
388 var element = this._ds._addElement(parent, uri);
389 this._removeReferenceFromElement(parent);
396 // No back assertions that are sensible to use
398 element = this._ds._addElement(this._ds._document.documentElement, uri);
400 element.listCounter = 1;
401 this._applyToElement(element);
402 this._elements.push(element);
407 * When a DOM node representing this subject is removed from the document
408 * we must remove the node and recreate any child assertions elsewhere.
410 _removeElement(element) {
411 var pos = this._elements.indexOf(element);
413 throw new Error("invalid element");
414 this._elements.splice(pos, 1);
415 if (element.parentNode != element.ownerDocument.documentElement)
416 this._addReferenceToElement(element.parentNode);
417 this._ds._removeElement(element);
419 // Find all the assertions that are represented here and create new
421 for (var predicate in this._assertions) {
422 for (let assertion of this._assertions[predicate]) {
423 if (assertion._getSubjectElement() == element)
424 this._createDOMNodeForAssertion(assertion);
430 * Creates a DOM node to represent the assertion in the document. If the
431 * assertion has rdf:type as the predicate then an attempt will be made to
432 * create a typed subject Element, otherwise a new property Element is
433 * created. For list items an attempt is made to find an appropriate container
434 * that an rdf:li element can be added to.
436 _createDOMNodeForAssertion(assertion) {
438 if (RDF_LISTITEM.test(assertion.getPredicate())) {
439 // Find all the containers
440 elements = this._elements.filter(function(element) {
441 return (element.namespaceURI == NS_RDF && (element.localName == "Seq" ||
442 element.localName == "Bag" ||
443 element.localName == "Alt"));
445 if (elements.length > 0) {
446 // Look for one whose listCounter matches the item we want to add
447 var item = parseInt(assertion.getPredicate().substring(NS_RDF.length + 1));
448 for (let element of elements) {
449 if (element.listCounter == item) {
450 assertion._DOMnode = this._ds._addElement(element, RDF_R("li"));
451 assertion._applyToDOMNode();
452 element.listCounter++;
456 // No good container to add to, shove in the first real container
457 assertion._DOMnode = this._ds._addElement(elements[0], assertion.getPredicate());
458 assertion._applyToDOMNode();
461 // TODO No containers, this will end up in a non-container for now
462 } else if (assertion.getPredicate() == RDF_R("type")) {
463 // Try renaming an existing rdf:Description
464 for (let element of this.elements) {
465 if (element.namespaceURI == NS_RDF &&
466 element.localName == "Description") {
468 var prefix = this._ds._resolvePrefix(element.parentNode, assertion.getObject().getURI());
469 element = renameNode(element, prefix.namespaceURI, prefix.qname);
470 assertion._DOMnode = element;
471 assertion._isSubjectElement = true;
474 // If the type cannot be sensibly turned into a prefix then just set
475 // as a regular property
481 // Filter out all the containers
482 elements = this._elements.filter(function(element) {
483 return (element.namespaceURI != NS_RDF || (element.localName != "Seq" &&
484 element.localName != "Bag" &&
485 element.localName != "Alt"));
487 if (elements.length == 0) {
488 // Create a new node of the right type
489 if (assertion.getPredicate() == RDF_R("type")) {
491 assertion._DOMnode = this._createElement(assertion.getObject().getURI());
492 assertion._isSubjectElement = true;
495 // If the type cannot be sensibly turned into a prefix then just set
496 // as a regular property
499 elements[0] = this._createElement(RDF_R("Description"));
501 assertion._DOMnode = this._ds._addElement(elements[0], assertion.getPredicate());
502 assertion._applyToDOMNode();
506 * Removes the DOM node representing the assertion.
508 _removeDOMNodeForAssertion(assertion) {
509 if (isAttr(assertion._DOMnode)) {
510 var parent = assertion._DOMnode.ownerElement;
511 parent.removeAttributeNode(assertion._DOMnode);
512 } else if (assertion._isSubjectElement) {
513 var domnode = renameNode(assertion._DOMnode, NS_RDF, "Description");
514 if (domnode != assertion._DOMnode) {
515 var pos = this._elements.indexOf(assertion._DOMnode);
516 this._elements.splice(pos, 1, domnode);
520 var object = assertion.getObject();
521 if (object instanceof RDFSubject && assertion._DOMnode.firstChild) {
522 // Object is a subject that has an Element inside this assertion's node.
523 for (let element of object._elements) {
524 if (element.parentNode == assertion._DOMnode) {
525 object._removeElement(element);
530 parent = assertion._DOMnode.parentNode;
531 if (assertion._DOMnode.namespaceURI == NS_RDF &&
532 assertion._DOMnode.localName == "li")
533 parent.listCounter--;
534 this._ds._removeElement(assertion._DOMnode);
537 // If there are no assertions left using the assertion's containing dom node
538 // then remove it from the document.
539 // TODO could do with a quick lookup list for assertions attached to a node
540 for (var p in this._assertions) {
541 for (let assertion of this._assertions[p]) {
542 if (assertion._getSubjectElement() == parent)
546 // No assertions left in this element.
547 this._removeElement(parent);
551 * Parses the given Element from the DOM document
553 /* eslint-disable complexity */
554 _parseElement(element) {
555 this._elements.push(element);
557 // There might be an inferred rdf:type assertion in the element name
558 if (element.namespaceURI != NS_RDF ||
559 element.localName != "Description") {
560 if (element.namespaceURI == NS_RDF && element.localName == "li")
561 throw new Error("rdf:li is not a valid type for a subject node");
562 var assertion = new RDFAssertion(this, RDF_R("type"),
563 this._ds.getResource(element.namespaceURI + element.localName));
564 assertion._DOMnode = element;
565 assertion._isSubjectElement = true;
566 this._addAssertion(assertion);
569 // Certain attributes can be literal properties
570 for (let attr of element.attributes) {
571 if (attr.namespaceURI == NS_XML || attr.namespaceURI == NS_XMLNS ||
572 attr.nodeName == "xmlns")
574 if ((attr.namespaceURI == NS_RDF || !attr.namespaceURI) &&
575 (["nodeID", "about", "resource", "ID", "parseType"].includes(attr.localName)))
578 if (attr.namespaceURI == NS_RDF) {
579 if (attr.localName == "type")
580 object = this._ds.getResource(attr.nodeValue);
581 else if (attr.localName == "li")
582 throw new Error("rdf:li is not allowed as a property attribute");
583 else if (attr.localName == "aboutEach")
584 throw new Error("rdf:aboutEach is deprecated");
585 else if (attr.localName == "aboutEachPrefix")
586 throw new Error("rdf:aboutEachPrefix is deprecated");
587 else if (attr.localName == "aboutEach")
588 throw new Error("rdf:aboutEach is deprecated");
589 else if (attr.localName == "bagID")
590 throw new Error("rdf:bagID is deprecated");
593 object = new RDFLiteral(attr.nodeValue);
594 assertion = new RDFAssertion(this, attr.namespaceURI + attr.localName, object);
595 assertion._DOMnode = attr;
596 this._addAssertion(assertion);
599 var child = element.firstChild;
600 element.listCounter = 1;
602 if (isText(child) && /\S/.test(child.nodeValue)) {
603 ERROR(`Text ${child.nodeValue} is not allowed in a subject node`);
604 throw new Error("subject nodes cannot contain text content");
605 } else if (isElement(child)) {
607 var predicate = child.namespaceURI + child.localName;
608 if (child.namespaceURI == NS_RDF) {
609 if (RDF_PROPERTY_INVALID_TYPES.includes(child.localName) &&
610 !child.localName.match(/^_\d+$/))
611 throw new Error(`${child.nodeName} is an invalid property`);
612 if (child.localName == "li") {
613 predicate = RDF_R(`_${element.listCounter}`);
614 element.listCounter++;
618 // Check for and bail out on unknown attributes on the property element
619 for (let attr of child.attributes) {
620 // Ignore XML namespaced attributes
621 if (attr.namespaceURI == NS_XML)
623 // These are reserved by XML for future use
624 if (attr.localName.substring(0, 3).toLowerCase() == "xml")
626 // We can handle these RDF attributes
627 if ((!attr.namespaceURI || attr.namespaceURI == NS_RDF) &&
628 ["resource", "nodeID"].includes(attr.localName))
630 // This is a special attribute we handle for compatibility with Mozilla RDF
631 if (attr.namespaceURI == NS_NC &&
632 attr.localName == "parseType")
634 throw new Error(`Attribute ${attr.nodeName} is not supported`);
637 var parseType = child.getAttributeNS(NS_NC, "parseType");
638 if (parseType && parseType != "Date" && parseType != "Integer") {
639 ERROR(`parseType ${parseType} is not supported`);
640 throw new Error("unsupported parseType");
643 var resource = getRDFAttribute(child, "resource");
644 var nodeID = getRDFAttribute(child, "nodeID");
645 if ((resource && (nodeID || parseType)) ||
646 (nodeID && (resource || parseType))) {
647 ERROR("Cannot use more than one of parseType, resource and nodeID on a single node");
648 throw new Error("Invalid rdf assertion");
651 if (resource !== undefined) {
652 var base = Services.io.newURI(element.baseURI);
653 object = this._ds.getResource(base.resolve(resource));
654 } else if (nodeID !== undefined) {
655 if (!nodeID.match(XML_NCNAME))
656 throw new Error("rdf:nodeID must be a valid XML name");
657 object = this._ds.getBlankNode(nodeID);
660 var childElement = null;
661 var subchild = child.firstChild;
663 if (isText(subchild) && /\S/.test(subchild.nodeValue)) {
665 } else if (isElement(subchild)) {
667 new Error(`Multiple object elements found in ${child.nodeName}`);
669 childElement = subchild;
671 subchild = subchild.nextSibling;
674 if ((resource || nodeID) && (hasText || childElement)) {
675 ERROR("Assertion references a resource so should not contain additional contents");
676 throw new Error("assertion cannot contain multiple objects");
679 if (hasText && childElement) {
680 ERROR(`Both literal and resource objects found in ${child.nodeName}`);
681 throw new Error("assertion cannot contain multiple objects");
686 ERROR("Cannot specify a parseType for an assertion with resource object");
687 throw new Error("parseType is not valid in this context");
689 object = this._ds._getSubjectForElement(childElement);
690 object._parseElement(childElement);
691 } else if (parseType == "Integer") {
692 object = new RDFIntLiteral(child.textContent);
693 } else if (parseType == "Date") {
694 object = new RDFDateLiteral(new Date(child.textContent));
696 object = new RDFLiteral(child.textContent);
700 assertion = new RDFAssertion(this, predicate, object);
701 this._addAssertion(assertion);
702 assertion._DOMnode = child;
704 child = child.nextSibling;
707 /* eslint-enable complexity */
710 * Adds a new assertion to the internal hashes. Should be called for every
711 * new assertion parsed or created programmatically.
713 _addAssertion(assertion) {
714 var predicate = assertion.getPredicate();
715 if (predicate in this._assertions)
716 this._assertions[predicate].push(assertion);
718 this._assertions[predicate] = [ assertion ];
720 var object = assertion.getObject();
721 if (object instanceof RDFSubject) {
722 // Create reverse assertion
723 if (predicate in object._backwards)
724 object._backwards[predicate].push(assertion);
726 object._backwards[predicate] = [ assertion ];
731 * Removes an assertion from the internal hashes. Should be called for all
732 * assertions that are programmatically deleted.
734 _removeAssertion(assertion) {
735 var predicate = assertion.getPredicate();
736 if (predicate in this._assertions) {
737 var pos = this._assertions[predicate].indexOf(assertion);
739 this._assertions[predicate].splice(pos, 1);
740 if (this._assertions[predicate].length == 0)
741 delete this._assertions[predicate];
744 var object = assertion.getObject();
745 if (object instanceof RDFSubject) {
746 // Delete reverse assertion
747 if (predicate in object._backwards) {
748 pos = object._backwards[predicate].indexOf(assertion);
750 object._backwards[predicate].splice(pos, 1);
751 if (object._backwards[predicate].length == 0)
752 delete object._backwards[predicate];
758 * Returns the ordinal assertions from this subject in order.
760 _getChildAssertions() {
762 for (var i in this._assertions) {
763 if (RDF_LISTITEM.test(i))
764 assertions.push(...this._assertions[i]);
766 assertions.sort(predicateOrder);
771 * Compares this to another rdf node
774 // subjects are created by the datasource so no two objects ever correspond
776 return this === rdfnode;
780 * Adds a new assertion with this as the subject
782 assert(predicate, object) {
783 if (predicate == RDF_R("type") && !(object instanceof RDFResource))
784 throw new Error("rdf:type must be an RDFResource");
786 var assertion = new RDFAssertion(this, predicate, object);
787 this._createDOMNodeForAssertion(assertion);
788 this._addAssertion(assertion);
792 * Removes an assertion matching the predicate and node given, if such an
795 unassert(predicate, object) {
796 if (!(predicate in this._assertions))
799 for (let assertion of this._assertions[predicate]) {
800 if (assertion.getObject().equals(object)) {
801 this._removeAssertion(assertion);
802 this._removeDOMNodeForAssertion(assertion);
809 * Returns an array of all the predicates that exist in assertions from this
813 return Object.keys(this._assertions);
817 * Returns all objects in assertions with this subject and the given predicate.
819 getObjects(predicate) {
820 if (predicate in this._assertions)
821 return Array.from(this._assertions[predicate],
828 * Returns all of the ordinal children of this subject in order.
831 return Array.from(this._getChildAssertions(),
836 * Removes the child at the given index. This is the index based on the
837 * children returned from getChildren. Forces a reordering of the later
842 throw new Error("no such child");
843 var assertions = this._getChildAssertions();
844 if (pos >= assertions.length)
845 throw new Error("no such child");
846 for (var i = pos; i < assertions.length; i++) {
847 this._removeAssertion(assertions[i]);
848 this._removeDOMNodeForAssertion(assertions[i]);
852 index = parseInt(assertions[pos - 1].getPredicate().substring(NS_RDF.length + 1)) + 1;
853 for (let i = pos + 1; i < assertions.length; i++) {
854 assertions[i]._predicate = RDF_R(`_${index}`);
855 this._addAssertion(assertions[i]);
856 this._createDOMNodeForAssertion(assertions[i]);
862 * Removes the child with the given object. It is unspecified which child is
863 * removed if the object features more than once.
865 removeChild(object) {
866 var assertions = this._getChildAssertions();
867 for (var pos = 0; pos < assertions.length; pos++) {
868 if (assertions[pos].getObject().equals(object)) {
869 for (var i = pos; i < assertions.length; i++) {
870 this._removeAssertion(assertions[i]);
871 this._removeDOMNodeForAssertion(assertions[i]);
875 index = parseInt(assertions[pos - 1].getPredicate().substring(NS_RDF.length + 1)) + 1;
876 for (let i = pos + 1; i < assertions.length; i++) {
877 assertions[i]._predicate = RDF_R(`_${index}`);
878 this._addAssertion(assertions[i]);
879 this._createDOMNodeForAssertion(assertions[i]);
885 throw new Error("no such child");
889 * Adds a new ordinal child to this subject.
893 for (var i in this._assertions) {
894 if (RDF_LISTITEM.test(i))
895 max = Math.max(max, parseInt(i.substring(NS_RDF.length + 1)));
898 this.assert(RDF_R(`_${max}`), object);
902 * This reorders the child assertions to remove duplicates and gaps in the
903 * sequence. Generally this will move all children to be under the same
904 * container element and all represented as an rdf:li
907 var assertions = this._getChildAssertions();
908 for (let assertion of assertions) {
909 this._removeAssertion(assertion);
910 this._removeDOMNodeForAssertion(assertion);
913 for (let assertion of assertions) {
914 assertion._predicate = RDF_R(`_${index}`);
915 this._addAssertion(assertion);
916 this._createDOMNodeForAssertion(assertion);
922 * Returns the type of this subject or null if there is no specified type.
925 var type = this.getProperty(RDF_R("type"));
926 if (type && type instanceof RDFResource)
927 return type.getURI();
932 * Tests if a property exists for the given predicate.
934 hasProperty(predicate) {
935 return (predicate in this._assertions);
939 * Retrieves the first property value for the given predicate.
941 getProperty(predicate) {
942 if (predicate in this._assertions)
943 return this._assertions[predicate][0].getObject();
948 * Sets the property value for the given predicate, clearing any existing
951 setProperty(predicate, object) {
952 // TODO optimise by replacing the first assertion and clearing the rest
953 this.clearProperty(predicate);
954 this.assert(predicate, object);
958 * Clears any existing properties for the given predicate.
960 clearProperty(predicate) {
961 if (!(predicate in this._assertions))
964 var assertions = this._assertions[predicate];
965 while (assertions.length > 0) {
966 var assertion = assertions[0];
967 this._removeAssertion(assertion);
968 this._removeDOMNodeForAssertion(assertion);
974 * Creates a new RDFResource for the datasource. Private.
976 class RDFResource extends RDFSubject {
977 constructor(ds, uri) {
978 if (!(ds instanceof RDFDataSource))
979 throw new Error("datasource must be an RDFDataSource");
982 throw new Error("An RDFResource requires a non-null uri");
985 // This is the uri that the resource represents.
990 * Sets attributes on the DOM element to mark it as representing this resource
992 _applyToElement(element) {
993 if (USE_RDFNS_ATTR) {
994 var prefix = this._ds._resolvePrefix(element, RDF_R("about"));
995 element.setAttributeNS(prefix.namespaceURI, prefix.qname, this._uri);
997 element.setAttribute("about", this._uri);
1002 * Adds a reference to this resource to the given property Element.
1004 _addReferenceToElement(element) {
1005 if (USE_RDFNS_ATTR) {
1006 var prefix = this._ds._resolvePrefix(element, RDF_R("resource"));
1007 element.setAttributeNS(prefix.namespaceURI, prefix.qname, this._uri);
1009 element.setAttribute("resource", this._uri);
1014 * Removes any reference to this resource from the given property Element.
1016 _removeReferenceFromElement(element) {
1017 if (element.hasAttributeNS(NS_RDF, "resource"))
1018 element.removeAttributeNS(NS_RDF, "resource");
1019 if (element.hasAttribute("resource"))
1020 element.removeAttribute("resource");
1029 * Creates a new blank node. Private.
1031 class RDFBlankNode extends RDFSubject {
1032 constructor(ds, nodeID) {
1033 if (!(ds instanceof RDFDataSource))
1034 throw new Error("datasource must be an RDFDataSource");
1037 // The nodeID of this node. May be null if there is no ID.
1038 this._nodeID = nodeID;
1042 * Sets attributes on the DOM element to mark it as representing this node
1044 _applyToElement(element) {
1047 if (USE_RDFNS_ATTR) {
1048 var prefix = this._ds._resolvePrefix(element, RDF_R("nodeID"));
1049 element.setAttributeNS(prefix.namespaceURI, prefix.qname, this._nodeID);
1051 element.setAttribute("nodeID", this._nodeID);
1056 * Creates a new Element in the document for holding assertions about this
1057 * subject. The URI controls what tagname to use.
1059 _createNewElement(uri) {
1060 // If there are already nodes representing this in the document then we need
1061 // a nodeID to match them
1062 if (!this._nodeID && this._elements.length > 0) {
1063 this._ds._createNodeID(this);
1064 for (let element of this._elements)
1065 this._applyToElement(element);
1068 return super._createNewElement.call(uri);
1072 * Adds a reference to this node to the given property Element.
1074 _addReferenceToElement(element) {
1075 if (this._elements.length > 0 && !this._nodeID) {
1076 // In document elsewhere already
1077 // Create a node ID and update the other nodes referencing
1078 this._ds._createNodeID(this);
1079 for (let element of this._elements)
1080 this._applyToElement(element);
1084 if (USE_RDFNS_ATTR) {
1085 let prefix = this._ds._resolvePrefix(element, RDF_R("nodeID"));
1086 element.setAttributeNS(prefix.namespaceURI, prefix.qname, this._nodeID);
1088 element.setAttribute("nodeID", this._nodeID);
1091 // Add the empty blank node, this is generally right since further
1092 // assertions will be added to fill this out
1093 var newelement = this._ds._addElement(element, RDF_R("Description"));
1094 newelement.listCounter = 1;
1095 this._elements.push(newelement);
1100 * Removes any reference to this node from the given property Element.
1102 _removeReferenceFromElement(element) {
1103 if (element.hasAttributeNS(NS_RDF, "nodeID"))
1104 element.removeAttributeNS(NS_RDF, "nodeID");
1105 if (element.hasAttribute("nodeID"))
1106 element.removeAttribute("nodeID");
1110 return this._nodeID;
1115 * Creates a new RDFDataSource from the given document. The document will be
1116 * changed as assertions are added and removed to the RDF. Pass a null document
1117 * to start with an empty graph.
1119 class RDFDataSource {
1120 constructor(document) {
1121 // All known resources, indexed on URI
1122 this._resources = {};
1124 this._allBlankNodes = [];
1125 // All blank nodes with IDs, indexed on ID
1126 this._blankNodes = {};
1127 // Suggested prefixes to use for namespaces, index is prefix, value is namespaceURI.
1134 // Creating a document through xpcom leaves out the xml prolog so just parse
1136 var parser = Cc["@mozilla.org/xmlextras/domparser;1"].
1137 createInstance(Ci.nsIDOMParser);
1138 var doctext = `<?xml version="1.0"?>\n<rdf:RDF xmlns:rdf="${NS_RDF}"/>\n`;
1139 document = parser.parseFromString(doctext, "text/xml");
1141 // The underlying DOM document for this datasource
1142 this._document = document;
1143 this._parseDocument();
1146 static loadFromString(text) {
1147 let parser = new DOMParser();
1148 let document = parser.parseFromString(text, "application/xml");
1150 return new this(document);
1153 static loadFromBuffer(buffer) {
1154 let parser = new DOMParser();
1155 let document = parser.parseFromBuffer(new Uint8Array(buffer), "application/xml");
1157 return new this(document);
1160 static async loadFromFile(uri) {
1161 if (uri instanceof Ci.nsIFile)
1162 uri = Services.io.newFileURI(uri);
1163 else if (typeof(uri) == "string")
1164 uri = Services.io.newURI(uri);
1166 let resp = await fetch(uri.spec);
1167 return this.loadFromBuffer(await resp.arrayBuffer());
1171 return this._document.documentURI;
1175 * Creates a new nodeID for an unnamed blank node. Just node<number>.
1177 _createNodeID(blanknode) {
1179 while (`node${i}` in this._blankNodes)
1181 blanknode._nodeID = `node${i}`;
1182 this._blankNodes[blanknode._nodeID] = blanknode;
1186 * Returns an rdf subject for the given DOM Element. If the subject has not
1187 * been seen before a new one is created.
1189 _getSubjectForElement(element) {
1190 if (element.namespaceURI == NS_RDF &&
1191 RDF_NODE_INVALID_TYPES.includes(element.localName))
1192 throw new Error(`${element.nodeName} is not a valid class for a subject node`);
1194 var about = getRDFAttribute(element, "about");
1195 var id = getRDFAttribute(element, "ID");
1196 var nodeID = getRDFAttribute(element, "nodeID");
1198 if ((about && (id || nodeID)) ||
1199 (nodeID && (id || about))) {
1200 ERROR("More than one of about, ID and nodeID present on the same subject");
1201 throw new Error("invalid subject in rdf");
1204 if (about !== undefined) {
1205 let base = Services.io.newURI(element.baseURI);
1206 return this.getResource(base.resolve(about));
1208 if (id !== undefined) {
1209 if (!id.match(XML_NCNAME))
1210 throw new Error("rdf:ID must be a valid XML name");
1211 let base = Services.io.newURI(element.baseURI);
1212 return this.getResource(base.resolve(`#${id}`));
1214 if (nodeID !== undefined)
1215 return this.getBlankNode(nodeID);
1216 return this.getBlankNode(null);
1220 * Parses the document for subjects at the top level.
1223 if (!this._document.documentElement) {
1224 ERROR("No document element in document");
1225 throw new Error("document contains no root element");
1228 if (this._document.documentElement.namespaceURI != NS_RDF ||
1229 this._document.documentElement.localName != "RDF") {
1230 ERROR(`${this._document.documentElement.nodeName} is not rdf:RDF`);
1231 throw new Error("document does not appear to be RDF");
1234 var domnode = this._document.documentElement.firstChild;
1236 if (isText(domnode) && /\S/.test(domnode.nodeValue)) {
1237 ERROR("RDF does not allow for text in the root of the document");
1238 throw new Error("invalid markup in document");
1239 } else if (isElement(domnode)) {
1240 var subject = this._getSubjectForElement(domnode);
1241 subject._parseElement(domnode);
1243 domnode = domnode.nextSibling;
1248 * Works out a sensible namespace prefix to use for the given uri. node should
1249 * be the parent of where the element is to be inserted, or the node that an
1250 * attribute is to be added to. This will recursively walk to the top of the
1251 * document finding an already registered prefix that matches for the uri.
1252 * If none is found a new prefix is registered.
1253 * This returns an object with keys namespaceURI, prefix, localName and qname.
1254 * Pass null or undefined for badPrefixes for the first call.
1256 _resolvePrefix(domnode, uri, badPrefixes) {
1260 // No known prefix, try to create one from the lookup list
1261 if (!domnode || isDocument(domnode)) {
1262 for (let i in this._prefixes) {
1263 if (badPrefixes.includes(i))
1265 if (this._prefixes[i] == uri.substring(0, this._prefixes[i].length)) {
1266 var local = uri.substring(this._prefixes[i].length);
1267 var test = URI_SUFFIX.exec(local);
1268 // Remaining part of uri is a good XML Name
1269 if (test && test[0] == local) {
1270 this._document.documentElement.setAttributeNS(NS_XMLNS, `xmlns:${i}`, this._prefixes[i]);
1272 namespaceURI: this._prefixes[i],
1275 qname: i ? `${i}:${local}` : local,
1281 // No match, make something up
1282 test = URI_SUFFIX.exec(uri);
1284 var namespaceURI = uri.substring(0, uri.length - test[0].length);
1287 while (badPrefixes.includes(`NS${i}`))
1289 this._document.documentElement.setAttributeNS(NS_XMLNS, `xmlns:NS${i}`, namespaceURI);
1294 qname: `NS${i}:${local}`,
1297 // There is no end part of this URI that is an XML Name
1298 throw new Error(`invalid node name: ${uri}`);
1301 for (let attr of domnode.attributes) {
1302 // Not a namespace declaration, ignore this attribute
1303 if (attr.namespaceURI != NS_XMLNS && attr.nodeName != "xmlns")
1306 var prefix = attr.prefix ? attr.localName : "";
1307 // Seen this prefix before, cannot use it
1308 if (badPrefixes.includes(prefix))
1311 // Namespace matches the start of the uri
1312 if (attr.value == uri.substring(0, attr.value.length)) {
1313 local = uri.substring(attr.value.length);
1314 test = URI_SUFFIX.exec(local);
1315 // Remaining part of uri is a good XML Name
1316 if (test && test[0] == local) {
1318 namespaceURI: attr.value,
1321 qname: prefix ? `${prefix}:${local}` : local,
1326 badPrefixes.push(prefix);
1329 // No prefix found here, move up the document
1330 return this._resolvePrefix(domnode.parentNode, uri, badPrefixes);
1334 * Guess the indent level within the given Element. The method looks for
1335 * elements that are preceded by whitespace including a newline. The
1336 * whitespace following the newline is presumed to be the indentation for the
1338 * If the indentation cannot be guessed then it recurses up the document
1339 * hierarchy until it can guess the indent or until the Document is reached.
1341 _guessIndent(element) {
1342 // The indent at document level is 0
1343 if (!element || isDocument(element))
1346 // Check the text immediately preceding each child node. One could be
1349 var child = element.firstChild;
1351 if (isText(child)) {
1352 pretext += child.nodeValue;
1353 } else if (isElement(child)) {
1354 var result = INDENT.exec(pretext);
1359 child = child.nextSibling;
1362 // pretext now contains any trailing text in the element. This can be
1363 // the indent of the end tag. If so add a little to it.
1364 result = INDENT.exec(pretext);
1366 return `${result[1]} `;
1368 // Check the text immediately before this node
1370 var sibling = element.previousSibling;
1371 while (sibling && isText(sibling)) {
1372 pretext += sibling.nodeValue;
1373 sibling = sibling.previousSibling;
1376 // If there is a sensible indent then just add to it.
1377 result = INDENT.exec(pretext);
1379 return `${result[1]} `;
1381 // Last chance, get the indent level for the tag above and add to it
1382 return `${this._guessIndent(element.parentNode)} `;
1385 _addElement(parent, uri) {
1386 var prefix = this._resolvePrefix(parent, uri);
1387 var element = this._document.createElementNS(prefix.namespaceURI, prefix.qname);
1389 if (parent.lastChild) {
1390 // We want to insert immediately after the last child element
1391 var last = parent.lastChild;
1392 while (last && isText(last))
1393 last = last.previousSibling;
1394 // No child elements so insert at the start
1396 last = parent.firstChild;
1398 last = last.nextSibling;
1400 let indent = this._guessIndent(parent);
1401 parent.insertBefore(this._document.createTextNode(`\n${indent}`), last);
1402 parent.insertBefore(element, last);
1404 // No children, must indent our element and the end tag
1405 let indent = this._guessIndent(parent.parentNode);
1406 parent.append(`\n${indent} `, element, `\n${indent}`);
1412 * Removes the element from its parent. Should also remove surrounding
1413 * white space as appropriate.
1415 _removeElement(element) {
1416 var parent = element.parentNode;
1417 var sibling = element.previousSibling;
1418 // Drop any text nodes immediately preceding the element
1419 while (sibling && isText(sibling)) {
1421 sibling = sibling.previousSibling;
1422 parent.removeChild(temp);
1425 sibling = element.nextSibling;
1427 parent.removeChild(element);
1429 // If the next node after element is now the first child then element was
1430 // the first child. If there are no other child elements then remove the
1431 // remaining child nodes.
1432 if (parent.firstChild == sibling) {
1433 while (sibling && isText(sibling))
1434 sibling = sibling.nextSibling;
1436 // No other child elements
1437 while (parent.lastChild)
1438 parent.removeChild(parent.lastChild);
1444 * Requests that a given prefix be used for the namespace where possible.
1445 * This must be called before any assertions are made using the namespace
1446 * and the registration will not override any existing prefix used in the
1449 registerPrefix(prefix, namespaceURI) {
1450 this._prefixes[prefix] = namespaceURI;
1454 * Gets a blank node. nodeID may be null and if so a new blank node is created.
1455 * If a nodeID is given then the blank node with that ID is returned or created.
1457 getBlankNode(nodeID) {
1458 if (nodeID && nodeID in this._blankNodes)
1459 return this._blankNodes[nodeID];
1461 if (nodeID && !nodeID.match(XML_NCNAME))
1462 throw new Error("rdf:nodeID must be a valid XML name");
1464 var rdfnode = new RDFBlankNode(this, nodeID);
1465 this._allBlankNodes.push(rdfnode);
1467 this._blankNodes[nodeID] = rdfnode;
1472 * Gets all blank nodes
1474 getAllBlankNodes() {
1475 return this._allBlankNodes.slice();
1479 * Gets the resource for the URI. The resource is created if it has not been
1483 if (uri in this._resources)
1484 return this._resources[uri];
1486 var resource = new RDFResource(this, uri);
1487 this._resources[uri] = resource;
1492 * Gets all resources that have been used.
1495 return Object.values(this._resources);
1499 * Returns all blank nodes and resources
1502 return [...Object.values(this._resources),
1503 ...this._allBlankNodes];
1507 * Saves the RDF/XML to a string.
1509 serializeToString() {
1510 var serializer = new XMLSerializer();
1511 return serializer.serializeToString(this._document);
1515 * Saves the RDF/XML to a file.
1517 async saveToFile(file) {
1518 return OS.File.writeAtomic(file, new TextEncoder().encode(this.serializeToString()));