Search in sources :

Example 1 with IFileInfo

use of org.eclipse.core.filesystem.IFileInfo in project che by eclipse.

the class FileStoreTextFileBuffer method revert.

/*
	 * @see org.eclipse.core.filebuffers.IFileBuffer#revert(org.eclipse.core.runtime.IProgressMonitor)
	 */
public void revert(IProgressMonitor monitor) throws CoreException {
    if (isDisconnected())
        return;
    IDocument original = null;
    fStatus = null;
    try {
        original = fManager.createEmptyDocument(getLocationOrName(), LocationKind.LOCATION);
        cacheEncodingState();
        setDocumentContent(original, fFileStore, fEncoding, fHasBOM, monitor);
    } catch (CoreException x) {
        fStatus = x.getStatus();
    }
    if (original == null)
        return;
    String originalContents = original.get();
    boolean replaceContents = !originalContents.equals(fDocument.get());
    if (!replaceContents && !fCanBeSaved)
        return;
    fManager.fireStateChanging(this);
    try {
        if (replaceContents) {
            fManager.fireBufferContentAboutToBeReplaced(this);
            fDocument.set(original.get());
        }
        boolean fireDirtyStateChanged = fCanBeSaved;
        if (fCanBeSaved) {
            fCanBeSaved = false;
            addFileBufferContentListeners();
        }
        if (replaceContents)
            fManager.fireBufferContentReplaced(this);
        IFileInfo info = fFileStore.fetchInfo();
        if (info.exists())
            fSynchronizationStamp = fFileStore.fetchInfo().getLastModified();
        if (fireDirtyStateChanged)
            fManager.fireDirtyStateChanged(this, fCanBeSaved);
    } catch (RuntimeException x) {
        fManager.fireStateChangeFailed(this);
        throw x;
    }
}
Also used : IFileInfo(org.eclipse.core.filesystem.IFileInfo) CoreException(org.eclipse.core.runtime.CoreException) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with IFileInfo

use of org.eclipse.core.filesystem.IFileInfo in project che by eclipse.

the class FileStoreTextFileBuffer method commitFileBufferContent.

/*
	 * @see org.eclipse.core.internal.filebuffers.FileBuffer#commitFileBufferContent(org.eclipse.core.runtime.IProgressMonitor, boolean)
	 */
protected void commitFileBufferContent(IProgressMonitor monitor, boolean overwrite) throws CoreException {
    //		if (!isSynchronized() && !overwrite)
    //			throw new CoreException(new Status(IStatus.WARNING, FileBuffersPlugin.PLUGIN_ID, IResourceStatus.OUT_OF_SYNC_LOCAL, FileBuffersMessages.FileBuffer_error_outOfSync, null));
    String encoding = computeEncoding();
    Charset charset;
    try {
        charset = Charset.forName(encoding);
    } catch (UnsupportedCharsetException ex) {
        String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_unsupported_encoding_message_arg, encoding);
        IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, message, ex);
        throw new CoreException(s);
    } catch (IllegalCharsetNameException ex) {
        String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_illegal_encoding_message_arg, encoding);
        IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, message, ex);
        throw new CoreException(s);
    }
    CharsetEncoder encoder = charset.newEncoder();
    encoder.onMalformedInput(CodingErrorAction.REPLACE);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    byte[] bytes;
    int bytesLength;
    try {
        ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(fDocument.get()));
        bytesLength = byteBuffer.limit();
        if (byteBuffer.hasArray())
            bytes = byteBuffer.array();
        else {
            bytes = new byte[bytesLength];
            byteBuffer.get(bytes);
        }
    } catch (CharacterCodingException ex) {
        Assert.isTrue(ex instanceof UnmappableCharacterException);
        String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_charset_mapping_failed_message_arg, encoding);
        IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CHARSET_MAPPING_FAILED, message, null);
        throw new CoreException(s);
    }
    IFileInfo fileInfo = fFileStore.fetchInfo();
    if (fileInfo != null && fileInfo.exists()) {
        if (!overwrite)
            checkSynchronizationState();
        InputStream stream = new ByteArrayInputStream(bytes, 0, bytesLength);
        /*
			 * XXX:
			 * This is a workaround for a corresponding bug in Java readers and writer,
			 * see http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
			 */
        if (fHasBOM && CHARSET_UTF_8.equals(encoding))
            stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_8), stream);
        // here the file synchronizer should actually be removed and afterwards added again. However,
        // we are already inside an operation, so the delta is sent AFTER we have added the listener
        setFileContents(stream, monitor);
        // set synchronization stamp to know whether the file synchronizer must become active
        fSynchronizationStamp = fFileStore.fetchInfo().getLastModified();
    //			if (fAnnotationModel instanceof IPersistableAnnotationModel) {
    //				IPersistableAnnotationModel persistableModel= (IPersistableAnnotationModel) fAnnotationModel;
    //				persistableModel.commit(fDocument);
    //			}
    } else {
        fFileStore.getParent().mkdir(EFS.NONE, null);
        OutputStream out = fFileStore.openOutputStream(EFS.NONE, null);
        try {
            /*
				 * XXX:
				 * This is a workaround for a corresponding bug in Java readers and writer,
				 * see http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
				 */
            if (fHasBOM && CHARSET_UTF_8.equals(encoding))
                out.write(IContentDescription.BOM_UTF_8);
            out.write(bytes, 0, bytesLength);
            out.flush();
            out.close();
        } catch (IOException x) {
            IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, x.getLocalizedMessage(), x);
            throw new CoreException(s);
        } finally {
            try {
                out.close();
            } catch (IOException x) {
            }
        }
        // set synchronization stamp to know whether the file synchronizer must become active
        fSynchronizationStamp = fFileStore.fetchInfo().getLastModified();
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) IFileInfo(org.eclipse.core.filesystem.IFileInfo) CoreException(org.eclipse.core.runtime.CoreException) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException)

