diff --git a/pom.xml b/pom.xml index febd22022..fad18b7a9 100644 --- a/pom.xml +++ b/pom.xml @@ -37,19 +37,36 @@ sct-coverage/** ../sct-coverage/target/site/jacoco-aggregate/jacoco.xml ${basedir}/${aggregate.report.dir} - 0.9.1 + 0.12.0 0.0.4 3.4.1 3.2.1 5.9.0 + 1.4.5 + 3.22.0 + 1.18.24 + + + org.lfenergy.compas.core + scl2007b4 + + + org.lfenergy.compas.core + scl-extension + + + ch.qos.logback + logback-classic + + org.projectlombok lombok - 1.18.24 + ${lombok.version} org.junit.jupiter @@ -72,12 +89,12 @@ org.lfenergy.compas.core scl2007b4 - 0.2.1 + ${compas-core.version} org.lfenergy.compas.core scl-extension - ${scl-extension.version} + ${compas-core.version} org.lfenergy.compas.xsd @@ -87,24 +104,15 @@ ch.qos.logback logback-classic - 1.2.6 + ${logback-classic.version} + + + org.assertj + assertj-core + ${assertj.version} - - - org.lfenergy.compas.core - scl2007b4 - - - org.lfenergy.compas.core - scl-extension - - - ch.qos.logback - logback-classic - - sct-commons sct-coverage @@ -123,12 +131,16 @@ org.lfenergy.compas.core scl-extension - ${scl-extension.version} + ${compas-core.version} org.apache.maven.plugins maven-compiler-plugin 3.8.1 + + ${java.version} + ${java.version} + org.jacoco diff --git a/sct-app/pom.xml b/sct-app/pom.xml index bb9d20448..f2ea2dca7 100644 --- a/sct-app/pom.xml +++ b/sct-app/pom.xml @@ -54,16 +54,9 @@ junit-jupiter-engine test - - org.mockito - mockito-core - 3.6.28 - test - org.assertj assertj-core - 3.23.1 @@ -71,10 +64,6 @@ org.apache.maven.plugins maven-compiler-plugin - - 17 - 17 - org.apache.maven.plugins @@ -102,6 +91,7 @@ org.apache.maven.plugins maven-dependency-plugin + 3.3.0 unpack @@ -120,11 +110,6 @@ org.lfenergy.compas.core scl-extension - - org.lfenergy.compas.core - scl-extension - 0.8.0 - ${project.build.directory} diff --git a/sct-app/src/test/resources/std_1.xml b/sct-app/src/test/resources/std_1.xml index a844f0fc6..de295e154 100644 --- a/sct-app/src/test/resources/std_1.xml +++ b/sct-app/src/test/resources/std_1.xml @@ -34,7 +34,7 @@ - + @@ -341,4 +341,4 @@ Y - \ No newline at end of file + diff --git a/sct-commons/pom.xml b/sct-commons/pom.xml index 3a1b6c6ce..7ab70c5a8 100644 --- a/sct-commons/pom.xml +++ b/sct-commons/pom.xml @@ -20,8 +20,7 @@ ${basedir}/${aggregate.report.dir} **/scl2007b4/**/* - 3.22.0 - 5.6 + 5.7.1 4.9.0 @@ -91,11 +90,6 @@ ${mockito.version} test - - com.fasterxml.jackson.dataformat - jackson-dataformat-yaml - 2.12.3 - jakarta.annotation jakarta.annotation-api @@ -110,7 +104,6 @@ org.assertj assertj-core - ${assertJ.version} test @@ -125,10 +118,6 @@ org.apache.maven.plugins maven-compiler-plugin - - 17 - 17 - org.apache.maven.plugins @@ -161,6 +150,7 @@ org.apache.maven.plugins maven-dependency-plugin + 3.3.0 unpack @@ -179,11 +169,6 @@ org.lfenergy.compas.core scl-extension - - org.lfenergy.compas.core - scl-extension - 0.8.0 - ${project.build.directory} diff --git a/sct-commons/src/main/resources/jaxb-marshaller-config.yml b/sct-commons/src/main/resources/jaxb-marshaller-config.yml deleted file mode 100644 index a9a107021..000000000 --- a/sct-commons/src/main/resources/jaxb-marshaller-config.yml +++ /dev/null @@ -1,13 +0,0 @@ -# SPDX-FileCopyrightText: 2021 RTE FRANCE -# -# SPDX-License-Identifier: Apache-2.0 - ---- -compas: - scl: - # XsdPath currently is a reference to a XSD on the classpath, searched from the root. - # ContextPath is the package where the ObjectFactory class can be found, mostly generated there. - schemas: - - xsdPath: "target/xsd/SCL2007B4/SCL.xsd" - namespace: "http://www.iec.ch/61850/2003/SCL" - contextPath: "org.lfenergy.compas.scl2007b4.model" diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/dto/IedDTOTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/dto/IedDTOTest.java index 3c827766f..d0e3910e1 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/dto/IedDTOTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/dto/IedDTOTest.java @@ -45,4 +45,4 @@ void testAddLDevice(){ assertTrue(iedDTO.getLDeviceDTO(DTO.HOLDER_LD_INST).isPresent()); } -} \ No newline at end of file +} diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/SclServiceTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/SclServiceTest.java index 8c825160a..e4f28d5e4 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/SclServiceTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/SclServiceTest.java @@ -14,6 +14,7 @@ import org.lfenergy.compas.sct.commons.dto.*; import org.lfenergy.compas.sct.commons.exception.ScdException; import org.lfenergy.compas.sct.commons.scl.ied.*; +import org.lfenergy.compas.sct.commons.testhelpers.MarshallerWrapper; import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller; import java.util.*; @@ -26,7 +27,6 @@ import static org.lfenergy.compas.sct.commons.testhelpers.DataTypeUtils.createDa; import static org.lfenergy.compas.sct.commons.testhelpers.DataTypeUtils.createDo; import static org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller.assertIsMarshallable; -import static org.lfenergy.compas.sct.commons.testhelpers.marshaller.SclTestMarshaller.createWrapper; import static org.lfenergy.compas.sct.commons.util.PrivateEnum.COMPAS_SCL_FILE_TYPE; class SclServiceTest { @@ -700,8 +700,8 @@ void testImportSTDElementsInSCD() throws Exception { void testImportSTDElementsInSCD_with_Multiple_STD() throws Exception { SCL scd = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/scd_lnode_with_many_compas_icdheader.xml"); SCL std0 = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/std.xml"); - SCL std1 = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/std_SITESITE1GTW1.xml"); - SCL std2 = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/std_SITESITE1GTW2.xml"); + SCL std1 = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/std_SITESITE1SCU1.xml"); + SCL std2 = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/std_SITESITE1SCU2.xml"); SclRootAdapter scdRootAdapter = new SclRootAdapter(scd); SclRootAdapter expectedScdAdapter = assertDoesNotThrow( () -> SclService.importSTDElementsInSCD( @@ -835,11 +835,11 @@ void updateLDeviceStatus_shouldReturnReportWithError_MissingRequiredObject(Strin // Given assertTrue(getLDeviceStatusValue(scl, "IedName1", "LDSUIED").isPresent()); assertEquals("off", getLDeviceStatusValue(scl, "IedName1", "LDSUIED").get().getValue()); - String before = createWrapper().marshall(scl); + String before = MarshallerWrapper.marshall(scl); // When SclReport sclReport = SclService.updateLDeviceStatus(scl); // Then - String after = createWrapper().marshall(sclReport.getSclRootAdapter().getCurrentElem()); + String after = MarshallerWrapper.marshall(sclReport.getSclRootAdapter().getCurrentElem()); assertFalse(sclReport.isSuccess()); assertThat(sclReport.getSclReportItems()) .hasSize(1) @@ -888,11 +888,11 @@ void updateLDeviceStatus_shouldReturnReportWithError_WhenLDeviceStatusActiveOrUn assertEquals("off", getLDeviceStatusValue(scl, "IedName1", "LDSUIED").get().getValue()); assertEquals("on", getLDeviceStatusValue(scl, "IedName2", "LDSUIED").get().getValue()); assertFalse(getLDeviceStatusValue(scl, "IedName3", "LDSUIED").isPresent()); - String before = createWrapper().marshall(scl); + String before = MarshallerWrapper.marshall(scl); // When SclReport sclReport = SclService.updateLDeviceStatus(scl); // Then - String after = createWrapper().marshall(sclReport.getSclRootAdapter().getCurrentElem()); + String after = MarshallerWrapper.marshall(sclReport.getSclRootAdapter().getCurrentElem()); assertFalse(sclReport.isSuccess()); assertThat(sclReport.getSclReportItems()) .hasSize(3) diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/com/ConnectedAPAdapterTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/com/ConnectedAPAdapterTest.java index c868c8a82..01229afbe 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/com/ConnectedAPAdapterTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/com/ConnectedAPAdapterTest.java @@ -45,7 +45,7 @@ void testAmChildElementRef() { } @Test - void addPrivate() throws Exception { + void addPrivate() { ConnectedAPAdapter connectedAPAdapter = subNetworkAdapter.getConnectedAPAdapter(DTO.HOLDER_IED_NAME, DTO.AP_NAME); TPrivate tPrivate = new TPrivate(); tPrivate.setType("Private Type"); @@ -107,4 +107,4 @@ void elementXPath(String iedName, String apName, String message) { assertThat(elementXPath).isEqualTo(message); } -} \ No newline at end of file +} diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/dtt/AbstractDTTLevel.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/dtt/AbstractDTTLevel.java index 0795b4134..a358d3c84 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/dtt/AbstractDTTLevel.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/dtt/AbstractDTTLevel.java @@ -4,12 +4,11 @@ package org.lfenergy.compas.sct.commons.scl.dtt; -import org.apache.commons.io.IOUtils; import org.lfenergy.compas.scl2007b4.model.SCL; import org.lfenergy.compas.scl2007b4.model.TDataTypeTemplates; -import org.lfenergy.compas.sct.commons.testhelpers.MarshallerWrapper; import org.lfenergy.compas.sct.commons.scl.SclElementAdapter; import org.lfenergy.compas.sct.commons.scl.SclRootAdapter; +import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller; import org.mockito.Mockito; @@ -26,7 +25,7 @@ public void init(){ } protected static DataTypeTemplateAdapter initDttAdapterFromFile(String fileName) throws Exception { - SCL scd = AbstractDTTLevel.getSCLFromFile(fileName); + SCL scd = SclTestMarshaller.getSCLFromFile(fileName); SclRootAdapter sclRootAdapter = new SclRootAdapter(scd); return new DataTypeTemplateAdapter( sclRootAdapter, @@ -43,15 +42,4 @@ protected SclElementAdapter getMockedSclParentAdapter(){ } protected abstract void completeInit() ; - public static SCL getSCLFromFile(String filename) throws Exception { - MarshallerWrapper marshallerWrapper = createWrapper(); - byte[] rawXml = IOUtils.resourceToByteArray(filename); - return marshallerWrapper.unmarshall(rawXml,SCL.class); - } - - public static MarshallerWrapper createWrapper() { - return MarshallerWrapper.builder() - .withProperties("classpath:scl_schema.yml") - .build(); - } } diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/dtt/DataTypeTemplateAdapterTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/dtt/DataTypeTemplateAdapterTest.java index 7b8ae510e..c825b14a0 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/dtt/DataTypeTemplateAdapterTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/dtt/DataTypeTemplateAdapterTest.java @@ -340,9 +340,8 @@ void testImportLNodeType() throws Exception { assertTrue(nbLNodeType < rcvDttAdapter.getLNodeTypeAdapters().size()); assertFalse(mapOldNewId.isEmpty()); - MarshallerWrapper marshallerWrapper = AbstractDTTLevel.createWrapper(); - System.out.println(marshallerWrapper.marshall(rcvDttAdapter.getParentAdapter().getCurrentElem())); - System.out.println(marshallerWrapper.marshall(prvDttAdapter.getParentAdapter().getCurrentElem())); + System.out.println(MarshallerWrapper.marshall(rcvDttAdapter.getParentAdapter().getCurrentElem())); + System.out.println(MarshallerWrapper.marshall(prvDttAdapter.getParentAdapter().getCurrentElem())); } @Test @@ -358,9 +357,8 @@ void testImportDOType() throws Exception { rcvDttAdapter.importDOType("IEDName",prvDttAdapter); assertTrue(nbDOType < rcvDttAdapter.getDOTypeAdapters().size()); - MarshallerWrapper marshallerWrapper = AbstractDTTLevel.createWrapper(); - System.out.println(marshallerWrapper.marshall(rcvDttAdapter.getParentAdapter().getCurrentElem())); - System.out.println(marshallerWrapper.marshall(prvDttAdapter.getParentAdapter().getCurrentElem())); + System.out.println(MarshallerWrapper.marshall(rcvDttAdapter.getParentAdapter().getCurrentElem())); + System.out.println(MarshallerWrapper.marshall(prvDttAdapter.getParentAdapter().getCurrentElem())); } @Test @@ -375,9 +373,8 @@ void testImportDAType() throws Exception { rcvDttAdapter.importDAType("IEDName",prvDttAdapter); assertTrue(nbDAType < rcvDttAdapter.getDATypeAdapters().size()); - MarshallerWrapper marshallerWrapper = AbstractDTTLevel.createWrapper(); - System.out.println(marshallerWrapper.marshall(prvDttAdapter.getParentAdapter().getCurrentElem())); - System.out.println(marshallerWrapper.marshall(rcvDttAdapter.getParentAdapter().getCurrentElem())); + System.out.println(MarshallerWrapper.marshall(prvDttAdapter.getParentAdapter().getCurrentElem())); + System.out.println(MarshallerWrapper.marshall(rcvDttAdapter.getParentAdapter().getCurrentElem())); } @ParameterizedTest diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/DAITrackerTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/DAITrackerTest.java index 143c05c42..ae38fdb9f 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/DAITrackerTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/DAITrackerTest.java @@ -12,7 +12,6 @@ import org.lfenergy.compas.sct.commons.scl.SclRootAdapter; import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller; -import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; class DAITrackerTest { diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/IEDAdapterTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/IEDAdapterTest.java index 31fe9dc36..e486660bd 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/IEDAdapterTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/IEDAdapterTest.java @@ -13,7 +13,6 @@ import org.lfenergy.compas.sct.commons.exception.ScdException; import org.lfenergy.compas.sct.commons.scl.ObjectReference; import org.lfenergy.compas.sct.commons.scl.SclRootAdapter; -import org.lfenergy.compas.sct.commons.scl.com.SubNetworkAdapter; import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller; import org.mockito.Mockito; diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/LNAdapterTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/LNAdapterTest.java index aa8871e49..20643753c 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/LNAdapterTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/ied/LNAdapterTest.java @@ -12,7 +12,6 @@ import org.lfenergy.compas.sct.commons.dto.*; import org.lfenergy.compas.sct.commons.exception.ScdException; import org.lfenergy.compas.sct.commons.scl.SclRootAdapter; -import org.lfenergy.compas.sct.commons.scl.dtt.AbstractDTTLevel; import org.lfenergy.compas.sct.commons.testhelpers.MarshallerWrapper; import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller; import org.mockito.Mockito; @@ -363,7 +362,6 @@ void testUpdateDAI() throws Exception { tVal.setValue("newValue"); rDtt.setDaiValues(List.of(tVal)); assertDoesNotThrow(() -> lnAdapter.updateDAI(rDtt)); - MarshallerWrapper marshallerWrapper = AbstractDTTLevel.createWrapper(); lDeviceAdapter = assertDoesNotThrow(() -> iAdapter.findLDeviceAdapterByLdInst("LD_INS2").get()); AbstractLNAdapter lnAdapter2 = AbstractLNAdapter.builder() @@ -375,7 +373,7 @@ void testUpdateDAI() throws Exception { assertTrue(rDtt.isUpdatable()); assertDoesNotThrow(() -> lnAdapter2.updateDAI(rDtt)); - System.out.println(marshallerWrapper.marshall(scd)); + System.out.println(MarshallerWrapper.marshall(scd)); } diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/sstation/SubstationAdapterTest.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/sstation/SubstationAdapterTest.java index 0ea8e5143..e54e39756 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/sstation/SubstationAdapterTest.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/scl/sstation/SubstationAdapterTest.java @@ -93,4 +93,4 @@ void getIedAndLDeviceNamesForLN0FromLNode_shouldReturnListOf1Pair_WhenLNodeConta assertThat(iedNameLdInstList).containsExactly(Pair.of("IedName1", "LDSUIED")); } -} \ No newline at end of file +} diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/MarshallerWrapper.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/MarshallerWrapper.java index 3e61a8b87..ed0087732 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/MarshallerWrapper.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/MarshallerWrapper.java @@ -4,39 +4,49 @@ package org.lfenergy.compas.sct.commons.testhelpers; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; import org.lfenergy.compas.core.commons.exception.CompasErrorCode; import org.lfenergy.compas.core.commons.exception.CompasException; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import javax.xml.XMLConstants; +import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; +import java.net.URL; +import java.util.stream.Stream; @Slf4j +@AllArgsConstructor public class MarshallerWrapper { + private static MarshallerWrapper singleton; + private final Unmarshaller unmarshaller; private final Marshaller marshaller; - protected MarshallerWrapper(Unmarshaller unmarshaller, Marshaller marshaller) { - this.unmarshaller = unmarshaller; - this.marshaller = marshaller; - } - - public static SclMarshallerBuilder builder(){ - return new SclMarshallerBuilder(); - } - - public String marshall(final T obj) { + public static String marshall(final Object obj) { try { StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); - marshaller.marshal(obj, result); + getInstance().marshaller.marshal(obj, result); return sw.toString(); } catch (JAXBException exp) { @@ -46,23 +56,74 @@ public String marshall(final T obj) { } } - public T unmarshall(final byte[] xml, Class cls) { + public static T unmarshall(final byte[] xml, Class cls) { ByteArrayInputStream input = new ByteArrayInputStream(xml); return unmarshall(input, cls); } - public T unmarshall(final InputStream xml, Class cls) { + public static T unmarshall(final InputStream xml, Class cls) { try { - Object result = unmarshaller.unmarshal(new StreamSource(xml)); + Object result = getInstance().unmarshaller.unmarshal(new StreamSource(xml)); if (!result.getClass().isAssignableFrom(cls)) { throw new CompasException(CompasErrorCode.UNMARSHAL_ERROR_CODE, - "Error unmarshalling to the Class. Invalid class"); + "Error unmarshalling to the Class. Invalid class"); } return cls.cast(result); } catch (JAXBException exp) { String message = String.format("Error unmarshalling to the Class: %s", exp.getLocalizedMessage()); log.error(message, exp); - throw new CompasException(CompasErrorCode.UNMARSHAL_ERROR_CODE, message); + throw new CompasException(CompasErrorCode.UNMARSHAL_ERROR_CODE, message, exp); + } + } + + private static MarshallerWrapper getInstance() { + if (singleton == null) { + try { + singleton = createMarshallerWrapper(); + } catch (JAXBException | SAXException | ParserConfigurationException exp) { + var message = "Error creating JAXB Marshaller and Unmarshaller."; + log.error(message, exp); + throw new CompasException(CompasErrorCode.CREATION_ERROR_CODE, message); + } + } + return singleton; + } + + private static MarshallerWrapper createMarshallerWrapper() throws JAXBException, SAXException, ParserConfigurationException { + // Create marshaller and unmarshaller + JAXBContext jaxbContext = JAXBContext.newInstance("org.lfenergy.compas.scl2007b4.model"); + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); + jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + // Setup schema validation + Schema schema = loadSchema(); + jaxbMarshaller.setSchema(schema); + jaxbUnmarshaller.setSchema(schema); + + return new MarshallerWrapper(jaxbUnmarshaller, jaxbMarshaller); + } + + private static Schema loadSchema() throws SAXException, ParserConfigurationException { + XMLReader xmlReader = SAXParserFactory.newDefaultNSInstance().newSAXParser().getXMLReader(); + Source[] schemaSources = Stream.of("/xsd/SCL2007B4/SCL.xsd", "/xsd/SCL_CoMPAS.xsd") + .map(xsdPath -> toSchemaSource(xsdPath, xmlReader)) + .toArray(Source[]::new); + SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + return schemaFactory.newSchema(schemaSources); + } + + private static Source toSchemaSource(String path, XMLReader xmlReader) { + URL url; + try { + url = IOUtils.resourceToURL(path); + } catch (IOException e) { + var message = "Error loading XML schema : " + path; + log.error(message, e); + throw new CompasException(CompasErrorCode.CREATION_ERROR_CODE, message); } + InputSource inputSource = new InputSource(url.toString()); + return new SAXSource(xmlReader, inputSource); } + } diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/SclMarshallerBuilder.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/SclMarshallerBuilder.java deleted file mode 100644 index 8d61a65c0..000000000 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/SclMarshallerBuilder.java +++ /dev/null @@ -1,206 +0,0 @@ -// SPDX-FileCopyrightText: 2021 RTE FRANCE -// -// SPDX-License-Identifier: Apache-2.0 -package org.lfenergy.compas.sct.commons.testhelpers; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.JsonNodeType; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.io.IOUtils; -import org.lfenergy.compas.core.commons.exception.CompasErrorCode; -import org.lfenergy.compas.core.commons.exception.CompasException; -import org.xml.sax.SAXException; - -import javax.xml.XMLConstants; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.transform.stream.StreamSource; -import javax.xml.validation.SchemaFactory; -import java.io.IOException; -import java.io.StringReader; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - - -@Slf4j -public class SclMarshallerBuilder { - - private static final String COMPAS_SCL_SCHEMAS_JSONPATH = "/compas/scl/schemas"; - private static final String CONTEXT_PATH_PROP = "contextPath"; - private static final String XSD_PATH_PROP = "xsdPath"; - private static final String NAMESPACE_PROP = "namespace"; - - // Path to the YAML File containing the schema properties. - private String yamlFilePath; - private Map jaxbProperties; - - public SclMarshallerBuilder withProperties(String yamlFilePath) { - this.yamlFilePath = yamlFilePath; - return this; - } - - - public URL getResource(String filePath) throws IOException { - final String clsPathIndicator = "classpath:"; - if(filePath.startsWith(clsPathIndicator)){ - String classPathRes = filePath.substring(clsPathIndicator.length()); - return IOUtils.resourceToURL("/" + classPathRes); - } - Path path = Paths.get(filePath).toAbsolutePath(); - if(!Files.exists(path)) { - throw new IOException(path + ": No such file or directory"); - } - return path.toUri().toURL(); - } - - - private List getSchemaConfigs() { - if (yamlFilePath == null || yamlFilePath.isBlank()) { - throw new CompasException(CompasErrorCode.CONFIGURATION_ERROR_CODE, - "No configuration file configured (yamlFilePath)"); - } - - URL source; - try { - source = getResource(yamlFilePath); - } catch (IOException e){ - String message = String.format("Resource %s not found", yamlFilePath); - log.error(message, e); - throw new CompasException(CompasErrorCode.RESOURCE_NOT_FOUND_ERROR_CODE,message); - } - - List schemaConfigs = new ArrayList<>(); - try { - ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); - JsonNode jsonNode = objectMapper.readTree(source); - - JsonNode pathsNode = jsonNode.at(COMPAS_SCL_SCHEMAS_JSONPATH); - if (pathsNode != null && pathsNode.getNodeType() == JsonNodeType.ARRAY) { - Iterable nodes = pathsNode::elements; - // Walk through the schemas and check if all needed parameters are filled. - nodes.forEach(node -> { - if (node.has(XSD_PATH_PROP) && node.has(NAMESPACE_PROP) && node.has(CONTEXT_PATH_PROP)) { - schemaConfigs.add( - new SchemaConfig(node.get(XSD_PATH_PROP).textValue(), - node.get(NAMESPACE_PROP).textValue(), - node.get(CONTEXT_PATH_PROP).textValue())); - } else { - throw new CompasException(CompasErrorCode.PROPERTY_ERROR_ERROR_CODE, - String.format("One of the properties (%s, %s, %s) has no value", - XSD_PATH_PROP, NAMESPACE_PROP, CONTEXT_PATH_PROP)); - } - }); - } else { - throw new CompasException(CompasErrorCode.CONFIGURATION_ERROR_CODE, - String.format("Configuration for marshaller (%s) didn't contain the path %s", - yamlFilePath, COMPAS_SCL_SCHEMAS_JSONPATH)); - } - } catch (IOException exp) { - var message = "I/O Error reading YAML File"; - log.error(message, exp); - throw new CompasException(CompasErrorCode.INVALID_YML_ERROR_CODE, message); - } - - return schemaConfigs; - } - - private List getContextPaths(List schemaConfigs) { - // Convert the SchemaConfig List to a List containing only the ContextPaths. - return schemaConfigs.stream() - .map(SchemaConfig::getContextPath) - .collect(Collectors.toList()); - } - - private String getImportStatements(List schemaConfigs) { - // Convert the SchemaConfig List to a String containing the Import Statements sued to combine multiple - // XSD Schemas. - return schemaConfigs.stream() - .map(schemaConfig -> - "\n") - .collect(Collectors.joining()); - } - - public MarshallerWrapper build() { - try { - // Create the JAXB Context with the configured context paths. The list is separated by a colon - // as needed by the JAxbContext. - var schemaConfigs = getSchemaConfigs(); - var contextPaths = String.join(":", getContextPaths(schemaConfigs)); - - var jaxbContext = JAXBContext.newInstance(contextPaths); - var jaxbMarshaller = jaxbContext.createMarshaller(); - var jaxbUnmarshaller = jaxbContext.createUnmarshaller(); - - // Setup schema validator - var factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - // To make load of all different type of schemas and also make imports/includes in these schemas - // work the solution is to make a combined XSD Schema containing all other schemas as import. - // Import is used, because the schemas can have different namespaces. - var combinedXsdSchema = - "\n" - + "\n" - + getImportStatements(schemaConfigs) - + ""; - var schema = factory.newSchema( - new StreamSource(new StringReader(combinedXsdSchema), "topSchema") - ); - jaxbMarshaller.setSchema(schema); - jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - jaxbUnmarshaller.setSchema(schema); - - return new MarshallerWrapper(jaxbUnmarshaller,jaxbMarshaller); - - } catch (JAXBException | SAXException exp) { - var message = "Error creating JAXB Marshaller and Unmarshaller."; - log.error(message, exp); - throw new CompasException(CompasErrorCode.CREATION_ERROR_CODE, message); - } - } - - /** - * POJO Class to hold all the schema configuration, so that we only need to walk through the JSON Content once. - */ - protected class SchemaConfig { - private final String xsdPath; - private final String namespace; - private final String contextPath; - - public SchemaConfig(String xsdPath, String namespace, String contextPath) { - // Convert the XSD Path to a URL, that will be used in the import statement. - // If the XSD Path is not correct loading will fail. - this.xsdPath = xsdPath; - this.namespace = namespace; - this.contextPath = contextPath; - } - - public String getXsdPath() { - try { - return getResource(xsdPath).getPath(); - } catch (IOException e) { - log.error(e.getMessage(),e); - throw new CompasException(CompasErrorCode.RESOURCE_NOT_FOUND_ERROR_CODE,e.getMessage()); - } - } - - public String getNamespace() { - return namespace; - } - - public String getContextPath() { - return contextPath; - } - } -} - - diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/SclTestMarshaller.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/SclTestMarshaller.java index ac80d9ddb..185f6a868 100644 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/SclTestMarshaller.java +++ b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/SclTestMarshaller.java @@ -12,19 +12,12 @@ public class SclTestMarshaller { public static SCL getSCLFromFile(String filename) throws Exception { - MarshallerWrapper marshallerWrapper = createWrapper(); byte[] rawXml = IOUtils.resourceToByteArray(filename); - return marshallerWrapper.unmarshall(rawXml,SCL.class); + return MarshallerWrapper.unmarshall(rawXml, SCL.class); } public static String assertIsMarshallable(SCL scl) { - MarshallerWrapper marshallerWrapper = createWrapper(); - return assertDoesNotThrow(() -> marshallerWrapper.marshall(scl)); + return assertDoesNotThrow(() -> MarshallerWrapper.marshall(scl)); } - private static MarshallerWrapper createWrapper() { - return MarshallerWrapper.builder() - .withProperties("classpath:scl_schema.yml") - .build(); - } } diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/Employee.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/Employee.java deleted file mode 100644 index ed12ba74f..000000000 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/Employee.java +++ /dev/null @@ -1,313 +0,0 @@ -// SPDX-FileCopyrightText: 2021 RTE FRANCE -// -// SPDX-License-Identifier: Apache-2.0 - -package org.lfenergy.compas.sct.commons.testhelpers.marshaller; - -import java.util.ArrayList; -import java.util.List; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlType; - - -/** - *

