Search in sources :

Example 41 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.

the class FormatActionDelegate method format.

protected void format(IProgressMonitor monitor, IFile file) {
    if (monitor == null || monitor.isCanceled())
        return;
    try {
        monitor.beginTask("", 100);
        IContentDescription contentDescription = file.getContentDescription();
        monitor.worked(5);
        if (contentDescription != null) {
            IContentType contentType = contentDescription.getContentType();
            IStructuredFormatProcessor formatProcessor = getFormatProcessor(contentType.getId());
            if (formatProcessor != null && (monitor == null || !monitor.isCanceled())) {
                String message = NLS.bind(SSEUIMessages.FormatActionDelegate_3, new String[] { file.getFullPath().toString().substring(1) });
                monitor.subTask(message);
                formatProcessor.setProgressMonitor(monitor);
                formatProcessor.formatFile(file);
            }
        }
        monitor.worked(95);
        monitor.done();
    } catch (MalformedInputExceptionWithDetail e) {
        String message = NLS.bind(SSEUIMessages.FormatActionDelegate_5, new String[] { file.getFullPath().toString() });
        fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
    } catch (IOException e) {
        String message = NLS.bind(SSEUIMessages.FormatActionDelegate_4, new String[] { file.getFullPath().toString() });
        fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
    } catch (CoreException e) {
        String message = NLS.bind(SSEUIMessages.FormatActionDelegate_4, new String[] { file.getFullPath().toString() });
        fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
    }
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) MalformedInputExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 42 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.

the class TestCodedReader method doCoreTest.

protected Reader doCoreTest(String expectedJavaCharset, String expectedDetectedCharset, IFile file) throws CoreException, IOException {
    Reader reader;
    // create these first, to test exception being thrown correctly
    CodedReaderCreator codedReaderCreator = new CodedReaderCreator();
    codedReaderCreator.set(file);
    reader = codedReaderCreator.getCodedReader();
    String javaCharsetName = file.getCharset();
    IContentDescription description = file.getContentDescription();
    javaCharsetName = massageCharset(javaCharsetName);
    // codedReaderCreator.getEncodingMemento().getJavaCharsetName();
    if (expectedJavaCharset.equals("expectPlatformCharset")) {
        String platformDefault = NonContentBasedEncodingRules.useDefaultNameRules(null);
        assertTrue(javaCharsetName.equals(platformDefault));
    } else {
        boolean asExpected = javaCharsetName.equals(expectedJavaCharset);
        assertTrue(javaCharsetName + " did not equal the expected " + expectedJavaCharset + " (this is a VM dependent test)", asExpected);
    }
    String javaCharsetNameProperty = (String) description.getProperty(IContentDescription.CHARSET);
    String detectedCharsetNameProperty = getDetectedCharsetName(description);
    detectedCharsetNameProperty = massageCharset(detectedCharsetNameProperty);
    // }
    if (!expectedJavaCharset.equals("expectPlatformCharset")) {
        boolean expecedResult = expectedJavaCharset.equals(javaCharsetNameProperty);
        assertTrue("java based charset name was not as expected", expecedResult);
    } else {
        String expectedDefault = NonContentBasedEncodingRules.useDefaultNameRules(null);
        boolean ExpectedResult = expectedDefault.equals(javaCharsetNameProperty);
        assertTrue("java based charset name not as expected when platform default expected", ExpectedResult);
    }
    if (expectedDetectedCharset != null) {
        boolean expectedResult = expectedDetectedCharset.equals(detectedCharsetNameProperty);
        assertTrue("detected charset name was not as expected", expectedResult);
    }
    // test if can read/write file
    StringBuffer buffer = readInputStream(reader);
    if (DEBUG_TEST_DETAIL) {
        System.out.print(buffer);
    }
    return reader;
}
Also used : CodedReaderCreator(org.eclipse.wst.sse.core.internal.encoding.CodedReaderCreator) Reader(java.io.Reader) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 43 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.

the class TestContentTypeDetection method doGetContentTypeBasedOnFile.

protected IContentDescription doGetContentTypeBasedOnFile(IFile file) throws CoreException {
    IContentDescription fileContentDescription = file.getContentDescription();
    assertNotNull("file content description was null", fileContentDescription);
    return fileContentDescription;
}
Also used : IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 44 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.

the class TestContentTypeDetection method doGetContentTypeBasedOnStream.

protected IContentDescription doGetContentTypeBasedOnStream(IFile file) throws CoreException, IOException {
    IContentDescription streamContentDescription = null;
    InputStream inputStream = null;
    try {
        inputStream = file.getContents();
        streamContentDescription = Platform.getContentTypeManager().getDescriptionFor(inputStream, file.getName(), IContentDescription.ALL);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
    assertNotNull("content description was null", streamContentDescription);
    return streamContentDescription;
}
Also used : InputStream(java.io.InputStream) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 45 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.

the class TestSourceValidationFramework method detectContentTypes.

private IContentType[] detectContentTypes(String fileName) {
    IContentType[] types = null;
    IFile file = ensureFileIsAccessible(PROJECT_NAME + SEPARATOR + fileName, null);
    types = Platform.getContentTypeManager().findContentTypesFor(file.getName());
    if (types.length == 0) {
        IContentDescription d = null;
        try {
            // optimized description lookup, might not succeed
            d = file.getContentDescription();
            if (d != null) {
                types = new IContentType[] { d.getContentType() };
            }
        } catch (CoreException e) {
        /*
				 * should not be possible given the accessible and file type
				 * check above
				 */
        }
    }
    if (types == null) {
        types = Platform.getContentTypeManager().findContentTypesFor(file.getName());
    }
    return types;
}
Also used : 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)

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