Search in sources :

Example 46 with IStorage

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;
}
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 47 with IStorage

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;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 48 with IStorage

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;
}
Also used : ILocationInEclipseResource(org.eclipse.xtext.ui.generator.trace.ILocationInEclipseResource) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) InputStream(java.io.InputStream) IOException(java.io.IOException) IStorage(org.eclipse.core.resources.IStorage) Function(com.google.common.base.Function) CoreException(org.eclipse.core.runtime.CoreException) IEclipseTrace(org.eclipse.xtext.ui.generator.trace.IEclipseTrace) WrappedRuntimeException(org.eclipse.core.internal.utils.WrappedRuntimeException)

Example 49 with IStorage

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;
}
Also used : IFile(org.eclipse.core.resources.IFile) IStorage(org.eclipse.core.resources.IStorage) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 50 with IStorage

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

Aggregations

IStorage (org.eclipse.core.resources.IStorage)96 URI (org.eclipse.emf.common.util.URI)32 IFile (org.eclipse.core.resources.IFile)31 IProject (org.eclipse.core.resources.IProject)27 CoreException (org.eclipse.core.runtime.CoreException)25 IResource (org.eclipse.core.resources.IResource)20 Pair (org.eclipse.xtext.util.Pair)16 Test (org.junit.Test)15 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 IStorageEditorInput (org.eclipse.ui.IStorageEditorInput)12 IPath (org.eclipse.core.runtime.IPath)11 IJavaProject (org.eclipse.jdt.core.IJavaProject)10 IOException (java.io.IOException)7 WrappedException (org.eclipse.emf.common.util.WrappedException)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 GitSynchronizeData (org.eclipse.egit.core.synchronize.dto.GitSynchronizeData)6 GitSynchronizeDataSet (org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet)6 Storage2UriMapperJavaImpl (org.eclipse.xtext.ui.resource.Storage2UriMapperJavaImpl)6