Search in sources :

Example 46 with IContentDescription

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

the class CodedStreamCreator method checkForEncodingInContents.

private EncodingMemento checkForEncodingInContents() throws CoreException, IOException {
    EncodingMemento result = null;
    // if encoding memento already set, and no need to get again.
    if (fEncodingMemento != null) {
        result = fEncodingMemento;
    } else {
        if (fClientSuppliedReader) {
            fReader.reset();
            IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
            try {
                IContentDescription contentDescription = contentTypeManager.getDescriptionFor(fReader, fFilename, IContentDescription.ALL);
                if (contentDescription != null) {
                    fEncodingMemento = createMemento(contentDescription);
                } else {
                    // $NON-NLS-1$
                    fEncodingMemento = CodedIO.createEncodingMemento("UTF-8");
                }
            } catch (NullPointerException e) {
                // TODO: work around for 5/14 bug in base, should be
                // removed when move up to 5/21
                // just created a simple default one
                // $NON-NLS-1$
                fEncodingMemento = CodedIO.createEncodingMemento("UTF-8");
            }
            result = fEncodingMemento;
        } else {
            // $NON-NLS-1$
            throw new IllegalStateException("unexpected state: encodingMemento was null but no input stream supplied");
        }
    }
    // }
    return result;
}
Also used : IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 47 with IContentDescription

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

the class DebugTextEditor method detectContentType.

IContentType detectContentType(IPath location) {
    IContentType type = null;
    IResource resource = FileBuffers.getWorkspaceFileAtLocation(location);
    if (resource != null) {
        if (resource.getType() == IResource.FILE && resource.isAccessible()) {
            IContentDescription d = null;
            try {
                // Optimized description lookup, might not succeed
                d = ((IFile) resource).getContentDescription();
                if (d != null) {
                    type = d.getContentType();
                }
            } catch (CoreException e) {
            // Should not be possible given the accessible and file
            // type check above
            }
            if (type == null) {
                type = Platform.getContentTypeManager().findContentTypeFor(resource.getName());
            }
        }
    } else {
        File file = FileBuffers.getSystemFileAtLocation(location);
        if (file != null) {
            InputStream input = null;
            try {
                input = new FileInputStream(file);
                type = Platform.getContentTypeManager().findContentTypeFor(input, location.toOSString());
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            } finally {
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException e1) {
                    }
                }
            }
            if (type == null) {
                type = Platform.getContentTypeManager().findContentTypeFor(file.getName());
            }
        }
    }
    if (type == null) {
        type = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT);
    }
    return type;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) FileInputStream(java.io.FileInputStream)

Example 48 with IContentDescription

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

the class DocumentRegionProcessor method getContentType.

protected String getContentType(IDocument doc) {
    if (doc == null)
        return null;
    String contentTypeId = null;
    IContentType ct = null;
    try {
        IContentDescription desc = Platform.getContentTypeManager().getDescriptionFor(new StringReader(doc.get()), null, IContentDescription.ALL);
        if (desc != null) {
            ct = desc.getContentType();
            if (ct != null)
                contentTypeId = ct.getId();
        }
    } catch (IOException e) {
    // just bail
    }
    return contentTypeId;
}
Also used : StringReader(java.io.StringReader) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 49 with IContentDescription

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

the class TestContentDescription method getContentDescription.

/**
 * Returns content description for an input stream
 * Assumes it's JSP content.
 * Closes the input stream when finished.
 *
 * @param in
 * @return the IContentDescription for in
 */
private IContentDescription getContentDescription(InputStream in) {
    if (in == null)
        return null;
    IContentDescription desc = null;
    try {
        IContentType contentTypeJSP = Platform.getContentTypeManager().getContentType(ContentTypeIdForJSP.ContentTypeID_JSP);
        desc = contentTypeJSP.getDescriptionFor(in, IContentDescription.ALL);
    } catch (IOException e) {
        Logger.logException(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                Logger.logException(e);
            }
        }
    }
    return desc;
}
Also used : IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 50 with IContentDescription

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

the class TestContentDescription method doTestContentDescription.

private void doTestContentDescription(String filePath, HashMap expectedProperties) {
    IContentDescription desc = getContentDescription(filePath);
    assertNotNull("couldn't get IContentDescription for file:[" + filePath + "]", desc);
    Object[] keys = expectedProperties.keySet().toArray();
    for (int i = 0; i < keys.length; i++) {
        Object expected = expectedProperties.get(keys[i]);
        Object detected = desc.getProperty((QualifiedName) keys[i]);
        assertEquals("unexpected property value", expected, detected);
    }
}
Also used : 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