Java class for anonymous complex type. - * - *

The following schema fragment specifies the expected content contained within this class. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "", propOrder = { - "name", - "address", - "assestsAllocated", - "id" -}) -@XmlRootElement(name = "employee") -public class Employee { - - @XmlElement(required = true) - protected String name; - @XmlElement(required = true) - protected Employee.Address address; - protected List assestsAllocated; - protected byte id; - - /** - * Gets the value of the name property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getName() { - return name; - } - - /** - * Sets the value of the name property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setName(String value) { - this.name = value; - } - - /** - * Gets the value of the address property. - * - * @return - * possible object is - * {@link Employee.Address } - * - */ - public Employee.Address getAddress() { - return address; - } - - /** - * Sets the value of the address property. - * - * @param value - * allowed object is - * {@link Employee.Address } - * - */ - public void setAddress(Employee.Address value) { - this.address = value; - } - - /** - * Gets the value of the assestsAllocated property. - * - *

- * This accessor method returns a reference to the live list, - * not a snapshot. Therefore any modification you make to the - * returned list will be present inside the JAXB object. - * This is why there is not a set method for the assestsAllocated property. - * - *

- * For example, to add a new item, do as follows: - * getAssestsAllocated().add(newItem); - * - * - *

- * Objects of the following type(s) are allowed in the list - * {@link String } - * - * - */ - public List getAssestsAllocated() { - if (assestsAllocated == null) { - assestsAllocated = new ArrayList(); - } - return this.assestsAllocated; - } - - /** - * Gets the value of the id property. - * - */ - public byte getId() { - return id; - } - - /** - * Sets the value of the id property. - * - */ - public void setId(byte value) { - this.id = value; - } - - - /** - *

Java class for anonymous complex type. - * - *

The following schema fragment specifies the expected content contained within this class. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - */ - @XmlAccessorType(XmlAccessType.FIELD) - @XmlType(name = "", propOrder = { - "addressLine1", - "addressLine2", - "country", - "state", - "zip" - }) - public static class Address { - - @XmlElement(required = true) - protected String addressLine1; - @XmlElement(required = true) - protected String addressLine2; - @XmlElement(required = true) - protected String country; - @XmlElement(required = true) - protected String state; - protected short zip; - - /** - * Gets the value of the addressLine1 property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getAddressLine1() { - return addressLine1; - } - - /** - * Sets the value of the addressLine1 property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setAddressLine1(String value) { - this.addressLine1 = value; - } - - /** - * Gets the value of the addressLine2 property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getAddressLine2() { - return addressLine2; - } - - /** - * Sets the value of the addressLine2 property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setAddressLine2(String value) { - this.addressLine2 = value; - } - - /** - * Gets the value of the country property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getCountry() { - return country; - } - - /** - * Sets the value of the country property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setCountry(String value) { - this.country = value; - } - - /** - * Gets the value of the state property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getState() { - return state; - } - - /** - * Sets the value of the state property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setState(String value) { - this.state = value; - } - - /** - * Gets the value of the zip property. - * - */ - public short getZip() { - return zip; - } - - /** - * Sets the value of the zip property. - * - */ - public void setZip(short value) { - this.zip = value; - } - } - -} diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/MarshallerWrapper.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/MarshallerWrapper.java deleted file mode 100644 index 631309c34..000000000 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/MarshallerWrapper.java +++ /dev/null @@ -1,68 +0,0 @@ -// SPDX-FileCopyrightText: 2021 RTE FRANCE -// -// SPDX-License-Identifier: Apache-2.0 - -package org.lfenergy.compas.sct.commons.testhelpers.marshaller; - -import lombok.extern.slf4j.Slf4j; -import org.lfenergy.compas.core.commons.exception.CompasErrorCode; -import org.lfenergy.compas.core.commons.exception.CompasException; - -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; -import javax.xml.transform.Result; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.stream.StreamSource; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.StringWriter; - -@Slf4j -public class MarshallerWrapper { - private final Unmarshaller unmarshaller; - private final Marshaller marshaller; - - protected MarshallerWrapper(Unmarshaller unmarshaller, Marshaller marshaller) { - this.unmarshaller = unmarshaller; - this.marshaller = marshaller; - } - - public static SclMarshallerBuilder builder(){ - return new SclMarshallerBuilder(); - } - - public String marshall(final T obj) { - try { - StringWriter sw = new StringWriter(); - Result result = new StreamResult(sw); - marshaller.marshal(obj, result); - - return sw.toString(); - } catch (JAXBException exp) { - String message = String.format("Error marshalling the Class: %s", exp); - log.error(message); - throw new CompasException(CompasErrorCode.MARSHAL_ERROR_CODE, message); - } - } - - public T unmarshall(final byte[] xml, Class cls) { - ByteArrayInputStream input = new ByteArrayInputStream(xml); - return unmarshall(input, cls); - } - - public T unmarshall(final InputStream xml, Class cls) { - try { - Object result = unmarshaller.unmarshal(new StreamSource(xml)); - if (!result.getClass().isAssignableFrom(cls)) { - throw new CompasException(CompasErrorCode.UNMARSHAL_ERROR_CODE, - "Error unmarshalling to the Class. Invalid class"); - } - return cls.cast(result); - } catch (JAXBException exp) { - String message = String.format("Error unmarshalling to the Class: %s", exp.getLocalizedMessage()); - log.error(message); - throw new CompasException(CompasErrorCode.UNMARSHAL_ERROR_CODE, message); - } - } -} diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/ObjectFactory.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/ObjectFactory.java deleted file mode 100644 index 2ad2716cf..000000000 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/ObjectFactory.java +++ /dev/null @@ -1,51 +0,0 @@ -// SPDX-FileCopyrightText: 2021 RTE FRANCE -// -// SPDX-License-Identifier: Apache-2.0 - -package org.lfenergy.compas.sct.commons.testhelpers.marshaller; - -import javax.xml.bind.annotation.XmlRegistry; - - -/** - * This object contains factory methods for each - * Java content interface and Java element interface - * generated in the com.javacodegeeks.examples.xjc package. - *

