Skip to content

Commit

Permalink
Datasource: add mainExtension, existsStrict and use it in exists() me…
Browse files Browse the repository at this point in the history
…thods of importers

This allows to import the correct network when a path is provided, the importers will
respect the full path and not declare that they can import from the datasource
TODO explain more
  • Loading branch information
jonenst committed Jun 25, 2024
1 parent 6e18e4f commit d9f151b
Show file tree
Hide file tree
Showing 21 changed files with 135 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ public boolean exists(String suffix, String ext) throws IOException {
return ds.exists(suffix, ext) && filter.test(DataSourceUtil.getFileName(getBaseName(), suffix, ext));
}

@Override
public boolean existsStrict(String suffix, String ext) throws IOException {
return ds.existsStrict(suffix, ext) && filter.test(DataSourceUtil.getFileName(getBaseName(), suffix, ext));
}

@Override
public boolean exists(String fileName) throws IOException {
return ds.exists(fileName) && filter.test(fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public ReadOnlyDataSource dataSource() {
}

public boolean exists() {
// The user has requested a basename but we need unrestricted access to all files, abort
if (!dataSource().getBaseName().isEmpty()) {
return false;
}
// check that RDF and CIM16 are defined as namespaces in the data source
Set<String> foundNamespaces = namespaces();
if (!foundNamespaces.contains(RDF_NAMESPACE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
public class Bzip2FileDataSource extends FileDataSource {

public Bzip2FileDataSource(Path directory, String baseName, String mainExtension, DataSourceObserver observer) {
super(directory, baseName, mainExtension, observer);
}

public Bzip2FileDataSource(Path directory, String baseName, DataSourceObserver observer) {
super(directory, baseName, observer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,34 @@ static String getBaseName(String fileName) {
return pos == -1 ? fileName : fileName.substring(0, pos);
}

static String getMainExtension(String fileName) {
Objects.requireNonNull(fileName);
int pos = fileName.indexOf('.'); // find first dot in case of double extension (.xml.gz)
return pos == -1 ? fileName : fileName.substring(pos+1);
}

static DataSource createDataSource(Path directory, String basename, CompressionFormat compressionExtension, DataSourceObserver observer) {
return createDataSource(directory, basename, "", compressionExtension, observer);
}

static DataSource createDataSource(Path directory, String basename, String mainExtension, CompressionFormat compressionExtension, DataSourceObserver observer) {
Objects.requireNonNull(directory);
Objects.requireNonNull(basename);

if (compressionExtension == null) {
return new FileDataSource(directory, basename, observer);
return new FileDataSource(directory, basename, mainExtension, observer);
} else {
switch (compressionExtension) {
case BZIP2:
return new Bzip2FileDataSource(directory, basename, observer);
return new Bzip2FileDataSource(directory, basename, mainExtension, observer);
case GZIP:
return new GzFileDataSource(directory, basename, observer);
return new GzFileDataSource(directory, basename, mainExtension, observer);
case XZ:
return new XZFileDataSource(directory, basename, observer);
return new XZFileDataSource(directory, basename, mainExtension, observer);
case ZIP:
return new ZipFileDataSource(directory, basename, observer);
return new ZipFileDataSource(directory, basename, mainExtension, observer);
case ZSTD:
return new ZstdFileDataSource(directory, basename, observer);
return new ZstdFileDataSource(directory, basename, mainExtension, observer);
default:
throw new IllegalStateException("Unexpected CompressionFormat value: " + compressionExtension);
}
Expand All @@ -68,17 +78,23 @@ static DataSource createDataSource(Path directory, String fileNameOrBaseName, Da
Objects.requireNonNull(fileNameOrBaseName);

if (fileNameOrBaseName.endsWith(".zst")) {
return new ZstdFileDataSource(directory, getBaseName(fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 4)), observer);
String filenameWithoutCompressionExtension = fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 4);
return new ZstdFileDataSource(directory, getBaseName(filenameWithoutCompressionExtension), getMainExtension(filenameWithoutCompressionExtension), observer);
} else if (fileNameOrBaseName.endsWith(".zip")) {
return new ZipFileDataSource(directory, fileNameOrBaseName, getBaseName(fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 4)), observer);
String filenameWithoutCompressionExtension = fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 4);
return new ZipFileDataSource(directory, getBaseName(filenameWithoutCompressionExtension), getMainExtension(filenameWithoutCompressionExtension), observer);
} else if (fileNameOrBaseName.endsWith(".xz")) {
return new XZFileDataSource(directory, getBaseName(fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 3)), observer);
String filenameWithoutCompressionExtension = fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 3);
return new XZFileDataSource(directory, getBaseName(filenameWithoutCompressionExtension), getMainExtension(filenameWithoutCompressionExtension), observer);
} else if (fileNameOrBaseName.endsWith(".gz")) {
return new GzFileDataSource(directory, getBaseName(fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 3)), observer);
String filenameWithoutCompressionExtension = fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 3);
return new GzFileDataSource(directory, getBaseName(filenameWithoutCompressionExtension), getMainExtension(filenameWithoutCompressionExtension), observer);
} else if (fileNameOrBaseName.endsWith(".bz2")) {
return new Bzip2FileDataSource(directory, getBaseName(fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 4)), observer);
String filenameWithoutCompressionExtension = fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 4);
return new Bzip2FileDataSource(directory, getBaseName(filenameWithoutCompressionExtension), getMainExtension(filenameWithoutCompressionExtension), observer);
} else {
return new FileDataSource(directory, getBaseName(fileNameOrBaseName), observer);
return new FileDataSource(directory, getBaseName(fileNameOrBaseName), getMainExtension(fileNameOrBaseName), observer);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,22 @@ public class FileDataSource implements DataSource {

private final String baseName;

private final String mainExtension;

private final DataSourceObserver observer;

public FileDataSource(Path directory, String baseName) {
this(directory, baseName, null);
this(directory, baseName, "", null);
}

public FileDataSource(Path directory, String baseName, DataSourceObserver observer) {
this(directory, baseName, "", observer);
}

public FileDataSource(Path directory, String baseName, String mainExtension, DataSourceObserver observer) {
this.directory = Objects.requireNonNull(directory);
this.baseName = Objects.requireNonNull(baseName);
this.mainExtension = Objects.requireNonNull(mainExtension);
this.observer = observer;
}

Expand Down Expand Up @@ -115,4 +122,10 @@ public Set<String> listNames(String regex) throws IOException {
.collect(Collectors.toSet());
}
}

@Override
public boolean existsStrict(String suffix, String ext) throws IOException {
return ext.equals(mainExtension) && exists(suffix, ext);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public boolean exists(String suffix, String ext) throws IOException {
return false;
}

@Override
public boolean existsStrict(String suffix, String ext) throws IOException {
for (ReadOnlyDataSource dataSource : dataSources) {
if (dataSource.existsStrict(suffix, ext)) {
return true;
}
}
return false;
}

@Override
public boolean exists(String fileName) throws IOException {
for (ReadOnlyDataSource dataSource : dataSources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
*/
public class GzFileDataSource extends FileDataSource {

public GzFileDataSource(Path directory, String baseName, String mainExtension, DataSourceObserver observer) {
super(directory, baseName, mainExtension, observer);
}

public GzFileDataSource(Path directory, String baseName, DataSourceObserver observer) {
super(directory, baseName, observer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public boolean exists(String suffix, String ext) throws IOException {
});
}

@Override
public boolean existsStrict(String suffix, String ext) throws IOException {
return dataSources.stream().anyMatch(dataSource -> {
try {
return dataSource.existsStrict(suffix, ext);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}

@Override
public boolean exists(String fileName) throws IOException {
return dataSources.stream().anyMatch(dataSource -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public interface ReadOnlyDataSource {

boolean exists(String fileName) throws IOException;

boolean existsStrict(String suffix, String ext) throws IOException;

InputStream newInputStream(String suffix, String ext) throws IOException;

InputStream newInputStream(String fileName) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ public class ReadOnlyMemDataSource implements ReadOnlyDataSource {

private final String baseName;

private final String mainExtension;

public ReadOnlyMemDataSource() {
this("");
this("", "");
}

public ReadOnlyMemDataSource(String baseName) {
this(baseName, "");
}

public ReadOnlyMemDataSource(String baseName, String mainExtension) {
this.baseName = Objects.requireNonNull(baseName);
this.mainExtension = Objects.requireNonNull(mainExtension);
}

public byte[] getData(String suffix, String ext) {
Expand Down Expand Up @@ -67,6 +74,11 @@ public boolean exists(String suffix, String ext) throws IOException {
return exists(DataSourceUtil.getFileName(baseName, suffix, ext));
}

@Override
public boolean existsStrict(String suffix, String ext) throws IOException {
return ext.equals(mainExtension) && exists(suffix, ext);
}

@Override
public boolean exists(String fileName) throws IOException {
Objects.requireNonNull(fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ public class ResourceDataSource implements ReadOnlyDataSource {

private final String baseName;

private final String mainExtension;

private final List<ResourceSet> resourceSets;

public ResourceDataSource(String baseName, ResourceSet... resourceSets) {
this(baseName, Arrays.asList(resourceSets));
}

public ResourceDataSource(String baseName, List<ResourceSet> resourceSets) {
this(baseName, "", resourceSets);
}

public ResourceDataSource(String baseName, String mainExtension, List<ResourceSet> resourceSets) {
this.baseName = Objects.requireNonNull(baseName);
this.mainExtension = Objects.requireNonNull(mainExtension);
this.resourceSets = Objects.requireNonNull(resourceSets);
}

Expand All @@ -43,6 +50,11 @@ public boolean exists(String suffix, String ext) {
return exists(DataSourceUtil.getFileName(baseName, suffix, ext));
}

@Override
public boolean existsStrict(String suffix, String ext) {
return ext.equals(mainExtension) && exists(suffix, ext);
}

@Override
public boolean exists(String fileName) {
Objects.requireNonNull(fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
public class XZFileDataSource extends FileDataSource {

public XZFileDataSource(Path directory, String baseName, String mainExtension, DataSourceObserver observer) {
super(directory, baseName, mainExtension, observer);
}

public XZFileDataSource(Path directory, String baseName, DataSourceObserver observer) {
super(directory, baseName, observer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,22 @@ public class ZipFileDataSource implements DataSource {

private final String baseName;

private final String mainExtension;

private final DataSourceObserver observer;

public ZipFileDataSource(Path directory, String zipFileName, String baseName, DataSourceObserver observer) {
public ZipFileDataSource(Path directory, String zipFileName, String baseName, String mainExtension, DataSourceObserver observer) {
this.directory = Objects.requireNonNull(directory);
this.zipFileName = Objects.requireNonNull(zipFileName);
this.baseName = Objects.requireNonNull(baseName);
this.mainExtension = Objects.requireNonNull(mainExtension);
this.observer = observer;
}

public ZipFileDataSource(Path directory, String zipFileName, String baseName, DataSourceObserver observer) {
this(directory, zipFileName, baseName, "", observer);
}

public ZipFileDataSource(Path directory, String zipFileName, String baseName) {
this(directory, zipFileName, baseName, null);
}
Expand Down Expand Up @@ -77,6 +84,11 @@ public boolean exists(String suffix, String ext) throws IOException {
return exists(DataSourceUtil.getFileName(baseName, suffix, ext));
}

@Override
public boolean existsStrict(String suffix, String ext) throws IOException {
return ext.equals(mainExtension) && exists(suffix, ext);
}

private static boolean entryExists(Path zipFilePath, String fileName) {
if (Files.exists(zipFilePath)) {
try (ZipFile zipFile = ZipFile.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
public class ZstdFileDataSource extends FileDataSource {

public ZstdFileDataSource(Path directory, String baseName, String mainExtension, DataSourceObserver observer) {
super(directory, baseName, mainExtension, observer);
}

public ZstdFileDataSource(Path directory, String baseName, DataSourceObserver observer) {
super(directory, baseName, observer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public String getComment() {
@Override
public boolean exists(ReadOnlyDataSource dataSource) {
try {
if (dataSource.exists(null, EXT)) {
if (dataSource.existsStrict(null, EXT)) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(dataSource.newInputStream(null, EXT)))) {
String titleLine = reader.readLine();
if (titleLine != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String getComment() {
@Override
public boolean exists(ReadOnlyDataSource dataSource) {
try {
return dataSource == null || dataSource.exists(null, "tst");
return dataSource == null || dataSource.existsStrict(null, "tst");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public List<Parameter> getParameters() {

private String findExtension(ReadOnlyDataSource dataSource) throws IOException {
for (String ext : getExtensions()) {
if (dataSource.exists(null, ext)) {
if (dataSource.existsStrict(null, ext)) {
return ext;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ private static boolean reactiveLimitsAreOk(double minQ, double maxQ) {
@Override
public boolean exists(ReadOnlyDataSource dataSource) {
try {
return dataSource.exists(null, MatpowerConstants.EXT);
return dataSource.existsStrict(null, MatpowerConstants.EXT);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public String getComment() {
private Optional<PowerFactoryDataLoader<StudyCase>> findProjectLoader(ReadOnlyDataSource dataSource) {
for (PowerFactoryDataLoader<StudyCase> studyCaseLoader : PowerFactoryDataLoader.find(StudyCase.class)) {
try {
if (dataSource.exists(null, studyCaseLoader.getExtension())) {
if (dataSource.existsStrict(null, studyCaseLoader.getExtension())) {
return Optional.of(studyCaseLoader);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public boolean exists(ReadOnlyDataSource dataSource) {

private String findExtension(ReadOnlyDataSource dataSource) throws IOException {
for (String ext : EXTENSIONS) {
if (dataSource.exists(null, ext)) {
if (dataSource.existsStrict(null, ext)) {
return ext;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ public List<Parameter> getParameters() {

private String findExtension(ReadOnlyDataSource dataSource, boolean throwException) throws IOException {
for (String ext : EXTENSIONS) {
if (dataSource.exists(null, ext)) {
if (dataSource.existsStrict(null, ext)) {
return ext;
}
}
Expand Down

0 comments on commit d9f151b

Please sign in to comment.