blob: 48bdb82f02acad3c13571c44d9b9478299f02e27 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008 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.core.internal.utility;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.jpt.core.JptCorePlugin;
/**
* A collection of utilities for dealing with the Eclipse platform API.
*/
public class PlatformTools {
/**
* 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) {
JptCorePlugin.log(ex);
} finally {
try {
fileContents.close();
} catch (IOException ex) {
JptCorePlugin.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();
}
}