use of org.eclipse.core.runtime.content.IContentTypeManager in project hale by halestudio.
the class HaleIO method findContentTypesFor.
/**
* Find the content types that match the given file name and/or input.
*
* NOTE: The implementation should try to restrict the result to one content
* type and only use the input supplier if absolutely needed.
*
* @param types the types to match
* @param in the input supplier to use for testing, may be <code>null</code>
* if the file name is not <code>null</code>
* @param filename the file name, may be <code>null</code> if the input
* supplier is not <code>null</code>
* @return the matched content types
*/
public static List<IContentType> findContentTypesFor(Collection<IContentType> types, InputSupplier<? extends InputStream> in, String filename) {
Preconditions.checkArgument(filename != null || in != null, "At least one of input supplier and file name must not be null");
List<IContentType> results = new ArrayList<IContentType>();
if (filename != null && !filename.isEmpty()) {
// test file extension
String lowerFile = filename.toLowerCase();
for (IContentType type : types) {
String[] extensions = type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC);
boolean match = false;
for (int i = 0; i < extensions.length && !match; i++) {
if (lowerFile.endsWith("." + extensions[i].toLowerCase())) {
match = true;
}
}
if (match) {
results.add(type);
}
}
}
if ((results.isEmpty() || results.size() > 1) && in != null) {
// remember previous results
List<IContentType> extensionResults = null;
if (!results.isEmpty()) {
extensionResults = new ArrayList<>(results);
}
// clear results because only an ambiguous result was found
results.clear();
// use input stream to make a better test
IContentTypeManager ctm = HalePlatform.getContentTypeManager();
try {
InputStream is = in.getInput();
/*
* IContentTypeManager.findContentTypes seems to return all kind
* of content types that match in any way, but ordered by
* relevance - so if all but the allowed types are removed, the
* remaining types may be very irrelevant and not a match that
* actually was determined based on the input stream.
*
* Thus findContentTypesFor should not be used or only relied
* upon the single best match that is returned. It is now only
* used as fall-back if there is no result for
* findContentTypeFor.
*/
// instead use findContentTypeFor
IContentType candidate = ctm.findContentTypeFor(is, null);
if (types.contains(candidate)) {
results.add(candidate);
}
is.close();
} catch (IOException e) {
log.warn("Could not read input to determine content type", e);
}
if (results.isEmpty()) {
/*
* Fall-back to findContentTypesFor if there was no valid result
* for findContentTypeFor.
*/
try (InputStream is = in.getInput()) {
IContentType[] candidates = ctm.findContentTypesFor(is, filename);
for (IContentType candidate : candidates) {
if (types.contains(candidate)) {
results.add(candidate);
}
}
} catch (IOException e) {
log.warn("Could not read input to determine content type", e);
}
}
if (results.isEmpty() && extensionResults != null) {
/*
* If there was no valid result for the stream check, but for
* the extension checks, use those results. This may happen for
* instance for generic XML files, that have no specific root
* element.
*/
results.addAll(extensionResults);
}
}
return results;
}
use of org.eclipse.core.runtime.content.IContentTypeManager in project eclipse.platform.text by eclipse.
the class ResourceTextFileBufferManager method isTextFileLocation.
@Override
public boolean isTextFileLocation(IPath location, boolean strict) {
Assert.isNotNull(location);
location = FileBuffers.normalizeLocation(location);
IFile file = FileBuffers.getWorkspaceFileAtLocation(location, true);
if (file != null) {
if (file.exists()) {
try {
IContentDescription description = file.getContentDescription();
if (description != null) {
IContentType type = description.getContentType();
if (type != null)
return type.isKindOf(TEXT_CONTENT_TYPE);
}
} catch (CoreException x) {
// ignore: API specification tells return true if content type can't be determined
}
} else {
IContentTypeManager manager = Platform.getContentTypeManager();
IContentType[] contentTypes = manager.findContentTypesFor(file.getName());
if (contentTypes != null && contentTypes.length > 0) {
for (IContentType contentType : contentTypes) if (contentType.isKindOf(TEXT_CONTENT_TYPE))
return true;
return false;
}
}
return !strict;
}
return isTextFileLocation(FileBuffers.getFileStoreAtLocation(location), strict);
}
use of org.eclipse.core.runtime.content.IContentTypeManager in project eclipse.platform.text by eclipse.
the class TextFileBufferManager method isTextFileLocation.
/**
* Returns whether a file store at the given location is or can be considered a
* text file. If the file store exists, the concrete content type of the file store is
* checked. If the concrete content type for the existing file store can not be
* determined, this method returns <code>!strict</code>. If the file store does
* not exist, it is checked whether a text content type is associated with
* the given location. If no content type is associated with the location,
* this method returns <code>!strict</code>.
* <p>
* The provided location is either a full path of a workspace resource or an
* absolute path in the local file system. The file buffer manager does not
* resolve the location of workspace resources in the case of linked
* resources.
* </p>
*
* @param fileStore file store to check
* @param strict <code>true</code> if a file with unknown content type
* is not treated as text file, <code>false</code> otherwise
* @return <code>true</code> if the location is a text file location
* @since 3.3
*/
protected boolean isTextFileLocation(IFileStore fileStore, boolean strict) {
if (fileStore == null)
return false;
IContentTypeManager manager = Platform.getContentTypeManager();
IFileInfo fileInfo = fileStore.fetchInfo();
if (fileInfo.exists()) {
try (InputStream is = fileStore.openInputStream(EFS.NONE, null)) {
IContentDescription description = manager.getDescriptionFor(is, fileStore.getName(), IContentDescription.ALL);
if (description != null) {
IContentType type = description.getContentType();
if (type != null)
return type.isKindOf(TEXT_CONTENT_TYPE);
}
} catch (CoreException ex) {
// ignore: API specification tells return true if content type can't be determined
} catch (IOException ex) {
// ignore: API specification tells return true if content type can't be determined
}
return !strict;
}
IContentType[] contentTypes = manager.findContentTypesFor(fileStore.getName());
if (contentTypes != null && contentTypes.length > 0) {
for (IContentType contentType : contentTypes) if (contentType.isKindOf(TEXT_CONTENT_TYPE))
return true;
return false;
}
return !strict;
}
use of org.eclipse.core.runtime.content.IContentTypeManager in project erlide_eclipse by erlang.
the class CommonUtils method isErlangFileContentFileName.
public static boolean isErlangFileContentFileName(final String fileName) {
final IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
final IContentType[] contentTypes = contentTypeManager.findContentTypesFor(fileName);
for (final IContentType contentType : contentTypes) {
if ("org.erlide.core.content.erlang".equals(contentType.getId())) {
return true;
}
}
return false;
}
use of org.eclipse.core.runtime.content.IContentTypeManager in project liferay-ide by liferay.
the class DefaultScriptEditorHelper method createEditorPart.
public IEditorPart createEditorPart(ScriptPropertyEditorInput editorInput, IEditorSite editorSite) {
IEditorPart editorPart = null;
try {
String fileName = editorInput.getName();
IContentType contentType = null;
/*
* if ( editorInput.getProperty().hasAnnotation( ContentType.class )
* ) { String contentTypeId =
* editorInput.getProperty().getAnnotation( ContentType.class
* ).contentTypeId(); contentType =
* Platform.getContentTypeManager().getContentType( contentTypeId );
* } else
*/
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
InputStream inputStream = editorInput.getStorage().getContents();
IContentDescription contentDescription = contentTypeManager.getDescriptionFor(inputStream, fileName, IContentDescription.ALL);
if (contentDescription != null) {
contentType = contentDescription.getContentType();
}
if (contentType == null) {
// use basic text content type
contentType = Platform.getContentTypeManager().getContentType("org.eclipse.core.runtime.text");
}
IWorkbench workBench = PlatformUI.getWorkbench();
IEditorRegistry editRegistry = workBench.getEditorRegistry();
IEditorDescriptor defaultEditorDescriptor = editRegistry.getDefaultEditor(fileName, contentType);
String editorId = defaultEditorDescriptor.getId();
IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
IConfigurationElement[] editorConfigs = extensionRegistry.getConfigurationElementsFor("org.eclipse.ui.editors");
for (IConfigurationElement config : editorConfigs) {
if (editorId.equals(config.getAttribute("id"))) {
editorPart = (IEditorPart) config.createExecutableExtension("class");
break;
}
}
editorPart.init(editorSite, editorInput);
} catch (Exception e) {
KaleoUI.logError("Could not create default script editor.", e);
}
return editorPart;
}
Aggregations