@kit-data-manager/pid-component
Version:
The PID-Component is a web component that can be used to evaluate and display FAIR Digital Objects, PIDs, ORCiDs, and possibly other identifiers in a user-friendly way. It is easily extensible to support other identifier types.
119 lines (118 loc) • 4.56 kB
JavaScript
/*!
*
* Copyright 2024 Karlsruhe Institute of Technology.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { locationType, PID } from "./PID";
import { typeMap, unresolvables } from "../../utils/utils";
import { cachedFetch } from "../../utils/DataCache";
export class PIDDataType {
constructor(pid, name, description, redirectURL, regex) {
this._pid = pid;
this._name = name;
this._description = description;
this._regex = regex;
this._redirectURL = redirectURL;
}
get pid() {
return this._pid;
}
get name() {
return this._name;
}
get description() {
return this._description;
}
get redirectURL() {
return this._redirectURL;
}
get regex() {
return this._regex;
}
static async resolveDataType(pid) {
if (typeMap.has(pid))
return typeMap.get(pid);
if (!pid.isResolvable()) {
console.debug(`PID ${pid.toString()} has been marked as unresolvable`);
return undefined;
}
const pidRecord = await pid.resolve();
if (pidRecord === undefined) {
console.debug(`PID ${pid.toString()} could not be resolved via the API`);
unresolvables.add(pid);
return undefined;
}
const tempDataType = { name: '', description: '', redirectURL: '', ePICJSON: {} };
for (let i = 0; i < pidRecord.values.length; i++) {
const currentValue = pidRecord.values[i];
if (currentValue.type === locationType || currentValue.type.toString() === locationType.toString()) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(currentValue.data.value, 'text/xml');
const xmlLocations = xmlDoc.getElementsByTagName('location');
for (let j = 0; j < xmlLocations.length; j++) {
const newLocation = {
href: xmlLocations[j].getAttribute('href'),
weight: undefined,
view: undefined,
resolvedData: undefined,
};
try {
newLocation.weight = parseInt(xmlLocations[j].getAttribute('weight'));
}
catch (ignored) { }
try {
newLocation.view = xmlLocations[j].getAttribute('view');
}
catch (ignored) { }
try {
if (newLocation.view === 'json') {
newLocation.resolvedData = await cachedFetch(newLocation.href);
tempDataType.ePICJSON = newLocation.resolvedData;
tempDataType.name = newLocation.resolvedData['name'];
tempDataType.description = newLocation.resolvedData['description'];
}
else {
tempDataType.redirectURL = newLocation.href;
}
}
catch (ignored) { }
}
}
}
try {
const type = new PIDDataType(pid, tempDataType.name, tempDataType.description, tempDataType.redirectURL, tempDataType.regex);
typeMap.set(pid, type);
return type;
}
catch (e) {
console.error(e);
return undefined;
}
}
static fromJSON(serialized) {
const data = JSON.parse(serialized);
return new PIDDataType(PID.fromJSON(data.pid), data.name, data.description, data.redirectURL, data.regex);
}
toObject() {
return {
pid: JSON.stringify(this._pid.toObject()),
name: this._name,
description: this._description,
redirectURL: this._redirectURL,
regex: this._regex,
};
}
}
//# sourceMappingURL=PIDDataType.js.map