An ObjectFactory allows you to programatically - * construct new instances of the Java representation - * for XML content. The Java representation of XML - * content can consist of schema derived interfaces - * and classes representing the binding of schema - * type definitions, element declarations and model - * groups. Factory methods for each of these are - * provided in this class. - * - */ -@XmlRegistry -public class ObjectFactory { - - - /** - * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.javacodegeeks.examples.xjc - * - */ - public ObjectFactory() { - } - - /** - * Create an instance of {@link Employee } - * - */ - public Employee createEmployee() { - return new Employee(); - } - - /** - * Create an instance of {@link Employee.Address } - * - */ - public Employee.Address createEmployeeAddress() { - return new Employee.Address(); - } - -} diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/SclMarshallerBuilder.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/SclMarshallerBuilder.java deleted file mode 100644 index 4f71c9a9c..000000000 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/SclMarshallerBuilder.java +++ /dev/null @@ -1,206 +0,0 @@ -// SPDX-FileCopyrightText: 2021 RTE FRANCE -// -// SPDX-License-Identifier: Apache-2.0 -package org.lfenergy.compas.sct.commons.testhelpers.marshaller; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.JsonNodeType; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.io.IOUtils; -import org.lfenergy.compas.core.commons.exception.CompasErrorCode; -import org.lfenergy.compas.core.commons.exception.CompasException; -import org.xml.sax.SAXException; - -import javax.xml.XMLConstants; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.transform.stream.StreamSource; -import javax.xml.validation.SchemaFactory; -import java.io.IOException; -import java.io.StringReader; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - - -@Slf4j -public class SclMarshallerBuilder { - - private static final String COMPAS_SCL_SCHEMAS_JSONPATH = "/compas/scl/schemas"; - private static final String CONTEXT_PATH_PROP = "contextPath"; - private static final String XSD_PATH_PROP = "xsdPath"; - private static final String NAMESPACE_PROP = "namespace"; - - // Path to the YAML File containing the schema properties. - private String yamlFilePath; - private Map jaxbProperties; - - public SclMarshallerBuilder withProperties(String yamlFilePath) { - this.yamlFilePath = yamlFilePath; - return this; - } - - - public URL getResource(String filePath) throws IOException { - final String clsPathIndicator = "classpath:"; - if(filePath.startsWith(clsPathIndicator)){ - String classPathRes = filePath.substring(clsPathIndicator.length()); - return IOUtils.resourceToURL("/" + classPathRes); - } - Path path = Paths.get(filePath).toAbsolutePath(); - if(!Files.exists(path)) { - throw new IOException(path + ": No such file or directory"); - } - return path.toUri().toURL(); - } - - - private List getSchemaConfigs() { - if (yamlFilePath == null || yamlFilePath.isBlank()) { - throw new CompasException(CompasErrorCode.CONFIGURATION_ERROR_CODE, - "No configuration file configured (yamlFilePath)"); - } - - URL source; - try { - source = getResource(yamlFilePath); - } catch (IOException e){ - String message = String.format("Resource %s not found", yamlFilePath); - log.error(message, e); - throw new CompasException(CompasErrorCode.RESOURCE_NOT_FOUND_ERROR_CODE,message); - } - - List schemaConfigs = new ArrayList<>(); - try { - ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); - JsonNode jsonNode = objectMapper.readTree(source); - - JsonNode pathsNode = jsonNode.at(COMPAS_SCL_SCHEMAS_JSONPATH); - if (pathsNode != null && pathsNode.getNodeType() == JsonNodeType.ARRAY) { - Iterable nodes = pathsNode::elements; - // Walk through the schemas and check if all needed parameters are filled. - nodes.forEach(node -> { - if (node.has(XSD_PATH_PROP) && node.has(NAMESPACE_PROP) && node.has(CONTEXT_PATH_PROP)) { - schemaConfigs.add( - new SchemaConfig(node.get(XSD_PATH_PROP).textValue(), - node.get(NAMESPACE_PROP).textValue(), - node.get(CONTEXT_PATH_PROP).textValue())); - } else { - throw new CompasException(CompasErrorCode.PROPERTY_ERROR_ERROR_CODE, - String.format("One of the properties (%s, %s, %s) has no value", - XSD_PATH_PROP, NAMESPACE_PROP, CONTEXT_PATH_PROP)); - } - }); - } else { - throw new CompasException(CompasErrorCode.CONFIGURATION_ERROR_CODE, - String.format("Configuration for marshaller (%s) didn't contain the path %s", - yamlFilePath, COMPAS_SCL_SCHEMAS_JSONPATH)); - } - } catch (IOException exp) { - var message = "I/O Error reading YAML File"; - log.error(message, exp); - throw new CompasException(CompasErrorCode.INVALID_YML_ERROR_CODE, message); - } - - return schemaConfigs; - } - - private List getContextPaths(List schemaConfigs) { - // Convert the SchemaConfig List to a List containing only the ContextPaths. - return schemaConfigs.stream() - .map(SchemaConfig::getContextPath) - .collect(Collectors.toList()); - } - - private String getImportStatements(List schemaConfigs) { - // Convert the SchemaConfig List to a String containing the Import Statements sued to combine multiple - // XSD Schemas. - return schemaConfigs.stream() - .map(schemaConfig -> - "\n") - .collect(Collectors.joining()); - } - - public MarshallerWrapper build() { - try { - // Create the JAXB Context with the configured context paths. The list is separated by a colon - // as needed by the JAxbContext. - var schemaConfigs = getSchemaConfigs(); - var contextPaths = String.join(":", getContextPaths(schemaConfigs)); - - var jaxbContext = JAXBContext.newInstance(contextPaths); - var jaxbMarshaller = jaxbContext.createMarshaller(); - var jaxbUnmarshaller = jaxbContext.createUnmarshaller(); - - // Setup schema validator - var factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - // To make load of all different type of schemas and also make imports/includes in these schemas - // work the solution is to make a combined XSD Schema containing all other schemas as import. - // Import is used, because the schemas can have different namespaces. - var combinedXsdSchema = - "\n" - + "\n" - + getImportStatements(schemaConfigs) - + ""; - var schema = factory.newSchema( - new StreamSource(new StringReader(combinedXsdSchema), "topSchema") - ); - jaxbMarshaller.setSchema(schema); - jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - jaxbUnmarshaller.setSchema(schema); - - return new MarshallerWrapper(jaxbUnmarshaller,jaxbMarshaller); - - } catch (JAXBException | SAXException exp) { - var message = "Error creating JAXB Marshaller and Unmarshaller."; - log.error(message, exp); - throw new CompasException(CompasErrorCode.CREATION_ERROR_CODE, message); - } - } - - /** - * POJO Class to hold all the schema configuration, so that we only need to walk through the JSON Content once. - */ - protected class SchemaConfig { - private final String xsdPath; - private final String namespace; - private final String contextPath; - - public SchemaConfig(String xsdPath, String namespace, String contextPath) { - // Convert the XSD Path to a URL, that will be used in the import statement. - // If the XSD Path is not correct loading will fail. - this.xsdPath = xsdPath; - this.namespace = namespace; - this.contextPath = contextPath; - } - - public String getXsdPath() { - try { - return getResource(xsdPath).getPath(); - } catch (IOException e) { - log.error(e.getMessage(),e); - throw new CompasException(CompasErrorCode.RESOURCE_NOT_FOUND_ERROR_CODE,e.getMessage()); - } - } - - public String getNamespace() { - return namespace; - } - - public String getContextPath() { - return contextPath; - } - } -} - - diff --git a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/SclTestMarshaller.java b/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/SclTestMarshaller.java deleted file mode 100644 index 90ad71881..000000000 --- a/sct-commons/src/test/java/org/lfenergy/compas/sct/commons/testhelpers/marshaller/SclTestMarshaller.java +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-FileCopyrightText: 2021 RTE FRANCE -// -// SPDX-License-Identifier: Apache-2.0 - -package org.lfenergy.compas.sct.commons.testhelpers.marshaller; - -import org.apache.commons.io.IOUtils; -import org.lfenergy.compas.scl2007b4.model.SCL; -import org.lfenergy.compas.sct.commons.testhelpers.MarshallerWrapper; - -public class SclTestMarshaller { - - public static SCL getSCLFromFile(String filename) throws Exception { - MarshallerWrapper marshallerWrapper = createWrapper(); - byte[] rawXml = IOUtils.resourceToByteArray(filename); - return marshallerWrapper.unmarshall(rawXml,SCL.class); - } - - public static MarshallerWrapper createWrapper() { - return MarshallerWrapper.builder() - .withProperties("classpath:scl_schema.yml") - .build(); - } -} diff --git a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/scd.xml b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/scd.xml index 5080de6e1..1248f24fb 100644 --- a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/scd.xml +++ b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/scd.xml @@ -14,7 +14,7 @@ 0 - + @@ -25,7 +25,7 @@ - + diff --git a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/scd_lnode_with_many_compas_icdheader.xml b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/scd_lnode_with_many_compas_icdheader.xml index 393510a21..5542476fb 100644 --- a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/scd_lnode_with_many_compas_icdheader.xml +++ b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/scd_lnode_with_many_compas_icdheader.xml @@ -14,7 +14,7 @@ 0 - + @@ -25,13 +25,13 @@ - + - + - + diff --git a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/ssd.xml b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/ssd.xml index 7488c9a34..d6f0de482 100644 --- a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/ssd.xml +++ b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/ssd.xml @@ -16,7 +16,7 @@ - + diff --git a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std.xml b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std.xml index 1e3d4da21..9428ba6d9 100644 --- a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std.xml +++ b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std.xml @@ -34,7 +34,7 @@ - + @@ -341,4 +341,4 @@ Y - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1GTW1.xml b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1SCU1.xml similarity index 99% rename from sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1GTW1.xml rename to sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1SCU1.xml index 09d31de67..33c6aa8bb 100644 --- a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1GTW1.xml +++ b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1SCU1.xml @@ -34,7 +34,7 @@ - + @@ -341,4 +341,4 @@ Y - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1GTW2.xml b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1SCU2.xml similarity index 99% rename from sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1GTW2.xml rename to sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1SCU2.xml index d9d112f2e..43b7712f7 100644 --- a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1GTW2.xml +++ b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_SITESITE1SCU2.xml @@ -34,7 +34,7 @@ - + @@ -341,4 +341,4 @@ Y - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_with_same_ICDSystemVersionUUID.xml b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_with_same_ICDSystemVersionUUID.xml index 48aafb3cb..54a7c0cb7 100644 --- a/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_with_same_ICDSystemVersionUUID.xml +++ b/sct-commons/src/test/resources/scd-ied-dtt-com-import-stds/std_with_same_ICDSystemVersionUUID.xml @@ -34,7 +34,7 @@ - + @@ -341,4 +341,4 @@ Y - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test1_LD_STATUS_INACTIVE.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test1_LD_STATUS_INACTIVE.scd index b6f2124f7..9aa92348e 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test1_LD_STATUS_INACTIVE.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test1_LD_STATUS_INACTIVE.scd @@ -19,7 +19,7 @@ 0 - + @@ -221,4 +221,4 @@ test/blocked - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test2_LD_STATUS_INACTIVE.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test2_LD_STATUS_INACTIVE.scd index f94cd1956..316933867 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test2_LD_STATUS_INACTIVE.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test2_LD_STATUS_INACTIVE.scd @@ -19,7 +19,7 @@ 0 - + @@ -219,4 +219,4 @@ test/blocked - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_Dai_Not_Updatable.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_Dai_Not_Updatable.scd index 1de3c1de7..48c81222d 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_Dai_Not_Updatable.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_Dai_Not_Updatable.scd @@ -19,7 +19,7 @@ 0 - + @@ -220,4 +220,4 @@ test/blocked - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingBeh.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingBeh.scd index 068f2cf7f..ee8ac2930 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingBeh.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingBeh.scd @@ -1,114 +1,114 @@ - - - - - - - SCD - -

