Skip to content

Commit

Permalink
Disambiguate files on import
Browse files Browse the repository at this point in the history
Signed-off-by: HARPER Jon <jon.harper87@gmail.com>
  • Loading branch information
jonenst committed Jun 26, 2024
1 parent 6e18e4f commit eda4baf
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* @author Luma Zamarreño {@literal <zamarrenolm at aia.es>}
*/
public class CgmesOnDataSource {

private static final String EXTENSION = "xml";

public CgmesOnDataSource(ReadOnlyDataSource ds) {
this.dataSource = ds;
}
Expand All @@ -33,25 +36,62 @@ public ReadOnlyDataSource dataSource() {
}

public boolean exists() {
// if given a main file with our extension, check that the mainfile is our format
if (EXTENSION.equals(dataSource.getBaseExtension())) {
try (InputStream is = dataSource.newInputStream(null, EXTENSION)) {
if (!existsNamespaces(NamespaceReader.namespaces(is))) {
return false;
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
// if there is an extension and it's not compatible, don't import
} else if (!dataSource.getBaseExtension().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)) {
return existsNamespaces(foundNamespaces);
}

private boolean existsNamespaces(Set<String> namespaces) {
if (!namespaces.contains(RDF_NAMESPACE)) {
return false;
}
// FIXME(Luma) This is legacy behaviour, we do not consider CIM14 valid in this check
// But I think we do not need to support 14 separately?
return foundNamespaces.contains(CIM_16_NAMESPACE) || foundNamespaces.contains(CIM_100_NAMESPACE);
return namespaces.contains(CIM_16_NAMESPACE) || namespaces.contains(CIM_100_NAMESPACE);
}

public boolean existsCim14() {
// if given a main file with our extension, check that the mainfile is our format
if (EXTENSION.equals(dataSource.getBaseExtension())) {
try (InputStream is = dataSource.newInputStream(null, EXTENSION)) {
if (!existsNamespacesCim14(NamespaceReader.namespaces(is))) {
return false;
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
// if given a main file with an extension and it's not compatible, don't import
// to avoid importing a file when the user specified another
} else if (!dataSource.getBaseExtension().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)) {
return existsNamespacesCim14(foundNamespaces);
}

private boolean existsNamespacesCim14(Set<String> namespaces) {
if (!namespaces.contains(RDF_NAMESPACE)) {
return false;
}
// FIXME(Luma) This is legacy behaviour, we do not consider CIM14 valid in this check
// But I think we do not need to support 14 separately?
if (!foundNamespaces.contains(CIM_14_NAMESPACE)) {
if (!namespaces.contains(CIM_14_NAMESPACE)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ static String getBaseName(String fileName) {
return pos == -1 ? fileName : fileName.substring(0, pos);
}

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

static DataSource createDataSource(Path directory, String basename, CompressionFormat compressionExtension, DataSourceObserver observer) {
Objects.requireNonNull(directory);
Objects.requireNonNull(basename);
Expand Down Expand Up @@ -78,7 +88,7 @@ static DataSource createDataSource(Path directory, String fileNameOrBaseName, Da
} else if (fileNameOrBaseName.endsWith(".bz2")) {
return new Bzip2FileDataSource(directory, getBaseName(fileNameOrBaseName.substring(0, fileNameOrBaseName.length() - 4)), 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,26 @@ public class FileDataSource implements DataSource {

private final String baseName;

private final String baseExtension;

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 baseExtension) {
this(directory, baseName, baseExtension, null);
}

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

Expand All @@ -46,6 +57,11 @@ public String getBaseName() {
return baseName;
}

@Override
public String getBaseExtension() {
return baseExtension;
}

protected String getCompressionExt() {
return COMPRESSION_EXT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public interface ReadOnlyDataSource {

String getBaseName();

default String getBaseExtension() {
return "";
}

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

boolean exists(String fileName) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public String getComment() {

@Override
public boolean exists(ReadOnlyDataSource dataSource) {
// if given a main file with an extension and it's not compatible, don't import
// to avoid importing a file when the user specified another
if (!dataSource.getBaseExtension().isEmpty()) {
return false;
}

try {
if (dataSource.exists(null, EXT)) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(dataSource.newInputStream(null, EXT)))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -96,6 +97,15 @@ public List<Parameter> getParameters() {
}

private String findExtension(ReadOnlyDataSource dataSource) throws IOException {
// if given a main file compatible with our extensions, try that as the mainfile.
if (Arrays.asList(getExtensions()).contains(dataSource.getBaseExtension())) {
return dataSource.getBaseExtension();
// if given a main file with an extension and it's not compatible, don't import
// to avoid importing a file when the user specified another
} else if (!dataSource.getBaseExtension().isEmpty()) {
return null;
}

for (String ext : getExtensions()) {
if (dataSource.exists(null, ext)) {
return ext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,12 @@ private static boolean reactiveLimitsAreOk(double minQ, double maxQ) {

@Override
public boolean exists(ReadOnlyDataSource dataSource) {
// if given a main file with an extension and it's not compatible, don't import
// to avoid importing a file when the user specified another
if (!dataSource.getBaseExtension().isEmpty()) {
return false;
}

try {
return dataSource.exists(null, MatpowerConstants.EXT);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ public String getComment() {
}

private Optional<PowerFactoryDataLoader<StudyCase>> findProjectLoader(ReadOnlyDataSource dataSource) {
for (PowerFactoryDataLoader<StudyCase> studyCaseLoader : PowerFactoryDataLoader.find(StudyCase.class)) {
List<PowerFactoryDataLoader<StudyCase>> loaders = PowerFactoryDataLoader.find(StudyCase.class);
Map<String, List<PowerFactoryDataLoader<StudyCase>>> extensions = loaders.stream().collect(
Collectors.groupingBy(PowerFactoryDataLoader::getExtension));
// if given a main file compatible with our extensions, try that as the mainfile.
if (extensions.containsKey(dataSource.getBaseExtension())) {
return Optional.of(extensions.get(dataSource.getBaseExtension()).get(0));
// if given a main file with an extension and it's not compatible, don't import
// to avoid importing a file when the user specified another
} else if (!dataSource.getBaseExtension().isEmpty()) {
return Optional.empty();
}

for (PowerFactoryDataLoader<StudyCase> studyCaseLoader : loaders) {
try {
if (dataSource.exists(null, studyCaseLoader.getExtension())) {
return Optional.of(studyCaseLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ public boolean exists(ReadOnlyDataSource dataSource) {
}

private String findExtension(ReadOnlyDataSource dataSource) throws IOException {
// if given a main file compatible with our extensions, try that as the mainfile.
if (Arrays.asList(EXTENSIONS).contains(dataSource.getBaseExtension())) {
return dataSource.getBaseExtension();
// if given a main file with an extension and it's not compatible, don't import
// to avoid importing a file when the user specified another
} else if (!dataSource.getBaseExtension().isEmpty()) {
return null;
}

for (String ext : EXTENSIONS) {
if (dataSource.exists(null, ext)) {
return ext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,15 @@ public List<Parameter> getParameters() {
}

private String findExtension(ReadOnlyDataSource dataSource, boolean throwException) throws IOException {
// if given a main file compatible with our extensions, try that as the mainfile.
if (Arrays.asList(EXTENSIONS).contains(dataSource.getBaseExtension())) {
return dataSource.getBaseExtension();
// if given a main file with an extension and it's not compatible, don't import
// to avoid importing a file when the user specified another
} else if (!dataSource.getBaseExtension().isEmpty()) {
return null;
}

for (String ext : EXTENSIONS) {
if (dataSource.exists(null, ext)) {
return ext;
Expand Down

0 comments on commit eda4baf

Please sign in to comment.