|
| 1 | +/* Much of this is modeled on strict-mods.js */ |
| 2 | +import xpath from 'xpath' |
| 3 | +import { DOMParser as xmldom } from '@xmldom/xmldom' |
| 4 | +import { |
| 5 | + safeSelect, |
| 6 | + copyAttributes, |
| 7 | + moveChildren, |
| 8 | + moveAndTransformElement, |
| 9 | + createElement, |
| 10 | + isElementEmpty, |
| 11 | + hasDirectTextContent |
| 12 | +} from './xml-helpers.js' |
| 13 | +import {convertPartNumbers, removeEmptyElements} from './strict-mods.js' |
| 14 | + |
| 15 | +/** |
| 16 | + * Main conversion function to convert Syllabus 'courseInfo' XML to MODS |
| 17 | + * |
| 18 | + * @param {string} xmlString - XML string to convert |
| 19 | + * @returns {string} Converted MODS XML string with namespace, ready for validation |
| 20 | + * @throws {Error} If XML is malformed, cannot be parsed, or input is invalid |
| 21 | + */ |
| 22 | +export function convertSyllabusXMLtoMODS(xmlString) { |
| 23 | + // Handle invalid input types |
| 24 | + if (typeof xmlString !== 'string') { |
| 25 | + throw new Error('XML input must be a string') |
| 26 | + } |
| 27 | + |
| 28 | + // Handle empty input |
| 29 | + if (!xmlString.trim()) { |
| 30 | + throw new Error('XML input cannot be empty') |
| 31 | + } |
| 32 | + |
| 33 | + const parser = new xmldom() |
| 34 | + let doc |
| 35 | + |
| 36 | + try { |
| 37 | + doc = parser.parseFromString(xmlString, 'text/xml') |
| 38 | + } catch (error) { |
| 39 | + throw new Error(`Failed to parse XML: ${error.message}`, { cause: error }) |
| 40 | + } |
| 41 | + |
| 42 | + // Check for parse errors in the document |
| 43 | + const parseError = doc.getElementsByTagName('parsererror')[0] |
| 44 | + if (parseError) { |
| 45 | + throw new Error(`XML parsing error: ${parseError.textContent}`) |
| 46 | + } |
| 47 | + |
| 48 | + // Ensure we have a root mods element |
| 49 | + const modsElements = safeSelect("//mods", doc) |
| 50 | + let mods |
| 51 | + if (modsElements.length > 0) { |
| 52 | + mods = modsElements[0] // default to first mods element if multiple exist |
| 53 | + } else { |
| 54 | + mods = createElement(doc, 'mods') |
| 55 | + } |
| 56 | + mods.setAttribute("xmlns", 'http://www.loc.gov/mods/v3') |
| 57 | + mods.setAttribute("version", '3.8') |
| 58 | + |
| 59 | + // We do this last in strict mods but it might simplify the tree to do it first here |
| 60 | + removeEmptyElements(doc) |
| 61 | + |
| 62 | + return mods.toString() |
| 63 | +} |
| 64 | + |
| 65 | +// CLI functionality - run when executed directly |
| 66 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 67 | + const fs = await import('fs') |
| 68 | + const path = await import('path') |
| 69 | + |
| 70 | + const args = process.argv.slice(2) |
| 71 | + |
| 72 | + if (args.length === 0 || args.includes('--help') || args.includes('-h')) { |
| 73 | + const helpText = `Usage: node syllabus.js <input-file> |
| 74 | +
|
| 75 | +Convert EQUELLA custom Syllabus "courseInfo" XML to schema-compliant MODS. |
| 76 | +` |
| 77 | + if (args.includes('--help') || args.includes('-h')) { |
| 78 | + console.log(helpText) |
| 79 | + process.exit(0) |
| 80 | + } else { |
| 81 | + console.error(helpText) |
| 82 | + process.exit(1) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + const inputFile = args.find(arg => !arg.startsWith('--')) |
| 87 | + |
| 88 | + if (!inputFile) { |
| 89 | + console.error('Error: No input file specified') |
| 90 | + process.exit(1) |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + const xmlString = fs.readFileSync(inputFile, 'utf-8') |
| 95 | + const result = convertSyllabusXMLtoMODS(xmlString) |
| 96 | + console.log(result.toString()) |
| 97 | + } catch (error) { |
| 98 | + console.error(`Error: ${error.message}`) |
| 99 | + process.exit(1) |
| 100 | + } |
| 101 | +} |
0 commit comments