-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring XML IIDM serialization classes (#2481)
Signed-off-by: VEDELAGO MIORA <miora.ralambotiana@rte-france.com>
- Loading branch information
Showing
25 changed files
with
208 additions
and
97 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractComplexIdentifiableXml.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <miora.ralambotiana at rte-france.com> | ||
*/ | ||
abstract class AbstractComplexIdentifiableXml<T extends Identifiable, A extends IdentifiableAdder<A>, P extends Identifiable> extends AbstractIdentifiableXml<T, A, P> { | ||
|
||
protected abstract void readRootElementAttributes(A adder, List<Consumer<T>> 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<Consumer<T>> 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<Consumer<T>> toApply, NetworkXmlReaderContext context) throws XMLStreamException; | ||
|
||
@Override | ||
public final void read(P parent, NetworkXmlReaderContext context) throws XMLStreamException { | ||
List<Consumer<T>> 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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
.../iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/AbstractSimpleIdentifiableXml.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <miora.ralambotiana at rte-france.com> | ||
*/ | ||
abstract class AbstractSimpleIdentifiableXml<T extends Identifiable, A extends IdentifiableAdder<A>, P extends Identifiable> extends AbstractIdentifiableXml<T, A, P> { | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.