Skip to content

Commit

Permalink
Cgmes load zipped profiles mutualize stream (#3325)
Browse files Browse the repository at this point in the history
* Mutualize code to extract InputStream
* try-with-resources through namespaceGetter function

Signed-off-by: CARON Alice <[email protected]>
Co-authored-by: Florian Dupuy <[email protected]>
  • Loading branch information
alicecaron and flo-dup authored Feb 19, 2025
1 parent 5de9a2d commit 959f6f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import com.powsybl.commons.datasource.CompressionFormat;
import com.powsybl.commons.datasource.ReadOnlyDataSource;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.zip.ZipInputStream;

import static com.powsybl.cgmes.model.CgmesNamespace.*;
Expand Down Expand Up @@ -91,23 +90,7 @@ private boolean existsNamespacesCim14(Set<String> namespaces) {
public String baseName() {
// Get the base URI if present, else build an absolute URI from the data source base name
return names().stream()
.map(n -> {
String fileExtension = n.substring(n.lastIndexOf('.') + 1);
if (fileExtension.equals(CompressionFormat.ZIP.getExtension())) {
ZipSecurityHelper.checkIfZipExtractionIsSafe(dataSource, n);
try (ZipInputStream is = new ZipInputStream(dataSource.newInputStream(n))) {
is.getNextEntry();
return NamespaceReader.base(is);
} catch (IOException x) {
throw new UncheckedIOException(x);
}
}
try (InputStream is = dataSource.newInputStream(n)) {
return NamespaceReader.base(is);
} catch (IOException x) {
throw new UncheckedIOException(x);
}
})
.map(n -> loadInputStreamAndGetNamespace(n, NamespaceReader::base))
.filter(Objects::nonNull)
.findFirst()
.orElseGet(() -> {
Expand Down Expand Up @@ -138,51 +121,33 @@ private boolean existsInDatasource(String fileName) {
}
}

private boolean containsValidNamespace(String name) {
String fileExtension = name.substring(name.lastIndexOf('.') + 1);
if (fileExtension.equals(CompressionFormat.ZIP.getExtension())) {
ZipSecurityHelper.checkIfZipExtractionIsSafe(dataSource, name);
try (ZipInputStream is = new ZipInputStream(dataSource.newInputStream(name))) {
is.getNextEntry();
Set<String> ns = NamespaceReader.namespaces1(is);
return ns.contains(RDF_NAMESPACE) && ns.stream().anyMatch(CgmesNamespace::isValid);
} catch (XMLStreamException e) {
return false;
} catch (IOException x) {
throw new CgmesModelException(String.format(LISTING_CGMES_NAMES_IN_DATA_SOURCE, dataSource), x);
}
}
try (InputStream is = dataSource.newInputStream(name)) {
Set<String> ns = NamespaceReader.namespaces1(is);
return ns.contains(RDF_NAMESPACE) && ns.stream().anyMatch(CgmesNamespace::isValid);
} catch (XMLStreamException e) {
return false;
} catch (IOException x) {
throw new CgmesModelException(String.format(LISTING_CGMES_NAMES_IN_DATA_SOURCE, dataSource), x);
}
}

public Set<String> namespaces() {
Set<String> ns = new HashSet<>();
names().forEach(n -> {
private <T> T loadInputStreamAndGetNamespace(String n, Function<InputStream, T> namespaceGetter) {
try (InputStream in = dataSource.newInputStream(n)) {
String fileExtension = n.substring(n.lastIndexOf('.') + 1);
if (fileExtension.equals(CompressionFormat.ZIP.getExtension())) {
ZipSecurityHelper.checkIfZipExtractionIsSafe(dataSource, n);
try (ZipInputStream is = new ZipInputStream(dataSource.newInputStream(n))) {
is.getNextEntry();
ns.addAll(NamespaceReader.namespaces(is));
return;
} catch (IOException x) {
throw new UncheckedIOException(x);
try (ZipInputStream zis = new ZipInputStream(in)) {
zis.getNextEntry();
return namespaceGetter.apply(zis);
}
} else {
return namespaceGetter.apply(in);
}
try (InputStream is = dataSource.newInputStream(n)) {
ns.addAll(NamespaceReader.namespaces(is));
} catch (IOException x) {
throw new UncheckedIOException(x);
}
});
return ns;
} catch (IOException e) {
throw new CgmesModelException(String.format(LISTING_CGMES_NAMES_IN_DATA_SOURCE, dataSource), e);
}
}

private boolean containsValidNamespace(String name) {
Set<String> ns = loadInputStreamAndGetNamespace(name, NamespaceReader::namespacesOrEmpty);
return ns.contains(RDF_NAMESPACE) && ns.stream().anyMatch(CgmesNamespace::isValid);
}

public Set<String> namespaces() {
return names().stream()
.map(name -> loadInputStreamAndGetNamespace(name, NamespaceReader::namespaces))
.flatMap(Set::stream)
.collect(Collectors.toSet());
}

public String cimNamespace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ public static Set<String> namespaces(InputStream is) {
}
}

public static Set<String> namespaces1(InputStream is) throws XMLStreamException {
public static Set<String> namespacesOrEmpty(InputStream is) {
try {
return namespaces1(is);
} catch (XMLStreamException x) {
return Set.of();
}
}

private static Set<String> namespaces1(InputStream is) throws XMLStreamException {
Set<String> found = new HashSet<>();
XMLStreamReader xmlsr = XML_INPUT_FACTORY_SUPPLIER.get().createXMLStreamReader(is);
try {
Expand Down

0 comments on commit 959f6f4

Please sign in to comment.