Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 76f99b71cf2c4c8718de49625452d9b345a8283f (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
200
/*******************************************************************************
 * Copyright (c) 2012 protos software gmbh (http://www.protos.de).
 * 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
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.validation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.room.RoomPackage;
import org.eclipse.xtext.validation.AbstractDeclarativeValidator;
import org.eclipse.xtext.validation.Check;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.CheckType;
import org.eclipse.xtext.validation.EValidatorRegistrar;
import org.eclipse.xtext.validation.ValidationMessageAcceptor;

/**
 * @author Henrik Rentz-Reichert
 *
 */
public class ValidatorExtensionManager extends AbstractDeclarativeValidator {
	
	public static class Registry {
		
		private static final String PACKAGE = RoomPackage.class.getPackage().getName()+".";
		private static final String IVALIDATOR_ID = "org.eclipse.etrice.core.room.validation";
		private static Registry instance = null;
		private HashMap<String, ArrayList<IRoomValidator>> fastClass2Ext = new HashMap<String, ArrayList<IRoomValidator>>();
		private HashMap<String, ArrayList<IRoomValidator>> normalClass2Ext = new HashMap<String, ArrayList<IRoomValidator>>();
		private HashMap<String, ArrayList<IRoomValidator>> expensiveClass2Ext = new HashMap<String, ArrayList<IRoomValidator>>();
		
		public static Registry getInstance() {
			if (instance==null)
				instance = new Registry();
			
			return instance;
		}
		
		public void loadValidatorExtensions() {
			IConfigurationElement[] config = Platform.getExtensionRegistry()
					.getConfigurationElementsFor(IVALIDATOR_ID);
			
			// compute all sub classes for all classes of the Room package 
			HashMap<String, ArrayList<String>> cls2sub = new HashMap<String, ArrayList<String>>();
			for (EClassifier cls : RoomPackage.eINSTANCE.getEClassifiers()) {
				if (cls instanceof EClass) {
					EList<EClass> superTypes = ((EClass) cls).getESuperTypes();
					for (EClass sup : superTypes) {
						put(sup.getName(), ((EClass) cls).getName(), cls2sub);
					}
				}
			}
			
			// now we add each extension to our maps
			try {
				for (IConfigurationElement e : config) {
					final Object ext = e.createExecutableExtension("class");
					if (ext instanceof IRoomValidator) {
						String mode = e.getAttribute("mode");
						String classToCheck = e.getAttribute("classToCheck");
						if (classToCheck.startsWith(PACKAGE))
							classToCheck = classToCheck.substring(PACKAGE.length());
						EClassifier cls = RoomPackage.eINSTANCE.getEClassifier(classToCheck);
						if (cls instanceof EClass) {
							HashMap<String,ArrayList<IRoomValidator>> map = getMap(mode);
							if (map!=null) {
								put(map, ((EClass) cls).getName(), (IRoomValidator) ext);
								ArrayList<String> subTypes = cls2sub.get(cls.getName());
								if (subTypes!=null)
									for (String type : subTypes) {
										put(map, type, (IRoomValidator) ext);
									}
							}
						}
					}
					else {
						System.out.println("ValidatorExtensionManager: unexpected extension");
					}
				}
			} catch (CoreException ex) {
				System.out.println(ex.getMessage());
			}
		}
		
		public void validate(EObject object, CheckMode checkMode,
				ValidationMessageAcceptor messageAcceptor) {
			HashSet<IRoomValidator> executed = new HashSet<IRoomValidator>();
			if (checkMode.shouldCheck(CheckType.FAST))
				validate(object, messageAcceptor, fastClass2Ext, executed);
			if (checkMode.shouldCheck(CheckType.NORMAL))
				validate(object, messageAcceptor, normalClass2Ext, executed);
			if (checkMode.shouldCheck(CheckType.EXPENSIVE))
				validate(object, messageAcceptor, expensiveClass2Ext, executed);
		}

		private void validate(EObject object,
				ValidationMessageAcceptor messageAcceptor,
				HashMap<String, ArrayList<IRoomValidator>> map, HashSet<IRoomValidator> executed) {
			ArrayList<IRoomValidator> validators = map.get(object.eClass().getName());
			if (validators!=null)
				for (IRoomValidator validator : validators) {
					if (!executed.contains(validator)) {
						executed.add(validator);
						executeExtension(validator, object, messageAcceptor);
					}
				}
		}
		
		private void put(String cls, String sub,
				HashMap<String,ArrayList<String>> cls2sub) {
			
			ArrayList<String> list = cls2sub.get(cls);
			if (list==null) {
				list = new ArrayList<String>();
				cls2sub.put(cls, list);
			}
			list.add(sub);
		}

		private void put(HashMap<String, ArrayList<IRoomValidator>> map, String cls, IRoomValidator val) {
			ArrayList<IRoomValidator> list = map.get(cls);
			if (list==null) {
				list = new ArrayList<IRoomValidator>();
				map.put(cls, list);
			}
			list.add(val);
		}
		
		private HashMap<String, ArrayList<IRoomValidator>> getMap(String mode) {
			if (mode.equals(CheckType.FAST.name()))
				return fastClass2Ext;
			else if (mode.equals(CheckType.NORMAL.name()))
				return normalClass2Ext;
			else if (mode.equals(CheckType.EXPENSIVE.name()))
				return normalClass2Ext;
			else
				return null;
		}
		
		private void executeExtension(
				final IRoomValidator validator,
				final EObject object,
				final ValidationMessageAcceptor messageAcceptor) {
			
			ISafeRunnable runnable = new ISafeRunnable() {
				@Override
				public void handleException(Throwable exception) {
					System.out.println("Exception in IRoomValidator");
				}

				@Override
				public void run() throws Exception {
					validator.validate(object, messageAcceptor);
				}
			};
			SafeRunner.run(runnable);
		}
	}

	public ValidatorExtensionManager() {
		
	}
	
	@Check
	public void checkObject(EObject object) {
		Registry.getInstance().validate(object, getCheckMode(), getMessageAcceptor());
	}

	@Override
	public void register(EValidatorRegistrar registrar) {
		// HOWTO: if this validator is registered for the RoomPackage then it is called twice
	}

//	@Override
//	protected List<EPackage> getEPackages() {
//	    List<EPackage> result = new ArrayList<EPackage>();
//	    result.add(RoomPackage.eINSTANCE);
//	    return result;
//	}

}

Back to the top