blob: 2e612bd779acabb66875256f8d9de30455203636 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2006, 2021 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.ecommons.io.internal;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.statet.ecommons.io.FileUtil;
/**
* Impl of FileUtil for Eclipse workspace files.
*/
public class WorkspaceUtilImpl extends FileUtil {
private static final InputStream EMPTY_INPUT = new ByteArrayInputStream(new byte[0]);
private static final String LABEL_2_WORKSPACE = "' ("+Messages.FileType_Workspace_name+")"; //$NON-NLS-1$ //$NON-NLS-2$
private final IFile fFile;
public WorkspaceUtilImpl(final IFile file) {
fFile = file;
}
@Override
public String getLabel() {
return "'"+fFile.getFullPath().makeAbsolute().toString()+LABEL_2_WORKSPACE; //$NON-NLS-1$
}
@Override
public URI getURI() {
return fFile.getLocationURI();
}
@Override
public long getTimeStamp(final IProgressMonitor monitor) throws CoreException {
final long stamp = fFile.getLocalTimeStamp();
return stamp;
}
@Override
public ReadTextFileOperation createReadTextFileOp(final ReaderAction action) {
return new ReadTextFileOperation() {
@Override
protected FileInput getInput(final IProgressMonitor monitor) throws CoreException, IOException {
try {
final InputStream raw = fFile.getContents(true);
return new FileInput(raw, fFile.getCharset(true));
}
finally {
monitor.done();
}
}
@Override
protected ReaderAction getAction() {
return action;
}
@Override
public void doOperation(final IProgressMonitor monitor) throws CoreException, OperationCanceledException {
runAsWorkspaceRunnable(monitor, null);
}
};
}
@Override
public WriteTextFileOperation createWriteTextFileOp(final String content) {
return new WriteTextFileOperation() {
@Override
public void doOperation(final IProgressMonitor monitor) throws CoreException, OperationCanceledException {
final ISchedulingRule rule = (fFile.exists()) ?
fFile.getWorkspace().getRuleFactory().modifyRule(fFile) :
fFile.getWorkspace().getRuleFactory().createRule(fFile);
runAsWorkspaceRunnable(monitor, rule);
}
@Override
protected void writeImpl(final IProgressMonitor monitor) throws CoreException, UnsupportedEncodingException {
final boolean exists = fFile.exists();
if (exists && ((fMode & EFS.APPEND) != 0)) {
if (fForceCharset) {
fFile.setCharset(fCharset, new SubProgressMonitor(monitor, 20));
}
else {
fCharset = fFile.getCharset(true);
monitor.worked(20);
}
fFile.appendContents(new ByteArrayInputStream(content.getBytes(fCharset)),
(IResource.FORCE | IResource.KEEP_HISTORY),
new SubProgressMonitor(monitor, 80));
}
else {
if (exists && ((fMode & EFS.OVERWRITE) != 0)) {
fFile.setContents(EMPTY_INPUT, IResource.FORCE | IResource.KEEP_HISTORY,
new SubProgressMonitor(monitor, 15));
}
else {
fFile.create(EMPTY_INPUT, IResource.FORCE, new SubProgressMonitor(monitor, 15));
}
if (fForceCharset || !fCharset.equals(fFile.getCharset(true))) {
fFile.setCharset(fCharset, new SubProgressMonitor(monitor, 5));
} else {
monitor.worked(5);
}
fFile.setContents(new ByteArrayInputStream(content.getBytes(fCharset)),
IResource.NONE, new SubProgressMonitor(monitor, 80));
}
}
};
}
}