Skip to main content
summaryrefslogtreecommitdiffstats
blob: 33dd86a667b949b6559fe235ee0adb70faf583b8 (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
/*******************************************************************************
 * Copyright (c) 2009 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
 *******************************************************************************/
package org.eclipse.cdt.utils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.IFilesystemUtility;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;

/**
 * Manager class that consults contributors to the FileSystemUtility extension point to perform operations corresponding to those filesystems.
 *
 * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
 * part of a work in progress. There is no guarantee that this API will work or
 * that it will remain the same. Please do not use this API without consulting
 * with the CDT team.
 * 
 * @author crecoskie
 * @noextend This class is not intended to be extended by clients.
 * @since 5.0.3
 */
public class FileSystemUtilityManager {
	
	private static FileSystemUtilityManager instance;
	
	private Map<String, IFilesystemUtility> fSchemeToUtilityImplementerMap;
	
	private static String EXTENSION_ID = "FileSystemUtility"; //$NON-NLS-1$
	
	private FileSystemUtilityManager() {
		fSchemeToUtilityImplementerMap = new HashMap<String, IFilesystemUtility>();
		loadExtensions();
	}
	
	private void loadExtensions() {
		IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(CCorePlugin.PLUGIN_ID,
				EXTENSION_ID);
		if (extension != null) {
			IExtension[] extensions = extension.getExtensions();
			for (int i = 0; i < extensions.length; i++) {
				IConfigurationElement[] configElements = extensions[i].getConfigurationElements();
				for (int j = 0; j < configElements.length; j++) {

					String scheme = configElements[j].getAttribute("scheme"); //$NON-NLS-1$
					String utility = configElements[j].getAttribute("class"); //$NON-NLS-1$

					if (utility != null) {
						try {
							Object execExt = configElements[j].createExecutableExtension("class"); //$NON-NLS-1$
							if (execExt instanceof IFilesystemUtility) {
								fSchemeToUtilityImplementerMap.put(scheme, (IFilesystemUtility) execExt);
							}
						} catch (CoreException e) {
							CCorePlugin.log(e);
						}
					}

				}
			}
		}

	}

	public synchronized static FileSystemUtilityManager getDefault() {
		if(instance == null) {
			instance = new FileSystemUtilityManager();
		}
		return instance;
	}
	
	/**
	 * Gets the path out of a URI.  Right now this is hardcoded to deal with a select few filesystems.
	 * In the future, it would be better if EFS had an API for this.
	 * 
	 * @param locationURI
	 * @return String representing the path.
	 */
	public String getPathFromURI(URI locationURI) {
		IFilesystemUtility utility = fSchemeToUtilityImplementerMap.get(locationURI.getScheme());
		
		if(utility == null) {
			return locationURI.getPath();
		}
			
		else {
			return utility.getPathFromURI(locationURI);
		}
		
	}
	
	/**
	 * In the case of a managed (linked) filesystem, returns the URI that this URI ultimately will
	 * point to.  Otherwise, returns null.
	 * 
	 * @param locationURI
	 * @return URI
	 */
	public URI getManagedURI(URI locationURI) {
		IFilesystemUtility utility = fSchemeToUtilityImplementerMap.get(locationURI.getScheme());
		
		if(utility == null) {
			return null;
		}
			
		else {
			return utility.getBaseURI(locationURI);
		}
	}

	/**
	 * Creates a new URI which clones the contents of the original URI, but with the path
	 * replaced by the given path.  Returns null on error.
	 * 
	 * @param uri
	 * @param path
	 * @return URI
	 */
	public URI replacePath(URI uri, String path) {
		IFilesystemUtility utility = fSchemeToUtilityImplementerMap.get(uri.getScheme());
		
		if(utility == null) {
			// if there is no corresponding utility, then assume we can just replace the path field
			try {
				return  new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
			               path, // replaced! 
			               uri.getQuery(),uri.getFragment());
			} catch (URISyntaxException e) {
				CCorePlugin.log(e);
			}
			
			return null;
		}
			
		else {
			return utility.replacePathInURI(uri, path);
		}
	}

}

Back to the top