Search in sources :

Example 1 with ContainerCreator

use of org.eclipse.core.filebuffers.manipulation.ContainerCreator in project eclipse.platform.text by eclipse.

the class FileDocumentProvider method doSaveDocument.

@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {
    if (element instanceof IFileEditorInput) {
        IFileEditorInput input = (IFileEditorInput) element;
        String encoding = null;
        FileInfo info = (FileInfo) getElementInfo(element);
        IFile file = input.getFile();
        encoding = getCharsetForNewFile(file, document, info);
        if (info != null && info.fBOM == IContentDescription.BOM_UTF_16LE && CHARSET_UTF_16.equals(encoding))
            encoding = CHARSET_UTF_16LE;
        Charset charset;
        try {
            charset = Charset.forName(encoding);
        } catch (UnsupportedCharsetException ex) {
            String message = NLSUtility.format(TextEditorMessages.DocumentProvider_error_unsupported_encoding_message_arg, encoding);
            IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, ex);
            throw new CoreException(s);
        } catch (IllegalCharsetNameException ex) {
            String message = NLSUtility.format(TextEditorMessages.DocumentProvider_error_illegal_encoding_message_arg, encoding);
            IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, ex);
            throw new CoreException(s);
        }
        CharsetEncoder encoder = charset.newEncoder();
        encoder.onMalformedInput(CodingErrorAction.REPLACE);
        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
        InputStream stream;
        try {
            byte[] bytes;
            ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(document.get()));
            if (byteBuffer.hasArray())
                bytes = byteBuffer.array();
            else {
                bytes = new byte[byteBuffer.limit()];
                byteBuffer.get(bytes);
            }
            stream = new ByteArrayInputStream(bytes, 0, byteBuffer.limit());
        } catch (CharacterCodingException ex) {
            Assert.isTrue(ex instanceof UnmappableCharacterException);
            String message = NLSUtility.format(TextEditorMessages.DocumentProvider_error_charset_mapping_failed_message_arg, encoding);
            IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, EditorsUI.CHARSET_MAPPING_FAILED, message, null);
            throw new CoreException(s);
        }
        /*
			 * 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 (info != null && info.fBOM == IContentDescription.BOM_UTF_8 && CHARSET_UTF_8.equals(encoding))
            stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_8), stream);
        if (info != null && info.fBOM == IContentDescription.BOM_UTF_16LE && CHARSET_UTF_16LE.equals(encoding))
            stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_16LE), stream);
        if (file.exists()) {
            if (info != null && !overwrite)
                checkSynchronizationState(info.fModificationStamp, file);
            // inform about the upcoming content change
            fireElementStateChanging(element);
            try {
                file.setContents(stream, overwrite, true, monitor);
            } catch (CoreException x) {
                // inform about failure
                fireElementStateChangeFailed(element);
                throw x;
            } catch (RuntimeException x) {
                // inform about failure
                fireElementStateChangeFailed(element);
                throw x;
            }
            if (info != null) {
                ResourceMarkerAnnotationModel model = (ResourceMarkerAnnotationModel) info.fModel;
                if (model != null)
                    model.updateMarkers(info.fDocument);
                info.fModificationStamp = computeModificationStamp(file);
            }
        } else {
            SubMonitor subMonitor = SubMonitor.convert(monitor, TextEditorMessages.FileDocumentProvider_task_saving, 2);
            ContainerCreator creator = new ContainerCreator(file.getWorkspace(), file.getParent().getFullPath());
            creator.createContainer(subMonitor.split(1));
            file.create(stream, false, subMonitor.split(1));
        }
    } else {
        super.doSaveDocument(monitor, element, document, overwrite);
    }
}
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) ResourceMarkerAnnotationModel(org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel) IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) SubMonitor(org.eclipse.core.runtime.SubMonitor) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CoreException(org.eclipse.core.runtime.CoreException) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IFileEditorInput(org.eclipse.ui.IFileEditorInput) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) ContainerCreator(org.eclipse.core.filebuffers.manipulation.ContainerCreator)

Example 2 with ContainerCreator

use of org.eclipse.core.filebuffers.manipulation.ContainerCreator in project eclipse.platform.text by eclipse.

the class ResourceHelper method createFolder.

public static IFolder createFolder(String portableFolderPath) throws CoreException {
    ContainerCreator creator = new ContainerCreator(ResourcesPlugin.getWorkspace(), new Path(portableFolderPath));
    IContainer container = creator.createContainer(NULL_MONITOR);
    if (container instanceof IFolder)
        return (IFolder) container;
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IContainer(org.eclipse.core.resources.IContainer) ContainerCreator(org.eclipse.core.filebuffers.manipulation.ContainerCreator) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with ContainerCreator

use of org.eclipse.core.filebuffers.manipulation.ContainerCreator in project eclipse.platform.text by eclipse.

the class ResourceTextFileBuffer method commitFileBufferContent.

@Override
protected void commitFileBufferContent(IProgressMonitor monitor, boolean overwrite) throws CoreException {
    if (!isSynchronized() && !overwrite) {
        String message = NLSUtility.format(FileBuffersMessages.FileBuffer_error_outOfSync, getFileStore().toURI());
        throw new CoreException(new Status(IStatus.WARNING, FileBuffersPlugin.PLUGIN_ID, IResourceStatus.OUT_OF_SYNC_LOCAL, message, null));
    }
    String encoding = computeEncoding();
    if (fBOM == IContentDescription.BOM_UTF_16LE && CHARSET_UTF_16.equals(encoding))
        encoding = CHARSET_UTF_16LE;
    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);
    InputStream stream;
    try {
        byte[] bytes;
        ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(fDocument.get()));
        if (byteBuffer.hasArray())
            bytes = byteBuffer.array();
        else {
            bytes = new byte[byteBuffer.limit()];
            byteBuffer.get(bytes);
        }
        stream = new ByteArrayInputStream(bytes, 0, byteBuffer.limit());
    } 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, ex);
        throw new CoreException(s);
    }
    /*
		 * 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 (fBOM == IContentDescription.BOM_UTF_8 && CHARSET_UTF_8.equals(encoding))
        stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_8), stream);
    if (fBOM == IContentDescription.BOM_UTF_16LE && CHARSET_UTF_16LE.equals(encoding))
        stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_16LE), stream);
    if (fFile.exists()) {
        // 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
        fFile.setContents(stream, overwrite, true, monitor);
        if (fDocument instanceof IDocumentExtension4) {
            fSynchronizationStamp = ((IDocumentExtension4) fDocument).getModificationStamp();
            fFile.revertModificationStamp(fSynchronizationStamp);
        } else
            fSynchronizationStamp = fFile.getModificationStamp();
        if (fAnnotationModel instanceof IPersistableAnnotationModel) {
            IPersistableAnnotationModel persistableModel = (IPersistableAnnotationModel) fAnnotationModel;
            persistableModel.commit(fDocument);
        }
    } else {
        SubMonitor subMonitor = SubMonitor.convert(monitor, FileBuffersMessages.ResourceTextFileBuffer_task_saving, 2);
        ContainerCreator creator = new ContainerCreator(fFile.getWorkspace(), fFile.getParent().getFullPath());
        creator.createContainer(subMonitor.split(1));
        fFile.create(stream, false, subMonitor.split(1));
        // set synchronization stamp to know whether the file synchronizer must become active
        fSynchronizationStamp = fFile.getModificationStamp();
        subMonitor.split(1);
    // TODO commit persistable annotation model
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IResourceStatus(org.eclipse.core.resources.IResourceStatus) IPersistableAnnotationModel(org.eclipse.core.filebuffers.IPersistableAnnotationModel) IStatus(org.eclipse.core.runtime.IStatus) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) SubMonitor(org.eclipse.core.runtime.SubMonitor) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CoreException(org.eclipse.core.runtime.CoreException) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) ContainerCreator(org.eclipse.core.filebuffers.manipulation.ContainerCreator)

Aggregations

ContainerCreator (org.eclipse.core.filebuffers.manipulation.ContainerCreator)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 SequenceInputStream (java.io.SequenceInputStream)2 ByteBuffer (java.nio.ByteBuffer)2 CharacterCodingException (java.nio.charset.CharacterCodingException)2 Charset (java.nio.charset.Charset)2 CharsetEncoder (java.nio.charset.CharsetEncoder)2 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 IResourceStatus (org.eclipse.core.resources.IResourceStatus)2 CoreException (org.eclipse.core.runtime.CoreException)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 IPersistableAnnotationModel (org.eclipse.core.filebuffers.IPersistableAnnotationModel)1 IContainer (org.eclipse.core.resources.IContainer)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1