-
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 (to prevent hacks)
Signed-off-by: VEDELAGO MIORA <miora.ralambotiana@rte-france.com>
- Loading branch information
Showing
25 changed files
with
230 additions
and
109 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...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,79 @@ | ||
/** | ||
* 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.google.common.base.Supplier; | ||
import com.powsybl.commons.PowsyblException; | ||
import com.powsybl.commons.xml.XmlUtil; | ||
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 javax.xml.stream.XMLStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* @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 void readUntilEndRootElement(XMLStreamReader reader, XmlUtil.XmlEventHandler eventHandler) throws XMLStreamException { | ||
XmlUtil.readUntilEndElement(getRootElementName(), reader, eventHandler); | ||
} | ||
|
||
protected abstract void readRootElementAttributes(A adder, List<Consumer<T>> toApply, NetworkXmlReaderContext context); | ||
|
||
protected void readSubElements(String id, A adder, 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 void readElement(String id, A adder, List<Consumer<T>> toApply, NetworkXmlReaderContext context) throws XMLStreamException { | ||
readRootElementAttributes(adder, toApply, context); | ||
readSubElements(id, adder, toApply, context); | ||
} | ||
|
||
protected void apply(T identifiable, List<Consumer<T>> toApply) { | ||
toApply.forEach(consumer -> consumer.accept(identifiable)); | ||
} | ||
|
||
public final void read(Supplier<A> createAdder, Function<A, T> create, NetworkXmlReaderContext context) throws XMLStreamException { | ||
List<Consumer<T>> toApply = new ArrayList<>(); | ||
A adder = read(createAdder, toApply, context); | ||
T identifiable = create.apply(adder); | ||
apply(identifiable, toApply); | ||
} | ||
|
||
public final A read(Supplier<A> createAdder, List<Consumer<T>> toApply, NetworkXmlReaderContext context) throws XMLStreamException { | ||
A adder = createAdder.get(); | ||
read(adder, toApply, context); | ||
return adder; | ||
} | ||
|
||
private void read(A adder, List<Consumer<T>> toApply, NetworkXmlReaderContext context) throws XMLStreamException { | ||
String id = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, "id")); | ||
String name = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, "name")); | ||
adder.setId(id) | ||
.setName(name); | ||
IidmXmlUtil.runFromMinimumVersion(IidmXmlVersion.V_1_2, context, () -> { | ||
boolean fictitious = XmlUtil.readOptionalBoolAttribute(context.getReader(), "fictitious", false); | ||
adder.setFictitious(fictitious); | ||
}); | ||
readElement(id, adder, toApply, 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
62 changes: 62 additions & 0 deletions
62
.../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,62 @@ | ||
/** | ||
* 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.commons.xml.XmlUtil; | ||
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 javax.xml.stream.XMLStreamReader; | ||
|
||
/** | ||
* @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 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() + ">"); | ||
} | ||
} | ||
|
||
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); | ||
String id = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, "id")); | ||
String name = context.getAnonymizer().deanonymizeString(context.getReader().getAttributeValue(null, "name")); | ||
adder.setId(id) | ||
.setName(name); | ||
IidmXmlUtil.runFromMinimumVersion(IidmXmlVersion.V_1_2, context, () -> { | ||
boolean fictitious = XmlUtil.readOptionalBoolAttribute(context.getReader(), "fictitious", false); | ||
adder.setFictitious(fictitious); | ||
}); | ||
readElement(id, adder, 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
Oops, something went wrong.