Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6fee3ac7097f7e988f3df4f78d774b49821114d7 (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
201
/*******************************************************************************
 * <copyright>
 * Copyright (c) 2008 itemis AG and others.
 * 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:
 * committers of openArchitectureWare - initial API and implementation
 * </copyright>
 *******************************************************************************/
package org.eclipse.xtend.typesystem.emf.check;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.EPackage;
import org.eclipse.emf.ecore.EValidator;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.issues.IssuesImpl;
import org.eclipse.emf.mwe.core.issues.MWEDiagnostic;
import org.eclipse.emf.mwe.core.resources.ResourceLoader;
import org.eclipse.emf.mwe.core.resources.ResourceLoaderFactory;
import org.eclipse.emf.mwe.core.resources.ResourceLoaderImpl;
import org.eclipse.internal.xtend.xtend.ast.ExtensionFile;
import org.eclipse.xtend.check.CheckUtils;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.expression.ExecutionContextImpl;
import org.eclipse.xtend.expression.ResourceManager;
import org.eclipse.xtend.expression.ResourceManagerDefaultImpl;
import org.eclipse.xtend.expression.TypeSystemImpl;
import org.eclipse.xtend.typesystem.emf.EmfMetaModel;

/**
 * An implementation of {@link org.eclipse.emf.ecore.EValidator} that executes
 * oAW checks. Further EValidators can be nested.
 * 
 * Check files can be added with reparse option. If true, the check files are
 * reparse on each validation.
 * 
 * @author Jan Köhnlein
 */
public class CheckEValidatorAdapter implements EValidator {

	private final Log log = LogFactory.getLog(CheckEValidatorAdapter.class);

	private List<CheckFileWithContext> _checkFiles;

	private EValidator _nestedValidator;

	private EPackage _ePackage;
	
	private ResourceManager _externalResourceManager;

	public CheckEValidatorAdapter(EPackage ePackage) {
		_ePackage = ePackage;
		_checkFiles = new ArrayList<CheckFileWithContext>();
	}

	public CheckEValidatorAdapter(EPackage ePackage,
			EValidator existingValidator) {
		this(ePackage);
		if (existingValidator != null) {
			_nestedValidator = existingValidator;
		}
	}

	public void addCheckFile(CheckFileWithContext checkFile) {
		_checkFiles.add(checkFile);
	}

	public boolean validate(EObject eObject, DiagnosticChain diagnostics,
			Map<Object, Object> context) {
		return validate(eObject.eClass(), eObject, diagnostics, context);
	}

	public boolean validate(EClass eClass, EObject eObject,
			DiagnosticChain diagnostics, Map<Object, Object> context) {
		List<EObject> allElements = Collections.singletonList(eObject);
		boolean isValid = runOawCheck(diagnostics, allElements);
		if (_nestedValidator != null) {
			isValid &= _nestedValidator.validate(eClass, eObject, diagnostics,
					context);
		}
		return isValid;
	}

	public boolean validate(EDataType dataType, Object value,
			DiagnosticChain diagnostics, Map<Object, Object> context) {
		List<?> allElements = Collections.singletonList(dataType);
		boolean isValid = runOawCheck(diagnostics, allElements);
		if (_nestedValidator != null) {
			isValid &= _nestedValidator.validate(dataType, value, diagnostics,
					context);
		}
		return isValid;
	}

	
	public void setExternalResourceManager(ResourceManager externalResourceManager) {
		_externalResourceManager = externalResourceManager;
	}
	
	private ResourceManager getResourceManager() {
		if(_externalResourceManager != null) {
			return _externalResourceManager;
		}
		return new ResourceManagerDefaultImpl();
	}

	private ExecutionContext createExecutionContext(
			CheckFileWithContext checkFile, ResourceManager resourceManager) {
		final Set<EPackage> allEPackages = new HashSet<EPackage>();
		allEPackages.add(_ePackage);
		for (String nsURI : checkFile.getImportedEPackageNsUris()) {
			try {
				EPackage importedEPackage = EPackage.Registry.INSTANCE
						.getEPackage(nsURI);
				if (importedEPackage != null) {
					allEPackages.add(importedEPackage);
				}
			} catch (Exception exc) {
				log.error(exc);
			}
		}
		TypeSystemImpl typeSystem = new TypeSystemImpl();
		typeSystem.registerMetaModel(new EmfMetaModel() {
			private EPackage[] _allEPackages = allEPackages
					.toArray(new EPackage[allEPackages.size()]);

			@Override
			protected EPackage[] allPackages() {
				return _allEPackages;
			}
		});
		ExecutionContext executionContext = new ExecutionContextImpl(
				resourceManager, typeSystem, null);
		return executionContext;
	}

	private boolean runOawCheck(DiagnosticChain diagnostics, List<?> allElements) {
		boolean isValid = true;
		ResourceLoader current = ResourceLoaderFactory.getCurrentThreadResourceLoader();
		try {
			ResourceLoaderFactory.setCurrentThreadResourceLoader(new ResourceLoaderImpl(getClass().getClassLoader()));
			for (CheckFileWithContext checkFile : _checkFiles) {
				Issues issues = new IssuesImpl();
				ResourceManager resourceManager = getResourceManager();
				ExtensionFile parsedCheckFile = (ExtensionFile) resourceManager
						.loadResource(checkFile.getFileName(),
								CheckUtils.FILE_EXTENSION);
				if (parsedCheckFile==null)
					throw new IllegalArgumentException("Couldn't find file "+checkFile.getFileName()+"."+CheckUtils.FILE_EXTENSION+". Maybe it has been misspelled or it's not on the classpath?");
				ExecutionContext executionContext = createExecutionContext(checkFile, resourceManager);
				runOawCheck(parsedCheckFile, allElements, diagnostics,
						executionContext);
				isValid &= issues.hasErrors();
			}
		} finally {
			ResourceLoaderFactory.setCurrentThreadResourceLoader(current);
		}
		return isValid;
	}

	private Issues runOawCheck(ExtensionFile parsedCheckFile,
			List<?> allElements, DiagnosticChain diagnostics,
			ExecutionContext executionContext) {
		Issues issues = new IssuesImpl();
		parsedCheckFile.check(executionContext, allElements, issues, false);
		addDiagnosticFromIssues(diagnostics, issues);
		return issues;
	}

	private void addDiagnosticFromIssues(
			DiagnosticChain allDiagnostics, Issues issues) {
		MWEDiagnostic[] errors = issues.getErrors();
		addDiagnostics(allDiagnostics, errors);
			
		MWEDiagnostic[] warnings = issues.getWarnings();
		addDiagnostics(allDiagnostics, warnings);
	}

	private void addDiagnostics(DiagnosticChain allDiagnostics,
			MWEDiagnostic[] issues) {
		for (MWEDiagnostic issue : issues) {
			allDiagnostics.add(issue);
		}
	}

}

Back to the top