-
-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathxml-validator.ts
More file actions
45 lines (39 loc) · 1.3 KB
/
xml-validator.ts
File metadata and controls
45 lines (39 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { EOL } from "os";
import * as constants from "./constants";
export class XmlValidator implements IXmlValidator {
constructor(private $fs: IFileSystem,
private $logger: ILogger) { }
public validateXmlFiles(sourceFiles: string[]): boolean {
let xmlHasErrors = false;
sourceFiles
.filter(file => _.endsWith(file, constants.XML_FILE_EXTENSION))
.forEach(file => {
const errorOutput = this.getXmlFileErrors(file);
const hasErrors = !!errorOutput;
xmlHasErrors = xmlHasErrors || hasErrors;
if (hasErrors) {
this.$logger.info(`${file} has syntax errors.`.red.bold);
this.$logger.info(errorOutput.yellow);
}
});
return !xmlHasErrors;
}
public getXmlFileErrors(sourceFile: string): string {
let errorOutput = "";
const fileContents = this.$fs.readText(sourceFile);
const domErrorHandler = (level: any, msg: string) => {
errorOutput += level + EOL + msg + EOL;
};
this.getDomParser(domErrorHandler).parseFromString(fileContents, "text/xml");
return errorOutput || null;
}
private getDomParser(errorHandler: (level: any, msg: string) => void): any {
const DomParser = require("xmldom").DOMParser;
const parser = new DomParser({
locator: {},
errorHandler: errorHandler
});
return parser;
}
}
$injector.register("xmlValidator", XmlValidator);