diff options
author | Ansgar Radermacher | 2015-12-15 12:29:32 +0000 |
---|---|---|
committer | Ansgar Radermacher | 2015-12-15 12:31:36 +0000 |
commit | 0011d5cbd8f0fd5a7f3edca3466e477b0f65a9ca (patch) | |
tree | bdb6332c0f149c511496e38ffa2bb1e07094aaf1 | |
parent | 7750db682e6076f054470dcce56da2a3d1116a84 (diff) | |
download | org.eclipse.papyrus-0011d5cbd8f0fd5a7f3edca3466e477b0f65a9ca.tar.gz org.eclipse.papyrus-0011d5cbd8f0fd5a7f3edca3466e477b0f65a9ca.tar.xz org.eclipse.papyrus-0011d5cbd8f0fd5a7f3edca3466e477b0f65a9ca.zip |
Bug 408215 - [OCL] Loaded Complete OCL resource does not contribute to validation
Change-Id: I356190972a4af852a600147b4564925b3823b8e7
5 files changed, 288 insertions, 322 deletions
diff --git a/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/META-INF/MANIFEST.MF b/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/META-INF/MANIFEST.MF index d18112c3ed5..1d8d33f339c 100644 --- a/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/META-INF/MANIFEST.MF +++ b/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/META-INF/MANIFEST.MF @@ -17,8 +17,7 @@ Require-Bundle: org.eclipse.ui, org.eclipse.papyrus.infra.gmfdiag.commands;bundle-version="1.2.0",
org.eclipse.papyrus.infra.core;bundle-version="1.2.0",
org.eclipse.papyrus.infra.emf;bundle-version="1.2.0",
- org.eclipse.papyrus.infra.services.markerlistener;bundle-version="1.2.0",
- org.eclipse.uml2.uml;bundle-version="4.1.0"
+ org.eclipse.papyrus.infra.services.markerlistener;bundle-version="1.2.0"
Bundle-Vendor: %pluginProvider
Bundle-ActivationPolicy: lazy
Bundle-Version: 1.2.0.qualifier
diff --git a/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/EValidatorAdapter.java b/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/EValidatorAdapter.java index 7fababa7de9..d5c4f2fc060 100644 --- a/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/EValidatorAdapter.java +++ b/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/EValidatorAdapter.java @@ -10,6 +10,7 @@ * Christian W. Damus (CEA) - Target EObject must be the diagnostic's first data element * Benoit Maggi (CEA LIST) - Add an unique id as source for diagnostic * Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 446865 + * Ed Willink, Klaas Gadeyne, A. Radermacher - Remove hard-coded UML dependency - Bug 408215 *****************************************************************************/ @@ -28,17 +29,14 @@ import org.eclipse.emf.common.util.DiagnosticChain; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EDataType; import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EValidator; +import org.eclipse.emf.ecore.EcorePackage; import org.eclipse.emf.validation.model.EvaluationMode; import org.eclipse.emf.validation.model.IConstraintStatus; import org.eclipse.emf.validation.model.IModelConstraint; import org.eclipse.emf.validation.service.IBatchValidator; import org.eclipse.emf.validation.service.IConstraintDescriptor; import org.eclipse.emf.validation.service.ModelValidationService; -import org.eclipse.uml2.uml.LiteralInteger; -import org.eclipse.uml2.uml.LiteralUnlimitedNatural; -import org.eclipse.uml2.uml.MultiplicityElement; -import org.eclipse.uml2.uml.ValueSpecification; -import org.eclipse.uml2.uml.util.UMLValidator; /** @@ -51,7 +49,12 @@ import org.eclipse.uml2.uml.util.UMLValidator; * bug 405160 - avoid "false" errors by using the UMLValidator instead of EObjectValidator as base class */ public class EValidatorAdapter - extends UMLValidator { + implements EValidator { + + /** + * The eValidator from the EMF EValidator Registry. + */ + protected final EValidator registeredValidator; /** * Model Validation Service interface for batch validation of EMF elements. @@ -62,15 +65,23 @@ public class EValidatorAdapter * Initializes me. */ public EValidatorAdapter() { + this((EValidator) EValidator.Registry.INSTANCE.get(EcorePackage.eINSTANCE)); + } + + /** + * Constructor. + * + * @param registeredValidator a validator to which the adapter will delegate validation requests + */ + public EValidatorAdapter(EValidator registeredValidator) { super(); - + this.registeredValidator = registeredValidator; batchValidator = ModelValidationService.getInstance().newValidator( EvaluationMode.BATCH); batchValidator.setIncludeLiveConstraints(true); batchValidator.setReportSuccesses(false); } - @Override public boolean validate(EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) { return validate(eObject.eClass(), eObject, diagnostics, context); @@ -79,11 +90,10 @@ public class EValidatorAdapter /** * Implements validation by delegation to the EMF validation framework. */ - @Override public boolean validate(EClass eClass, EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) { // first, do whatever the basic EcoreValidator does - super.validate(eClass, eObject, diagnostics, context); + registeredValidator.validate(eClass, eObject, diagnostics, context); return batchValidate(eObject, diagnostics, context); } @@ -110,7 +120,6 @@ public class EValidatorAdapter appendDiagnostics(status, diagnostics); } } - return status.isOK(); } @@ -118,46 +127,9 @@ public class EValidatorAdapter * Direct validation of {@link EDataType}s is not supported by the EMF * validation framework; they are validated indirectly via the {@link EObject}s that hold their values. */ - @Override public boolean validate(EDataType eDataType, Object value, DiagnosticChain diagnostics, Map<Object, Object> context) { - return super.validate(eDataType, value, diagnostics, context); - } - - /** - * {@inheritDoc} - * - * @see org.eclipse.uml2.uml.util.UMLValidator#validateMultiplicityElement_validateUpperGeLower(org.eclipse.uml2.uml.MultiplicityElement, org.eclipse.emf.common.util.DiagnosticChain, java.util.Map) - */ - @Override - public boolean validateMultiplicityElement_validateUpperGeLower(MultiplicityElement multiplicityElement, DiagnosticChain diagnostics, Map<Object, Object> context) { - boolean result = false; - if (canCompareUpperGeLower(multiplicityElement)) { - result = super.validateMultiplicityElement_validateUpperGeLower(multiplicityElement, diagnostics, context); - } - - return result; - } - - /** - * This allows to define if the multiplicity element can compare the lower and the upper values (depending to the type of ValueSpecifications). - * - * @param eObject - * The {@link EObject} to check. - * @return <code>true</code> if the lower and upper can be compared (or if this is not a MultiplicityElement), <code>false</code> otherwise. - */ - protected boolean canCompareUpperGeLower(final EObject eObject) { - boolean result = true; - if (eObject instanceof MultiplicityElement) { - final MultiplicityElement multiplicityElement = (MultiplicityElement) eObject; - ValueSpecification lower = multiplicityElement.getLowerValue(); - ValueSpecification upper = multiplicityElement.getUpperValue(); - if (!((lower instanceof LiteralInteger || lower instanceof LiteralUnlimitedNatural || lower == null) - && (upper instanceof LiteralInteger || upper instanceof LiteralUnlimitedNatural || upper == null))) { - result = false; - } - } - return result; + return registeredValidator.validate(eDataType, value, diagnostics, context); } /** diff --git a/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/EcoreDiagnostician.java b/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/EcoreDiagnostician.java index 8331962860d..5338d5e6edf 100644 --- a/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/EcoreDiagnostician.java +++ b/plugins/infra/services/org.eclipse.papyrus.infra.services.validation/src/org/eclipse/papyrus/infra/services/validation/EcoreDiagnostician.java @@ -1,86 +1,86 @@ -/*****************************************************************************
- * Copyright (c) 2010, 2013 CEA LIST.
- *
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * CoPatrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.infra.services.validation;
-
-import java.util.Map;
-
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.emf.common.notify.AdapterFactory;
-import org.eclipse.emf.common.util.DiagnosticChain;
-import org.eclipse.emf.ecore.EClass;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.util.Diagnostician;
-import org.eclipse.emf.ecore.util.EObjectValidator;
-import org.eclipse.emf.edit.provider.IItemLabelProvider;
-
-/**
- * this is a generic diagnostician to validate Ecore model in papyrus
- *
- */
-public class EcoreDiagnostician extends Diagnostician implements IPapyrusDiagnostician {
-
- protected AdapterFactory adapterFactory;
- protected IProgressMonitor progressMonitor;
-
- /**
- * Create diagnostician with custom validator (that must subclass ECore validator)
- *
- * @param validatorAdapter
- * custom validator adapter
- */
- public EcoreDiagnostician(EValidatorAdapter validatorAdapter) {
- this.validatorAdapter = validatorAdapter;
- }
-
- public EcoreDiagnostician() {
- validatorAdapter = new EValidatorAdapter();
- }
-
- public void initialize(final AdapterFactory adapterFactory, final IProgressMonitor progressMonitor) {
- this.adapterFactory = adapterFactory;
- this.progressMonitor = progressMonitor;
- }
-
- protected EValidatorAdapter validatorAdapter;
-
- @Override
- public String getObjectLabel(EObject eObject) {
- if (adapterFactory != null && !eObject.eIsProxy())
- {
- IItemLabelProvider itemLabelProvider = (IItemLabelProvider) adapterFactory.adapt(eObject, IItemLabelProvider.class);
- if (itemLabelProvider != null) {
- return itemLabelProvider.getText(eObject);
- }
- }
- return super.getObjectLabel(eObject);
- }
-
- @Override
- public boolean validate(EClass eClass, EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) {
- assert (progressMonitor != null);
- progressMonitor.worked(1);
-
- // copied from superclass, difference: use EValidatorAdapter instead of first value from eValidatorRegistry
- // fix of bug 397518
-
- boolean circular = context.get(EObjectValidator.ROOT_OBJECT) == eObject;
- boolean result = validatorAdapter.validate(eClass, eObject, diagnostics, context);
- if ((result || diagnostics != null) && !circular)
- {
- result &= doValidateContents(eObject, diagnostics, context);
- }
- return result;
- }
-
-
+/***************************************************************************** + * Copyright (c) 2010, 2013 CEA LIST. + * + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - Initial API and implementation + * Ed Willink, Klaas Gadeyne, A. Radermacher - Remove hard-coded UML dependency - Bug 408215 + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.services.validation; + +import java.util.Map; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.emf.common.notify.AdapterFactory; +import org.eclipse.emf.common.util.DiagnosticChain; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EValidator; +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.util.Diagnostician; +import org.eclipse.emf.ecore.util.EObjectValidator; +import org.eclipse.emf.edit.provider.IItemLabelProvider; + +/** + * This is a generic diagnostician to validate Ecore model in Papyrus + * + */ +public class EcoreDiagnostician extends Diagnostician implements IPapyrusDiagnostician { + + protected AdapterFactory adapterFactory; + protected IProgressMonitor progressMonitor; + protected EValidatorAdapter validatorAdapter; + + /** + * Create diagnostician with custom validator (that must subclass ECore validator) + * + * @param validatorAdapter + * custom validator adapter + */ + public EcoreDiagnostician(EValidatorAdapter validatorAdapter) { + this.validatorAdapter = validatorAdapter; + } + + public EcoreDiagnostician() { + validatorAdapter = new EValidatorAdapter((EValidator) EValidator.Registry.INSTANCE.get(EcorePackage.eINSTANCE)); + } + + public void initialize(final AdapterFactory adapterFactory, final IProgressMonitor progressMonitor) { + this.adapterFactory = adapterFactory; + this.progressMonitor = progressMonitor; + } + + @Override + public String getObjectLabel(EObject eObject) { + if (adapterFactory != null && !eObject.eIsProxy()) + { + IItemLabelProvider itemLabelProvider = (IItemLabelProvider) adapterFactory.adapt(eObject, IItemLabelProvider.class); + if (itemLabelProvider != null) { + return itemLabelProvider.getText(eObject); + } + } + return super.getObjectLabel(eObject); + } + + @Override + public boolean validate(EClass eClass, EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) { + assert (progressMonitor != null); + progressMonitor.worked(1); + + // copied from superclass, difference: use EValidatorAdapter instead of first value from eValidatorRegistry + // fix of bug 397518 + + boolean circular = context.get(EObjectValidator.ROOT_OBJECT) == eObject; + boolean result = validatorAdapter.validate(eClass, eObject, diagnostics, context); + if ((result || diagnostics != null) && !circular) + { + result &= doValidateContents(eObject, diagnostics, context); + } + return result; + } }
\ No newline at end of file diff --git a/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/OCLEValidatorAdapter.java b/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/OCLEValidatorAdapter.java index 7f8cf94dc23..d16ca2f0a81 100644 --- a/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/OCLEValidatorAdapter.java +++ b/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/OCLEValidatorAdapter.java @@ -8,6 +8,7 @@ * Contributors: * IBM - Initial API and implementation * Christian W. Damus (CEA) - Target EObject must be the diagnostic's first data element + * Ed Willink, Klaas Gadeyne, A. Radermacher - Bug 408215 * *****************************************************************************/ @@ -16,10 +17,13 @@ package org.eclipse.papyrus.uml.service.validation; import java.util.Map; +import org.eclipse.emf.common.util.BasicDiagnostic; import org.eclipse.emf.common.util.Diagnostic; import org.eclipse.emf.common.util.DiagnosticChain; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EValidator; +import org.eclipse.emf.ecore.plugin.EcorePlugin; import org.eclipse.emf.ecore.util.EObjectValidator; import org.eclipse.emf.validation.model.IConstraintStatus; import org.eclipse.ocl.pivot.internal.delegate.OCLDelegateValidator; @@ -43,68 +47,58 @@ import org.eclipse.uml2.uml.OpaqueExpression; public class OCLEValidatorAdapter extends EValidatorAdapter { + /** + * Constructor. + * + */ + public OCLEValidatorAdapter(EValidator registeredValidator) { + super(registeredValidator); + } + // Overridden to invoke OCLDelegateValidator @Override public boolean validate(EClass eClass, EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) { if (eObject.eIsProxy()) { - if (context != null && context.get(ROOT_OBJECT) != null) { + if (context != null && context.get(EObjectValidator.ROOT_OBJECT) != null) { if (diagnostics != null) { - diagnostics.add(createDiagnostic(Diagnostic.ERROR, EObjectValidator.DIAGNOSTIC_SOURCE, - EOBJECT__EVERY_PROXY_RESOLVES, "_UI_UnresolvedProxy_diagnostic", - new Object[] { - getFeatureLabel(eObject.eContainmentFeature(), context), - getObjectLabel(eObject.eContainer(), context), - getObjectLabel(eObject, context) }, + diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, EObjectValidator.DIAGNOSTIC_SOURCE, + EObjectValidator.EOBJECT__EVERY_PROXY_RESOLVES, + // create message + EcorePlugin.INSTANCE.getString("_UI_UnresolvedProxy_diagnostic", //$NON-NLS-1$ + new Object[] { + EObjectValidator.getFeatureLabel(eObject.eContainmentFeature(), context), + EObjectValidator.getObjectLabel(eObject.eContainer(), context), + EObjectValidator.getObjectLabel(eObject, context) } + ), new Object[] { eObject.eContainer(), eObject.eContainmentFeature(), - eObject }, - context)); + eObject } + )); } } - } else if (eClass.eContainer() == getEPackage()) { - validate(eClass.getClassifierID(), eObject, diagnostics, context); - } else { - new OCLDelegateValidator(this) { + } + else if (EValidator.Registry.INSTANCE.get(eClass.eContainer()) == registeredValidator) { + if (eObject instanceof InstanceSpecification) { + UMLOCLEValidator.INSTANCE.validateInstanceSpecification((InstanceSpecification) eObject, diagnostics, context); + } + else if (eObject instanceof OpaqueAction) { + UMLOCLEValidator.INSTANCE.validateOpaqueAction((OpaqueAction) eObject, diagnostics, context); + } + else if (eObject instanceof OpaqueBehavior) { + return UMLOCLEValidator.INSTANCE.validateOpaqueBehavior((OpaqueBehavior) eObject, diagnostics, context); + } + else if (eObject instanceof OpaqueExpression) { + return UMLOCLEValidator.INSTANCE.validateOpaqueExpression((OpaqueExpression) eObject, diagnostics, context); + } + registeredValidator.validate(eClass, eObject, diagnostics, context); + } + else { + new OCLDelegateValidator(EObjectValidator.INSTANCE) { // Ensure that the class loader for this class will be used downstream. }.validate(eClass, eObject, diagnostics, context); } + return batchValidate(eObject, diagnostics, context); } - - @Override - public boolean validateInstanceSpecification(InstanceSpecification instanceSpecification, - DiagnosticChain diagnostics, Map<Object, Object> context) { - if (!super.validateInstanceSpecification(instanceSpecification, diagnostics, context)) { - return false; - } - return UMLOCLEValidator.INSTANCE.validateInstanceSpecification(instanceSpecification, diagnostics, context); - } - - @Override - public boolean validateOpaqueAction(OpaqueAction opaqueAction, - DiagnosticChain diagnostics, Map<Object, Object> context) { - if (!super.validateOpaqueAction(opaqueAction, diagnostics, context)) { - return false; - } - return UMLOCLEValidator.INSTANCE.validateOpaqueAction(opaqueAction, diagnostics, context); - } - - @Override - public boolean validateOpaqueBehavior(OpaqueBehavior opaqueBehavior, - DiagnosticChain diagnostics, Map<Object, Object> context) { - if (!super.validateOpaqueBehavior(opaqueBehavior, diagnostics, context)) { - return false; - } - return UMLOCLEValidator.INSTANCE.validateOpaqueBehavior(opaqueBehavior, diagnostics, context); - } - - @Override - public boolean validateOpaqueExpression(OpaqueExpression opaqueExpression, - DiagnosticChain diagnostics, Map<Object, Object> context) { - if (!super.validateOpaqueExpression(opaqueExpression, diagnostics, context)) { - return false; - } - return UMLOCLEValidator.INSTANCE.validateOpaqueExpression(opaqueExpression, diagnostics, context); - } -} +}
\ No newline at end of file diff --git a/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/UMLDiagnostician.java b/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/UMLDiagnostician.java index ef1e107c13c..7ed41023470 100644 --- a/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/UMLDiagnostician.java +++ b/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/UMLDiagnostician.java @@ -1,136 +1,137 @@ -/*****************************************************************************
- * Copyright (c) 2010, 2013 CEA LIST.
- *
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - Initial API and implementation
- * Ansgar Radermacher (CEA LIST) ansgar.radermacher@cea.fr - Contribution related to bug 410457, 410119 and 410059
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.service.validation;
-
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import org.eclipse.emf.common.util.BasicDiagnostic;
-import org.eclipse.emf.common.util.Diagnostic;
-import org.eclipse.emf.common.util.DiagnosticChain;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.ocl.pivot.internal.delegate.OCLDelegateDomain;
-import org.eclipse.papyrus.infra.services.validation.EcoreDiagnostician;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.util.UMLUtil;
-
-/**
- * This is a specific diagnostician used to validate stereotype applications
- *
- */
-public class UMLDiagnostician extends EcoreDiagnostician {
-
- public UMLDiagnostician() {
- super(new OCLEValidatorAdapter());
- validateStereotype = false;
- }
-
- @Override
- public Map<Object, Object> createDefaultContext() {
- Map<Object, Object> context = super.createDefaultContext();
- if (context != null) {
- OCLDelegateDomain.initializePivotOnlyDiagnosticianContext(context);
- }
- return context;
- }
-
- @Override
- public BasicDiagnostic createDefaultDiagnostic(EObject eObject) {
- if (eObject == null || eObject.eResource() == null){
- return super.createDefaultDiagnostic(eObject);
- }
- ResourceSet resourceSet = eObject.eResource().getResourceSet();
- if (resourceSet != null) {
- OCLDelegateDomain.initializePivotOnlyDiagnosticianResourceSet(resourceSet);
- }
- return super.createDefaultDiagnostic(eObject);
- }
-
- /**
- * Explicitly validate stereotype applications.
- *
- * @param eObject the eObject to validate
- * @param diagnostics the diagnostic chain
- * @param context the context
- * @return
- */
- protected boolean doValidateStereotypeApplications(EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) {
- if (validateStereotype) {
- // this function is called recursively. Avoid trying to obtain stereotype applications, if we are
- // already examining a stereotype
- return true;
- }
- List<EObject> stereotypeApplications = eObject instanceof Element ? ((Element) eObject).getStereotypeApplications() : Collections.<EObject> emptyList();
- if (!stereotypeApplications.isEmpty()) {
- Iterator<EObject> i = stereotypeApplications.iterator();
- validateStereotype = true;
- boolean result = validate(i.next(), diagnostics, context);
- while (i.hasNext() && (result || diagnostics != null)) {
- result &= validate(i.next(), diagnostics, context);
- }
- validateStereotype = false;
- return result;
- } else {
- return true;
- }
- }
-
- @Override
- protected boolean doValidateContents(EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) {
- boolean result = doValidateStereotypeApplications(eObject, diagnostics, context);
- if (result || diagnostics != null) {
- result &= super.doValidateContents(eObject, diagnostics, context);
- }
- return result;
- }
-
- @Override
- public boolean validate(EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) {
- if (!context.containsKey(this)) {
- // put instance of the UMLDiagnostician into context map to identify first invocation
- // (validate is called recursively)
- context.put(this, null);
- BasicDiagnostic newDiagChain = createDefaultDiagnostic(eObject);
- boolean ok = super.validate(eObject, newDiagChain, context);
- // replace markers here instead of using a validation adapter, see
- // bug 410457 - [Validation] Ghost markers when validating profile constraints
- // bug 410119 - [Validation] markers related to stereotype applications are not updated in diagrams
- // bug 410059 - [Validation] delete subtree does not remove markers associated with stereotypes
- for (Diagnostic d : newDiagChain.getChildren()) {
- Object data[] = d.getData().toArray();
- if (data.length > 0) {
- Object target = data[0];
- if (target instanceof EObject) {
- EObject base = UMLUtil.getBaseElement((EObject) target);
- if (base != null) {
- data[0] = base;
- }
- }
- }
- diagnostics.add(new BasicDiagnostic(
- d.getSeverity(), d.getSource(), d.getCode(), d.getMessage(), data));
- }
- return ok;
- }
- else {
- return super.validate(eObject, diagnostics, context);
- }
- }
-
- protected boolean validateStereotype;
-
-}
+/***************************************************************************** + * Copyright (c) 2010, 2013 CEA LIST. + * + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - Initial API and implementation + * Ansgar Radermacher (CEA LIST) ansgar.radermacher@cea.fr - Contribution related to bug 410457, 410119 and 410059 + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.service.validation; + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.eclipse.emf.common.util.BasicDiagnostic; +import org.eclipse.emf.common.util.Diagnostic; +import org.eclipse.emf.common.util.DiagnosticChain; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EValidator; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.ocl.pivot.internal.delegate.OCLDelegateDomain; +import org.eclipse.papyrus.infra.services.validation.EcoreDiagnostician; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.UMLPackage; +import org.eclipse.uml2.uml.util.UMLUtil; + +/** + * This is a specific diagnostician used to validate stereotype applications + * + */ +public class UMLDiagnostician extends EcoreDiagnostician { + + public UMLDiagnostician() { + super(new OCLEValidatorAdapter((EValidator) EValidator.Registry.INSTANCE.get(UMLPackage.eINSTANCE))); + validateStereotype = false; + } + + @Override + public Map<Object, Object> createDefaultContext() { + Map<Object, Object> context = super.createDefaultContext(); + if (context != null) { + OCLDelegateDomain.initializePivotOnlyDiagnosticianContext(context); + } + return context; + } + + @Override + public BasicDiagnostic createDefaultDiagnostic(EObject eObject) { + if (eObject == null || eObject.eResource() == null){ + return super.createDefaultDiagnostic(eObject); + } + ResourceSet resourceSet = eObject.eResource().getResourceSet(); + if (resourceSet != null) { + OCLDelegateDomain.initializePivotOnlyDiagnosticianResourceSet(resourceSet); + } + return super.createDefaultDiagnostic(eObject); + } + + /** + * Explicitly validate stereotype applications. + * + * @param eObject the eObject to validate + * @param diagnostics the diagnostic chain + * @param context the context + * @return + */ + protected boolean doValidateStereotypeApplications(EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) { + if (validateStereotype) { + // this function is called recursively. Avoid trying to obtain stereotype applications, if we are + // already examining a stereotype + return true; + } + List<EObject> stereotypeApplications = eObject instanceof Element ? ((Element) eObject).getStereotypeApplications() : Collections.<EObject> emptyList(); + if (!stereotypeApplications.isEmpty()) { + Iterator<EObject> i = stereotypeApplications.iterator(); + validateStereotype = true; + boolean result = validate(i.next(), diagnostics, context); + while (i.hasNext() && (result || diagnostics != null)) { + result &= validate(i.next(), diagnostics, context); + } + validateStereotype = false; + return result; + } else { + return true; + } + } + + @Override + protected boolean doValidateContents(EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) { + boolean result = doValidateStereotypeApplications(eObject, diagnostics, context); + if (result || diagnostics != null) { + result &= super.doValidateContents(eObject, diagnostics, context); + } + return result; + } + + @Override + public boolean validate(EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) { + if (!context.containsKey(this)) { + // put instance of the UMLDiagnostician into context map to identify first invocation + // (validate is called recursively) + context.put(this, null); + BasicDiagnostic newDiagChain = createDefaultDiagnostic(eObject); + boolean ok = super.validate(eObject, newDiagChain, context); + // replace markers here instead of using a validation adapter, see + // bug 410457 - [Validation] Ghost markers when validating profile constraints + // bug 410119 - [Validation] markers related to stereotype applications are not updated in diagrams + // bug 410059 - [Validation] delete subtree does not remove markers associated with stereotypes + for (Diagnostic d : newDiagChain.getChildren()) { + Object data[] = d.getData().toArray(); + if (data.length > 0) { + Object target = data[0]; + if (target instanceof EObject) { + EObject base = UMLUtil.getBaseElement((EObject) target); + if (base != null) { + data[0] = base; + } + } + } + diagnostics.add(new BasicDiagnostic( + d.getSeverity(), d.getSource(), d.getCode(), d.getMessage(), data)); + } + return ok; + } + else { + return super.validate(eObject, diagnostics, context); + } + } + + protected boolean validateStereotype; +} |