Skip to main content
summaryrefslogtreecommitdiffstats
blob: 97735d8230feae58d5002680146ec0d678405592 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare 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
 *******************************************************************************/
package org.eclipse.xtend.typesystem.emf.ui;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcorePackage;
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.emf.ecore.util.EcoreUtil;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.ExternalPackageFragmentRoot;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.eclipse.xtend.shared.ui.Activator;
import org.eclipse.xtend.shared.ui.core.internal.JDTUtil;
import org.eclipse.xtend.shared.ui.core.internal.ResourceID;
import org.eclipse.xtend.typesystem.emf.ui.internal.EmfToolsLog;

/**
 * Analyzes a project's classpath for ecore metamodels. We need to take care of
 * Ecore files
 * <ul>
 * <li>directly contained in the classpath</li>
 * <li>in JARs within the classpath</li>
 * <li>in the classpath of projects referenced by this project (recursively)</li>
 * <li>in plugins referenced by the project, either within the target platform
 * or as referenced plugin projects (recursively)</li>
 * </ul>
 * <p>
 * Reading Ecore files occurs in background Jobs. Each Job uses its own
 * ResourceSet. This avoids concurrency issues.
 * </p>
 */
final class ProjectAnalyzer extends Job {
	private IJavaProject project;
	private ResourceSet rs;
	private Map<IStorage, Resource> mapping;
	private Map<String, EPackage> packages;

	public ProjectAnalyzer(IJavaProject project) {
		super("Analyzing accessible EMF metamodels for project " + project.getProject().getProject().getName());
		this.project = project;
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		if (EmfToolsPlugin.trace)
			System.out.println("Analyzing EMF metamodels for project " + project.getProject().getProject().getName());

		// load models
		rs = new ResourceSetImpl();
		mapping = new HashMap<IStorage, Resource>();
		packages = new HashMap<String, EPackage>();
		loadMetamodelsForProject(project, rs, monitor);
		// always add ecore
		packages.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);

		// done. now trigger build for the project.
		// only do this if it is an oaw project
		// do not build referencing projects. the EmfToolsPlugin will take care
		// of this
		if (Activator.getExtXptModelManager().findProject(project.getProject()) != null) {
			new BuildJob(project.getProject()).schedule();
		}

		return Status.OK_STATUS;
	}

	@SuppressWarnings("restriction")
	private void loadMetamodelsForProject(IJavaProject javaProject, final ResourceSet rs, IProgressMonitor monitor) {
		try {
			final String ext = "ecore";
			for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
				if (!root.isArchive()) {
					IResource rootResource = null;
					if (root instanceof ExternalPackageFragmentRoot) {
						rootResource = ((ExternalPackageFragmentRoot) root).resource();
					}
					else {
						rootResource = root.getUnderlyingResource();
					}
					if (rootResource != null) {
						try {
							rootResource.accept(new IResourceVisitor() {
								public boolean visit(IResource resource) throws CoreException {
									if (resource instanceof IFile && ext.equals(((IFile) resource).getFileExtension())) {
										loadModelFromStorage(rs, (IFile) resource);
									}
									return true;
								}
							});
						}
						catch (CoreException e) {
							EmfToolsLog.logError(e);
						}
					}
				}
				else {
					// skip JRE jars
					if (((JarPackageFragmentRoot) root).getPath().toString().contains("jre/lib")) {
						if (EmfToolsPlugin.trace)
							System.out.println("Skipping " + ((JarPackageFragmentRoot) root).getPath().toString());
						continue;
					}

					root.open(monitor);
					try {
						ZipFile zip = ((JarPackageFragmentRoot) root).getJar();

						Enumeration<? extends ZipEntry> entries = zip.entries();
						while (entries.hasMoreElements()) {
							ZipEntry entry = entries.nextElement();
							String name = entry.getName();
							if (name.endsWith(ext)) {
								String fqn = name.substring(0, name.length() - ext.length() - 1).replaceAll("/", "::");
								ResourceID resourceID = new ResourceID(fqn, ext);
								IStorage findStorage = JDTUtil.loadFromJar(resourceID, root);
								if (findStorage != null)
									loadModelFromStorage(rs, findStorage);
							}
						}
					}
					catch (CoreException e) {
						EmfToolsLog.logError(e);
					}
					finally {
						root.close();
					}
				}
			}
		}
		catch (JavaModelException e) {
			EmfToolsLog.logError(e);
		}
	}

	private void loadModelFromStorage(ResourceSet rs, IStorage storage) {
		URI uri = URI.createPlatformResourceURI(storage.getFullPath().toString(), true);
		if (EmfToolsPlugin.trace)
			System.out.println("Loading EMF metamodel " + storage.getFullPath().toString());

		final Resource r = rs.createResource(uri);
		if (r.isLoaded() && !r.isModified())
			return;
		try {
			r.load(storage.getContents(), Collections.EMPTY_MAP);
			mapping.put(storage, r);
			Collection<EPackage> packages = EcoreUtil.getObjectsByType(r.getContents(), EcorePackage.Literals.EPACKAGE);
			for (EPackage pack : packages) {
				registerPackage(storage, pack);
			}
		}
		catch (IOException e) {
			EmfToolsLog.logError(e);
		}
		catch (CoreException e) {
			EmfToolsLog.logError(e);
		}
	}

	private void registerPackage(IStorage storage, EPackage pack) {
		// finding duplicates by nsURI is better than by name since package
		// names may be used across MMs
		if (this.packages.containsKey(pack.getNsURI())) {
			if (EmfToolsPlugin.trace)
				System.out.println("Did not register '" + pack.getName() + "' from " + storage.getFullPath()
						+ " because an EPackage with the same nsURI has already been registered.");
		}
		else {
			this.packages.put(pack.getNsURI(), pack);
		}
		// recurse into subpackages
		for (EPackage p : pack.getESubpackages()) {
			registerPackage(storage, p);
		}
	}

	public Map<String, EPackage> getNamedEPackageMap() {
		if (packages == null) {
			run(new NullProgressMonitor());
		}
		return Collections.unmodifiableMap(packages);
	}
}

Back to the top