diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractComplexIdentifiableXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractComplexIdentifiableXml.java new file mode 100644 index 00000000000..578adebd3e7 --- /dev/null +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractComplexIdentifiableXml.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.iidm.xml; + +import com.powsybl.commons.PowsyblException; +import com.powsybl.iidm.network.Identifiable; +import com.powsybl.iidm.network.IdentifiableAdder; +import com.powsybl.iidm.xml.util.IidmXmlUtil; + +import javax.xml.stream.XMLStreamException; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +/** + * Abstract class for equipment that need their sub-elements to be entirely created. + * + * @author Miora Vedelago + */ +abstract class AbstractComplexIdentifiableXml, P extends Identifiable> extends AbstractIdentifiableXml { + + protected abstract void readRootElementAttributes(A adder, List> toApply, NetworkXmlReaderContext context); + + protected abstract T add(A adder); // TODO: to change when IIDM adders API is changed (add() is added in API) + + protected void readSubElements(String id, List> toApply, NetworkXmlReaderContext context) throws XMLStreamException { + if (context.getReader().getLocalName().equals(PropertiesXml.PROPERTY)) { + PropertiesXml.read(toApply, context); + } else if (context.getReader().getLocalName().equals(AliasesXml.ALIAS)) { + IidmXmlUtil.assertMinimumVersion(getRootElementName(), AliasesXml.ALIAS, IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_3, context); + AliasesXml.read(toApply, context); + } else { + throw new PowsyblException("Unknown element name <" + context.getReader().getLocalName() + "> in <" + id + ">"); + } + } + + protected abstract void readSubElements(String id, A adder, List> toApply, NetworkXmlReaderContext context) throws XMLStreamException; + + @Override + public final void read(P parent, NetworkXmlReaderContext context) throws XMLStreamException { + List> toApply = new ArrayList<>(); + A adder = createAdder(parent); + String id = readIdentifierAttributes(adder, context); + readRootElementAttributes(adder, toApply, context); + readSubElements(id, adder, toApply, context); + T identifiable = add(adder); + toApply.forEach(consumer -> consumer.accept(identifiable)); + } +} diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractIdentifiableXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractIdentifiableXml.java index 6a0a5c4a9c5..13b3f281ab6 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractIdentifiableXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractIdentifiableXml.java @@ -6,7 +6,6 @@ */ package com.powsybl.iidm.xml; -import com.powsybl.commons.PowsyblException; import com.powsybl.commons.exceptions.UncheckedXmlStreamException; import com.powsybl.commons.xml.XmlUtil; import com.powsybl.iidm.network.Identifiable; @@ -17,7 +16,6 @@ import javax.xml.stream.XMLStreamReader; /** - * * @author Geoffroy Jamgotchian */ abstract class AbstractIdentifiableXml, P extends Identifiable> { @@ -80,34 +78,11 @@ public final void write(T identifiable, P parent, NetworkXmlWriterContext contex context.addExportedEquipment(identifiable); } - protected void readUntilEndRootElement(XMLStreamReader reader, XmlUtil.XmlEventHandler eventHandler) throws XMLStreamException { - XmlUtil.readUntilEndElement(getRootElementName(), reader, eventHandler); - } - protected abstract A createAdder(P parent); - protected abstract T readRootElementAttributes(A adder, NetworkXmlReaderContext context); - - protected void readSubElements(T identifiable, NetworkXmlReaderContext context) throws XMLStreamException { - if (context.getReader().getLocalName().equals(PropertiesXml.PROPERTY)) { - PropertiesXml.read(identifiable, context); - } else if (context.getReader().getLocalName().equals(AliasesXml.ALIAS)) { - IidmXmlUtil.assertMinimumVersion(getRootElementName(), AliasesXml.ALIAS, IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_3, context); - AliasesXml.read(identifiable, context); - } else { - throw new PowsyblException("Unknown element name <" + context.getReader().getLocalName() + "> in <" + identifiable.getId() + ">"); - } - } + public abstract void read(P parent, NetworkXmlReaderContext context) throws XMLStreamException; - protected void readElement(String id, A adder, NetworkXmlReaderContext context) throws XMLStreamException { - T identifiable = readRootElementAttributes(adder, context); - if (identifiable != null) { - readSubElements(identifiable, context); - } - } - - public final void read(P parent, NetworkXmlReaderContext context) throws XMLStreamException { - A adder = createAdder(parent); + protected String readIdentifierAttributes(A adder, NetworkXmlReaderContext context) { String id = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, "id")); String name = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, "name")); adder.setId(id) @@ -116,6 +91,10 @@ public final void read(P parent, NetworkXmlReaderContext context) throws XMLStre boolean fictitious = XmlUtil.readOptionalBoolAttribute(context.getReader(), "fictitious", false); adder.setFictitious(fictitious); }); - readElement(id, adder, context); + return id; + } + + protected void readUntilEndRootElement(XMLStreamReader reader, XmlUtil.XmlEventHandler eventHandler) throws XMLStreamException { + XmlUtil.readUntilEndElement(getRootElementName(), reader, eventHandler); } } diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractSimpleIdentifiableXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractSimpleIdentifiableXml.java new file mode 100644 index 00000000000..386853a9766 --- /dev/null +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractSimpleIdentifiableXml.java @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.iidm.xml; + +import com.powsybl.commons.PowsyblException; +import com.powsybl.iidm.network.Identifiable; +import com.powsybl.iidm.network.IdentifiableAdder; +import com.powsybl.iidm.xml.util.IidmXmlUtil; + +import javax.xml.stream.XMLStreamException; + +/** + * Abstract class for equipment that can and/or must be entirely created before reading their sub-elements. + * + * @author Miora Vedelago + */ +abstract class AbstractSimpleIdentifiableXml, P extends Identifiable> extends AbstractIdentifiableXml { + + protected abstract T readRootElementAttributes(A adder, NetworkXmlReaderContext context); + + protected void readSubElements(T identifiable, NetworkXmlReaderContext context) throws XMLStreamException { + if (context.getReader().getLocalName().equals(PropertiesXml.PROPERTY)) { + PropertiesXml.read(identifiable, context); + } else if (context.getReader().getLocalName().equals(AliasesXml.ALIAS)) { + IidmXmlUtil.assertMinimumVersion(getRootElementName(), AliasesXml.ALIAS, IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_3, context); + AliasesXml.read(identifiable, context); + } else { + throw new PowsyblException("Unknown element name <" + context.getReader().getLocalName() + "> in <" + identifiable.getId() + ">"); + } + } + + @Override + public final void read(P parent, NetworkXmlReaderContext context) throws XMLStreamException { + A adder = createAdder(parent); + readIdentifierAttributes(adder, context); + T identifiable = readRootElementAttributes(adder, context); + if (identifiable != null) { + readSubElements(identifiable, context); + } + } +} diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractSwitchXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractSwitchXml.java index 00dae880995..ebb4f4289b2 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractSwitchXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractSwitchXml.java @@ -18,7 +18,7 @@ * * @author Geoffroy Jamgotchian */ -abstract class AbstractSwitchXml> extends AbstractIdentifiableXml { +abstract class AbstractSwitchXml> extends AbstractSimpleIdentifiableXml { static final String ROOT_ELEMENT_NAME = "switch"; diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractTransformerXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractTransformerXml.java index 58eb1e7674b..1d6daadb8e3 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractTransformerXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractTransformerXml.java @@ -19,7 +19,7 @@ /** * @author Geoffroy Jamgotchian */ -abstract class AbstractTransformerXml> extends AbstractConnectableXml>> { +abstract class AbstractTransformerXml> extends AbstractSimpleIdentifiableXml>> { private interface StepConsumer { void accept(double r, double x, double g, double b, double rho); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AliasesXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AliasesXml.java index 33eba41067c..f648c02b49b 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AliasesXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AliasesXml.java @@ -11,6 +11,8 @@ import com.powsybl.iidm.xml.util.IidmXmlUtil; import javax.xml.stream.XMLStreamException; +import java.util.List; +import java.util.function.Consumer; /** * @author Sebastien Murgey @@ -35,14 +37,22 @@ public static void write(Identifiable identifiable, String rootElementName, N } } - public static void read(Identifiable identifiable, NetworkXmlReaderContext context) throws XMLStreamException { + public static void read(T identifiable, NetworkXmlReaderContext context) throws XMLStreamException { + read(context).accept(identifiable); + } + + public static void read(List> toApply, NetworkXmlReaderContext context) throws XMLStreamException { + toApply.add(read(context)); + } + + private static Consumer read(NetworkXmlReaderContext context) throws XMLStreamException { if (!context.getReader().getLocalName().equals(ALIAS)) { throw new IllegalStateException(); } String[] aliasType = new String[1]; IidmXmlUtil.runFromMinimumVersion(IidmXmlVersion.V_1_4, context, () -> aliasType[0] = context.getReader().getAttributeValue(null, "type")); String alias = context.getAnonymizer().deanonymizeString(context.getReader().getElementText()); - identifiable.addAlias(alias, aliasType[0]); + return identifiable -> identifiable.addAlias(alias, aliasType[0]); } private AliasesXml() { diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BatteryXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BatteryXml.java index 3b32df90e0c..b43c0815f1f 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BatteryXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BatteryXml.java @@ -14,10 +14,12 @@ import javax.xml.stream.XMLStreamException; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * @author Ghiles Abdellah */ -class BatteryXml extends AbstractConnectableXml { +class BatteryXml extends AbstractSimpleIdentifiableXml { static final BatteryXml INSTANCE = new BatteryXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BusXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BusXml.java index 50283553a9b..4a05739a96e 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BusXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BusXml.java @@ -18,7 +18,7 @@ * * @author Geoffroy Jamgotchian */ -class BusXml extends AbstractIdentifiableXml { +class BusXml extends AbstractSimpleIdentifiableXml { static final BusXml INSTANCE = new BusXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BusbarSectionXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BusbarSectionXml.java index f023b413202..1c86a422697 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BusbarSectionXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/BusbarSectionXml.java @@ -19,7 +19,7 @@ * * @author Geoffroy Jamgotchian */ -class BusbarSectionXml extends AbstractIdentifiableXml { +class BusbarSectionXml extends AbstractSimpleIdentifiableXml { static final BusbarSectionXml INSTANCE = new BusbarSectionXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractConnectableXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ConnectableXmlUtil.java similarity index 92% rename from iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractConnectableXml.java rename to iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ConnectableXmlUtil.java index da1ddb5b649..271b51d2278 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractConnectableXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ConnectableXmlUtil.java @@ -18,7 +18,10 @@ /** * @author Geoffroy Jamgotchian */ -public abstract class AbstractConnectableXml, P extends Container> extends AbstractIdentifiableXml { +public final class ConnectableXmlUtil { + + private ConnectableXmlUtil() { + } private static final String BUS = "bus"; private static final String CONNECTABLE_BUS = "connectableBus"; @@ -39,21 +42,21 @@ private static String indexToString(Integer index) { return index != null ? index.toString() : ""; } - protected static boolean hasValidOperationalLimits(Branch branch, NetworkXmlWriterContext context) { + public static boolean hasValidOperationalLimits(Branch branch, NetworkXmlWriterContext context) { if (context.getVersion().compareTo(IidmXmlVersion.V_1_5) >= 0) { return !branch.getOperationalLimits1().isEmpty() || !branch.getOperationalLimits2().isEmpty(); } return branch.getCurrentLimits1().isPresent() || branch.getCurrentLimits2().isPresent(); } - protected static boolean hasValidOperationalLimits(FlowsLimitsHolder limitsHolder, NetworkXmlWriterContext context) { + public static boolean hasValidOperationalLimits(FlowsLimitsHolder limitsHolder, NetworkXmlWriterContext context) { if (context.getVersion().compareTo(IidmXmlVersion.V_1_5) >= 0) { return !limitsHolder.getOperationalLimits().isEmpty(); } return limitsHolder.getCurrentLimits().isPresent(); } - protected static void writeNodeOrBus(Integer index, Terminal t, NetworkXmlWriterContext context) throws XMLStreamException { + public static void writeNodeOrBus(Integer index, Terminal t, NetworkXmlWriterContext context) throws XMLStreamException { TopologyLevel topologyLevel = TopologyLevel.min(t.getVoltageLevel().getTopologyKind(), context.getOptions().getTopologyLevel()); switch (topologyLevel) { case NODE_BREAKER: @@ -88,7 +91,7 @@ private static void writeBus(Integer index, Bus bus, Bus connectableBus, Network } } - protected static void readNodeOrBus(InjectionAdder adder, NetworkXmlReaderContext context) { + public static void readNodeOrBus(InjectionAdder adder, NetworkXmlReaderContext context) { String bus = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, BUS)); String connectableBus = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, CONNECTABLE_BUS)); Integer node = XmlUtil.readOptionalIntegerAttribute(context.getReader(), NODE); @@ -103,7 +106,7 @@ protected static void readNodeOrBus(InjectionAdder adder, NetworkXmlReaderContex } } - protected static void readNodeOrBus(BranchAdder adder, NetworkXmlReaderContext context) { + public static void readNodeOrBus(BranchAdder adder, NetworkXmlReaderContext context) { String bus1 = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, "bus1")); String connectableBus1 = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, "connectableBus1")); Integer node1 = XmlUtil.readOptionalIntegerAttribute(context.getReader(), "node1"); @@ -134,7 +137,7 @@ protected static void readNodeOrBus(BranchAdder adder, NetworkXmlReaderContext c adder.setVoltageLevel2(voltageLevelId2); } - protected static void readNodeOrBus(int index, LegAdder adder, NetworkXmlReaderContext context) { + public static void readNodeOrBus(int index, LegAdder adder, NetworkXmlReaderContext context) { String bus = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, BUS + index)); String connectableBus = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, CONNECTABLE_BUS + index)); Integer node = XmlUtil.readOptionalIntegerAttribute(context.getReader(), NODE + index); @@ -151,12 +154,12 @@ protected static void readNodeOrBus(int index, LegAdder adder, NetworkXmlReaderC adder.setVoltageLevel(voltageLevelId); } - protected static void writePQ(Integer index, Terminal t, XMLStreamWriter writer) throws XMLStreamException { + public static void writePQ(Integer index, Terminal t, XMLStreamWriter writer) throws XMLStreamException { XmlUtil.writeOptionalDouble("p" + indexToString(index), t.getP(), Double.NaN, writer); XmlUtil.writeOptionalDouble("q" + indexToString(index), t.getQ(), Double.NaN, writer); } - protected static void readPQ(Integer index, Terminal t, XMLStreamReader reader) { + public static void readPQ(Integer index, Terminal t, XMLStreamReader reader) { double p = XmlUtil.readOptionalDoubleAttribute(reader, "p" + indexToString(index)); double q = XmlUtil.readOptionalDoubleAttribute(reader, "q" + indexToString(index)); t.setP(p) @@ -268,7 +271,7 @@ private static void writeLoadingLimits(Integer index, * @deprecated Use {@link TerminalRefXml#writeTerminalRef(Terminal, NetworkXmlWriterContext, String)} instead. */ @Deprecated - protected static void writeTerminalRef(Terminal t, NetworkXmlWriterContext context, String elementName) throws XMLStreamException { + public static void writeTerminalRef(Terminal t, NetworkXmlWriterContext context, String elementName) throws XMLStreamException { TerminalRefXml.writeTerminalRef(t, context, elementName); } @@ -276,7 +279,7 @@ protected static void writeTerminalRef(Terminal t, NetworkXmlWriterContext conte * @deprecated Use {@link TerminalRefXml#readTerminalRef(Network, String, String)} instead. */ @Deprecated - protected static Terminal readTerminalRef(Network network, String id, String side) { + public static Terminal readTerminalRef(Network network, String id, String side) { return TerminalRefXml.readTerminalRef(network, id, side); } } diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/DanglingLineXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/DanglingLineXml.java index cac800cf1bc..f7e12b17adb 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/DanglingLineXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/DanglingLineXml.java @@ -13,11 +13,13 @@ import javax.xml.stream.XMLStreamException; import java.util.Optional; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * * @author Geoffroy Jamgotchian */ -class DanglingLineXml extends AbstractConnectableXml { +class DanglingLineXml extends AbstractSimpleIdentifiableXml { private static final String GENERATION = "generation"; private static final String GENERATION_MAX_P = "generationMaxP"; private static final String GENERATION_MIN_P = "generationMinP"; diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/GeneratorXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/GeneratorXml.java index 382bbb39e31..ca30086db31 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/GeneratorXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/GeneratorXml.java @@ -14,10 +14,12 @@ import javax.xml.stream.XMLStreamException; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * @author Geoffroy Jamgotchian */ -class GeneratorXml extends AbstractConnectableXml { +class GeneratorXml extends AbstractSimpleIdentifiableXml { static final GeneratorXml INSTANCE = new GeneratorXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/HvdcLineXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/HvdcLineXml.java index 56d7e28866b..894a6d8beda 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/HvdcLineXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/HvdcLineXml.java @@ -17,7 +17,7 @@ * @author Geoffroy Jamgotchian * @author Mathieu Bague */ -class HvdcLineXml extends AbstractIdentifiableXml { +class HvdcLineXml extends AbstractSimpleIdentifiableXml { static final HvdcLineXml INSTANCE = new HvdcLineXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LccConverterStationXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LccConverterStationXml.java index fb2cb7049d7..6525a56ad44 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LccConverterStationXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LccConverterStationXml.java @@ -13,11 +13,13 @@ import javax.xml.stream.XMLStreamException; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * @author Geoffroy Jamgotchian * @author Mathieu Bague */ -public class LccConverterStationXml extends AbstractConnectableXml { +public class LccConverterStationXml extends AbstractSimpleIdentifiableXml { static final LccConverterStationXml INSTANCE = new LccConverterStationXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LineXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LineXml.java index 7fc53988cf0..20a4162468a 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LineXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LineXml.java @@ -13,11 +13,13 @@ import javax.xml.stream.XMLStreamException; import java.util.Optional; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * * @author Geoffroy Jamgotchian */ -class LineXml extends AbstractConnectableXml { +class LineXml extends AbstractSimpleIdentifiableXml { static final LineXml INSTANCE = new LineXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LoadXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LoadXml.java index f8cc360b311..14b2e0f8b42 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LoadXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/LoadXml.java @@ -14,11 +14,13 @@ import javax.xml.stream.XMLStreamException; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * * @author Geoffroy Jamgotchian */ -class LoadXml extends AbstractConnectableXml { +class LoadXml extends AbstractSimpleIdentifiableXml { static final LoadXml INSTANCE = new LoadXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/PropertiesXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/PropertiesXml.java index d64c2d3a97c..a6789c9d793 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/PropertiesXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/PropertiesXml.java @@ -12,6 +12,8 @@ import com.powsybl.iidm.xml.util.IidmXmlUtil; import javax.xml.stream.XMLStreamException; +import java.util.List; +import java.util.function.Consumer; /** * @author Mathieu Bague @@ -35,12 +37,20 @@ public static void write(Identifiable identifiable, NetworkXmlWriterContext c } public static void read(Identifiable identifiable, NetworkXmlReaderContext context) { + read(context).accept(identifiable); + } + + public static void read(List> toApply, NetworkXmlReaderContext context) { + toApply.add(read(context)); + } + + private static Consumer read(NetworkXmlReaderContext context) { if (!context.getReader().getLocalName().equals(PROPERTY)) { throw new IllegalStateException(); } String name = context.getReader().getAttributeValue(null, NAME); String value = context.getReader().getAttributeValue(null, VALUE); - identifiable.setProperty(name, value); + return identifiable -> identifiable.setProperty(name, value); } private PropertiesXml() { diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ShuntXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ShuntXml.java index 15c9b6e7d79..c3fc1ffea94 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ShuntXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ShuntXml.java @@ -12,13 +12,16 @@ import com.powsybl.iidm.xml.util.IidmXmlUtil; import javax.xml.stream.XMLStreamException; -import java.util.HashMap; -import java.util.Map; +import java.util.List; +import java.util.function.Consumer; + +import static com.powsybl.iidm.xml.ConnectableXmlUtil.readNodeOrBus; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.writeNodeOrBus; /** * @author Geoffroy Jamgotchian */ -class ShuntXml extends AbstractConnectableXml { +class ShuntXml extends AbstractComplexIdentifiableXml { static final ShuntXml INSTANCE = new ShuntXml(); @@ -81,6 +84,11 @@ protected void writeSubElements(ShuntCompensator sc, VoltageLevel vl, NetworkXml } } + @Override + protected ShuntCompensatorAdder createAdder(VoltageLevel parent) { + return parent.newShuntCompensator(); + } + private static void writeModel(ShuntCompensator sc, NetworkXmlWriterContext context) throws XMLStreamException { if (sc.getModelType() == ShuntCompensatorModelType.LINEAR) { context.getWriter().writeEmptyElement(context.getVersion().getNamespaceURI(context.isValid()), SHUNT_LINEAR_MODEL); @@ -103,17 +111,7 @@ private static void writeModel(ShuntCompensator sc, NetworkXmlWriterContext cont } @Override - protected ShuntCompensatorAdder createAdder(VoltageLevel vl) { - return vl.newShuntCompensator(); - } - - @Override - protected ShuntCompensator readRootElementAttributes(ShuntCompensatorAdder adder, NetworkXmlReaderContext context) { - throw new UnsupportedOperationException(); - } - - @Override - protected void readElement(String id, ShuntCompensatorAdder adder, NetworkXmlReaderContext context) throws XMLStreamException { + protected void readRootElementAttributes(ShuntCompensatorAdder adder, List> toApply, NetworkXmlReaderContext context) { IidmXmlUtil.runFromMinimumVersion(IidmXmlVersion.V_1_2, context, () -> { String voltageRegulatorOn = context.getReader().getAttributeValue(null, "voltageRegulatorOn"); double targetV = XmlUtil.readOptionalDoubleAttribute(context.getReader(), "targetV"); @@ -142,26 +140,22 @@ protected void readElement(String id, ShuntCompensatorAdder adder, NetworkXmlRea readNodeOrBus(adder, context); double p = XmlUtil.readOptionalDoubleAttribute(context.getReader(), "p"); double q = XmlUtil.readOptionalDoubleAttribute(context.getReader(), "q"); - String[] regId = new String[1]; - String[] regSide = new String[1]; - Map properties = new HashMap<>(); - Map aliases = new HashMap<>(); + toApply.add(sc -> sc.getTerminal().setP(p).setQ(q)); + } + + @Override + protected ShuntCompensator add(ShuntCompensatorAdder adder) { + return adder.add(); + } + + @Override + protected void readSubElements(String id, ShuntCompensatorAdder adder, List> toApply, NetworkXmlReaderContext context) throws XMLStreamException { readUntilEndRootElement(context.getReader(), () -> { switch (context.getReader().getLocalName()) { case REGULATING_TERMINAL: - regId[0] = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, "id")); - regSide[0] = context.getReader().getAttributeValue(null, "side"); - break; - case PropertiesXml.PROPERTY: - String name = context.getReader().getAttributeValue(null, "name"); - String value = context.getReader().getAttributeValue(null, "value"); - properties.put(name, value); - break; - case AliasesXml.ALIAS: - IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, AliasesXml.ALIAS, IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_3, context); - String aliasType = context.getReader().getAttributeValue(null, "type"); - String alias = context.getAnonymizer().deanonymizeString(context.getReader().getElementText()); - aliases.put(alias, aliasType); + String regId = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, "id")); + String regSide = context.getReader().getAttributeValue(null, "side"); + toApply.add(sc -> context.getEndTasks().add(() -> sc.setRegulatingTerminal(TerminalRefXml.readTerminalRef(sc.getNetwork(), regId, regSide)))); break; case SHUNT_LINEAR_MODEL: IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, SHUNT_LINEAR_MODEL, IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_3, context); @@ -192,15 +186,8 @@ protected void readElement(String id, ShuntCompensatorAdder adder, NetworkXmlRea modelAdder.add(); break; default: - throw new PowsyblException("Unknown element name <" + context.getReader().getLocalName() + "> in <" + id + ">"); + super.readSubElements(id, toApply, context); } }); - ShuntCompensator sc = adder.add(); - if (regId[0] != null) { - context.getEndTasks().add(() -> sc.setRegulatingTerminal(TerminalRefXml.readTerminalRef(sc.getNetwork(), regId[0], regSide[0]))); - } - properties.forEach(sc::setProperty); - aliases.forEach(sc::addAlias); - sc.getTerminal().setP(p).setQ(q); } } diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/StaticVarCompensatorXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/StaticVarCompensatorXml.java index 0b17a1a3d78..c9571be56b1 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/StaticVarCompensatorXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/StaticVarCompensatorXml.java @@ -14,10 +14,12 @@ import javax.xml.stream.XMLStreamException; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * @author Geoffroy Jamgotchian */ -public class StaticVarCompensatorXml extends AbstractConnectableXml { +public class StaticVarCompensatorXml extends AbstractSimpleIdentifiableXml { static final StaticVarCompensatorXml INSTANCE = new StaticVarCompensatorXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/SubstationXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/SubstationXml.java index 0f420b5500d..2b9f7f1be79 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/SubstationXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/SubstationXml.java @@ -18,7 +18,7 @@ * * @author Geoffroy Jamgotchian */ -class SubstationXml extends AbstractIdentifiableXml { +class SubstationXml extends AbstractSimpleIdentifiableXml { static final SubstationXml INSTANCE = new SubstationXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ThreeWindingsTransformerXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ThreeWindingsTransformerXml.java index 3af64cfb3b2..de2298782f9 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ThreeWindingsTransformerXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/ThreeWindingsTransformerXml.java @@ -14,6 +14,8 @@ import javax.xml.stream.XMLStreamException; import java.util.Optional; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * @author Geoffroy Jamgotchian */ diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/TieLineXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/TieLineXml.java index 75a6f1f368b..a87dbf3e13f 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/TieLineXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/TieLineXml.java @@ -15,11 +15,13 @@ import javax.xml.stream.XMLStreamException; import java.util.Optional; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * * @author Geoffroy Jamgotchian */ -class TieLineXml extends AbstractConnectableXml { +class TieLineXml extends AbstractSimpleIdentifiableXml { private static final Logger LOGGER = LoggerFactory.getLogger(TieLineXml.class); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/TwoWindingsTransformerXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/TwoWindingsTransformerXml.java index e1f769087d7..719a706e311 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/TwoWindingsTransformerXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/TwoWindingsTransformerXml.java @@ -13,6 +13,8 @@ import javax.xml.stream.XMLStreamException; import java.util.Optional; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * * @author Geoffroy Jamgotchian diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/VoltageLevelXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/VoltageLevelXml.java index 29df3806853..27a5599b54c 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/VoltageLevelXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/VoltageLevelXml.java @@ -22,7 +22,7 @@ /** * @author Geoffroy Jamgotchian */ -class VoltageLevelXml extends AbstractIdentifiableXml>> { +class VoltageLevelXml extends AbstractSimpleIdentifiableXml>> { static final VoltageLevelXml INSTANCE = new VoltageLevelXml(); diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/VscConverterStationXml.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/VscConverterStationXml.java index 6e249c9e722..da263b31ab7 100644 --- a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/VscConverterStationXml.java +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/VscConverterStationXml.java @@ -15,11 +15,13 @@ import javax.xml.stream.XMLStreamException; import java.util.Objects; +import static com.powsybl.iidm.xml.ConnectableXmlUtil.*; + /** * @author Geoffroy Jamgotchian * @author Mathieu Bague */ -class VscConverterStationXml extends AbstractConnectableXml { +class VscConverterStationXml extends AbstractSimpleIdentifiableXml { static final VscConverterStationXml INSTANCE = new VscConverterStationXml();