Search in sources :

Example 1 with IEncodedStorage

use of org.eclipse.core.resources.IEncodedStorage in project eclipse.platform.text by eclipse.

the class LastSaveReferenceProvider method readDocument.

/**
 * Reads in the saved document into <code>fReference</code>.
 *
 * @param monitor a progress monitor, or <code>null</code>
 * @param force <code>true</code> if the reference document should also
 *        be read if the current document is <code>null</code>,<code>false</code>
 *        if it should only be updated if it already existed.
 */
private void readDocument(IProgressMonitor monitor, boolean force) {
    // protect against concurrent disposal
    IDocumentProvider prov = fDocumentProvider;
    IEditorInput inp = fEditorInput;
    IDocument doc = fReference;
    ITextEditor editor = fEditor;
    if (prov instanceof IStorageDocumentProvider && inp instanceof IStorageEditorInput) {
        IStorageEditorInput input = (IStorageEditorInput) inp;
        IStorageDocumentProvider provider = (IStorageDocumentProvider) prov;
        if (doc == null)
            if (force || fDocumentRead)
                doc = new Document();
            else
                return;
        IJobManager jobMgr = Job.getJobManager();
        try {
            IStorage storage = input.getStorage();
            // check for null for backward compatibility (we used to check before...)
            if (storage == null)
                return;
            fProgressMonitor = monitor;
            ISchedulingRule rule = getSchedulingRule(storage);
            // delay for any other job requiring the lock on file
            try {
                lockDocument(monitor, jobMgr, rule);
                String encoding;
                if (storage instanceof IEncodedStorage)
                    encoding = ((IEncodedStorage) storage).getCharset();
                else
                    encoding = null;
                boolean skipUTF8BOM = isUTF8BOM(encoding, storage);
                setDocumentContent(doc, storage, encoding, monitor, skipUTF8BOM);
            } finally {
                unlockDocument(jobMgr, rule);
                fProgressMonitor = null;
            }
        } catch (CoreException e) {
            return;
        }
        if (monitor != null && monitor.isCanceled())
            return;
        // update state
        synchronized (fLock) {
            if (fDocumentProvider == provider && fEditorInput == input) {
                // only update state if our provider / input pair has not
                // been updated in between (dispose or setActiveEditor)
                fReference = doc;
                fDocumentRead = true;
                addElementStateListener(editor, prov);
            }
        }
    }
}
Also used : IStorageDocumentProvider(org.eclipse.ui.editors.text.IStorageDocumentProvider) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IEncodedStorage(org.eclipse.core.resources.IEncodedStorage) IJobManager(org.eclipse.core.runtime.jobs.IJobManager) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IStorage(org.eclipse.core.resources.IStorage) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with IEncodedStorage

use of org.eclipse.core.resources.IEncodedStorage in project linuxtools by eclipse.

the class LineComparator method getEncoding.

private static String getEncoding(IStorage storage, String outputEncoding) throws CoreException {
    if (storage instanceof IEncodedStorage) {
        IEncodedStorage es = (IEncodedStorage) storage;
        String charset = es.getCharset();
        if (charset != null)
            return charset;
    }
    return outputEncoding;
}
Also used : IEncodedStorage(org.eclipse.core.resources.IEncodedStorage)

Example 3 with IEncodedStorage

use of org.eclipse.core.resources.IEncodedStorage in project webtools.sourceediting by eclipse.

the class StorageModelProvider method getPersistedEncoding.

/* (non-Javadoc)
	 * @see org.eclipse.ui.editors.text.StorageDocumentProvider#getPersistedEncoding(java.lang.Object)
	 */
