Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing update to Jakarta XML 4.0 #1358

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading