Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ae6a22faeeefd23c3fe4c3ed110f1d191e393ff9 (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
/*****************************************************************************
 * Copyright (c) 2019 CEA LIST 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
 * http://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.toolsmiths.validation.profile.internal.checkers;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.papyrus.toolsmiths.validation.common.utils.MarkersService;
import org.eclipse.papyrus.toolsmiths.validation.common.utils.PluginValidationService;
import org.eclipse.papyrus.toolsmiths.validation.common.utils.ProjectManagementService;
import org.eclipse.papyrus.toolsmiths.validation.profile.Activator;
import org.eclipse.papyrus.toolsmiths.validation.profile.constants.ProfilePluginValidationConstants;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.uml2.uml.Profile;

/**
 * This allows to check a profile plug-in (extensions, builds, dependencies).
 */
public class ProfilePluginChecker {

	/**
	 * This allows to check the profile plug-in.
	 *
	 * @param project
	 *            The current project to check.
	 */
	public static void checkProfilePlugin(final IProject project) {

		// Get the shell to manage the validation in an UI
		final Shell shell = Display.getCurrent().getActiveShell();

		try {
			// Open the progress monitor dialog
			new ProgressMonitorDialog(shell).run(true, true, monitor -> {
				final Collection<IFile> profileFiles = ProjectManagementService.getFilesFromProject(project, "profile.uml", true); //$NON-NLS-1$
				monitor.beginTask("Validate Profile plug-in.", 1 + (profileFiles.size() * 4)); // $NON-NLS-1$

				monitor.subTask("Prepare validation."); //$NON-NLS-1$
				// First of all, delete the existing markers for project
				MarkersService.deleteMarkers(project, ProfilePluginValidationConstants.PROFILE_PLUGIN_VALIDATION_TYPE);
				monitor.worked(1);

				// Create the plug-in validation service
				final PluginValidationService pluginValidationService = new PluginValidationService();

				// For all profiles files in the plug-in
				for (final IFile profileFile : profileFiles) {

					// get the existing profiles
					final URI profileFileURI = URI.createPlatformResourceURI(profileFile.getFullPath().toOSString(), true);
					final Collection<Profile> profiles = loadProfiles(profileFileURI);

					if (!profiles.isEmpty()) {
						// First, create the extensions checker
						pluginValidationService.addPluginChecker(new ProfileExtensionsChecker(project, profileFile, profiles));

						// Create the profile definition checker (no definition must be done for static profiles)
						pluginValidationService.addPluginChecker(new ProfileDefinitionChecker(profileFile, profiles));

						// Create the dependencies checker (depending to the external profile references)
						pluginValidationService.addPluginChecker(new ProfileDependenciesChecker(project, profileFile, profiles.iterator().next().eResource()));
					}

					// Create the build checker
					pluginValidationService.addPluginChecker(new ProfileBuildChecker(project, profileFile));
				}

				monitor.worked(1);

				// Call the validate
				pluginValidationService.validate(monitor);
			});
		} catch (InvocationTargetException e) {
			Activator.log.error(e);
		} catch (InterruptedException e) {
			// Do nothing, just cancelled by user
		}
	}

	/**
	 * Loads the EObject from the given URI.
	 *
	 * @param uri
	 *            The URI from which the EObject is loaded.
	 * @return
	 *         The profiles available in the model (can be empty if no profile or if an error occurred).
	 */
	private static Collection<Profile> loadProfiles(final URI uri) {
		final Collection<Profile> profiles = new HashSet<>();

		final ResourceSet resourceSet = new ResourceSetImpl();
		try {
			final Resource resource = resourceSet.getResource(uri, true);
			if (null != resource) {
				if (!resource.getContents().isEmpty()) {
					final Iterator<EObject> contentIt = resource.getAllContents();
					while (contentIt.hasNext()) {
						final EObject content = contentIt.next();
						if (content instanceof Profile) {
							profiles.add((Profile) content);
						}
					}
				}
			}
		} catch (final Exception ex) {
			Activator.log.error("Cannot load file: " + uri.toString(), ex); //$NON-NLS-1$
		}

		return profiles;
	}

}

Back to the top