- - - -
- - - - - - 0 - - - - - - - - - - - - - - - - - - - - - - - -
-

00000001

-
-
-
- - -
-

Adresse IP du serveur Syslog

-
-
-
-
- - SAMU - SAMU - - - - - - - - - - - - - - - - - 01.00.000 - - - 01.00.000 - - - - - off - - - - - - - - - - - - - - - - - - - - - - - blocked - test - test/blocked - off - on - - - - \ No newline at end of file + + + + + + + SCD + +
+ + + +
+ + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + +
+

00000001

+
+
+
+ + +
+

Adresse IP du serveur Syslog

+
+
+
+
+ + SAMU + SAMU + + + + + + + + + + + + + + + + + 01.00.000 + + + 01.00.000 + + + + + off + + + + + + + + + + + + + + + + + + + + + + + blocked + test + test/blocked + off + on + + + +
diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingLDevicePrivate.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingLDevicePrivate.scd index 48877c0b5..eba19585c 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingLDevicePrivate.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingLDevicePrivate.scd @@ -1,112 +1,112 @@ - - - - - - - SCD - -
- - - -
- - - - - - 0 - - - - - - - - - - - - - - - - - - - - - - - -
-

00000001

-
-
-
- - -
-

Adresse IP du serveur Syslog

