Skip to content

Commit efeef88

Browse files
committed
Refactor to remove code duplication
Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
1 parent 9303d45 commit efeef88

1 file changed

Lines changed: 7 additions & 20 deletions

File tree

network-area-diagram/src/main/java/com/powsybl/nad/svg/SvgWriter.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -219,25 +219,8 @@ private void insertSvgComponent(Injection injection, XMLStreamWriter writer) thr
219219
writer.writeStartElement(GROUP_ELEMENT_NAME);
220220
writer.writeAttribute(TRANSFORM_ATTRIBUTE, getTranslateString(injection.getIconOrigin(svgParameters.getInjectionCircleRadius())));
221221

222-
Result result = new SAXResult(new SvgContentHandlerToXMLStreamWriter(writer));
223222
String componentType = injection.getComponentType();
224-
writeStyleClasses(writer, componentLibrary.getComponentStyleClass(componentType).map(List::of).orElse(List.of()));
225-
226-
try {
227-
Transformer transformer = componentLibrary.getSvgTransformer();
228-
Map<String, List<Element>> subComponents = componentLibrary.getSvgElements(componentType);
229-
for (Map.Entry<String, List<Element>> scEntry : subComponents.entrySet()) {
230-
List<String> edgeStyleClasses = componentLibrary.getSubComponentStyleClass(componentType, scEntry.getKey())
231-
.map(List::of).orElse(List.of());
232-
writeStyleClasses(writer, edgeStyleClasses);
233-
for (Element element : scEntry.getValue()) {
234-
transformer.transform(new DOMSource(element), result);
235-
}
236-
}
237-
} catch (TransformerException e) {
238-
throw new PowsyblException("Cannot insert SVG for injection of type " + injection.getType(), e);
239-
}
240-
writer.writeEndElement();
223+
writeSvgComponent(writer, componentType, "Cannot insert SVG for injection of type " + injection.getType());
241224
}
242225

243226
private void drawComponentOnBranchEdgeMiddle(XMLStreamWriter writer, String componentType) throws XMLStreamException {
@@ -250,6 +233,10 @@ private void drawComponentOnBranchEdgeMiddle(XMLStreamWriter writer, String comp
250233
Point trans = new Point(-componentSize.width() / 2, -componentSize.height() / 2);
251234
writer.writeAttribute(TRANSFORM_ATTRIBUTE, getTranslateString(trans));
252235

236+
writeSvgComponent(writer, componentType, "Cannot insert SVG component of type " + componentType + " on branch edge middle");
237+
}
238+
239+
private void writeSvgComponent(XMLStreamWriter writer, String componentType, String errorMessage) throws XMLStreamException {
253240
Result result = new SAXResult(new SvgContentHandlerToXMLStreamWriter(writer));
254241
writeStyleClasses(writer, componentLibrary.getComponentStyleClass(componentType).map(List::of).orElse(List.of()));
255242

@@ -259,15 +246,15 @@ private void drawComponentOnBranchEdgeMiddle(XMLStreamWriter writer, String comp
259246
if (subComponents != null) {
260247
for (Map.Entry<String, List<Element>> scEntry : subComponents.entrySet()) {
261248
List<String> edgeStyleClasses = componentLibrary.getSubComponentStyleClass(componentType, scEntry.getKey())
262-
.map(List::of).orElse(List.of());
249+
.map(List::of).orElse(List.of());
263250
writeStyleClasses(writer, edgeStyleClasses);
264251
for (Element element : scEntry.getValue()) {
265252
transformer.transform(new DOMSource(element), result);
266253
}
267254
}
268255
}
269256
} catch (TransformerException e) {
270-
throw new PowsyblException("Cannot insert SVG component of type " + componentType + " on branch edge middle", e);
257+
throw new PowsyblException(errorMessage, e);
271258
}
272259
writer.writeEndElement();
273260
}

0 commit comments

Comments
 (0)