Skip to content

Commit f5cd228

Browse files
committed
draft
Signed-off-by: Clement Philipot <[email protected]>
1 parent c2867f3 commit f5cd228

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
import java.util.stream.Collectors;
1515
import java.util.stream.Stream;
1616

17-
import static com.powsybl.iidm.serde.IidmSerDeConstants.ITESLA_DOMAIN;
18-
import static com.powsybl.iidm.serde.IidmSerDeConstants.POWSYBL_DOMAIN;
17+
import static com.powsybl.iidm.serde.IidmSerDeConstants.*;
1918

2019
/**
2120
* @author Miora Ralambotiana {@literal <miora.ralambotiana at rte-france.com>}
@@ -85,6 +84,11 @@ public String getXsd(boolean valid) {
8584
public static IidmVersion fromNamespaceURI(String namespaceURI) {
8685
String version = namespaceURI.substring(namespaceURI.lastIndexOf('/') + 1);
8786
IidmVersion v = of(version, "_");
87+
88+
if (v.compareTo(CURRENT_IIDM_VERSION) > 0) {
89+
throw new PowsyblException("Unsupported IIDM version " + v.toString(".") + ", maximum supported version : " + CURRENT_IIDM_VERSION.toString(".") + ".");
90+
}
91+
8892
String namespaceUriV = v.getNamespaceURI();
8993
if (!namespaceURI.equals(namespaceUriV)) {
9094
if (v.compareTo(V_1_7) >= 0 && namespaceURI.equals(v.getNamespaceURI(false))) {
@@ -103,4 +107,28 @@ public static IidmVersion of(String version, String separator) {
103107
.findFirst() // there can only be 0 or exactly 1 match
104108
.orElseThrow(() -> new PowsyblException("Version " + version + " is not supported."));
105109
}
110+
111+
public static int compareVersions(String v1, String v2) {
112+
int[] version1 = parseVersion(v1);
113+
int[] version2 = parseVersion(v2);
114+
115+
if (version1[0] != version2[0]) {
116+
return Integer.compare(version1[0], version2[0]);
117+
}
118+
int result = Integer.compare(version1[1], version2[1]);
119+
return result;
120+
}
121+
122+
private static int[] parseVersion(String v) {
123+
String version = v.startsWith("V_") ? v.substring(2) : v;
124+
String[] parts = version.split("_");
125+
int major = Integer.parseInt(parts[0]);
126+
int minor = Integer.parseInt(parts[1]);
127+
return new int[]{major, minor};
128+
}
129+
130+
public static boolean isVersionSupported(IidmVersion version) {
131+
return version.compareTo(CURRENT_IIDM_VERSION) <= 0;
132+
}
133+
106134
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.powsybl.iidm.serde;
99

1010
import com.google.auto.service.AutoService;
11+
import com.powsybl.commons.PowsyblException;
1112
import com.powsybl.commons.config.PlatformConfig;
1213
import com.powsybl.commons.datasource.ReadOnlyDataSource;
1314
import com.powsybl.commons.xml.XmlUtil;
@@ -69,9 +70,14 @@ protected boolean exists(ReadOnlyDataSource dataSource, String ext) throws IOExc
6970
if (eventType == XMLStreamConstants.START_ELEMENT) {
7071
String name = xmlsr.getLocalName();
7172
String ns = xmlsr.getNamespaceURI();
72-
return NetworkSerDe.NETWORK_ROOT_ELEMENT_NAME.equals(name)
73-
&& (Stream.of(IidmVersion.values()).anyMatch(v -> v.getNamespaceURI().equals(ns))
74-
|| Stream.of(IidmVersion.values()).filter(v -> v.compareTo(IidmVersion.V_1_7) >= 0).anyMatch(v -> v.getNamespaceURI(false).equals(ns)));
73+
74+
String xmlIidmVersion = ns.substring(ns.lastIndexOf('/') + 1);
75+
if (!xmlIidmVersion.isEmpty() && IidmVersion.compareVersions(xmlIidmVersion, CURRENT_IIDM_VERSION.toString()) <= 0) {
76+
return NetworkSerDe.NETWORK_ROOT_ELEMENT_NAME.equals(name)
77+
&& (Stream.of(IidmVersion.values()).anyMatch(v -> v.getNamespaceURI().equals(ns))
78+
|| Stream.of(IidmVersion.values()).filter(v -> v.compareTo(IidmVersion.V_1_7) >= 0).anyMatch(v -> v.getNamespaceURI(false).equals(ns)));
79+
}
80+
throw new PowsyblException("Unsupported IIDM Version" + xmlIidmVersion);
7581
}
7682
}
7783
} finally {

iidm/iidm-serde/src/test/resources/testNetworkNodeBreaker.xiidm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<iidm:network xmlns:iidm="http://www.powsybl.org/schema/iidm/1_14" id="testCase11" caseDate="2022-07-26T13:56:23.057+02:00" forecastDistance="0" sourceFormat="test" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
2+
<iidm:network xmlns:iidm="http://www.powsybl.org/schema/iidm/1_19" id="testCase11" caseDate="2022-07-26T13:56:23.057+02:00" forecastDistance="0" sourceFormat="test" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
33
<iidm:substation id="subst" name="subst" country="FR">
44
<iidm:voltageLevel id="vl1" name="vl1" nominalV="380.0" topologyKind="NODE_BREAKER">
55
<iidm:nodeBreakerTopology>
@@ -115,4 +115,4 @@
115115
</iidm:voltageLevel>
116116
</iidm:substation>
117117
<iidm:line id="line1" name="line1" r="2.0" x="14.745" g1="1.0" b1="1.0" g2="1.0" b2="1.0" node1="35" voltageLevelId1="vl1" node2="2" voltageLevelId2="vlSubst2"/>
118-
</iidm:network>
118+
</iidm:network>

0 commit comments

Comments
 (0)