-
-
-
-
- - SAMU - SAMU - - - - - - - - - - - - - - 01.00.000 - - - 01.00.000 - - - - - off - - - - - - - - - - - - - - - - - - - - - - - - blocked - test - test/blocked - off - on - - - -
\ No newline at end of file + + + + + + + SCD + +
+ + + +
+ + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + +
+

00000001

+
+
+
+ + +
+

Adresse IP du serveur Syslog

+
+
+
+
+ + SAMU + SAMU + + + + + + + + + + + + + + 01.00.000 + + + 01.00.000 + + + + + off + + + + + + + + + + + + + + + + + + + + + + + + blocked + test + test/blocked + off + on + + + +
diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingLDevicePrivateAttribute.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingLDevicePrivateAttribute.scd index 8ab10e71c..c2e919b1b 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingLDevicePrivateAttribute.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingLDevicePrivateAttribute.scd @@ -1,115 +1,115 @@ - - - - - - - SCD - -
- - - -
- - - - - - 0 - - - - - - - - - - - - - - - - - - - - - - - -
-

00000001

-
-
-
- - -
-

Adresse IP du serveur Syslog

-
-
-
-
- - SAMU - SAMU - - - - - - - - - - - - - - - - - 01.00.000 - - - 01.00.000 - - - - - off - - - - - - - - - - - - - - - - - - - - - - - - blocked - test - test/blocked - off - on - - - -
\ No newline at end of file + + + + + + + SCD + +
+ + + +
+ + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + +
+

