-
Notifications
You must be signed in to change notification settings - Fork 45
/
EMFValidationService.java
135 lines (119 loc) · 5.08 KB
/
EMFValidationService.java
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
/*******************************************************************************
* Copyright (c) 2021, 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.emf.services;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EValidator;
import org.eclipse.emf.ecore.EValidator.Registry;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.sirius.components.collaborative.validation.api.IValidationService;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext;
import org.springframework.stereotype.Service;
/**
* Used to validate EMF elements.
*
* @author gcoutable
*/
@Service
public class EMFValidationService implements IValidationService {
private final Registry eValidatorRegistry;
private final ComposedAdapterFactory composedAdapterFactory;
public EMFValidationService(EValidator.Registry eValidatorRegistry, ComposedAdapterFactory composedAdapterFactory) {
this.eValidatorRegistry = Objects.requireNonNull(eValidatorRegistry);
this.composedAdapterFactory = Objects.requireNonNull(composedAdapterFactory);
}
@Override
public List<Object> validate(IEditingContext editingContext) {
// @formatter:off
return Optional.of(editingContext)
.filter(IEMFEditingContext.class::isInstance)
.map(IEMFEditingContext.class::cast)
.map(this::validate)
.orElseGet(List::of);
// @formatter:on
}
@Override
public List<Object> validate(Object object, Object feature) {
if (object instanceof EObject) {
Diagnostician diagnostician = this.getNewDiagnostician();
Diagnostic diagnostic = diagnostician.validate((EObject) object);
if (Diagnostic.OK != diagnostic.getSeverity()) {
// @formatter:off
return diagnostic.getChildren().stream()
.filter(diag -> this.filterDiagnosticByObject(diag, object))
.filter(diag -> this.filterDiagnosticByFeature(diag, feature))
.map(Object.class::cast)
.toList();
// @formatter:on
}
}
return List.of();
}
private boolean filterDiagnosticByObject(Diagnostic diagnostic, Object object) {
if (diagnostic.getData() != null && !diagnostic.getData().isEmpty() && object != null) {
// @formatter:off
return diagnostic.getData().stream()
.anyMatch(object::equals);
// @formatter:on
}
return false;
}
private boolean filterDiagnosticByFeature(Diagnostic diagnostic, Object feature) {
if (diagnostic.getData() != null && !diagnostic.getData().isEmpty() && feature != null) {
// @formatter:off
return diagnostic.getData().stream()
.anyMatch(feature::equals);
// @formatter:on
}
return false;
}
private List<Object> validate(IEMFEditingContext editingContext) {
AdapterFactoryEditingDomain domain = editingContext.getDomain();
Map<Object, Object> options = new HashMap<>();
options.put(Diagnostician.VALIDATE_RECURSIVELY, true);
Diagnostician diagnostician = this.getNewDiagnostician();
// @formatter:off
return domain.getResourceSet().getResources().stream()
.map(Resource::getContents)
.flatMap(Collection::stream)
.map(eObject -> diagnostician.validate(eObject, options))
.map(Diagnostic::getChildren)
.flatMap(Collection::stream)
.map(Object.class::cast)
.toList();
// @formatter:on
}
private Diagnostician getNewDiagnostician() {
return new Diagnostician(this.eValidatorRegistry) {
@Override
public String getObjectLabel(EObject eObject) {
IItemLabelProvider itemLabelProvider = (IItemLabelProvider) EMFValidationService.this.composedAdapterFactory.adapt(eObject, IItemLabelProvider.class);
if (itemLabelProvider != null) {
return itemLabelProvider.getText(eObject);
}
return super.getObjectLabel(eObject);
}
};
}
}