use of org.eclipse.core.runtime.content.IContentDescription 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;
}
use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.
the class FormatHandler method processorAvailable.
/* (non-Javadoc)
* @see org.eclipse.wst.sse.ui.internal.actions.ResourceActionDelegate#processorAvailable(org.eclipse.core.resources.IResource)
*/
protected boolean processorAvailable(IResource resource) {
boolean result = false;
if (resource.isAccessible()) {
try {
if (resource instanceof IFile) {
IFile file = (IFile) resource;
IStructuredFormatProcessor formatProcessor = null;
IContentDescription contentDescription = file.getContentDescription();
if (contentDescription != null) {
IContentType contentType = contentDescription.getContentType();
formatProcessor = getFormatProcessor(contentType.getId());
}
if (formatProcessor != null)
result = true;
} else if (resource instanceof IContainer) {
IContainer container = (IContainer) resource;
IResource[] members;
members = container.members();
for (int i = 0; i < members.length; i++) {
boolean available = processorAvailable(members[i]);
if (available) {
result = true;
break;
}
}
}
} catch (CoreException e) {
Logger.logException(e);
}
}
return result;
}
use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.
the class CodedReaderCreator method checkForEncodingInContents.
private EncodingMemento checkForEncodingInContents(InputStream limitedStream) throws CoreException, IOException {
EncodingMemento result = null;
// have been set, and no need to get again.
if (fEncodingMemento != null) {
result = fEncodingMemento;
} else {
if (fClientSuppliedStream) {
try {
limitedStream.reset();
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
IContentDescription contentDescription = contentTypeManager.getDescriptionFor(limitedStream, fFilename, IContentDescription.ALL);
if (contentDescription != null) {
fEncodingMemento = createMemento(contentDescription);
}
result = fEncodingMemento;
} finally {
limitedStream.reset();
}
} else {
// throw new IllegalStateException("unexpected state:
// encodingMemento was null but no input stream supplied by
// client"); //$NON-NLS-1$
result = null;
}
}
if (result != null && !result.isValid() && !forceDefault()) {
throw new UnsupportedCharsetExceptionWithDetail(result);
}
return result;
}
use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.
the class CodedReaderCreator method findMementoFromFileCase.
/**
* @param iFile
* @throws CoreException
* @throws IOException
*/
private EncodingMemento findMementoFromFileCase() throws CoreException, IOException {
EncodingMemento result = null;
IContentDescription contentDescription = null;
try {
// cost of sometimes returning null
if (fIFile.exists())
contentDescription = fIFile.getContentDescription();
} catch (CoreException e) {
// Assume if core exception occurs, we can still try more
// expensive
// discovery options.
Logger.logException(e);
}
if (contentDescription == null && fIFile.isAccessible()) {
InputStream contents = null;
try {
contents = fIFile.getContents();
contentDescription = Platform.getContentTypeManager().getDescriptionFor(contents, fIFile.getName(), IContentDescription.ALL);
} catch (CoreException e1) {
// Assume if core exception occurs, we can't really do much
// with
// determining encoding, etc.
Logger.logException(e1);
throw e1;
} catch (IOException e2) {
// We likely couldn't get the contents of the file, something
// is really wrong
Logger.logException(e2);
throw e2;
}
if (contents != null) {
try {
contents.close();
} catch (IOException e2) {
Logger.logException(e2);
}
}
}
if (contentDescription != null) {
result = createMemento(contentDescription);
}
return result;
}
use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.
the class CodedReaderCreator method findMementoFromStreamCase.
/**
* The primary method which contains the highest level rules for how to
* decide appropriate decoding rules: 1. first check for unicode stream 2.
* then looked for encoding specified in content (according to the type of
* content that is it ... xml, html, jsp, etc. 3. then check for various
* settings: file settings first, if null check project settings, if null,
* check user preferences. 4. lastly (or, what is the last user
* preference) is to use "workbench defaults".
*
* @throws IOException
* @throws CoreException
*/
private EncodingMemento findMementoFromStreamCase() throws CoreException, IOException {
EncodingMemento result = null;
InputStream resettableLimitedStream = null;
try {
resettableLimitedStream = getLimitedStream(getResettableStream());
if (resettableLimitedStream != null) {
// first check for unicode stream
result = checkStreamForBOM(resettableLimitedStream);
// if not that, then check contents
if (result == null) {
resettableLimitedStream.reset();
result = checkForEncodingInContents(resettableLimitedStream);
}
} else {
// stream null, may name's not.
if (fFilename != null) {
// filename not null
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
IContentDescription contentDescription = contentTypeManager.getDescriptionFor(new NullInputStream(), fFilename, IContentDescription.ALL);
if (contentDescription != null) {
result = createMemento(contentDescription);
}
}
}
} finally {
if (resettableLimitedStream != null) {
handleStreamClose(resettableLimitedStream);
}
}
return result;
}
Aggregations