Example 3 with IFileInfo

use of org.eclipse.core.filesystem.IFileInfo in project che by eclipse.

the class RefactoringHistoryManager method removeIndexTree.

/**
	 * Removes the refactoring history index tree spanned by the specified file
	 * store.
	 *
	 * @param store
	 *            the file store spanning the history index tree
	 * @param monitor
	 *            the progress monitor to use
	 * @param task
	 *            the task label to use
	 * @throws CoreException
	 *             if an error occurs while removing the index tree
	 */
private static void removeIndexTree(final IFileStore store, final IProgressMonitor monitor, final String task) throws CoreException {
    try {
        monitor.beginTask(task, 16);
        final IFileInfo info = store.fetchInfo(EFS.NONE, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        if (info.isDirectory()) {
            if (info.getName().equalsIgnoreCase(RefactoringHistoryService.NAME_HISTORY_FOLDER))
                return;
            final IFileStore[] stores = store.childStores(EFS.NONE, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
            final IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL);
            try {
                subMonitor.beginTask(RefactoringCoreMessages.RefactoringHistoryService_updating_history, stores.length);
                for (int index = 0; index < stores.length; index++) {
                    final IFileInfo current = stores[index].fetchInfo(EFS.NONE, new SubProgressMonitor(subMonitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
                    if (current.isDirectory()) {
                        final char[] characters = stores[index].getName().toCharArray();
                        for (int offset = 0; offset < characters.length; offset++) {
                            if (Character.isDigit(characters[offset]))
                                return;
                            else
                                continue;
                        }
                    }
                }
            } finally {
                subMonitor.done();
            }
        }
        final IFileStore parent = store.getParent();
        store.delete(0, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        removeIndexTree(parent, new SubProgressMonitor(monitor, 12, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL), task);
    } finally {
        monitor.done();
    }
}
Also used : IFileInfo(org.eclipse.core.filesystem.IFileInfo) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFileStore(org.eclipse.core.filesystem.IFileStore) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 4 with IFileInfo

use of org.eclipse.core.filesystem.IFileInfo in project KaiZen-OpenAPI-Editor by RepreZen.

the class DocumentUtils method getExternalFile.

public static IFileStore getExternalFile(IPath path) {
    IFileStore fileStore = EFS.getLocalFileSystem().getStore(path);
    IFileInfo fileInfo = fileStore.fetchInfo();
    return fileInfo != null && fileInfo.exists() ? fileStore : null;
}
Also used : IFileInfo(org.eclipse.core.filesystem.IFileInfo) IFileStore(org.eclipse.core.filesystem.IFileStore)

Example 5 with IFileInfo

use of org.eclipse.core.filesystem.IFileInfo in project eclipse.platform.text by eclipse.

the class FileBufferFunctions method test5.

/*
	 * Tests getModificationStamp.
	 */
@Test
public void test5() throws Exception {
    fManager.connect(fPath, LocationKind.NORMALIZE, null);
    try {
        ITextFileBuffer fileBuffer = fManager.getTextFileBuffer(fPath, LocationKind.NORMALIZE);
        long modificationStamp = fileBuffer.getModificationStamp();
        IFileStore fileStore = FileBuffers.getFileStoreAtLocation(fPath);
        IFileInfo fileInfo = fileStore.fetchInfo();
        assertEquals(modificationStamp != IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP, fileInfo.exists());
        fileInfo.setLastModified(1000);
        if (fileInfo.exists())
            fileStore.putInfo(fileInfo, EFS.SET_LAST_MODIFIED, null);
        long lastModified = fileStore.fetchInfo().getLastModified();
        assertTrue(lastModified == EFS.NONE || modificationStamp != fileBuffer.getModificationStamp());
    } finally {
        fManager.disconnect(fPath, LocationKind.NORMALIZE, null);
    }
}
Also used : IFileInfo(org.eclipse.core.filesystem.IFileInfo) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IFileStore(org.eclipse.core.filesystem.IFileStore) Test(org.junit.Test)

Aggregations

IFileInfo (org.eclipse.core.filesystem.IFileInfo)48 IFileStore (org.eclipse.core.filesystem.IFileStore)32 CoreException (org.eclipse.core.runtime.CoreException)22 IOException (java.io.IOException)13 URI (java.net.URI)12 IPath (org.eclipse.core.runtime.IPath)11 IFile (org.eclipse.core.resources.IFile)10 Test (org.junit.Test)8 URISyntaxException (java.net.URISyntaxException)7 OutputStream (java.io.OutputStream)6 IRemoteFileProxy (org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)5 IStatus (org.eclipse.core.runtime.IStatus)5 IDocument (org.eclipse.jface.text.IDocument)5 InputStream (java.io.InputStream)4 Path (org.eclipse.core.runtime.Path)4 Status (org.eclipse.core.runtime.Status)4 Reader (java.io.Reader)3