-
Notifications
You must be signed in to change notification settings - Fork 49
Handle unsupported IIDM file versions with a better error message #3646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
0521a4d
f195735
31fdd51
11c6c77
56c0467
a316dfa
575ab56
de049f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,7 @@ | |
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static com.powsybl.iidm.serde.IidmSerDeConstants.ITESLA_DOMAIN; | ||
| import static com.powsybl.iidm.serde.IidmSerDeConstants.POWSYBL_DOMAIN; | ||
| import static com.powsybl.iidm.serde.IidmSerDeConstants.*; | ||
|
|
||
| /** | ||
| * @author Miora Ralambotiana {@literal <miora.ralambotiana at rte-france.com>} | ||
|
|
@@ -82,25 +81,34 @@ public String getXsd(boolean valid) { | |
| return "iidm_equipment_V" + toString("_") + ".xsd"; | ||
| } | ||
|
|
||
| public static IidmVersion fromNamespaceURI(String namespaceURI) { | ||
| String version = namespaceURI.substring(namespaceURI.lastIndexOf('/') + 1); | ||
| IidmVersion v = of(version, "_"); | ||
| String namespaceUriV = v.getNamespaceURI(); | ||
| if (!namespaceURI.equals(namespaceUriV)) { | ||
| if (v.compareTo(V_1_7) >= 0 && namespaceURI.equals(v.getNamespaceURI(false))) { | ||
| return v; | ||
| } | ||
| throw new PowsyblException("Namespace " + namespaceURI + " is not supported. " + | ||
| "The namespace for IIDM XML version " + v.toString(".") + " is: " + namespaceUriV + "."); | ||
| } | ||
| return v; | ||
| } | ||
|
|
||
| public static IidmVersion of(String version, String separator) { | ||
| Objects.requireNonNull(version); | ||
| return Stream.of(IidmVersion.values()) | ||
| .filter(v -> version.equals(v.toString(separator))) | ||
| .findFirst() // there can only be 0 or exactly 1 match | ||
| .orElseThrow(() -> new PowsyblException("Version " + version + " is not supported.")); | ||
| } | ||
|
|
||
| public static int compareVersions(String v1, String v2) { | ||
|
||
| if (v1.isEmpty() || v2.isEmpty()) { | ||
| return 0; | ||
| } | ||
|
|
||
| int[] version1 = parseVersion(v1); | ||
| int[] version2 = parseVersion(v2); | ||
|
|
||
| if (version1[0] != version2[0]) { | ||
| return Integer.compare(version1[0], version2[0]); | ||
| } | ||
| return Integer.compare(version1[1], version2[1]); | ||
| } | ||
|
|
||
| private static int[] parseVersion(String v) { | ||
| String version = v.startsWith("V_") ? v.substring(2) : v; | ||
| String[] parts = version.split("_"); | ||
| int major = Integer.parseInt(parts[0]); | ||
| int minor = Integer.parseInt(parts[1]); | ||
| return new int[]{major, minor}; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||
| package com.powsybl.iidm.serde; | ||||||||
|
|
||||||||
| import com.google.auto.service.AutoService; | ||||||||
| import com.powsybl.commons.PowsyblException; | ||||||||
| import com.powsybl.commons.config.PlatformConfig; | ||||||||
| import com.powsybl.commons.datasource.ReadOnlyDataSource; | ||||||||
| import com.powsybl.commons.xml.XmlUtil; | ||||||||
|
|
@@ -58,32 +59,43 @@ public String getComment() { | |||||||
| } | ||||||||
|
|
||||||||
| protected boolean exists(ReadOnlyDataSource dataSource, String ext) throws IOException { | ||||||||
| try { | ||||||||
| if (ext != null) { | ||||||||
| try (InputStream is = dataSource.newInputStream(null, ext)) { | ||||||||
| // check the first root element is network and namespace is IIDM | ||||||||
| XMLStreamReader xmlsr = getXMLInputFactory().createXMLStreamReader(is); | ||||||||
| try { | ||||||||
| while (xmlsr.hasNext()) { | ||||||||
| int eventType = xmlsr.next(); | ||||||||
| if (eventType == XMLStreamConstants.START_ELEMENT) { | ||||||||
| String name = xmlsr.getLocalName(); | ||||||||
| String ns = xmlsr.getNamespaceURI(); | ||||||||
| return NetworkSerDe.NETWORK_ROOT_ELEMENT_NAME.equals(name) | ||||||||
| && (Stream.of(IidmVersion.values()).anyMatch(v -> v.getNamespaceURI().equals(ns)) | ||||||||
| || Stream.of(IidmVersion.values()).filter(v -> v.compareTo(IidmVersion.V_1_7) >= 0).anyMatch(v -> v.getNamespaceURI(false).equals(ns))); | ||||||||
| } | ||||||||
| } | ||||||||
| } finally { | ||||||||
| cleanClose(xmlsr); | ||||||||
| if (ext == null) { | ||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| try (InputStream is = dataSource.newInputStream(null, ext)) { | ||||||||
| XMLStreamReader xmlsr = getXMLInputFactory().createXMLStreamReader(is); | ||||||||
| try { | ||||||||
| return isValidNetworkRoot(xmlsr); | ||||||||
| } finally { | ||||||||
| cleanClose(xmlsr); | ||||||||
| } | ||||||||
| } catch (XMLStreamException e) { | ||||||||
| return false; // not a valid XML file | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| private boolean isValidNetworkRoot(XMLStreamReader xmlsr) throws XMLStreamException { | ||||||||
| while (xmlsr.hasNext()) { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment that previously existed was useful:
Suggested change
|
||||||||
| if (xmlsr.next() == XMLStreamConstants.START_ELEMENT) { | ||||||||
| String name = xmlsr.getLocalName(); | ||||||||
| String ns = xmlsr.getNamespaceURI(); | ||||||||
|
|
||||||||
| if (!ns.isEmpty()) { | ||||||||
| String xmlIidmVersion = ns.substring(ns.lastIndexOf('/') + 1); | ||||||||
| if (IidmVersion.compareVersions(xmlIidmVersion, CURRENT_IIDM_VERSION.toString()) > 0) { | ||||||||
|
||||||||
| throw new PowsyblException("Unsupported IIDM Version (maximum supported version: " | ||||||||
| + CURRENT_IIDM_VERSION.toString(".") + ")"); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return NetworkSerDe.NETWORK_ROOT_ELEMENT_NAME.equals(name) | ||||||||
olperr1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| && (Stream.of(IidmVersion.values()).anyMatch(v -> v.getNamespaceURI().equals(ns)) | ||||||||
| || Stream.of(IidmVersion.values()).filter(v -> v.compareTo(IidmVersion.V_1_7) >= 0) | ||||||||
| .anyMatch(v -> v.getNamespaceURI(false).equals(ns))); | ||||||||
| } | ||||||||
| return false; | ||||||||
| } catch (XMLStreamException e) { | ||||||||
| // not a valid xml file | ||||||||
| return false; | ||||||||
| } | ||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| private void cleanClose(XMLStreamReader xmlStreamReader) { | ||||||||
|
|
||||||||
Uh oh!
There was an error while loading. Please reload this page.