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);
}
}
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations