Skip to content

Commit

Permalink
Merge pull request #745 from Path-Check/jaxb-to-jackson-xml-read
Browse files Browse the repository at this point in the history
Using Jackson's XML reader instead of JAXB.unmarshal for ModelInfo files
  • Loading branch information
brynrhodes authored May 20, 2022
2 parents 2a71f51 + d1979a2 commit ec57b25
Show file tree
Hide file tree
Showing 27 changed files with 2,264 additions and 1,971 deletions.
3 changes: 3 additions & 0 deletions Src/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ configure(subprojects.findAll {it.name in ['model', 'elm', 'quick', 'qdm', 'cql-
xjc group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'
compile group: 'org.jvnet.jaxb2_commons', name: 'jaxb2-basics', version: '0.12.0'
compile group: 'jakarta.xml.bind', name: 'jakarta.xml.bind-api', version: '2.3.3'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.2'
compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.13.2'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.13.2'
compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-jaxb-annotations', version: '2.13.2'
compile group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.moxy', version: '2.7.7'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hl7.elm.r1.Retrieve;
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.elm_modelinfo.r1.ModelInfo;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import javax.xml.bind.*;
import java.io.*;
Expand Down Expand Up @@ -668,12 +669,17 @@ public static String convertToJxson(Library library) throws JsonProcessingExcept
return getJxsonMapper().writeValueAsString(wrapper);
}

public static void loadModelInfo(File modelInfoXML) {
final ModelInfo modelInfo = JAXB.unmarshal(modelInfoXML, ModelInfo.class);
final VersionedIdentifier modelId = new VersionedIdentifier().withId(modelInfo.getName()).withVersion(modelInfo.getVersion());
final ModelInfoProvider modelProvider = (VersionedIdentifier modelIdentifier) -> modelInfo;
final ModelInfoLoader modelInfoLoader = new ModelInfoLoader();
modelInfoLoader.registerModelInfoProvider(modelProvider);
public static void loadModelInfo(File modelInfoXML) {
try {
final ModelInfo modelInfo = JacksonXML.readValue(modelInfoXML, ModelInfo.class);
final VersionedIdentifier modelId = new VersionedIdentifier().withId(modelInfo.getName()).withVersion(modelInfo.getVersion());
final ModelInfoProvider modelProvider = (VersionedIdentifier modelIdentifier) -> modelInfo;
final ModelInfoLoader modelInfoLoader = new ModelInfoLoader();
modelInfoLoader.registerModelInfoProvider(modelProvider);
} catch (IOException e) {
e.printStackTrace();
return;
}
}

private static void outputExceptions(Iterable<CqlTranslatorException> exceptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.elm_modelinfo.r1.ModelInfo;

import javax.xml.bind.JAXB;
import java.io.*;
import java.nio.file.Path;

Expand Down Expand Up @@ -81,11 +80,13 @@ public boolean accept(File path, String name) {
try {
if (modelFile != null) {
InputStream is = new FileInputStream(modelFile);
return JAXB.unmarshal(is, ModelInfo.class);

return JacksonXML.readValue(is, ModelInfo.class);
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
throw new IllegalArgumentException(String.format("Could not load definition for model info %s.", modelIdentifier.getId()), e);
}
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.elm_modelinfo.r1.ModelInfo;

import javax.xml.bind.JAXB;
import java.io.IOException;

/**
* Created by Bryn on 4/15/2016.
Expand All @@ -27,47 +27,53 @@ private boolean isFHIRModelIdentifier(VersionedIdentifier modelIdentifier) {
public ModelInfo load(VersionedIdentifier modelIdentifier) {
if (isFHIRModelIdentifier(modelIdentifier)) {
String localVersion = modelIdentifier.getVersion() == null ? "" : modelIdentifier.getVersion();
switch (localVersion) {
case "1.0.2":
return JAXB.unmarshal(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.0.2.xml"),
ModelInfo.class);

case "1.4":
return JAXB.unmarshal(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.4.xml"),
ModelInfo.class);

case "1.6":
return JAXB.unmarshal(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.6.xml"),
ModelInfo.class);

case "1.8":
return JAXB.unmarshal(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.8.xml"),
ModelInfo.class);

case "3.0.0":
case "":
return JAXB.unmarshal(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.0.0.xml"),
ModelInfo.class);

case "3.0.1":
return JAXB.unmarshal(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.0.1.xml"),
ModelInfo.class);

case "3.2.0":
return JAXB.unmarshal(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.2.0.xml"),
ModelInfo.class);

case "4.0.0":
return JAXB.unmarshal(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-4.0.0.xml"),
ModelInfo.class);

case "4.0.1":
return JAXB.unmarshal(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-4.0.1.xml"),
ModelInfo.class);

// Do not throw, allow other providers to return the model if known
//default:
// throw new IllegalArgumentException(String.format("Unknown version %s of the FHIR model.", localVersion));
try {
switch (localVersion) {
case "1.0.2":
return JacksonXML.readValue(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.0.2.xml"),
ModelInfo.class);

case "1.4":
return JacksonXML.readValue(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.4.xml"),
ModelInfo.class);

case "1.6":
return JacksonXML.readValue(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.6.xml"),
ModelInfo.class);

case "1.8":
return JacksonXML.readValue(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.8.xml"),
ModelInfo.class);

case "3.0.0":
case "":
return JacksonXML.readValue(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.0.0.xml"),
ModelInfo.class);

case "3.0.1":
return JacksonXML.readValue(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.0.1.xml"),
ModelInfo.class);

case "3.2.0":
return JacksonXML.readValue(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.2.0.xml"),
ModelInfo.class);

case "4.0.0":
return JacksonXML.readValue(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-4.0.0.xml"),
ModelInfo.class);

case "4.0.1":
return JacksonXML.readValue(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-4.0.1.xml"),
ModelInfo.class);

// Do not throw, allow other providers to return the model if known
//default:
// throw new IllegalArgumentException(String.format("Unknown version %s of the FHIR model.", localVersion));
}
} catch (IOException e) {
e.printStackTrace();
// Do not throw, allow other providers to resolve
// throw new IllegalArgumentException(String.format("Unknown version %s of the Fhir model.", localVersion));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.cqframework.cql.cql2elm;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import org.hl7.elm_modelinfo.r1.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SimpleTypeInfo.class, name = "ns4:SimpleTypeInfo"),
@JsonSubTypes.Type(value = ClassInfo.class, name = "ns4:ClassInfo"),
@JsonSubTypes.Type(value = ChoiceTypeInfo.class, name = "ns4:ChoiceTypeInfo"),
@JsonSubTypes.Type(value = IntervalTypeInfo.class, name = "ns4:IntervalTypeInfo"),
@JsonSubTypes.Type(value = ListTypeInfo.class, name = "ns4:ListTypeInfo"),
@JsonSubTypes.Type(value = ProfileInfo.class, name = "ns4:ProfileInfo"),
@JsonSubTypes.Type(value = TupleTypeInfo.class, name = "ns4:TupleTypeInfo")
})
interface TypeInfoMixIn {}

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = NamedTypeSpecifier.class, name = "ns4:NamedTypeSpecifier"),
@JsonSubTypes.Type(value = ListTypeSpecifier.class, name = "ns4:ListTypeSpecifier"),
@JsonSubTypes.Type(value = IntervalTypeSpecifier.class, name = "ns4:IntervalTypeSpecifier"),
@JsonSubTypes.Type(value = ChoiceTypeSpecifier.class, name = "ns4:ChoiceTypeSpecifier"),
@JsonSubTypes.Type(value = ParameterTypeSpecifier.class, name = "ns4:ParameterTypeSpecifier"),
@JsonSubTypes.Type(value = BoundParameterTypeSpecifier.class, name = "ns4:BoundParameterTypeSpecifier"),
@JsonSubTypes.Type(value = TupleTypeSpecifier.class, name = "ns4:TupleTypeSpecifier")
})
interface TypeSpecifierMixIn {}

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
defaultImpl = NamedTypeSpecifier.class)
interface NamedTypeSpecifierMixIn {}

public class JacksonXML {
static XmlMapper mapper = new XmlMapper().builder()
.defaultUseWrapper(false)
.addMixIn(TypeInfo.class, TypeInfoMixIn.class)
.addMixIn(TypeSpecifier.class, TypeSpecifierMixIn.class)
.addMixIn(NamedTypeSpecifier.class, NamedTypeSpecifierMixIn.class)
.addModule(new JaxbAnnotationModule())
.build();

public static <T> T readValue(File src, Class<T> valueType) throws IOException {
return mapper.readValue(src, valueType);
}

public static <T> T readValue(InputStream src, Class<T> valueType) throws IOException {
return mapper.readValue(src, valueType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.elm_modelinfo.r1.ModelInfo;

import javax.xml.bind.JAXB;
import java.io.IOException;

public class QICoreModelInfoProvider implements ModelInfoProvider {
private NamespaceManager namespaceManager;
Expand All @@ -24,17 +24,23 @@ private boolean isQICoreModelIdentifier(VersionedIdentifier modelIdentifier) {
public ModelInfo load(VersionedIdentifier modelIdentifier) {
if (isQICoreModelIdentifier(modelIdentifier)) {
String localVersion = modelIdentifier.getVersion() == null ? "" : modelIdentifier.getVersion();
switch (localVersion) {
case "4.0.0":
return JAXB.unmarshal(QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.0.0.xml"),
ModelInfo.class);
case "4.1.0":
return JAXB.unmarshal(QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.1.0.xml"),
ModelInfo.class);
case "4.1.1":
default:
return JAXB.unmarshal(QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.1.1.xml"),
ModelInfo.class);
try {
switch (localVersion) {
case "4.0.0":
return JacksonXML.readValue(QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.0.0.xml"),
ModelInfo.class);
case "4.1.0":
return JacksonXML.readValue(QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.1.0.xml"),
ModelInfo.class);
case "4.1.1":
default:
return JacksonXML.readValue(QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.1.1.xml"),
ModelInfo.class);
}
} catch (IOException e) {
e.printStackTrace();
// Do not throw, allow other providers to resolve
// throw new IllegalArgumentException(String.format("Unknown version %s of the QI model.", localVersion));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.elm_modelinfo.r1.ModelInfo;

import javax.xml.bind.JAXB;
import java.io.IOException;

/**
* Created by Bryn on 2/3/2016.
Expand All @@ -27,42 +27,44 @@ private boolean isQDMModelIdentifier(VersionedIdentifier modelIdentifier) {
public ModelInfo load(VersionedIdentifier modelIdentifier) {
if (isQDMModelIdentifier(modelIdentifier)) {
String localVersion = modelIdentifier.getVersion() == null ? "" : modelIdentifier.getVersion();
switch (localVersion) {
case "4.1.2":
return JAXB.unmarshal(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo.xml"),
ModelInfo.class);
case "4.2":
return JAXB.unmarshal(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-4.2.xml"),
ModelInfo.class);
case "4.3":
return JAXB.unmarshal(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-4.3.xml"),
ModelInfo.class);
case "5.0":
return JAXB.unmarshal(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.xml"),
ModelInfo.class);
case "5.0.1":
return JAXB.unmarshal(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.1.xml"),
ModelInfo.class);
case "5.0.2":
return JAXB.unmarshal(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.2.xml"),
ModelInfo.class);
case "5.3":
return JAXB.unmarshal(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.3.xml"),
ModelInfo.class);
case "5.4":
return JAXB.unmarshal(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.4.xml"),
ModelInfo.class);
case "5.5":
return JAXB.unmarshal(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.5.xml"),
ModelInfo.class);
case "5.6":
case "":
return JAXB.unmarshal(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.6.xml"),
ModelInfo.class);

// Do not throw, allow other providers to resolve
//default:
// throw new IllegalArgumentException(String.format("Unknown version %s of the QDM model.", localVersion));
try {
switch (localVersion) {
case "4.1.2":
return JacksonXML.readValue(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo.xml"),
ModelInfo.class);
case "4.2":
return JacksonXML.readValue(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-4.2.xml"),
ModelInfo.class);
case "4.3":
return JacksonXML.readValue(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-4.3.xml"),
ModelInfo.class);
case "5.0":
return JacksonXML.readValue(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.xml"),
ModelInfo.class);
case "5.0.1":
return JacksonXML.readValue(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.1.xml"),
ModelInfo.class);
case "5.0.2":
return JacksonXML.readValue(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.2.xml"),
ModelInfo.class);
case "5.3":
return JacksonXML.readValue(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.3.xml"),
ModelInfo.class);
case "5.4":
return JacksonXML.readValue(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.4.xml"),
ModelInfo.class);
case "5.5":
return JacksonXML.readValue(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.5.xml"),
ModelInfo.class);
case "5.6":
case "":
return JacksonXML.readValue(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.6.xml"),
ModelInfo.class);
}
} catch (IOException e) {
e.printStackTrace();
// Do not throw, allow other providers to resolve
// throw new IllegalArgumentException(String.format("Unknown version %s of the QDM model.", localVersion));
}
}

Expand Down
Loading

0 comments on commit ec57b25

Please sign in to comment.