From 662ff617ccee6f06a8ae1273f9e615b5c0c7d0ab Mon Sep 17 00:00:00 2001 From: JP Date: Tue, 23 Apr 2024 12:59:26 -0600 Subject: [PATCH] Fixing update to Jakarta XML 4.0 (#1358) * Experimenting * Tests * Fix compile issues * Fixed test errors * Fix failing tests --------- Co-authored-by: Bryn Rhodes --- Src/java/.vscode/settings.json | 3 +- .../elm/serializing/jaxb/ElmJsonMapper.java | 8 ++- .../elm/serializing/jaxb/ElmXmlMapper.java | 11 ++- .../cql/elm/serializing/jaxb/jaxb.properties | 1 - .../jaxb/ElmJsonLibraryReaderTest.java | 18 +++++ .../jaxb/ElmXmlLibraryReaderTest.java | 19 ++++++ .../cql/elm/ElmDeserializeTests.java | 12 ++-- .../serializing/jaxb/XmlModelInfoReader.java | 67 +++++++++++++++++-- .../r1/serializing/jaxb/jaxb.properties | 1 - .../jaxb/XmlModelInfoReaderTest.java | 18 +++++ .../org/hl7/elm_modelinfo/r1/jaxb.properties | 1 - 11 files changed, 139 insertions(+), 20 deletions(-) delete mode 100644 Src/java/elm-jaxb/src/main/resources/org/cqframework/cql/elm/serializing/jaxb/jaxb.properties create mode 100644 Src/java/elm-jaxb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/ElmJsonLibraryReaderTest.java create mode 100644 Src/java/elm-jaxb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/ElmXmlLibraryReaderTest.java delete mode 100644 Src/java/model-jaxb/src/main/resources/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/jaxb.properties create mode 100644 Src/java/model-jaxb/src/test/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/XmlModelInfoReaderTest.java delete mode 100644 Src/java/model/src/main/resources/org/hl7/elm_modelinfo/r1/jaxb.properties diff --git a/Src/java/.vscode/settings.json b/Src/java/.vscode/settings.json index 14a75ac4b..d5ce5f70c 100644 --- a/Src/java/.vscode/settings.json +++ b/Src/java/.vscode/settings.json @@ -30,7 +30,8 @@ "tngtech", "trackback", "Ucum", - "unescaping" + "unescaping", + "Unmarshaller" ], "cSpell.enabledLanguageIds": [ "java", diff --git a/Src/java/elm-jaxb/src/main/java/org/cqframework/cql/elm/serializing/jaxb/ElmJsonMapper.java b/Src/java/elm-jaxb/src/main/java/org/cqframework/cql/elm/serializing/jaxb/ElmJsonMapper.java index 5ad12f5c2..a93503d04 100644 --- a/Src/java/elm-jaxb/src/main/java/org/cqframework/cql/elm/serializing/jaxb/ElmJsonMapper.java +++ b/Src/java/elm-jaxb/src/main/java/org/cqframework/cql/elm/serializing/jaxb/ElmJsonMapper.java @@ -2,17 +2,23 @@ import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; +import java.util.HashMap; import org.hl7.cql_annotations.r1.CqlToElmBase; import org.hl7.elm.r1.Library; public class ElmJsonMapper { private static JAXBContext jaxbContext; + private static HashMap properties = new HashMap<>(); + + static { + properties.put(JAXBContext.JAXB_CONTEXT_FACTORY, "org.eclipse.persistence.jaxb.JAXBContextFactory"); + } public static JAXBContext getJaxbContext() { if (jaxbContext == null) { try { - jaxbContext = JAXBContext.newInstance(Library.class, CqlToElmBase.class); + jaxbContext = JAXBContext.newInstance(new Class[] {Library.class, CqlToElmBase.class}, properties); } catch (JAXBException e) { throw new RuntimeException("Error creating JAXBContext - " + e.getMessage()); } diff --git a/Src/java/elm-jaxb/src/main/java/org/cqframework/cql/elm/serializing/jaxb/ElmXmlMapper.java b/Src/java/elm-jaxb/src/main/java/org/cqframework/cql/elm/serializing/jaxb/ElmXmlMapper.java index 71dc23d1a..6de055888 100644 --- a/Src/java/elm-jaxb/src/main/java/org/cqframework/cql/elm/serializing/jaxb/ElmXmlMapper.java +++ b/Src/java/elm-jaxb/src/main/java/org/cqframework/cql/elm/serializing/jaxb/ElmXmlMapper.java @@ -2,17 +2,24 @@ import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; +import java.util.HashMap; import org.cqframework.cql.elm.IdObjectFactory; public class ElmXmlMapper { private static JAXBContext jaxbContext; + private static HashMap properties = new HashMap<>(); + + static { + properties.put(JAXBContext.JAXB_CONTEXT_FACTORY, "org.eclipse.persistence.jaxb.JAXBContextFactory"); + } public static JAXBContext getJaxbContext() { if (jaxbContext == null) { try { - jaxbContext = - JAXBContext.newInstance(IdObjectFactory.class, org.hl7.cql_annotations.r1.ObjectFactory.class); + jaxbContext = JAXBContext.newInstance( + new Class[] {IdObjectFactory.class, org.hl7.cql_annotations.r1.ObjectFactory.class}, + properties); } catch (JAXBException e) { throw new RuntimeException("Error creating JAXBContext - " + e.getMessage()); } diff --git a/Src/java/elm-jaxb/src/main/resources/org/cqframework/cql/elm/serializing/jaxb/jaxb.properties b/Src/java/elm-jaxb/src/main/resources/org/cqframework/cql/elm/serializing/jaxb/jaxb.properties deleted file mode 100644 index b48bb869b..000000000 --- a/Src/java/elm-jaxb/src/main/resources/org/cqframework/cql/elm/serializing/jaxb/jaxb.properties +++ /dev/null @@ -1 +0,0 @@ -jakarta.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory \ No newline at end of file diff --git a/Src/java/elm-jaxb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/ElmJsonLibraryReaderTest.java b/Src/java/elm-jaxb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/ElmJsonLibraryReaderTest.java new file mode 100644 index 000000000..f3507acc7 --- /dev/null +++ b/Src/java/elm-jaxb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/ElmJsonLibraryReaderTest.java @@ -0,0 +1,18 @@ +package org.cqframework.cql.elm.serializing.jaxb; + +import static org.testng.Assert.assertNotNull; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import org.hl7.elm.r1.Library; +import org.testng.annotations.Test; + +public class ElmJsonLibraryReaderTest { + + @Test + public void testRead() throws IOException { + var reader = new ElmJsonLibraryReader(); + Library library = reader.read(new ByteArrayInputStream("{\"library\" : { \"type\" : \"Library\"}}".getBytes())); + assertNotNull(library); + } +} diff --git a/Src/java/elm-jaxb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/ElmXmlLibraryReaderTest.java b/Src/java/elm-jaxb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/ElmXmlLibraryReaderTest.java new file mode 100644 index 000000000..aee68f751 --- /dev/null +++ b/Src/java/elm-jaxb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/ElmXmlLibraryReaderTest.java @@ -0,0 +1,19 @@ +package org.cqframework.cql.elm.serializing.jaxb; + +import static org.testng.Assert.assertNotNull; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import org.hl7.elm.r1.Library; +import org.testng.annotations.Test; + +public class ElmXmlLibraryReaderTest { + + @Test + public void testRead() throws IOException { + var reader = new ElmXmlLibraryReader(); + Library library = + reader.read(new ByteArrayInputStream("".getBytes())); + assertNotNull(library); + } +} diff --git a/Src/java/elm-test/src/test/java/org/cqframework/cql/elm/ElmDeserializeTests.java b/Src/java/elm-test/src/test/java/org/cqframework/cql/elm/ElmDeserializeTests.java index a2e64b2e3..acd8e2ead 100644 --- a/Src/java/elm-test/src/test/java/org/cqframework/cql/elm/ElmDeserializeTests.java +++ b/Src/java/elm-test/src/test/java/org/cqframework/cql/elm/ElmDeserializeTests.java @@ -336,13 +336,13 @@ public void emptyStringsTest() throws IOException { new org.cqframework.cql.elm.serializing.jaxb.ElmXmlLibraryReader().read(new StringReader(jaxbXml)); validateEmptyStringsTest(xmlLibrary); - // xmlLibrary = new org.cqframework.cql.elm.serializing.jackson.ElmXmlLibraryReader().read(new - // StringReader(jacksonXml)); - // validateEmptyStringsTest(xmlLibrary); + Library jsonLibrary = new org.cqframework.cql.elm.serializing.jackson.ElmJsonLibraryReader() + .read(new StringReader(jacksonJson)); + validateEmptyStringsTest(jsonLibrary); - // xmlLibrary = new org.cqframework.cql.elm.serializing.jaxb.ElmXmlLibraryReader().read(new - // StringReader(jacksonXml)); - // validateEmptyStringsTest(xmlLibrary); + jsonLibrary = new org.cqframework.cql.elm.serializing.jaxb.ElmJsonLibraryReader() + .read(new StringReader(jaxbJson)); + validateEmptyStringsTest(xmlLibrary); } catch (IOException e) { e.printStackTrace(); } diff --git a/Src/java/model-jaxb/src/main/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/XmlModelInfoReader.java b/Src/java/model-jaxb/src/main/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/XmlModelInfoReader.java index 871bd69e7..c7e5a78c7 100644 --- a/Src/java/model-jaxb/src/main/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/XmlModelInfoReader.java +++ b/Src/java/model-jaxb/src/main/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/XmlModelInfoReader.java @@ -1,35 +1,88 @@ package org.hl7.elm_modelinfo.r1.serializing.jaxb; -import jakarta.xml.bind.JAXB; +import jakarta.xml.bind.JAXBContext; +import jakarta.xml.bind.JAXBElement; +import jakarta.xml.bind.JAXBException; +import jakarta.xml.bind.Unmarshaller; import java.io.*; import java.net.URI; import java.net.URL; +import java.util.HashMap; +import java.util.Map; import org.hl7.elm_modelinfo.r1.ModelInfo; +import org.hl7.elm_modelinfo.r1.ObjectFactory; import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReader; public class XmlModelInfoReader implements ModelInfoReader { + private final Map properties = new HashMap<>(); + private final Unmarshaller unmarshaller; + + public XmlModelInfoReader() { + properties.put(JAXBContext.JAXB_CONTEXT_FACTORY, "org.eclipse.persistence.jaxb.JAXBContextFactory"); + + try { + this.unmarshaller = JAXBContext.newInstance(new Class[] {ObjectFactory.class}, properties) + .createUnmarshaller(); + } catch (JAXBException e) { + throw new RuntimeException(e); + } + } + + private Unmarshaller getUnmarshaller() { + return unmarshaller; + } + + @SuppressWarnings("unchecked") + private ModelInfo toModelInfo(Object obj) { + return ((JAXBElement) obj).getValue(); + } + public ModelInfo read(File src) throws IOException { - return JAXB.unmarshal(src, ModelInfo.class); + try { + return toModelInfo(getUnmarshaller().unmarshal(src)); + } catch (JAXBException e) { + throw new IOException(e); + } } public ModelInfo read(Reader src) throws IOException { - return JAXB.unmarshal(src, ModelInfo.class); + try { + return toModelInfo(getUnmarshaller().unmarshal(src)); + } catch (JAXBException e) { + throw new IOException(e); + } } public ModelInfo read(InputStream src) throws IOException { - return JAXB.unmarshal(src, ModelInfo.class); + try { + return toModelInfo(getUnmarshaller().unmarshal(src)); + } catch (JAXBException e) { + throw new IOException(e); + } } public ModelInfo read(URL url) throws IOException { - return JAXB.unmarshal(url, ModelInfo.class); + try { + return toModelInfo(getUnmarshaller().unmarshal(url)); + } catch (JAXBException e) { + throw new IOException(e); + } } public ModelInfo read(URI uri) throws IOException { - return JAXB.unmarshal(uri, ModelInfo.class); + try { + return toModelInfo(getUnmarshaller().unmarshal(uri.toURL())); + } catch (JAXBException e) { + throw new IOException(e); + } } public ModelInfo read(String string) throws IOException { - return JAXB.unmarshal(string, ModelInfo.class); + try { + return toModelInfo(getUnmarshaller().unmarshal(new ByteArrayInputStream(string.getBytes()))); + } catch (JAXBException e) { + throw new IOException(e); + } } } diff --git a/Src/java/model-jaxb/src/main/resources/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/jaxb.properties b/Src/java/model-jaxb/src/main/resources/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/jaxb.properties deleted file mode 100644 index b48bb869b..000000000 --- a/Src/java/model-jaxb/src/main/resources/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/jaxb.properties +++ /dev/null @@ -1 +0,0 @@ -jakarta.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory \ No newline at end of file diff --git a/Src/java/model-jaxb/src/test/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/XmlModelInfoReaderTest.java b/Src/java/model-jaxb/src/test/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/XmlModelInfoReaderTest.java new file mode 100644 index 000000000..710d4a64d --- /dev/null +++ b/Src/java/model-jaxb/src/test/java/org/hl7/elm_modelinfo/r1/serializing/jaxb/XmlModelInfoReaderTest.java @@ -0,0 +1,18 @@ +package org.hl7.elm_modelinfo.r1.serializing.jaxb; + +import static org.testng.Assert.assertNotNull; + +import java.io.IOException; +import org.hl7.elm_modelinfo.r1.ModelInfo; +import org.testng.annotations.Test; + +public class XmlModelInfoReaderTest { + + @Test + public void testRead() throws IOException { + var reader = new XmlModelInfoReader(); + ModelInfo mi = reader.read( + ""); + assertNotNull(mi); + } +} diff --git a/Src/java/model/src/main/resources/org/hl7/elm_modelinfo/r1/jaxb.properties b/Src/java/model/src/main/resources/org/hl7/elm_modelinfo/r1/jaxb.properties deleted file mode 100644 index b48bb869b..000000000 --- a/Src/java/model/src/main/resources/org/hl7/elm_modelinfo/r1/jaxb.properties +++ /dev/null @@ -1 +0,0 @@ -jakarta.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory \ No newline at end of file