Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6c4f954a3a8565e26d8daf85ecb9ebb61d13bc40 (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
/*******************************************************************************
 * Copyright (c) 2013 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.ui.common.editor;

import static com.google.common.collect.Maps.newHashMap;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.plugin.EcorePlugin;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.ui.resource.IResourceSetProvider;
import org.eclipse.xtext.ui.resource.IStorage2UriMapperJdtExtensions;
import org.eclipse.xtext.ui.util.JdtClasspathUriResolver;
import org.eclipse.xtext.util.Pair;

import com.google.inject.Inject;
import com.google.inject.Provider;

/**
 * This class is copied from XtextResourceSetProvider with the only modification that we skip our own project in
 * the platform project mappings (marked with <b>[HRR]</b>)
 * 
 * @author Henrik Rentz-Reichert
 */
public class CustomResourceSetProvider implements IResourceSetProvider {

		private final static Logger LOG = Logger.getLogger(CustomResourceSetProvider.class);
		
		@Inject
		private Provider<XtextResourceSet> resourceSetProvider;

		@Inject
		private IStorage2UriMapperJdtExtensions storage2UriMapper;

		public ResourceSet get(IProject project) {
			XtextResourceSet set = resourceSetProvider.get();
			IJavaProject javaProject = JavaCore.create(project);
			if (javaProject != null && javaProject.exists()) {
				set.getURIConverter().getURIMap().putAll(computePlatformURIMap(javaProject));
				set.setClasspathURIContext(javaProject);
				set.setClasspathUriResolver(new JdtClasspathUriResolver());
			}
			return set;
		}

		protected Map<URI, URI> computePlatformURIMap(IJavaProject javaProject) {
			HashMap<URI, URI> hashMap = newHashMap();
			try {
				hashMap.putAll(EcorePlugin.computePlatformURIMap(false));
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
			}
			if (!javaProject.exists())
				return hashMap;
			try {
				IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots();
				for (IPackageFragmentRoot root : roots) {
					Pair<URI, URI> uriMapping = storage2UriMapper.getURIMapping(root);
					if (uriMapping != null) {
						
						// we could just install the prefix mapping, i.e. platform:resource/my.project/ -> file:/my/path/to/my.project/
						// but then we wouldn't be able to load resources when using hosted bundles, because the target path points to the bin folder.
						// so instead we install qualified file mappings, which also makes normalization faster (i.e. just a lookup in a map instead of testing prefix URIs)
						//
						Map<URI, IStorage> mapping = storage2UriMapper.getAllEntries(root);
						for (URI key : mapping.keySet()) {
							IStorage storage = mapping.get(key);
							URI physicalURI = null;
							if (storage instanceof IFile) {
								physicalURI = URI.createPlatformResourceURI(storage.getFullPath().toString(), true);
							} else {
								physicalURI = key.replacePrefix(uriMapping.getFirst(), uriMapping.getSecond());
							}
							hashMap.put(key, physicalURI);
							if (key.isPlatformResource()) {
								URI pluginURI = URI.createPlatformPluginURI(key.toPlatformString(false), false);
								hashMap.put(pluginURI, physicalURI);
							}
						}
					}
				}
				/*
				*/
				final IProject project = javaProject.getProject();
				for (IProject iProject : project.getWorkspace().getRoot().getProjects()) {
					// [HRR]: following line was:
					// if (iProject.isAccessible()) {
					if (iProject!=project && iProject.isAccessible()) {
						IPath location = iProject.getLocation();
						if (location != null) {
							// append a trailing slash so that URI.isPrefix is true.
							hashMap.put(URI.createPlatformResourceURI(iProject.getName()+"/", true), URI.createFileURI(location.toFile().getPath()+"/"));
						}
					}
				}
			} catch (JavaModelException e) {
				LOG.error(e.getMessage(), e);
			}
			return hashMap;
		}

	}

Back to the top