Search in sources :

Example 36 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.

the class StorageDocumentProvider method getContentType.

@Override
public IContentType getContentType(Object element) throws CoreException {
    if (element instanceof IStorageEditorInput) {
        IStorage storage = ((IStorageEditorInput) element).getStorage();
        try {
            IContentDescription desc;
            IDocument document = getDocument(element);
            if (document != null) {
                try (Reader reader = new DocumentReader(document)) {
                    desc = Platform.getContentTypeManager().getDescriptionFor(reader, storage.getName(), NO_PROPERTIES);
                }
            } else {
                try (InputStream stream = storage.getContents()) {
                    desc = Platform.getContentTypeManager().getDescriptionFor(stream, storage.getName(), NO_PROPERTIES);
                }
            }
            if (desc != null && desc.getContentType() != null)
                return desc.getContentType();
        } catch (IOException x) {
            IPath path = storage.getFullPath();
            String name;
            if (path != null)
                name = path.toOSString();
            else
                name = storage.getName();
            String message;
            if (name != null)
                message = NLSUtility.format(TextEditorMessages.StorageDocumentProvider_getContentDescriptionFor, name);
            else
                message = TextEditorMessages.StorageDocumentProvider_getContentDescription;
            throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, x));
        }
    }
    return super.getContentType(element);
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IResourceStatus(org.eclipse.core.resources.IResourceStatus) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IStorage(org.eclipse.core.resources.IStorage) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IDocument(org.eclipse.jface.text.IDocument)

Example 37 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.

the class FileStoreTextFileBuffer method computeEncoding.

private String computeEncoding() {
    // Make sure cache is up to date
    if (!fIsCacheUpdated)
        cacheEncodingState();
    // User-defined encoding has first priority
    if (fExplicitEncoding != null)
        return fExplicitEncoding;
    // Probe content
    try (Reader reader = new DocumentReader(fDocument)) {
        QualifiedName[] options = new QualifiedName[] { IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK };
        IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(reader, fFileStore.getName(), options);
        if (description != null) {
            String encoding = description.getCharset();
            if (encoding != null)
                return encoding;
        }
    } catch (IOException ex) {
    // Try next strategy
    }
    // Use file's encoding if the file has a BOM
    if (fHasBOM)
        return fEncoding;
    // Use global default
    return fManager.getDefaultEncoding();
}
Also used : QualifiedName(org.eclipse.core.runtime.QualifiedName) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 38 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.

the class ResourceTextFileBuffer method computeEncoding.

private String computeEncoding() {
    // User-defined encoding has first priority
    if (fExplicitEncoding != null)
        return fExplicitEncoding;
    try (Reader reader = new DocumentReader(fDocument)) {
        QualifiedName[] options = new QualifiedName[] { IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK };
        IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(reader, fFile.getName(), options);
        if (description != null) {
            String encoding = description.getCharset();
            if (encoding != null)
                return encoding;
        }
    } catch (IOException ex) {
    // try next strategy
    }
    // Use file's encoding if the file has a BOM
    if (fBOM != null)
        return fEncoding;
    // Use parent chain
    try {
        return fFile.getParent().getDefaultCharset();
    } catch (CoreException ex) {
        // Use global default
        return fManager.getDefaultEncoding();
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) QualifiedName(org.eclipse.core.runtime.QualifiedName) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 39 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.

the class ResourceTextFileBuffer method cacheBOM.

/**
 * Caches the BOM of the underlying file.
 *
 * @throws CoreException if reading of file's content description fails
 */
protected void cacheBOM() throws CoreException {
    fBOM = null;
    IContentDescription description = fFile.getContentDescription();
    if (description != null)
        fBOM = (byte[]) description.getProperty(IContentDescription.BYTE_ORDER_MARK);
}
Also used : IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 40 with IContentDescription

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

the class JSPContentValidator method isFragment.

/**
 * Determines if file is jsp fragment or not (does a deep, indepth check,
 * looking into contents of file)
 *
 * @param file
 *            assumes file is not null and exists
 * @return true if file is jsp fragment, false otherwise
 */
private boolean isFragment(IFile file) {
    boolean isFragment = false;
    InputStream is = null;
    try {
        IContentDescription contentDescription = file.getContentDescription();
        // it can be null
        if (contentDescription == null) {
            is = file.getContents();
            contentDescription = Platform.getContentTypeManager().getDescriptionFor(is, file.getName(), new QualifiedName[] { IContentDescription.CHARSET });
        }
        if (contentDescription != null) {
            String fileCtId = contentDescription.getContentType().getId();
            isFragment = (fileCtId != null && ContentTypeIdForJSP.ContentTypeID_JSPFRAGMENT.equals(fileCtId));
        }
    } catch (IOException e) {
    // ignore, assume it's invalid JSP
    } catch (CoreException e) {
    // ignore, assume it's invalid JSP
    } finally {
        /*
			 * must close input stream in case others need it
			 * (IFile.getContents() requirement as well)
			 */
        if (is != null)
            try {
                is.close();
            } catch (Exception e) {
            // not sure how to recover at this point
            }
    }
    return isFragment;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) QualifiedName(org.eclipse.core.runtime.QualifiedName) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException)

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