blob: 6981696e0d81ec70b28995035ef13c20b42d2e49 (
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
|
/*******************************************************************************
* Copyright (c) 2008, 2010 Oracle. 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:
* Oracle - initial API and implementation
******************************************************************************/
package org.eclipse.jpt.common.core.internal.utility;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.jpt.common.core.JptCommonCorePlugin;
/**
* A collection of utilities for dealing with the Eclipse platform API.
*/
public class PlatformTools {
/**
* Return the {@link IContainer} with the workspace relative "full" path
*/
public static IContainer getContainer(IPath fullContainerPath) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
return root.getContainerForLocation(root.getLocation().append(fullContainerPath));
}
/**
* Return the {@link IFile} with the workspace relative "full" path
*/
public static IFile getFile(IPath fullFilePath) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
return root.getFileForLocation(root.getLocation().append(fullFilePath));
}
/**
* Return the specified file's content type,
* using the Eclipse platform's content type manager.
*/
public static IContentType getContentType(IFile file) {
String fileName = file.getName();
InputStream fileContents = null;
try {
fileContents = file.getContents();
} catch (CoreException ex) {
// seems like we can ignore any exception that might occur here;
// e.g. we get a FNFE if the workspace is out of synch with the O/S file system
// JptCorePlugin.log(ex);
// look for content type based on the file name only(?)
return findContentTypeFor(fileName);
}
IContentType contentType = null;
try {
contentType = findContentTypeFor(fileContents, fileName);
} catch (IOException ex) {
JptCommonCorePlugin.log(ex);
} finally {
try {
fileContents.close();
} catch (IOException ex) {
JptCommonCorePlugin.log(ex);
}
}
return contentType;
}
private static IContentType findContentTypeFor(InputStream fileContents, String fileName) throws IOException {
return getContentTypeManager().findContentTypeFor(fileContents, fileName);
}
private static IContentType findContentTypeFor(String fileName) {
return getContentTypeManager().findContentTypeFor(fileName);
}
private static IContentTypeManager getContentTypeManager() {
return Platform.getContentTypeManager();
}
private PlatformTools() {
super();
throw new UnsupportedOperationException();
}
}
|