00000001

+
+
+
+ + +
+

Adresse IP du serveur Syslog

+
+
+
+
+ + SAMU + SAMU + + + + + + + + + + + + + + + + + 01.00.000 + + + 01.00.000 + + + + + off + + + + + + + + + + + + + + + + + + + + + + + + blocked + test + test/blocked + off + on + + + +
diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingMod.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingMod.scd index f6d2f2979..1c6850178 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingMod.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_KO_MissingMod.scd @@ -1,114 +1,114 @@ - - - - - - - SCD - -
- - - -
- - - - - - 0 - - - - - - - - - - - - - - - - - - - - - - - -
-

00000001

-
-
-
- - -
-

Adresse IP du serveur Syslog

-
-
-
-
- - SAMU - SAMU - - - - - - - - - - - - - - - - - 01.00.000 - - - 01.00.000 - - - - - off - - - - - - - - - - - - - - - - - - - - - - - blocked - test - test/blocked - off - on - - - -
\ No newline at end of file + + + + + + + SCD + +
+ + + +
+ + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + +
+

00000001

+
+
+
+ + +
+

Adresse IP du serveur Syslog

+
+
+
+
+ + SAMU + SAMU + + + + + + + + + + + + + + + + + 01.00.000 + + + 01.00.000 + + + + + off + + + + + + + + + + + + + + + + + + + + + + + blocked + test + test/blocked + off + on + + + +
diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_LD_STATUS_ACTIVE.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_LD_STATUS_ACTIVE.scd index 0823597c2..85a71fb25 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_LD_STATUS_ACTIVE.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_LD_STATUS_ACTIVE.scd @@ -19,7 +19,7 @@ 0 - + @@ -216,4 +216,4 @@ test/blocked - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_LD_STATUS_UNTESTED.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_LD_STATUS_UNTESTED.scd index 6f2cb7e9d..205bcff51 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_LD_STATUS_UNTESTED.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_LD_STATUS_UNTESTED.scd @@ -19,7 +19,7 @@ 0 - + @@ -216,4 +216,4 @@ test/blocked - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_Template.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_Template.scd index 140e2390d..651c5eff3 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_Template.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue68_Test_Template.scd @@ -19,7 +19,7 @@ 0 - + @@ -220,4 +220,4 @@ test/blocked - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-refresh-lnode/issue_165_enhance_68_Test_Dai_Updatable.scd b/sct-commons/src/test/resources/scd-refresh-lnode/issue_165_enhance_68_Test_Dai_Updatable.scd index 1de3c1de7..48c81222d 100644 --- a/sct-commons/src/test/resources/scd-refresh-lnode/issue_165_enhance_68_Test_Dai_Updatable.scd +++ b/sct-commons/src/test/resources/scd-refresh-lnode/issue_165_enhance_68_Test_Dai_Updatable.scd @@ -19,7 +19,7 @@ 0 - + @@ -220,4 +220,4 @@ test/blocked - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-substation-import-ssd/scd_with_substation.xml b/sct-commons/src/test/resources/scd-substation-import-ssd/scd_with_substation.xml index 332586e8b..49e15f457 100644 --- a/sct-commons/src/test/resources/scd-substation-import-ssd/scd_with_substation.xml +++ b/sct-commons/src/test/resources/scd-substation-import-ssd/scd_with_substation.xml @@ -10,7 +10,7 @@ 90 - + @@ -37,4 +37,4 @@ - \ No newline at end of file + diff --git a/sct-commons/src/test/resources/scd-substation-import-ssd/ssd.xml b/sct-commons/src/test/resources/scd-substation-import-ssd/ssd.xml index 7a38e3382..c4688b459 100644 --- a/sct-commons/src/test/resources/scd-substation-import-ssd/ssd.xml +++ b/sct-commons/src/test/resources/scd-substation-import-ssd/ssd.xml @@ -12,7 +12,7 @@ 0 - + @@ -23,7 +23,7 @@ - + @@ -40,7 +40,7 @@ 90 - + @@ -64,7 +64,7 @@ - + diff --git a/sct-commons/src/test/resources/scl-remove-controlBlocks-dataSet-extRefSrc/scl-with-control-blocks.xml b/sct-commons/src/test/resources/scl-remove-controlBlocks-dataSet-extRefSrc/scl-with-control-blocks.xml index a192ae121..94db16aa8 100644 --- a/sct-commons/src/test/resources/scl-remove-controlBlocks-dataSet-extRefSrc/scl-with-control-blocks.xml +++ b/sct-commons/src/test/resources/scl-remove-controlBlocks-dataSet-extRefSrc/scl-with-control-blocks.xml @@ -33,7 +33,7 @@ - + diff --git a/sct-commons/src/test/resources/scl_schema.yml b/sct-commons/src/test/resources/scl_schema.yml deleted file mode 100644 index cf53b7c51..000000000 --- a/sct-commons/src/test/resources/scl_schema.yml +++ /dev/null @@ -1,13 +0,0 @@ -# SPDX-FileCopyrightText: 2021 RTE FRANCE -# -# SPDX-License-Identifier: Apache-2.0 - ---- -compas: - scl: - # XsdPath currently is a reference to a XSD on the classpath, searched from the root. - # ContextPath is the package where the ObjectFactory class can be found, mostly generated there. - schemas: - - xsdPath: "target/xsd/SCL2007B4/SCL.xsd" - namespace: "http://www.iec.ch/61850/2003/SCL" - contextPath: "org.lfenergy.compas.scl2007b4.model" \ No newline at end of file