Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bbc845ca78accad8d0ee9495d22056063cc3e67f (plain) (blame)
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
/*****************************************************************************
 * Copyright (c) 2016, 2017 CEA LIST, Christian W. Damus, and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus - bug 514955
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.services.validation.internal;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.infra.services.validation.Activator;
import org.eclipse.papyrus.infra.services.validation.IPapyrusDiagnostician;
import org.eclipse.papyrus.infra.services.validation.IValidationFilter;
import org.eclipse.papyrus.infra.services.validation.IValidationHook;

/**
 * Provides access to the extensions diagnosticians and validationHooks
 */
public class ValidationRegistry {

	public static final String ID_DIAGNOSTICIANS = Activator.PLUGIN_ID + ".diagnosticians"; //$NON-NLS-1$

	public static final String ID_VALIDATION_HOOKS = Activator.PLUGIN_ID + ".validationHooks"; //$NON-NLS-1$

	/**
	 * Constants for tags in two extensions points
	 */
	public static final String DIAGNOSTICIAN = "diagnostician"; //$NON-NLS-1$

	public static final String VALIDATION_HOOK = "hook"; //$NON-NLS-1$

	public static final String FILTER = "filter"; //$NON-NLS-1$

	private static final String PRIORITY = "priority"; //$NON-NLS-1$

	public enum HookType {
		BEFORE, AFTER
	};

	private enum Priority {
		DEFAULT, LOWEST, LOW, NORMAL, HIGH, HIGHEST;
	}

	/**
	 * Return a diagnostician for an element of a model.
	 * 
	 * @param element
	 *            an element of a model (that must be contained in an eResource)
	 * @return
	 */
	public static IPapyrusDiagnostician getDiagnostician(EObject element) {
		IExtensionRegistry reg = Platform.getExtensionRegistry();
		IConfigurationElement[] configElements = sort(reg.getConfigurationElementsFor(ID_DIAGNOSTICIANS));
		for (IConfigurationElement configElement : configElements) {
			try {
				final Object obj = configElement.createExecutableExtension(FILTER);
				if (obj instanceof IValidationFilter) {
					IValidationFilter filter = (IValidationFilter) obj;
					if (filter.isApplicable(element)) {
						final Object diagnostician = configElement.createExecutableExtension(DIAGNOSTICIAN);
						if (diagnostician instanceof IPapyrusDiagnostician) {
							return (IPapyrusDiagnostician) diagnostician;
						}
					}
				}
			} catch (CoreException exception) {
				Activator.log.error(exception);
			}
		}
		// fall back to ecore diagnostician
		return new EcoreDiagnostician();
	}

	/**
	 * Sort an array of extension configuration elements by priority, from highest to lowest.
	 * 
	 * @param configElements
	 *            an array of configuration elements
	 * 
	 * @return the sorted array
	 */
	private static IConfigurationElement[] sort(IConfigurationElement[] configElements) {
		Comparator<IConfigurationElement> byPriority = new Comparator<IConfigurationElement>() {
			private Map<IConfigurationElement, Priority> priorities = new HashMap<IConfigurationElement, ValidationRegistry.Priority>();

			Priority getPriority(IConfigurationElement config) {
				Priority result = priorities.get(config);
				if (result == null) {
					String priorityName = config.getAttribute(PRIORITY);
					if (priorityName == null) {
						priorityName = Priority.DEFAULT.name();
					} else {
						priorityName = priorityName.toUpperCase();
					}

					try {
						result = Priority.valueOf(priorityName);
					} catch (Exception e) {
						// No such value? Go with the default
					}
					if (result == null) {
						result = Priority.DEFAULT;
					}

					priorities.put(config, result);
				}

				return result;
			}

			public int compare(IConfigurationElement o1, IConfigurationElement o2) {
				// Sort from highest to lowest priority
				return getPriority(o2).compareTo(getPriority(o1));
			}
		};

		Arrays.sort(configElements, byPriority);

		return configElements;
	}

	/**
	 * Obtain a diagnostician for a given language
	 * 
	 * @param languageID
	 *            the id of the language for which we want to obtain the diagnostician
	 * @return the associated diagnostician
	 */
	public static IPapyrusDiagnostician getDiagnostician(String languageID) {
		IExtensionRegistry reg = Platform.getExtensionRegistry();
		IConfigurationElement[] configElements = reg.getConfigurationElementsFor(ID_DIAGNOSTICIANS);
		for (IConfigurationElement configElement : configElements) {
			try {
				final String iConfiguratorIDext = configElement.getAttribute("id"); //$NON-NLS-1$
				if ((iConfiguratorIDext != null) && iConfiguratorIDext.equals(languageID)) {
					final Object obj = configElement.createExecutableExtension("class"); //$NON-NLS-1$
					if (obj instanceof IPapyrusDiagnostician) {
						return (IPapyrusDiagnostician) obj;
					}
				}
			} catch (CoreException exception) {
				Activator.log.error(exception);
			}
		}
		return null;
	}

	/**
	 * Execute validation hooks
	 * 
	 * @param element
	 *            An element of the model we want to validate
	 * @param hookType
	 *            The hook type (currently before or after)
	 */
	public static void executeHooks(EObject element, HookType hookType) {
		IExtensionRegistry reg = Platform.getExtensionRegistry();
		IConfigurationElement[] configElements = reg.getConfigurationElementsFor(ID_VALIDATION_HOOKS);
		for (IConfigurationElement configElement : configElements) {
			try {
				final Object obj = configElement.createExecutableExtension(FILTER);
				if (obj instanceof IValidationFilter) {
					IValidationFilter filter = (IValidationFilter) obj;
					if (filter.isApplicable(element)) {
						final Object hookObj = configElement.createExecutableExtension(VALIDATION_HOOK);
						if (hookObj instanceof IValidationHook) {
							IValidationHook validationHook = (IValidationHook) hookObj;
							if (hookType == HookType.BEFORE) {
								validationHook.beforeValidation(element);
							} else if (hookType == HookType.AFTER) {
								validationHook.afterValidation(element);
							}
						}
					}
				}
			} catch (CoreException exception) {
				Activator.log.error(exception);
			}
		}
	}
}

Back to the top