From 5123cf7f2c0a9d2b384d012a45ce36b91d640850 Mon Sep 17 00:00:00 2001
From: CesarCoelho <cbwhittle@hotmail.com>
Date: Thu, 10 Oct 2024 15:37:13 +0200
Subject: [PATCH] Updates the MAL to the latest changes and adds backward
 compatibility to the code

---
 .../esa/mo/tools/stubgen/StubGenerator.java   |  4 +-
 .../esa/mo/xsd/util/XmlSpecification.java     | 46 ++++++++++++++++++-
 .../main/resources/xml/area001-v003-MAL.xml   |  2 +-
 .../main/resources/xml/area001-v003-MAL.xml   |  2 +-
 4 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/api-generator/api-generator-maven-plugin/src/main/java/esa/mo/tools/stubgen/StubGenerator.java b/api-generator/api-generator-maven-plugin/src/main/java/esa/mo/tools/stubgen/StubGenerator.java
index 6e88be6cc..a0bb68f5b 100644
--- a/api-generator/api-generator-maven-plugin/src/main/java/esa/mo/tools/stubgen/StubGenerator.java
+++ b/api-generator/api-generator-maven-plugin/src/main/java/esa/mo/tools/stubgen/StubGenerator.java
@@ -368,7 +368,7 @@ public void execute() throws MojoExecutionException {
 
                 // run the specifications through each generator
                 // first process the list of languages to generate
-                if ((null != targetLanguages) && (0 < targetLanguages.length)) {
+                if ((targetLanguages != null) && (targetLanguages.length > 0)) {
 
                     if (forceGeneration || (outputDirectory.lastModified() < inputTimestamp)) {
                         if (forceGeneration) {
@@ -376,7 +376,7 @@ public void execute() throws MojoExecutionException {
                         }
                         for (String targetLanguage : targetLanguages) {
                             final Generator gen = GENERATOR_MAP.get(targetLanguage.toLowerCase());
-                            if (null != gen) {
+                            if (gen != null) {
                                 processWithGenerator(gen, refSpecs, refXsd, specs);
                             } else {
                                 getLog().warn("Could not find generator for language: " + targetLanguage);
diff --git a/api-generator/generator-interfaces/src/main/java/esa/mo/xsd/util/XmlSpecification.java b/api-generator/generator-interfaces/src/main/java/esa/mo/xsd/util/XmlSpecification.java
index 6113d398d..9c6eab156 100644
--- a/api-generator/generator-interfaces/src/main/java/esa/mo/xsd/util/XmlSpecification.java
+++ b/api-generator/generator-interfaces/src/main/java/esa/mo/xsd/util/XmlSpecification.java
@@ -21,8 +21,13 @@
 package esa.mo.xsd.util;
 
 import esa.mo.xsd.SpecificationType;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
@@ -73,14 +78,51 @@ public SpecificationType getSpecType() {
         return specType;
     }
 
-    public synchronized static XmlSpecification loadSpecification(final File is) throws IOException, JAXBException {
+    /**
+     * Modifies the content from a XML file and returns it. Note that the file
+     * is not saved with these changes. This method is a hack to have the new
+     * "ServiceSchema-v003" specifications, to be compatible with the old code.
+     * This method will have to be removed after the mo-services-java codebase
+     * fully removes support for the old MAL.
+     *
+     * @param file The file with the specification.
+     * @return An input stream with the modified content.
+     * @throws IOException if something went while modifying the content.
+     */
+    private static InputStream modifyFileToBeBackwardsCompatible(File file) throws IOException {
+        // Read the file content into a StringBuilder
+        StringBuilder fileContent = new StringBuilder();
+        try ( BufferedReader reader = new BufferedReader(new FileReader(file))) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                fileContent.append(line).append(System.lineSeparator());
+            }
+        }
+
+        // Replace the text "ServiceSchema-v003" with "ServiceSchema"
+        String modifiedContent = fileContent.toString().replace("ServiceSchema-v003", "ServiceSchema");
+
+        // Convert the modified content to an InputStream
+        return new ByteArrayInputStream(modifiedContent.getBytes(StandardCharsets.UTF_8));
+    }
+
+    /**
+     * Loads an XML Specification.
+     *
+     * @param file The file with the specification.
+     * @return The representation of the XML specification.
+     * @throws IOException if something went while modifying the content.
+     * @throws JAXBException if the xml could not be parsed.
+     */
+    public synchronized static XmlSpecification loadSpecification(final File file) throws IOException, JAXBException {
         if (jc == null) {
             jc = JAXBContext.newInstance("esa.mo.xsd");
         }
 
         final Unmarshaller unmarshaller = jc.createUnmarshaller();
+        final InputStream is = XmlSpecification.modifyFileToBeBackwardsCompatible(file);
         final JAXBElement rootElement = (JAXBElement) unmarshaller.unmarshal(is);
         SpecificationType specType = (SpecificationType) rootElement.getValue();
-        return new XmlSpecification(is, rootElement, specType);
+        return new XmlSpecification(file, rootElement, specType);
     }
 }
diff --git a/xml-service-specifications/xml-ccsds-mo-prototypes/src/main/resources/xml/area001-v003-MAL.xml b/xml-service-specifications/xml-ccsds-mo-prototypes/src/main/resources/xml/area001-v003-MAL.xml
index 21603586a..48dc5df81 100644
--- a/xml-service-specifications/xml-ccsds-mo-prototypes/src/main/resources/xml/area001-v003-MAL.xml
+++ b/xml-service-specifications/xml-ccsds-mo-prototypes/src/main/resources/xml/area001-v003-MAL.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<mal:specification xmlns:mal="http://www.ccsds.org/schema/ServiceSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+<mal:specification xmlns:mal="http://www.ccsds.org/schema/ServiceSchema-v003" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <mal:area name="MAL" number="1" version="3">
         <mal:dataTypes>
             <mal:fundamental name="Attribute" comment="Attribute is the base type of all Attributes of the MAL data model. Attributes are contained within Composites and are used to build complex structures that make the data model.">
diff --git a/xml-service-specifications/xml-ccsds-mo-standards/src/main/resources/xml/area001-v003-MAL.xml b/xml-service-specifications/xml-ccsds-mo-standards/src/main/resources/xml/area001-v003-MAL.xml
index 21603586a..48dc5df81 100644
--- a/xml-service-specifications/xml-ccsds-mo-standards/src/main/resources/xml/area001-v003-MAL.xml
+++ b/xml-service-specifications/xml-ccsds-mo-standards/src/main/resources/xml/area001-v003-MAL.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<mal:specification xmlns:mal="http://www.ccsds.org/schema/ServiceSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+<mal:specification xmlns:mal="http://www.ccsds.org/schema/ServiceSchema-v003" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <mal:area name="MAL" number="1" version="3">
         <mal:dataTypes>
             <mal:fundamental name="Attribute" comment="Attribute is the base type of all Attributes of the MAL data model. Attributes are contained within Composites and are used to build complex structures that make the data model.">