use of org.eclipse.core.resources.IStorage 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.IStorage in project xtext-xtend by eclipse.
the class XtendUIValidator method getExpectedPackageName.
protected String getExpectedPackageName(XtendFile xtendFile) {
URI fileURI = xtendFile.eResource().getURI();
for (Pair<IStorage, IProject> storage : storage2UriMapper.getStorages(fileURI)) {
if (storage.getFirst() instanceof IFile) {
IPath fileWorkspacePath = storage.getFirst().getFullPath();
IJavaProject javaProject = JavaCore.create(storage.getSecond());
if (javaProject != null && javaProject.exists() && javaProject.isOpen()) {
try {
for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
if (!root.isArchive() && !root.isExternal()) {
IResource resource = root.getResource();
if (resource != null) {
IPath sourceFolderPath = resource.getFullPath();
if (sourceFolderPath.isPrefixOf(fileWorkspacePath)) {
IPath claspathRelativePath = fileWorkspacePath.makeRelativeTo(sourceFolderPath);
return claspathRelativePath.removeLastSegments(1).toString().replace("/", ".");
}
}
}
}
} catch (JavaModelException e) {
LOG.error("Error resolving expected path for XtendFile", e);
}
}
}
}
return null;
}
use of org.eclipse.core.resources.IStorage in project xtext-xtend by eclipse.
the class DerivedSourceView method computeInput.
@Override
protected String computeInput(IWorkbenchPartSelection workbenchPartSelection) {
openEditorAction.setInputFile(null);
openEditorAction.setSelectedRegion(null);
IEclipseTrace trace = traceInformation.getTraceToTarget(getEditorResource(workbenchPartSelection));
if (trace != null) {
if (workbenchPartSelection instanceof DerivedSourceSelection) {
DerivedSourceSelection derivedSourceSelection = (DerivedSourceSelection) workbenchPartSelection;
selectedSource = derivedSourceSelection.getStorage();
} else {
derivedSources = Sets.newHashSet();
TextRegion localRegion = mapTextRegion(workbenchPartSelection);
Iterable<IStorage> transform = Iterables.filter(transform(trace.getAllAssociatedLocations(localRegion), new Function<ILocationInEclipseResource, IStorage>() {
@Override
public IStorage apply(ILocationInEclipseResource input) {
return input.getPlatformResource();
}
}), Predicates.notNull());
addAll(derivedSources, transform);
ILocationInEclipseResource bestAssociatedLocation = trace.getBestAssociatedLocation(localRegion);
if (bestAssociatedLocation != null) {
selectedSource = bestAssociatedLocation.getPlatformResource();
} else if (!derivedSources.isEmpty()) {
selectedSource = derivedSources.iterator().next();
}
}
}
IFile file = getSelectedFile();
if (file != null) {
try {
file.refreshLocal(1, new NullProgressMonitor());
if (file.exists()) {
openEditorAction.setInputFile(file);
try (InputStream contents = file.getContents()) {
return Files.readStreamIntoString(contents);
}
}
} catch (CoreException | IOException e) {
throw new WrappedRuntimeException(e);
}
}
return null;
}
use of org.eclipse.core.resources.IStorage in project applause by applause.
the class ApplauseEclipseResourceFileSystemAccess2 method getTraceFile.
/**
* @since 2.3
*/
@Nullable
protected IFile getTraceFile(IFile file) {
IStorage traceFile = fileBasedTraceInformation.getTraceFile(file);
if (traceFile instanceof IFile) {
IFile result = (IFile) traceFile;
syncIfNecessary(result);
return result;
}
return null;
}
use of org.eclipse.core.resources.IStorage 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);
}
Aggregations