js-options: setSystemId method

This commit is contained in:
Eric van der Vlist 2021-04-29 11:32:07 +02:00
parent 13be075795
commit b7bdb2ef27
2 changed files with 19 additions and 5 deletions

View File

@ -8,4 +8,7 @@ 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();
Packages.java.lang.System.out.println('options.systemId: ' + options.systemId);
Packages.java.lang.System.out.println('options.systemIdDigest: ' + options.systemIdDigest);
} }

View File

@ -6,16 +6,18 @@ Options = function (authorAccess, namespace) {
this.namespace = namespace; this.namespace = namespace;
this.authorAccess = authorAccess; this.authorAccess = authorAccess;
this.optionsStorage = this.authorAccess.getOptionsStorage(); this.optionsStorage = this.authorAccess.getOptionsStorage();
// Consider namespaces as a SYSTEM option
if (namespace != Options.SYSTEM) { if (namespace != Options.SYSTEM) {
var systemOptions = new Options(authorAccess, Options.SYSTEM); this.systemOptions = new Options(authorAccess, Options.SYSTEM);
var namespaces = JSON.parse(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();
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);
@ -49,9 +51,18 @@ Options.prototype.getOptions = function () {
Options.prototype.setSystemId = function(systemID) { Options.prototype.setSystemId = function(systemID) {
if (systemID === undefined) { if (systemID === undefined) {
this.systemID = this.authorAccess.getDocumentController().getAuthorDocumentNode().getSystemID(); this.systemId = String(this.authorAccess.getDocumentController().getAuthorDocumentNode().getSystemID());
} else { } else {
this.systemID = systemID; this.systemId = String(systemID);
} }
var md = java.security.MessageDigest.getInstance("SHA-1");
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);
var systemIds = JSON.parse(this.systemOptions.getOption(Options.SYSTEMIDS, '{}'));
systemIds[this.systemIdDigest] = {
date: (new Date()).toISOString(),
systemId: this.systemId
}
this.systemOptions.setOption(Options.SYSTEMIDS, JSON.stringify(systemIds));
} }