Repository navigation
Expand file tree
/
Copy pathscl-validating.ts
More file actions
459 lines (402 loc) · 15 KB
/
Copy pathscl-validating.ts
File metadata and controls
459 lines (402 loc) · 15 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* eslint-disable import/no-extraneous-dependencies */
import { LitElement, TemplateResult, css, html } from 'lit';
import { property, query, state } from 'lit/decorators.js';
import '@material/mwc-button';
import '@material/mwc-dialog';
import '@material/mwc-formfield';
import '@material/mwc-icon';
import '@material/mwc-icon-button-toggle';
import '@material/mwc-list';
import '@material/mwc-snackbar';
import '@material/mwc-switch';
import type { Button } from '@material/mwc-button';
import type { Dialog } from '@material/mwc-dialog';
import type { IconButtonToggle } from '@material/mwc-icon-button-toggle';
import type { Snackbar } from '@material/mwc-snackbar';
import { validate as validateOCL } from '@openenergytools/ocl-validator';
import type { ValidationIssue } from '@openenergytools/ocl-validator';
import { validate } from '@openenergytools/scl-template-validator';
import { Issue } from '@openenergytools/xml-schema-validator';
import { identity } from '@openscd/scl-lib';
import { validateSchema } from './schema/validateSchema.js';
/** An editor [[`plugin`]] to configure validators and display their issue centrally */
export default class SclValidatingPlugin extends LitElement {
/** The document being edited as provided to plugins by [[`OpenSCD`]]. */
@property({ attribute: false })
doc?: XMLDocument;
/** The name of the document being edited */
_docName = '';
@property()
set docName(docName: string) {
if (docName === '') return;
this.resetValidation();
this.autoValidate();
}
get docName(): string {
return this._docName;
}
/** SCL change indicator */
@property({ type: Number })
set editCount(count: number) {
if (this.doc) this.autoValidate();
}
/** Issues return by the schema validator */
@state() schemaIssues: Issue[] = [];
/** Issues returned by the template validator */
@state() templateIssues: Issue[] = [];
/** Issues returned by the OCL validator */
@state() oclIssues: ValidationIssue[] = [];
/** Whether schema validator has had the initial run */
@state() waitForSchemaRun = true;
/** Whether template validator has had the initial run */
@state() waitForTemplateRun = true;
/** Whether OCL validator has had the initial run */
@state() waitForOCLRun = true;
/** Whether schema validator shall run after each change to the doc */
@state() autoValidateSchema = false;
/** Whether template validator shall run after each change to the doc */
@state() autoValidateTemplate = false;
/** Whether OCL validator shall run after each change to the doc */
@state() autoValidateOCL = false;
@query('.content.dialog') dialog!: Dialog;
@query('.expand.template') expandTemplate!: IconButtonToggle;
@query('.expand.schema') expandSchema!: IconButtonToggle;
@query('.run.template') manualTriggerTemplateValidator!: Button;
@query('.run.schema') manualTriggerSchemaValidator!: Button;
@query('.alert.schema') alertSchemaIssue!: Snackbar;
@query('.alert.template') alertTemplateIssue!: Snackbar;
@query('.auto.schema') toggleAutoValidateSchema!: Snackbar;
@query('.auto.template') toggleAutoValidateTemplate!: Snackbar;
@query('.expand.ocl') expandOCL!: IconButtonToggle;
@query('.run.ocl') manualTriggerOCLValidator!: Button;
@query('.alert.ocl') alertOCLIssue!: Snackbar;
@query('.auto.ocl') toggleAutoValidateOCL!: Snackbar;
async run() {
this.dialog.show();
}
private async validateSchema(): Promise<void> {
this.schemaIssues.length = 0;
await this.requestUpdate('schemaIssues');
const result = await validateSchema(this.doc!, this.docName);
this.schemaIssues = result || [{ title: 'Invalid Schema!' }];
this.waitForSchemaRun = false;
if (this.schemaIssues.length) {
this.alertSchemaIssue.labelText =
this.schemaIssues[this.schemaIssues.length - 1].title;
this.alertSchemaIssue.show();
}
this.requestUpdate('schemaIssues');
}
private async validateTemplates(): Promise<void> {
this.templateIssues.length = 0;
this.waitForTemplateRun = false;
for await (const issue of validate(this.doc!)) {
this.templateIssues.push(...issue);
this.requestUpdate('templateIssues');
}
if (this.templateIssues.length) {
this.alertTemplateIssue.labelText =
this.templateIssues[this.templateIssues.length - 1].title;
this.alertTemplateIssue.show();
}
this.requestUpdate('templateIssues');
}
private async validateOCL(): Promise<void> {
this.oclIssues.length = 0;
this.waitForOCLRun = false;
for await (const issues of validateOCL(this.doc!, {
filename: this.docName,
})) {
this.oclIssues.push(...issues);
this.requestUpdate('oclIssues');
}
if (this.oclIssues.length) {
const lastIssue = this.oclIssues[this.oclIssues.length - 1];
const identityString = lastIssue.element
? identity(lastIssue.element)
: 'N/A';
this.alertOCLIssue.labelText = `[${lastIssue.severity}] ${lastIssue.id}: ${lastIssue.message} (${identityString})`;
this.alertOCLIssue.show();
}
this.requestUpdate('oclIssues');
}
private async autoValidate(): Promise<void> {
if (this.autoValidateTemplate) this.validateTemplates();
if (this.autoValidateSchema) this.validateSchema();
if (this.autoValidateOCL) this.validateOCL();
}
private async resetValidation(): Promise<void> {
this.schemaIssues.length = 0;
this.templateIssues.length = 0;
this.oclIssues.length = 0;
this.waitForSchemaRun = true;
this.waitForTemplateRun = true;
this.waitForOCLRun = true;
}
// eslint-disable-next-line class-methods-use-this
private renderValidatorsIssues(issues: Issue[]): TemplateResult[] {
if (issues.length === 0)
return [html`<li divider padded role="separator"></li>`];
return [
html`<li divider padded role="separator"></li>`,
...issues.map(
issue => html` <abbr title="${`${issue.title}\n${issue.message}`}"
><mwc-list-item ?twoline=${!!issue.message}>
<span> ${issue.title}</span>
<span slot="secondary">${issue.message}</span>
</mwc-list-item></abbr
>`
),
];
}
// eslint-disable-next-line class-methods-use-this
private getSeverityColor(severity: string): string {
switch (severity) {
case 'ERROR':
return '#d32f2f';
case 'WARNING':
return '#f57c00';
default:
return '#1976d2';
}
}
// eslint-disable-next-line class-methods-use-this
private getSeverityIcon(severity: string): string {
switch (severity) {
case 'ERROR':
return 'error';
case 'WARNING':
return 'warning';
default:
return 'info';
}
}
// eslint-disable-next-line class-methods-use-this
private renderOCLIssues(issues: ValidationIssue[]): TemplateResult[] {
if (issues.length === 0)
return [html`<li divider padded role="separator"></li>`];
return [
html`<li divider padded role="separator"></li>`,
...issues.map(issue => {
const severityColor = this.getSeverityColor(issue.severity);
const severityIcon = this.getSeverityIcon(issue.severity);
const identityString = issue.element ? identity(issue.element) : 'N/A';
return html` <abbr
title="${`[${issue.severity}] ${issue.id}\n${issue.message}\nElement: ${identityString}`}"
>
<mwc-list-item ?twoline=${!!issue.message} graphic="avatar">
<mwc-icon
slot="graphic"
style="color: ${severityColor}; background: ${severityColor}22; border-radius: 50%; padding: 4px;"
>${severityIcon}</mwc-icon
>
<span>
<span
style="color: ${severityColor}; font-weight: bold; margin-right: 8px;"
>[${issue.severity}]</span
>
${issue.id}
</span>
<span slot="secondary">
${issue.message}
<span
style="display: block; font-size: 0.85em; color: #666; font-family: monospace; margin-top: 4px;"
>📍 ${identityString}</span
>
</span>
</mwc-list-item></abbr
>`;
}),
];
}
private renderTemplateValidator(): TemplateResult {
return html`<div style="display: flex; flex-direction: row">
<div style="display: flex; flex-direction: column; flex: auto;">
<div style="display: flex; flex-direction: row">
<h3 style="flex:auto">
${`Template issues (${
this.waitForTemplateRun
? 'Run template validator'
: this.templateIssues.length
})`}
</h3>
<mwc-icon-button-toggle
class="expand template"
onIcon="expand_less"
offIcon="expand_more"
@click="${() => this.requestUpdate()}"
></mwc-icon-button-toggle>
</div>
<div style="display: flex; flex-direction: row">
<div style="display:flex; align-items:center; flex:auto">
<mwc-button
class="run template"
style="float: right"
label="${`${
this.waitForTemplateRun ? '' : 'Re-'
}Run manual validation`}"
@click="${this.validateTemplates}"
></mwc-button>
</div>
<div style="display: flex">
<mwc-formfield label="Auto validate on change" alignEnd>
<mwc-switch
class="auto template"
@click="${() => {
this.autoValidateTemplate = !this.autoValidateTemplate;
}}"
></mwc-switch>
</mwc-formfield>
</div>
</div>
</div>
</div>
${this.expandTemplate && this.expandTemplate.on
? html`<mwc-list>
<li divider padded role="separator"></li>
</mwc-list>`
: html`<mwc-list id="content" wrapFocus
>${this.renderValidatorsIssues(this.templateIssues)}</mwc-list
>`}`;
}
private renderOCLValidator(): TemplateResult {
return html`<div style="display: flex; flex-direction: row">
<div style="display: flex; flex-direction: column; flex: auto;">
<div style="display: flex; flex-direction: row">
<h3 style="flex:auto">
${`OCL issues (${
this.waitForOCLRun ? 'Run OCL validator' : this.oclIssues.length
})`}
</h3>
<mwc-icon-button-toggle
class="expand ocl"
onIcon="expand_less"
offIcon="expand_more"
@click="${() => this.requestUpdate()}"
></mwc-icon-button-toggle>
</div>
<div style="display: flex; flex-direction: row">
<div style="display:flex; align-items:center; flex:auto">
<mwc-button
class="run ocl"
style="float: right"
label="${`${
this.waitForOCLRun ? '' : 'Re-'
}Run OCL validation`}"
@click="${this.validateOCL}"
></mwc-button>
</div>
<div style="display: flex">
<mwc-formfield label="Auto validate on change" alignEnd>
<mwc-switch
class="auto ocl"
@click="${() => {
this.autoValidateOCL = !this.autoValidateOCL;
}}"
></mwc-switch>
</mwc-formfield>
</div>
</div>
</div>
</div>
${this.expandOCL && this.expandOCL.on
? html`<mwc-list>
<li divider padded role="separator"></li>
</mwc-list>`
: html`<mwc-list id="content" wrapFocus
>${this.renderOCLIssues(this.oclIssues)}</mwc-list
>`}`;
}
private renderSchemaValidator(): TemplateResult {
return html`<div style="display: flex; flex-direction: row">
<div style="display: flex; flex-direction: column; flex: auto;">
<div style="display: flex; flex-direction: row">
<h3 style="flex:auto">
${`Schema issues (${
this.waitForSchemaRun
? 'Run schema validator'
: this.schemaIssues.length
})`}
</h3>
<mwc-icon-button-toggle
class="expand schema"
onIcon="expand_less"
offIcon="expand_more"
@click="${() => this.requestUpdate()}"
></mwc-icon-button-toggle>
</div>
<div style="display: flex; flex-direction: row">
<div style="display:flex; align-items:center; flex:auto">
<mwc-button
class="run schema"
style="float: right"
label="${`${
this.waitForSchemaRun ? '' : 'Re-'
}Run manual validation`}"
@click="${this.validateSchema}"
></mwc-button>
</div>
<div style="display: flex">
<mwc-formfield label="Auto validate on change" alignEnd>
<mwc-switch
class="auto schema"
@click="${() => {
this.autoValidateSchema = !this.autoValidateSchema;
}}"
></mwc-switch>
</mwc-formfield>
</div>
</div>
</div>
</div>
${this.expandSchema && this.expandSchema.on
? html`<mwc-list>
<li divider padded role="separator"></li>
</mwc-list>`
: html`<mwc-list id="content" wrapFocus
>${this.renderValidatorsIssues(this.schemaIssues)}</mwc-list
>`}`;
}
render(): TemplateResult {
if (!this.doc)
return html`<mwc-dialog class="content dialog"
><div>No SCL file loaded, yet!</div>
<mwc-button
label="Cancel"
slot="secondaryAction"
dialogAction="close"
></mwc-button>
</mwc-dialog>`;
return html`<mwc-dialog class="content dialog">
${this.renderSchemaValidator()}${this.renderTemplateValidator()}${this.renderOCLValidator()}
<mwc-button
label="Cancel"
slot="secondaryAction"
dialogAction="close"
></mwc-button>
</mwc-dialog>
<mwc-snackbar class="alert schema" .timeoutMs=${5000}>
<mwc-button slot="action" @click=${() => this.dialog.show()}
>DETAIL</mwc-button
> </mwc-snackbar
><mwc-snackbar class="alert template" .timeoutMs=${4000}>
<mwc-button slot="action" @click=${() => this.dialog.show()}
>DETAIL</mwc-button
> </mwc-snackbar
><mwc-snackbar class="alert ocl" .timeoutMs=${6000}>
<mwc-button slot="action" @click=${() => this.dialog.show()}
>DETAIL</mwc-button
>
</mwc-snackbar>`;
}
static styles = css`
mwc-dialog {
--mdc-dialog-max-width: 90vw;
--mdc-dialog-min-width: 50vw;
}
abbr {
text-decoration: none;
border-bottom: none;
}
`;
}