-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathxml-validator.ts
52 lines (45 loc) · 1.47 KB
/
xml-validator.ts
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
46
47
48
49
50
51
52
///<reference path=".d.ts"/>
"use strict";
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[]): IFuture<boolean> {
return (() => {
let xmlHasErrors = false;
sourceFiles
.filter(file => _.endsWith(file, constants.XML_FILE_EXTENSION))
.forEach(file => {
let errorOutput = this.getXmlFileErrors(file).wait();
let hasErrors = !!errorOutput;
xmlHasErrors = xmlHasErrors || hasErrors;
if (hasErrors) {
this.$logger.info(`${file} has syntax errors.`.red.bold);
this.$logger.out(errorOutput.yellow);
}
});
return !xmlHasErrors;
}).future<boolean>()();
}
public getXmlFileErrors(sourceFile: string): IFuture<string> {
return ((): string => {
let errorOutput = "";
let fileContents = this.$fs.readText(sourceFile).wait();
let domErrorHandler = (level:any, msg:string) => {
errorOutput += level + EOL + msg + EOL;
};
this.getDomParser(domErrorHandler).parseFromString(fileContents, "text/xml");
return errorOutput || null;
}).future<string>()();
}
private getDomParser(errorHandler: (level:any, msg:string) => void): any {
let DomParser = require("xmldom").DOMParser;
let parser = new DomParser({
locator:{},
errorHandler: errorHandler
});
return parser;
}
}
$injector.register("xmlValidator", XmlValidator);