From 2e28a4d737dbe5d0fe1acde7a8ee66f2245047c4 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Fri, 17 Apr 2020 12:22:13 +0100 Subject: [PATCH] XML Validator added --- src/core/config/Categories.json | 1 + src/core/operations/XMLValidator.mjs | 59 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/core/operations/XMLValidator.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 77e3d319..37a84aa5 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -356,6 +356,7 @@ "JSON Minify", "XML Beautify", "XML Minify", + "XML Validator", "SQL Beautify", "SQL Minify", "CSS Beautify", diff --git a/src/core/operations/XMLValidator.mjs b/src/core/operations/XMLValidator.mjs new file mode 100644 index 00000000..36799bd6 --- /dev/null +++ b/src/core/operations/XMLValidator.mjs @@ -0,0 +1,59 @@ +/** + * @author n1073645 [n1073645@gmail.com] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; + +/** + * XML Validator operation + */ +class XMLValidator extends Operation { + + /** + * XMLValidator constructor + */ + constructor() { + super(); + + this.name = "XML Validator"; + this.module = "Default"; + this.description = ""; + this.infoURL = ""; + this.inputType = "string"; + this.outputType = "string"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + + const DOMParser = require("xmldom").DOMParser; + + try { + // Overwrite error handler since the built-in one does not raise exceptions. + (new DOMParser({errorHandler: { + warning: (msg) => { + throw new OperationError(msg); + }, + error: (msg) => { + throw new OperationError(msg); + }, + fatalError: (msg) => { + throw new OperationError(msg); + }, + }})).parseFromString(input); + } catch (err) { + throw new OperationError(err); + } + return input; + } +} + +export default XMLValidator;