|
1 | 1 | /** HED schema loading functions. */ |
2 | 2 |
|
3 | 3 | /* Imports */ |
4 | | -import { IssueError } from '../../../src/issues/issues' |
5 | | -import parseSchemaXML from '../../../src/utils/xml.js' |
6 | 4 | import { schemaData } from './vite-importer' |
| 5 | +import { IssueError } from '../../../src/issues/issues' |
| 6 | +import AbstractHedSchemaLoader from '../../../src/schema/abstractLoader' |
7 | 7 |
|
8 | | -/** |
9 | | - * Load schema XML data from a schema version or path description. |
10 | | - * |
11 | | - * @param {SchemaSpec} schemaDef The description of which schema to use. |
12 | | - * @returns {Promise<object>} The schema XML data. |
13 | | - * @throws {IssueError} If the schema could not be loaded. |
14 | | - */ |
15 | | -export async function loadSchema(schemaDef = null) { |
16 | | - const xmlData = await loadPromise(schemaDef) |
17 | | - if (xmlData === null) { |
18 | | - IssueError.generateAndThrow('invalidSchemaSpecification', { spec: JSON.stringify(schemaDef) }) |
| 8 | +export default class HedSchemaLoader extends AbstractHedSchemaLoader { |
| 9 | + /** |
| 10 | + * Load schema XML data from a local file. |
| 11 | + * |
| 12 | + * @param {string} path - The path to the schema XML data. |
| 13 | + * @returns {Promise<never>} The schema XML data. |
| 14 | + * @throws {IssueError} If the schema could not be loaded. |
| 15 | + * @override |
| 16 | + */ |
| 17 | + async loadLocalSchema(path) { |
| 18 | + IssueError.generateAndThrow('localSchemaLoadFailed', { |
| 19 | + path, |
| 20 | + error: 'Local schema loading is not supported in the browser.', |
| 21 | + }) |
19 | 22 | } |
20 | | - return xmlData |
21 | | -} |
22 | 23 |
|
23 | | -/** |
24 | | - * Choose the schema Promise from a schema version or path description. |
25 | | - * |
26 | | - * @param {SchemaSpec} schemaDef The description of which schema to use. |
27 | | - * @returns {Promise<object>} The schema XML data. |
28 | | - * @throws {IssueError} If the schema could not be loaded. |
29 | | - */ |
30 | | -async function loadPromise(schemaDef) { |
31 | | - if (schemaDef === null) { |
32 | | - return null |
33 | | - } else if (schemaDef.localPath) { |
34 | | - return loadLocalSchema(schemaDef.localPath) |
35 | | - } else if (schemaDef.localName) { |
36 | | - return loadBundledSchema(schemaDef) |
37 | | - } else { |
38 | | - return loadRemoteSchema(schemaDef) |
| 24 | + /** |
| 25 | + * Determine whether this validator bundles a particular schema. |
| 26 | + * |
| 27 | + * @param {SchemaSpec} schemaDef - The description of which schema to use. |
| 28 | + * @returns {boolean} Whether this validator bundles a particular schema. |
| 29 | + * @override |
| 30 | + */ |
| 31 | + hasBundledSchema(schemaDef) { |
| 32 | + const localPath = `../../../src/data/schemas/${schemaDef.localName}.xml` |
| 33 | + return localPath in schemaData |
39 | 34 | } |
40 | | -} |
41 | 35 |
|
42 | | -/** |
43 | | - * Load schema XML data from the HED GitHub repository. |
44 | | - * |
45 | | - * @param {SchemaSpec} schemaDef The standard schema version to load. |
46 | | - * @returns {Promise<object>} The schema XML data. |
47 | | - * @throws {IssueError} If the schema could not be loaded. |
48 | | - */ |
49 | | -function loadRemoteSchema(schemaDef) { |
50 | | - let url |
51 | | - if (schemaDef.library) { |
52 | | - url = `https://raw.githubusercontent.com/hed-standard/hed-schemas/refs/heads/main/library_schemas/${schemaDef.library}/hedxml/HED_${schemaDef.library}_${schemaDef.version}.xml` |
53 | | - } else { |
54 | | - url = `https://raw.githubusercontent.com/hed-standard/hed-schemas/refs/heads/main/standard_schema/hedxml/HED${schemaDef.version}.xml` |
| 36 | + /** |
| 37 | + * Retrieve the contents of a bundled schema. |
| 38 | + * |
| 39 | + * @param {SchemaSpec} schemaDef - The description of which schema to use. |
| 40 | + * @returns {Promise<string>} The raw schema XML data. |
| 41 | + * @throws {IssueError} If the schema could not be loaded. |
| 42 | + * @override |
| 43 | + */ |
| 44 | + async getBundledSchema(schemaDef) { |
| 45 | + const localPath = `../../../src/data/schemas/${schemaDef.localName}.xml` |
| 46 | + const schemaLoader = schemaData[localPath] |
| 47 | + if (!schemaLoader) { |
| 48 | + // We've already verified this exists, so this is a consistency error. |
| 49 | + IssueError.generateAndThrowInternalError('Schema loader has disappeared after already being checked') |
| 50 | + } |
| 51 | + return await schemaLoader() |
55 | 52 | } |
56 | | - return loadSchemaFile( |
57 | | - fetch(url).then((res) => res.text()), |
58 | | - 'remoteSchemaLoadFailed', |
59 | | - { spec: JSON.stringify(schemaDef) }, |
60 | | - ) |
61 | 53 | } |
62 | 54 |
|
63 | 55 | /** |
64 | | - * Load schema XML data from a local file. |
| 56 | + * Load schema XML data from a schema version or path description. |
65 | 57 | * |
66 | | - * @param {string} path The path to the schema XML data. |
67 | | - * @returns {Promise<object>} The schema XML data. |
68 | | - * @throws {IssueError} If the schema could not be loaded. |
69 | | - */ |
70 | | -function loadLocalSchema(path) { |
71 | | - throw new Error('Local schema loading is not supported in the browser.') |
72 | | -} |
73 | | - |
74 | | -/** |
75 | | - * Load schema XML data from a bundled file. |
| 58 | + * @deprecated |
76 | 59 | * |
77 | 60 | * @param {SchemaSpec} schemaDef The description of which schema to use. |
78 | 61 | * @returns {Promise<object>} The schema XML data. |
79 | 62 | * @throws {IssueError} If the schema could not be loaded. |
80 | 63 | */ |
81 | | -async function loadBundledSchema(schemaDef) { |
82 | | - const localPath = `../../../src/data/schemas/${schemaDef.localName}.xml` |
83 | | - const schemaLoader = schemaData[localPath] |
84 | | - if (!schemaLoader) { |
85 | | - // This occurs in the test environment or if the schema is not found. |
86 | | - return |
87 | | - } |
88 | | - try { |
89 | | - const data = await schemaLoader() |
90 | | - return parseSchemaXML(data) |
91 | | - } catch (error) { |
92 | | - const issueArgs = { spec: JSON.stringify(schemaDef), error: error.message } |
93 | | - IssueError.generateAndThrow('bundledSchemaLoadFailed', issueArgs) |
94 | | - } |
95 | | -} |
96 | | - |
97 | | -/** |
98 | | - * Actually load the schema XML file. |
99 | | - * |
100 | | - * @param {Promise<string>} xmlDataPromise The Promise containing the unparsed XML data. |
101 | | - * @param {string} issueCode The issue code. |
102 | | - * @param {Object<string, string>} issueArgs The issue arguments passed from the calling function. |
103 | | - *returns {Promise<object>} The parsed schema XML data. |
104 | | - * @throws {IssueError} If the schema could not be loaded. |
105 | | - */ |
106 | | -async function loadSchemaFile(xmlDataPromise, issueCode, issueArgs) { |
107 | | - try { |
108 | | - const data = await xmlDataPromise |
109 | | - return parseSchemaXML(data) |
110 | | - } catch (error) { |
111 | | - issueArgs.error = error.message |
112 | | - IssueError.generateAndThrow(issueCode, issueArgs) |
113 | | - } |
| 64 | +export async function loadSchema(schemaDef = null) { |
| 65 | + return new HedSchemaLoader().loadSchema(schemaDef) |
114 | 66 | } |
0 commit comments