lincd
Version:
LINCD is a JavaScript library for building user interfaces with linked data (also known as 'structured data', or RDF)
94 lines • 2.88 kB
JavaScript
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { CoreMap } from '../collections/CoreMap.js';
export class Prefix {
static getUriToPrefixMap() {
return this.uriToPrefix;
}
static getPrefixToUriMap() {
return this.prefixToUri;
}
static add(prefix, fullURI) {
this.uriToPrefix.set(fullURI, prefix);
this.prefixToUri.set(prefix, fullURI);
}
static delete(prefix) {
if (this.prefixToUri.has(prefix)) {
let fullURI = this.getFullURI(prefix);
this.uriToPrefix.delete(fullURI);
this.prefixToUri.delete(prefix);
}
}
static clear() {
this.uriToPrefix = new CoreMap();
this.prefixToUri = new CoreMap();
}
static getPrefix(fullURI) {
if (this.uriToPrefix.has(fullURI)) {
return this.uriToPrefix.get(fullURI);
}
let match = this.findMatch(fullURI);
if (match.length > 0)
return match[1];
}
static getFullURI(prefix) {
return this.prefixToUri.get(prefix);
}
static findMatch(fullURI) {
for (let [ontologyURI, prefix] of this.uriToPrefix.entries()) {
if (fullURI.substring(0, ontologyURI.length) == ontologyURI) {
return [ontologyURI, prefix, fullURI.substring(ontologyURI.length)];
}
}
return [];
}
static toPrefixed(fullURI) {
let match = this.findMatch(fullURI);
if (match.length > 0) {
const postFix = fullURI.substring(match[0].length);
if (!postFix.includes('/')) {
return match[1] + ':' + postFix;
}
}
}
static toPrefixedIfPossible(fullURI) {
return this.toPrefixed(fullURI) || fullURI;
}
static toFullIfPossible(fullURI) {
let res = this._toFull(fullURI);
if (res) {
return res;
}
return fullURI;
}
/**
* Converts a prefixed URI back to its full URI
* Will return the prefixed URI if no prefix was found
* @param uri
*/
static toFull(uri) {
let res = this._toFull(uri);
if (res) {
return res;
}
let [prefix, rest] = uri.split(':');
throw new Error('Unknown prefix ' +
prefix +
'. Could not convert ' +
uri +
' to a full URI');
}
static _toFull(uri) {
let [prefix, rest] = uri.split(':');
let ontologyURI = this.getFullURI(prefix);
if (ontologyURI) {
return ontologyURI + rest;
}
}
}
Prefix.uriToPrefix = new CoreMap();
Prefix.prefixToUri = new CoreMap();
//# sourceMappingURL=Prefix.js.map