Skip to content

Commit

Permalink
Fixing update to Jakarta XML 4.0 (#1358)
Browse files Browse the repository at this point in the history
* Experimenting

* Tests

* Fix compile issues

* Fixed test errors

* Fix failing tests

---------

Co-authored-by: Bryn Rhodes <bryn@databaseconsultinggroup.com>
  • Loading branch information
JPercival and brynrhodes committed Apr 23, 2024
1 parent 70722a1 commit 662ff61
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Src/java/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"tngtech",
"trackback",
"Ucum",
"unescaping"
"unescaping",
"Unmarshaller"
],
"cSpell.enabledLanguageIds": [
"java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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("<library xmlns=\"urn:hl7-org:elm:r1\"></library>".getBytes()));
assertNotNull(library);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<ModelInfo>) 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);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns4:modelInfo xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:ns4=\"urn:hl7-org:elm-modelinfo:r1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></ns4:modelInfo>");
assertNotNull(mi);
}
}

This file was deleted.

0 comments on commit 662ff61

Please sign in to comment.