Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 923412358b4f75f41c15552e6f664e077f5d66fc (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
/*******************************************************************************
 * Copyright (c) 2005 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 *  $$RCSfile: WorkspaceResourceHandler.java,v $$
 *  $$Revision: 1.2 $$  $$Date: 2005/02/15 23:04:14 $$ 
 */
package org.eclipse.jem.internal.util.emf.workbench;

import org.eclipse.core.resources.*;
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.URIConverterImpl;

import org.eclipse.jem.util.emf.workbench.ResourceHandler;
import org.eclipse.jem.util.emf.workbench.WorkbenchResourceHelperBase;
import org.eclipse.jem.util.plugin.JEMUtilPlugin;

/**
 * The main purpose of this class is to redirect, if necessary, to another 
 * ResourceSet.  This class should be used in conjunction with the WorkbenchURIConverter
 * so that the URIs passed will use the platform protocol.  Anything else will be considered
 * to be ambiguous and we will not be able to redirect.
 */
public class WorkspaceResourceHandler implements ResourceHandler {
	/**
	 * Constructor for WorkspaceResourceHandler.
	 */
	public WorkspaceResourceHandler() {
		super();
	}
	/*
	* @see IResourceHandler#getResource(ResourceSet, URI)
	*/
	public Resource getResource(ResourceSet originatingResourceSet, URI uri) {
		if (WorkbenchResourceHelperBase.isPlatformResourceURI(uri))
			return getResourceForPlatformProtocol(originatingResourceSet, uri);
		URI mappedURI = ((URIConverterImpl.URIMap)originatingResourceSet.getURIConverter().getURIMap()).getURI(uri);
		if (isGlobalPluginLoad(mappedURI))
			return getResourceForPlatformPluginProtocol(originatingResourceSet, uri);
		return null;
	}
	/**
	 * Redirect to the correct project based on the project name in the <code>uri</code>.
	 * The <code>uri</code> will be in the following format:   platform:/resource/[project name].
	 */
	protected Resource createResourceForPlatformProtocol(ResourceSet originatingResourceSet, URI uri) {
		String projectName = uri.segment(1);
		IProject project = getProject(projectName);
		if (project != null && project.isAccessible()) {
			ResourceSet set = WorkbenchResourceHelperBase.getResourceSet(project);
			if (originatingResourceSet != set)
				return createResource(uri, set);
		}
		return null;
	}
	/**
		 * Redirect to the correct project based on the project name in the <code>uri</code>.
		 * The <code>uri</code> will be in the following format:   platform:/resource/[project name].
		 */
	protected Resource createResourceForPlatformPluginProtocol(ResourceSet originatingResourceSet, URI uri) {
			
		ResourceSet set = JEMUtilPlugin.getPluginResourceSet();
		return createResource(uri, set);
		}
	protected Resource createResource(URI uri, ResourceSet redirectedResourceSet) {
		return redirectedResourceSet.createResource(uri);
	}
	/**
	 * Redirect to the correct project based on the first segment in the file name.
	 * This is for compatability purposes for people using the platform:/resource protocol.
	 */
	protected Resource getResourceForPlatformProtocol(ResourceSet originatingResourceSet, URI uri) {
		String projectName = uri.segment(1);
		IProject project = getProject(projectName);
		if (project != null && project.isAccessible()) {
			ResourceSet set = WorkbenchResourceHelperBase.getResourceSet(project);
			if (originatingResourceSet != set)
				return getResource(uri, set);
		}
		return null;
	}
	/**
		 * Redirect to the correct project based on the first segment in the file name.
		 * This is for compatability purposes for people using the platform:/resource protocol.
		 */
	protected Resource getResourceForPlatformPluginProtocol(ResourceSet originatingResourceSet, URI uri) {
			
		ResourceSet set = JEMUtilPlugin.getPluginResourceSet();
		return getResource(uri, set);
			
	}
	protected Resource getResource(URI uri, ResourceSet redirectedResourceSet) {
		return redirectedResourceSet.getResource(uri, false);
	}
	
	protected IWorkspace getWorkspace() {
		return ResourcesPlugin.getWorkspace();
	}
	protected IProject getProject(String projectName) {
		IWorkspace ws = getWorkspace();
		if (ws == null)
			return null;
		return ws.getRoot().getProject(projectName);
	}
	protected IProject getProject(ResourceSet resourceSet) {
		return WorkbenchResourceHelperBase.getProject(resourceSet);
	}
	/**
	 * @see org.eclipse.jem.util.ResourceHandler#createResource(ResourceSet, URI)
	 */
	public Resource createResource(ResourceSet originatingResourceSet, URI uri) {
		if (WorkbenchResourceHelperBase.isPlatformResourceURI(uri))
			return createResourceForPlatformProtocol(originatingResourceSet, uri);
		URI mappedURI = ((URIConverterImpl.URIMap)originatingResourceSet.getURIConverter().getURIMap()).getURI(uri);
		if (isGlobalPluginLoad(mappedURI))
			return createResourceForPlatformPluginProtocol(originatingResourceSet, uri);
		return null;
	}
	/**
	 * @see org.eclipse.jem.util.ResourceHandler#getEObjectFailed(ResourceSet, URI, boolean)
	 * Subclasses may override.
	 */
	public EObject getEObjectFailed(ResourceSet originatingResourceSet, URI uri, boolean loadOnDemand) {
		return null;
	}
	
	protected boolean isGlobalPluginLoad(URI aURI) {
		if (WorkbenchResourceHelperBase.isPlatformPluginResourceURI(aURI)) {
			String[] globalPlugins = JEMUtilPlugin.getGlobalLoadingPluginNames();
			for (int i=0;i<globalPlugins.length;i++) {
				if (aURI.segment(1).startsWith(globalPlugins[i]))
					return true;
			}
		}
		return false;
	}
}

Back to the top