-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXmlValidator.js
70 lines (57 loc) · 2.13 KB
/
XmlValidator.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import policySnippets from './PolicySnippets';
import PositionCalculator from './PositionCalculator';
class XmlValidator {
constructor(editor, monaco) {
this.editor = editor;
this.monaco = monaco;
this.positionCalculator = new PositionCalculator();
}
validate() {
const xmlDoc = this._parseXml();
const errors = [
...this._validatePolicyAttributes(xmlDoc),
];
this._setModelMarkers(errors);
return errors;
}
_parseXml() {
const xmlString = this.editor.getValue();
return new DOMParser().parseFromString(xmlString, 'text/xml');
}
_validatePolicyAttributes(xmlDoc) {
const errors = [];
policySnippets.forEach(snippet => {
const elements = xmlDoc.getElementsByTagName(snippet.label);
Array.from(elements).forEach(element => {
const allowedAttributes = snippet.attributes || [];
Array.from(element.attributes).forEach(attribute => {
if (!allowedAttributes.includes(attribute.name)) {
errors.push(this._createAttributeError(element, attribute));
}
});
});
});
return errors;
}
_createAttributeError(element, attribute) {
const xmlString = this.editor.getValue();
const lineNumber = this.positionCalculator.getLineNumber(xmlString, element);
const startColumn = this.positionCalculator.getColumnNumber(xmlString, element, attribute.name);
return {
severity: this.monaco.MarkerSeverity.Error,
message: `Unknown attribute: ${attribute.name}`,
startLineNumber: lineNumber,
startColumn: startColumn,
endLineNumber: lineNumber,
endColumn: startColumn + attribute.name.length,
};
}
_setModelMarkers(errors) {
this.monaco.editor.setModelMarkers(this.editor.getModel(), 'xml', errors);
}
}
export default XmlValidator;