js-options: per file methods

This commit is contained in:
Eric van der Vlist 2021-04-29 12:04:41 +02:00
parent b7bdb2ef27
commit 03794da2ba
2 changed files with 42 additions and 7 deletions

View File

@ -8,7 +8,13 @@ hello = function () {
var value = myOptions[key]; var value = myOptions[key];
Packages.java.lang.System.out.println(key + ' ' + value); Packages.java.lang.System.out.println(key + ' ' + value);
} }
options.setSystemId(); options.setSystemIdOption('Hello', 'aligator');
options.setSystemIdOption('See you later', 'World');
var myOptions = options.getSystemIdOptions();
for (var key in myOptions) {
var value = myOptions[key];
Packages.java.lang.System.out.println(key + ' ' + value);
}
Packages.java.lang.System.out.println('options.systemId: ' + options.systemId); Packages.java.lang.System.out.println('options.systemId: ' + options.systemId);
Packages.java.lang.System.out.println('options.systemIdDigest: ' + options.systemIdDigest); Packages.java.lang.System.out.println('options.systemIdDigest: ' + options.systemIdDigest);
} }

View File

@ -10,14 +10,13 @@ Options = function (authorAccess, namespace) {
if (namespace != Options.SYSTEM) { if (namespace != Options.SYSTEM) {
this.systemOptions = new Options(authorAccess, Options.SYSTEM); this.systemOptions = new Options(authorAccess, Options.SYSTEM);
var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}')); var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}'));
namespaces[this.namespace] = (new Date()).toISOString(); namespaces[ this.namespace] = (new Date()).toISOString();
this.systemOptions.setOption(Options.NAMESPACES, JSON.stringify(namespaces)) this.systemOptions.setOption(Options.NAMESPACES, JSON.stringify(namespaces))
} }
} }
Options.SYSTEM = '__system__'; Options.SYSTEM = '__system__';
Options.NAMESPACES = '__namespaces__'; Options.NAMESPACES = '__namespaces__';
Options.SYSTEMIDS = '__systemIds__';
Options.prototype.getOption = function (key, defaultValue) { Options.prototype.getOption = function (key, defaultValue) {
this.optionsStorage.setOptionsDoctypePrefix(this.namespace); this.optionsStorage.setOptionsDoctypePrefix(this.namespace);
@ -47,9 +46,18 @@ Options.prototype.getOptions = function () {
/* /*
* Per file extensions * Per file extensions
* *
* Setting a systemID creates a new namespace that concatenates the current namespace
* and the SHA1 of the document ID.
*
* Access to the options of this namespace is done through specific methods.
*
* The reverse mapping (SHA1 to system ID) is stored using the __systemIds__ namespace.
*
*/ */
Options.prototype.setSystemId = function(systemID) { Options.SYSTEMIDS = '__systemIds__';
Options.prototype.setSystemId = function (systemID) {
if (systemID === undefined) { if (systemID === undefined) {
this.systemId = String(this.authorAccess.getDocumentController().getAuthorDocumentNode().getSystemID()); this.systemId = String(this.authorAccess.getDocumentController().getAuthorDocumentNode().getSystemID());
} else { } else {
@ -59,10 +67,31 @@ Options.prototype.setSystemId = function(systemID) {
var digestBytes = md.digest(java.lang.String(this.systemId).getBytes(java.nio.charset.Charset.forName("UTF-8"))); var digestBytes = md.digest(java.lang.String(this.systemId).getBytes(java.nio.charset.Charset.forName("UTF-8")));
this.systemIdDigest = javax.xml.bind.DatatypeConverter.printHexBinary(digestBytes); this.systemIdDigest = javax.xml.bind.DatatypeConverter.printHexBinary(digestBytes);
var systemIds = JSON.parse(this.systemOptions.getOption(Options.SYSTEMIDS, '{}')); var systemIds = JSON.parse(this.systemOptions.getOption(Options.SYSTEMIDS, '{}'));
systemIds[this.systemIdDigest] = { systemIds[ this.systemIdDigest] = {
date: (new Date()).toISOString(), date: (new Date()).toISOString(),
systemId: this.systemId systemId: this.systemId
} }
this.systemOptions.setOption(Options.SYSTEMIDS, JSON.stringify(systemIds)); this.systemOptions.setOption(Options.SYSTEMIDS, JSON.stringify(systemIds));
this.systemIdOptions = new Options(this.authorAccess, this.namespace + '.' + this.systemIdDigest);
} }
Options.prototype.setSystemIdOption = function (key, value) {
if (this.systemIdOptions === undefined) {
this.setSystemId();
}
this.systemIdOptions.setOption(key, value);
}
Options.prototype.getSystemIdOption = function (key, defaultValue) {
if (this.systemIdOptions === undefined) {
this.setSystemId();
}
return this.systemIdOptions.getOption(key, defaultValue);
}
Options.prototype.getSystemIdOptions = function () {
if (this.systemIdOptions === undefined) {
this.setSystemId();
}
return this.systemIdOptions.getOptions();
}