Merge pull request #12 from kthoms/bug443574-fsa-exists

[bug 443574] Added interface IFileSystemAccessExtension4
This commit is contained in:
Sebastian Zarnekow 2015-04-20 08:29:42 +02:00
commit 794ac2268e
4 changed files with 73 additions and 10 deletions

View file

@ -7,18 +7,20 @@
*******************************************************************************/
package org.eclipse.xtext.generator;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.util.RuntimeIOException;
/**
*
* Abstract base class for file system access supporting {@link IFileSystemAccessExtension3}.
*
*
* Abstract base class for file system access supporting {@link IFileSystemAccessExtension3} and {@link IFileSystemAccessExtension4}.
*
* @author Sven Efftinge - Initial contribution and API
* @since 2.4
*/
public abstract class AbstractFileSystemAccess2 extends AbstractFileSystemAccess implements IFileSystemAccessExtension3 {
public abstract class AbstractFileSystemAccess2 extends AbstractFileSystemAccess implements IFileSystemAccessExtension3, IFileSystemAccessExtension4 {
/**
* @since 2.4
@ -44,17 +46,39 @@ public abstract class AbstractFileSystemAccess2 extends AbstractFileSystemAccess
return readTextFile(fileName, DEFAULT_OUTPUT);
}
/**
* Sets the context to further configure this file system access instance.
*
* @param context - a context from which project configuration can be obtained. Supported context types
* depend on the concrete implementation, but {@link Resource} is usually a good fit.
*
*
* @param context - a context from which project configuration can be obtained. Supported context types
* depend on the concrete implementation, but {@link Resource} is usually a good fit.
*
* @since 2.8
*/
public void setContext(Object context) {
// do nothing
}
/**
* {@inheritDoc}
* @since 2.9
*/
@Override
public boolean isFile(String path, String outputConfigurationName) throws RuntimeIOException {
InputStream is = null;
try {
is = readBinaryFile(path, outputConfigurationName);
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
return is!=null; // no exception => file exists
} catch (RuntimeIOException e) {
return false;
}
}
}

View file

@ -0,0 +1,28 @@
/*******************************************************************************
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.generator;
import org.eclipse.xtext.util.RuntimeIOException;
/**
* This interface extends {@link IFileSystemAccess} with the capability to check files for existence.
*
* @author Karsten Thoms - Initial contribution and API
* @since 2.9
*/
public interface IFileSystemAccessExtension4 {
/**
* Tests whether the file exists at the location denoted by the output configuration.
* @param path using '/' as path separator
* @param outputConfigurationName the name of the output configuration
* @return <code>true</code> when the file at the given path exists and is a normal file. Will return <code>false</code> when
* the path belongs to a directory.
*/
boolean isFile (String path, String outputConfigurationName) throws RuntimeIOException;
}

View file

@ -235,4 +235,13 @@ public class JavaIoFileSystemAccess extends AbstractFileSystemAccess2 {
throw new RuntimeIOException(e);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean isFile(String path, String outputConfigurationName) throws RuntimeIOException {
File file = getFile(path, outputConfigurationName);
return file!=null && file.exists() && file.isFile();
}
}

View file

@ -55,6 +55,8 @@ public class JavaIoFileSystemAccessTest extends Assert {
binFile = new File(dir, "Y");
assertTrue(binFile.exists());
assertFalse(fileSystemAccess.isFile("tmp", IFileSystemAccess.DEFAULT_OUTPUT)); // isFile evaluates to false for directories
assertTrue(fileSystemAccess.isFile("tmp/Y", IFileSystemAccess.DEFAULT_OUTPUT));
assertTrue(binFile.isFile());
InputStream stream = fileSystemAccess.readBinaryFile("tmp/Y");
try {