Skip to content

Package: ValidationService

ValidationService

nameinstructionbranchcomplexitylinemethod
ValidationService(IExcludedObjectsCallback)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
createdCachedTreeNode(Diagnostic)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getDefaultValue()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getDiagnostic(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getRootDiagnostic()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getSeverity(EObject)
M: 4 C: 34
89%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 8
89%
M: 0 C: 1
100%
validate(Collection)
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
validate(EObject)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2012 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: ******************************************************************************/
13: package org.eclipse.emf.ecp.internal.validation;
14:
15: import java.util.Collection;
16: import java.util.HashMap;
17: import java.util.HashSet;
18: import java.util.Map;
19: import java.util.Set;
20:
21: import org.eclipse.emf.common.util.BasicDiagnostic;
22: import org.eclipse.emf.common.util.Diagnostic;
23: import org.eclipse.emf.ecore.EObject;
24: import org.eclipse.emf.ecore.EValidator;
25: import org.eclipse.emf.ecore.util.Diagnostician;
26: import org.eclipse.emf.ecore.util.EObjectValidator;
27: import org.eclipse.emf.ecp.common.spi.cachetree.AbstractCachedTree;
28: import org.eclipse.emf.ecp.common.spi.cachetree.CachedTreeNode;
29: import org.eclipse.emf.ecp.common.spi.cachetree.IExcludedObjectsCallback;
30: import org.eclipse.emf.ecp.validation.api.IValidationService;
31:
32: /**
33: * Implementation of a validation service.
34: *
35: * @author emueller
36: * @author Tobias Verhoeven
37: *
38: */
39: public final class ValidationService extends AbstractCachedTree<Diagnostic> implements IValidationService {
40:
41:         /**
42:          * Constructor for the ECP ValidationService.
43:          *
44:          * @param callback to use
45:          */
46:         public ValidationService(IExcludedObjectsCallback callback) {
47:                 super(callback);
48:         }
49:
50:         /**
51:          * Tree node that caches the severity of its children.
52:          */
53:         public class CachedSeverityTreeNode extends CachedTreeNode<Diagnostic> {
54:
55:                 /**
56:                  * Constructor.
57:                  *
58:                  * @param diagnostic
59:                  * the initial diagnostic containing the severity and validation message
60:                  */
61:                 public CachedSeverityTreeNode(Diagnostic diagnostic) {
62:                         super(diagnostic);
63:                 }
64:
65:                 /**
66:                  * {@inheritDoc}
67:                  */
68:                 @Override
69:                 public void update() {
70:                         final Collection<Diagnostic> severities = values();
71:
72:                         if (severities.size() > 0) {
73:                                 Diagnostic mostSevereDiagnostic = values().iterator().next();
74:                                 for (final Diagnostic diagnostic : severities) {
75:                                         if (diagnostic.getSeverity() > mostSevereDiagnostic.getSeverity()) {
76:                                                 mostSevereDiagnostic = diagnostic;
77:                                         }
78:                                 }
79:                                 setChildValue(mostSevereDiagnostic);
80:                                 return;
81:                         }
82:                         setChildValue(getDefaultValue());
83:                 }
84:
85:                 @Override
86:                 public Diagnostic getDisplayValue() {
87:                         if (getChildValue() == null) {
88:                                 return getOwnValue();
89:                         }
90:                         return getOwnValue().getSeverity() > getChildValue().getSeverity() ? getOwnValue() : getChildValue();
91:                 }
92:         }
93:
94:         /**
95:          * {@inheritDoc}
96:          */
97:         @Override
98:         public Set<EObject> validate(Collection<EObject> eObjects) {
99:                 final Set<EObject> allAffected = new HashSet<EObject>();
100:•                for (final EObject eObject : eObjects) {
101:                         final Set<EObject> affected = validate(eObject);
102:                         allAffected.addAll(affected);
103:                 }
104:                 return allAffected;
105:         }
106:
107:         /**
108:          * {@inheritDoc}
109:          */
110:         @Override
111:         public Set<EObject> validate(EObject eObject) {
112:                 return update(eObject, getSeverity(eObject));
113:         }
114:
115:         /**
116:          * {@inheritDoc}
117:          */
118:         @Override
119:         public Diagnostic getDiagnostic(Object eObject) {
120:                 return getCachedValue(eObject);
121:         }
122:
123:         /**
124:          * {@inheritDoc}
125:          */
126:         @Override
127:         public Diagnostic getRootDiagnostic() {
128:                 return getRootValue();
129:         }
130:
131:         /**
132:          * {@inheritDoc}
133:          */
134:         @Override
135:         public Diagnostic getDefaultValue() {
136:                 return Diagnostic.OK_INSTANCE;
137:         }
138:
139:         /**
140:          * {@inheritDoc}
141:          */
142:         @Override
143:         public CachedTreeNode<Diagnostic> createdCachedTreeNode(Diagnostic diagnostic) {
144:                 return new CachedSeverityTreeNode(diagnostic);
145:         }
146:
147:         private Diagnostic getSeverity(EObject object) {
148:                 EValidator validator = EValidator.Registry.INSTANCE.getEValidator(object.eClass().getEPackage());
149:                 final BasicDiagnostic diagnostics = Diagnostician.INSTANCE.createDefaultDiagnostic(object);
150:
151:•                if (validator == null) {
152:                         validator = new EObjectValidator();
153:                 }
154:                 final Map<Object, Object> context = new HashMap<Object, Object>();
155:                 context.put(EValidator.SubstitutionLabelProvider.class, Diagnostician.INSTANCE);
156:                 context.put(EValidator.class, validator);
157:
158:                 validator.validate(object, diagnostics, context);
159:                 return diagnostics;
160:
161:         }
162:
163: }