Skip to content

Commit fdea43a

Browse files
committed
review notes
Signed-off-by: Samir Romdhani <samir.romdhani_externe@rte-france.com>
1 parent a2cfa27 commit fdea43a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/NetworkSerDe.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import java.time.ZonedDateTime;
5757
import java.time.format.DateTimeFormatter;
5858
import java.util.*;
59+
import java.util.concurrent.ConcurrentHashMap;
60+
import java.util.concurrent.ConcurrentMap;
5961
import java.util.concurrent.ExecutorService;
6062
import java.util.concurrent.ForkJoinPool;
6163
import java.util.function.Supplier;
@@ -89,7 +91,9 @@ public final class NetworkSerDe {
8991
static final byte[] BIIDM_MAGIC_NUMBER = {0x42, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x49, 0x49, 0x44, 0x4d};
9092

9193
private static final Supplier<Schema> DEFAULT_SCHEMA_SUPPLIER = Suppliers.memoize(() -> NetworkSerDe.createSchema(DefaultExtensionsSupplier.getInstance()));
92-
private static final Supplier<Map<IidmVersion, Schema>> DEFAULT_SCHEMAS_SUPPLIER = Suppliers.memoize(() -> NetworkSerDe.createDefaultSchemas(DefaultExtensionsSupplier.getInstance()));
94+
private static final Supplier<ConcurrentMap<IidmVersion, Schema>> DEFAULT_SCHEMAS_SUPPLIER = Suppliers.memoize(ConcurrentHashMap::new);
95+
private static final Supplier<Map<IidmVersion, Schema>> DEFAULT_SCHEMAS_SUPPLIER_V_TEST = Suppliers.memoize(() -> NetworkSerDe.createDefaultSchemas(DefaultExtensionsSupplier.getInstance()));
96+
9397
private static final int MAX_NAMESPACE_PREFIX_NUM = 100;
9498
private static final String XSD_RESOURCE_DIR = "/xsd/";
9599

@@ -177,7 +181,7 @@ private static void validate(InputStream is, IidmVersion version, ExtensionsSupp
177181
// XSD validation
178182
Schema schema;
179183
if (extensionsSupplier == DefaultExtensionsSupplier.getInstance()) {
180-
schema = DEFAULT_SCHEMAS_SUPPLIER.get().get(version);
184+
schema = DEFAULT_SCHEMAS_SUPPLIER.get().computeIfAbsent(version, v -> createSchema(DefaultExtensionsSupplier.getInstance(), v));
181185
if (schema == null) {
182186
throw new PowsyblException("Schema not found: version=" + version);
183187
}
@@ -301,7 +305,7 @@ private static List<String> extractSchemaLocations(byte[] xsdBytes) {
301305
&& XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(reader.getNamespaceURI())
302306
&& ("import".equals(reader.getLocalName()))) {
303307
String schemaLocation = reader.getAttributeValue(null, "schemaLocation");
304-
if (schemaLocation != null && !schemaLocation.isBlank()) {
308+
if (schemaLocation != null && !schemaLocation.isBlank() && isValidXsdFile(schemaLocation)) {
305309
locations.add(schemaLocation);
306310
}
307311
}
@@ -340,6 +344,15 @@ private static String readRootNamespace(byte[] xmlBytes) {
340344
}
341345
}
342346

347+
private static boolean isValidXsdFile(String shemaLocation) {
348+
for (IidmVersion version :IidmVersion.values()) {
349+
if(shemaLocation.equals("iidm_V" + version.toString("_") + ".xsd")){
350+
return true;
351+
}
352+
}
353+
return false;
354+
}
355+
343356
private static void throwExceptionIfOption(AbstractOptions<?> options, String message) {
344357
if (options.isThrowExceptionIfExtensionNotFound()) {
345358
throw new PowsyblException(message);

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/NetworkSerDeTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ void testValidateByVersionWhenMismatchedNetworkVersion() throws IOException {
419419
@Test
420420
void testValidateByVersionWhenInvalidNetwork() throws IOException {
421421
try (InputStream is = getClass().getResourceAsStream("/V1_16/shuntOldTagName.xml")) {
422+
assertNotNull(is);
422423
assertThatThrownBy(() -> NetworkSerDe.validate(is, IidmVersion.V_1_16))
423424
.isInstanceOf(com.powsybl.commons.exceptions.UncheckedSaxException.class)
424425
.hasMessageContaining("Invalid content was found starting with element '{\"http://www.powsybl.org/schema/iidm/1_16\":shunt}'");
@@ -429,13 +430,15 @@ void testValidateByVersionWhenInvalidNetwork() throws IOException {
429430
void testValidateByVersionWhenNetworkContainSlackTerminalExtension() throws IOException {
430431
// Given extension: slack_terminal, version 1.5 that require iidm version 1.8 when validate should succeed
431432
try (InputStream is = getClass().getResourceAsStream("/slackTerminal.xml")) {
433+
assertNotNull(is);
432434
assertDoesNotThrow(() -> NetworkSerDe.validate(is, IidmVersion.V_1_16));
433435
}
434436
}
435437

436438
@Test
437439
void testValidateByVersionWhenNetworkContainTerminalMockExtension() throws IOException {
438440
try (InputStream is = getClass().getResourceAsStream("/V1_16/eurostag-tutorial-example1-with-terminalMock-ext.xml")) {
441+
assertNotNull(is);
439442
assertDoesNotThrow(() -> NetworkSerDe.validate(is, IidmVersion.V_1_16));
440443
}
441444
}
@@ -448,8 +451,6 @@ void testValidateByVersionWhenInValidExtension() throws IOException {
448451
.isInstanceOf(com.powsybl.commons.exceptions.UncheckedSaxException.class)
449452
.hasMessageContaining("Value 'TEST' is not facet-valid with respect to enumeration " +
450453
"'[IN_OPERATION, PLANNED_OUTAGE, FORCED_OUTAGE]'. It must be a value from the enumeration.");
451-
452454
}
453455
}
454-
455456
}

0 commit comments

Comments
 (0)