Search in sources :

Example 6 with IContentDescription

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;
}
Also used : InputStream(java.io.InputStream) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) IContentType(org.eclipse.core.runtime.content.IContentType) IEditorPart(org.eclipse.ui.IEditorPart) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) IWorkbench(org.eclipse.ui.IWorkbench) IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IEditorRegistry(org.eclipse.ui.IEditorRegistry) IExtensionRegistry(org.eclipse.core.runtime.IExtensionRegistry)

Example 7 with IContentDescription

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;
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IContentType(org.eclipse.core.runtime.content.IContentType) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IContainer(org.eclipse.core.resources.IContainer)

Example 8 with IContentDescription

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;
}
Also used : IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) UnsupportedCharsetExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 9 with IContentDescription

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;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) BufferedInputStream(java.io.BufferedInputStream) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 10 with IContentDescription

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream) InputStream(java.io.InputStream) IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)

Aggregations

IContentDescription (org.eclipse.core.runtime.content.IContentDescription)58 CoreException (org.eclipse.core.runtime.CoreException)27 IOException (java.io.IOException)24 IContentType (org.eclipse.core.runtime.content.IContentType)22 InputStream (java.io.InputStream)19 IFile (org.eclipse.core.resources.IFile)14 QualifiedName (org.eclipse.core.runtime.QualifiedName)13 IContentTypeManager (org.eclipse.core.runtime.content.IContentTypeManager)8 Reader (java.io.Reader)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 BufferedReader (java.io.BufferedReader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStreamReader (java.io.InputStreamReader)4 IStructuredFormatProcessor (org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor)4 SequenceInputStream (java.io.SequenceInputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 IResource (org.eclipse.core.resources.IResource)3