protected String getPersistedEncoding(Object element) {
    String charset = super.getPersistedEncoding(element);
    if (charset == null && element instanceof IStorageEditorInput) {
        IStorage storage;
        try {
            storage = ((IStorageEditorInput) element).getStorage();
            if (storage != null && !(storage instanceof IEncodedStorage)) {
                InputStream contents = null;
                try {
                    contents = storage.getContents();
                    if (contents != null) {
                        QualifiedName[] detectionOptions = new QualifiedName[] { IContentDescription.BYTE_ORDER_MARK, IContentDescription.CHARSET };
                        IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(contents, storage.getName(), detectionOptions);
                        if (description != null) {
                            charset = description.getCharset();
                        }
                    }
                } catch (IOException e) {
                } finally {
                    if (contents != null)
                        try {
                            contents.close();
                        } catch (IOException e) {
                        // do nothing
                        }
                }
            }
        } catch (CoreException e) {
            Logger.logException(e);
        }
    }
    return charset;
}
Also used : IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) CoreException(org.eclipse.core.runtime.CoreException) IEncodedStorage(org.eclipse.core.resources.IEncodedStorage) InputStream(java.io.InputStream) QualifiedName(org.eclipse.core.runtime.QualifiedName) IOException(java.io.IOException) IStorage(org.eclipse.core.resources.IStorage) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 4 with IEncodedStorage

use of org.eclipse.core.resources.IEncodedStorage in project xtext-eclipse by eclipse.

the class StorageAwareTrace method getContentsAsText.

protected Reader getContentsAsText(IStorage storage) throws WrappedCoreException {
    if (storage instanceof IEncodedStorage) {
        try {
            IEncodedStorage enc = (IEncodedStorage) storage;
            Charset charset = Charset.forName(enc.getCharset());
            InputStream contents = storage.getContents();
            return new InputStreamReader(contents, charset);
        } catch (CoreException e) {
            throw new WrappedCoreException(e);
        }
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) CoreException(org.eclipse.core.runtime.CoreException) IEncodedStorage(org.eclipse.core.resources.IEncodedStorage) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset)

Example 5 with IEncodedStorage

use of org.eclipse.core.resources.IEncodedStorage in project xtext-eclipse by eclipse.

the class WorkspaceEncodingProvider method getEncoding.

@Override
public String getEncoding(URI uri) {
    if (workspace != null) {
        if (uri != null) {
            if (uri.isPlatformResource()) {
                IFile file = workspace.getRoot().getFile(new Path(uri.toPlatformString(true)));
                try {
                    return file.getCharset(true);
                } catch (CoreException e) {
                    LOG.error("Error getting file encoding for " + uri, e);
                }
            } else {
                Iterator<Pair<IStorage, IProject>> storages = storage2UriMapper.getStorages(uri).iterator();
                if (storages.hasNext()) {
                    Pair<IStorage, IProject> next = storages.next();
                    IStorage storage = next.getFirst();
                    if (storage instanceof IEncodedStorage) {
                        try {
                            return ((IEncodedStorage) storage).getCharset();
                        } catch (CoreException e) {
                            LOG.error("Error getting file encoding for " + uri, e);
                        }
                    } else {
                        try {
                            String result = next.getSecond().getDefaultCharset(true);
                            return result;
                        } catch (CoreException e) {
                            LOG.error("Error getting file encoding for " + uri, e);
                        }
                    }
                }
            }
        }
        try {
            return workspace.getRoot().getDefaultCharset();
        } catch (CoreException e) {
            LOG.error("Error getting file encoding for " + uri, e);
        }
    }
    // fallback to runtime encoding provider
    return runtimeEncodingProvider.getEncoding(uri);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IEncodedStorage(org.eclipse.core.resources.IEncodedStorage) IStorage(org.eclipse.core.resources.IStorage) IProject(org.eclipse.core.resources.IProject) Pair(org.eclipse.xtext.util.Pair)

Aggregations

IEncodedStorage (org.eclipse.core.resources.IEncodedStorage)6 CoreException (org.eclipse.core.runtime.CoreException)5 IStorage (org.eclipse.core.resources.IStorage)4 IStorageEditorInput (org.eclipse.ui.IStorageEditorInput)3 InputStream (java.io.InputStream)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Charset (java.nio.charset.Charset)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 Path (org.eclipse.core.runtime.Path)1 QualifiedName (org.eclipse.core.runtime.QualifiedName)1 IContentDescription (org.eclipse.core.runtime.content.IContentDescription)1 IJobManager (org.eclipse.core.runtime.jobs.IJobManager)1 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)1 URI (org.eclipse.emf.common.util.URI)1 WrappedException (org.eclipse.emf.common.util.WrappedException)1 Document (org.eclipse.jface.text.Document)1 IDocument (org.eclipse.jface.text.IDocument)1 IEditorInput (org.eclipse.ui.IEditorInput)1