From 963757410e749885cb4802be48e16f4bfc03d82c Mon Sep 17 00:00:00 2001 From: Jonathan Percival Date: Mon, 30 Dec 2024 20:18:41 -0700 Subject: [PATCH] Fix model multiplatform --- ...otlin-multiplatform-conventions.gradle.kts | 6 +- .../fhir/npm/NpmModelInfoProvider.java | 10 +- Src/java/cql-to-elm-cli/build.gradle.kts | 3 +- .../org/cqframework/cql/cql2elm/cli/Main.java | 6 +- Src/java/cql-to-elm/build.gradle.kts | 2 + .../cql/cql2elm/DefaultModelInfoProvider.kt | 5 +- .../cql/cql2elm/model/ConversionMap.kt | 6 +- .../cql/cql2elm/model/ModelImporter.kt | 3 +- .../cql/cql2elm/NamespaceTests.java | 9 +- .../cql2elm/TestFhirModelInfoProvider.java | 15 +-- .../cql/cql2elm/TestModelInfoProvider.java | 15 +-- .../model/GentestModelInfoProvider.java | 17 ++-- .../model/GentestModelInfoProviderSad1.java | 17 ++-- .../cql/cql2elm/operators/QueryTest.java | 2 +- .../requirements/ElmRequirementsContext.java | 4 +- .../fhir/DataRequirementsProcessor.java | 4 +- Src/java/elm-xmlutil/build.gradle.kts | 1 + .../cqf/cql/engine/execution/CqlEngine.java | 4 +- .../cqf/cql/engine/execution/Libraries.java | 4 +- Src/java/model-xmlutil/build.gradle.kts | 1 + .../xmlutil/ModelInfoReaderProvider.kt | 6 +- .../serializing/xmlutil/XmlModelInfoReader.kt | 47 ++------- .../xmlutil/XmlutilModelInfoLoadingTests.java | 68 +++++++------ Src/java/model/build.gradle.kts | 13 +++ .../kotlin/org/hl7/cql/model/BaseDataType.kt | 5 +- .../kotlin/org/hl7/cql/model/ClassType.kt | 19 ++-- .../org/hl7/cql/model/ClassTypeElement.kt | 3 + .../kotlin/org/hl7/cql/model/DataType.kt | 2 +- .../cql/model/GenericClassSignatureParser.kt | 9 +- .../kotlin/org/hl7/cql/model/ListType.kt | 4 +- .../org/hl7/cql/model/NamespaceManager.kt | 3 - .../kotlin/org/hl7/cql/model/ProfileType.kt | 6 +- .../kotlin/org/hl7/cql/model/SimpleType.kt | 8 +- .../kotlin/org/hl7/cql/model/TupleType.kt | 17 ++-- .../kotlin/org/hl7/cql/model/TypeParameter.kt | 8 +- .../r1/serializing/ModelInfoReader.kt | 20 +--- .../r1/serializing/ModelInfoReaderProvider.kt | 2 +- .../hl7/cql/model/SystemModelInfoProvider.kt | 5 +- .../r1/serializing/BigDecimalSerializer.kt | 2 - .../r1/serializing/ModelInfoReaderFactory.kt | 3 +- .../org/hl7/cql/model/ChoiceTypeTests.java | 9 +- .../GenericClassSignatureParserTest.java | 30 +++--- .../hl7/cql/model/ModelInfoComparerTest.java | 75 +++++++------- .../cql/cql2elm/qdm/QdmModelInfoProvider.java | 82 +++++++--------- .../cql2elm/quick/FhirModelInfoProvider.java | 97 ++++++++----------- .../quick/QICoreModelInfoProvider.java | 50 +++++----- .../quick/QuickFhirModelInfoProvider.java | 28 +++--- .../cql2elm/quick/QuickModelInfoProvider.java | 37 ++++--- .../quick/UsCoreModelInfoProvider.java | 39 ++++---- .../tools/xsd-to-modelinfo/build.gradle.kts | 1 + .../cql/tools/xsd2modelinfo/Main.java | 9 +- .../tools/xsd2modelinfo/ModelImporter.java | 13 ++- 52 files changed, 399 insertions(+), 455 deletions(-) rename Src/java/model/src/{commonMain => jvmMain}/kotlin/org/hl7/cql/model/SystemModelInfoProvider.kt (84%) rename Src/java/model/src/{commonMain => jvmMain}/kotlin/org/hl7/elm_modelinfo/r1/serializing/BigDecimalSerializer.kt (95%) rename Src/java/model/src/{commonMain => jvmMain}/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderFactory.kt (94%) rename Src/java/model/src/jvmTest/{resources => java}/org/hl7/cql/model/ChoiceTypeTests.java (99%) rename Src/java/model/src/jvmTest/{resources => java}/org/hl7/cql/model/GenericClassSignatureParserTest.java (92%) rename Src/java/model/src/jvmTest/{resources => java}/org/hl7/cql/model/ModelInfoComparerTest.java (91%) diff --git a/Src/java/buildSrc/src/main/kotlin/cql.kotlin-multiplatform-conventions.gradle.kts b/Src/java/buildSrc/src/main/kotlin/cql.kotlin-multiplatform-conventions.gradle.kts index b3f60adb7..09553bb56 100644 --- a/Src/java/buildSrc/src/main/kotlin/cql.kotlin-multiplatform-conventions.gradle.kts +++ b/Src/java/buildSrc/src/main/kotlin/cql.kotlin-multiplatform-conventions.gradle.kts @@ -83,7 +83,7 @@ kotlin { jvmTest { dependencies { - implementation(kotlin("test-junit")) + implementation(kotlin("test-junit5")) } } @@ -95,6 +95,10 @@ kotlin { } } +tasks.withType { + useJUnitPlatform() +} + tasks.register("dokkaHtmlJar") { dependsOn(tasks.dokkaHtml) from(tasks.dokkaHtml.flatMap { it.outputDirectory }) diff --git a/Src/java/cqf-fhir-npm/src/main/java/org/cqframework/fhir/npm/NpmModelInfoProvider.java b/Src/java/cqf-fhir-npm/src/main/java/org/cqframework/fhir/npm/NpmModelInfoProvider.java index f6d089087..91513e93c 100644 --- a/Src/java/cqf-fhir-npm/src/main/java/org/cqframework/fhir/npm/NpmModelInfoProvider.java +++ b/Src/java/cqf-fhir-npm/src/main/java/org/cqframework/fhir/npm/NpmModelInfoProvider.java @@ -1,5 +1,8 @@ package org.cqframework.fhir.npm; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -52,7 +55,9 @@ public ModelInfo load(ModelIdentifier modelIdentifier) { } InputStream is = new ByteArrayInputStream(a.getData()); var reader = new XmlModelInfoReader(); - return reader.read(is); + + var source = buffered(asSource(is)); + return reader.read(source); } } } @@ -60,8 +65,7 @@ public ModelInfo load(ModelIdentifier modelIdentifier) { logger.logDebugMessage( ILoggingService.LogCategory.PROGRESS, String.format( - "Exceptions occurred attempting to load npm library for model %s", - modelIdentifier.toString())); + "Exceptions occurred attempting to load npm library for model %s", modelIdentifier)); } } diff --git a/Src/java/cql-to-elm-cli/build.gradle.kts b/Src/java/cql-to-elm-cli/build.gradle.kts index bbe94b021..108ac463e 100644 --- a/Src/java/cql-to-elm-cli/build.gradle.kts +++ b/Src/java/cql-to-elm-cli/build.gradle.kts @@ -16,10 +16,9 @@ dependencies { implementation(project(":model-xmlutil")) implementation(project(":elm-xmlutil")) implementation(project(":ucum")) + implementation("org.jetbrains.kotlinx:kotlinx-io-core-jvm:0.6.0") implementation("net.sf.jopt-simple:jopt-simple:4.7") implementation("org.slf4j:slf4j-simple:2.0.13") implementation("org.glassfish.jaxb:jaxb-runtime:4.0.5") implementation("org.eclipse.persistence:org.eclipse.persistence.moxy:4.0.2") - testImplementation(project(":model-xmlutil")) - testImplementation(project(":elm-xmlutil")) } \ No newline at end of file diff --git a/Src/java/cql-to-elm-cli/src/main/java/org/cqframework/cql/cql2elm/cli/Main.java b/Src/java/cql-to-elm-cli/src/main/java/org/cqframework/cql/cql2elm/cli/Main.java index 13dfd626a..cf1fe80cd 100644 --- a/Src/java/cql-to-elm-cli/src/main/java/org/cqframework/cql/cql2elm/cli/Main.java +++ b/Src/java/cql-to-elm-cli/src/main/java/org/cqframework/cql/cql2elm/cli/Main.java @@ -1,9 +1,12 @@ package org.cqframework.cql.cql2elm.cli; import static java.nio.file.FileVisitResult.CONTINUE; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; import static org.cqframework.cql.cql2elm.CqlTranslator.fromFile; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.FileVisitResult; @@ -27,8 +30,9 @@ public class Main { public static ModelInfoProvider getModelInfoProvider(File modelInfoXML) { try { + var source = buffered(asSource(new FileInputStream(modelInfoXML))); final ModelInfo modelInfo = - ModelInfoReaderFactory.getReader("application/xml").read(modelInfoXML); + ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(source); return (ModelIdentifier modelIdentifier) -> modelInfo; } catch (IOException e) { System.err.printf("Could not load model-info XML: %s%n", modelInfoXML); diff --git a/Src/java/cql-to-elm/build.gradle.kts b/Src/java/cql-to-elm/build.gradle.kts index 67884252c..fcf433e91 100644 --- a/Src/java/cql-to-elm/build.gradle.kts +++ b/Src/java/cql-to-elm/build.gradle.kts @@ -7,6 +7,8 @@ dependencies { api(project(":model")) api(project(":elm")) + implementation("org.jetbrains.kotlinx:kotlinx-io-core-jvm:0.6.0") + // Temporary until we can get rid of the dependency on wrapping // the CQL annotations in a JAXBElement for narrative generation implementation("jakarta.xml.bind:jakarta.xml.bind-api:4.0.1") diff --git a/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/DefaultModelInfoProvider.kt b/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/DefaultModelInfoProvider.kt index caa0541a8..3eaae83f5 100644 --- a/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/DefaultModelInfoProvider.kt +++ b/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/DefaultModelInfoProvider.kt @@ -4,6 +4,8 @@ package org.cqframework.cql.cql2elm import java.io.* import java.nio.file.Path +import kotlinx.io.asSource +import kotlinx.io.buffered import org.cqframework.cql.cql2elm.model.Version import org.hl7.cql.model.ModelIdentifier import org.hl7.cql.model.ModelInfoProvider @@ -100,7 +102,8 @@ class DefaultModelInfoProvider() : ModelInfoProvider, PathAware { } try { val inputStream: InputStream = FileInputStream(modelFile) - return ModelInfoReaderFactory.getReader("application/xml")?.read(inputStream) + return ModelInfoReaderFactory.getReader("application/xml") + ?.read(inputStream.asSource().buffered()) } catch (e: IOException) { throw IllegalArgumentException( "Could not load definition for model info ${modelIdentifier.id}.", diff --git a/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/model/ConversionMap.kt b/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/model/ConversionMap.kt index 3f4c99a34..c62a6c39b 100644 --- a/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/model/ConversionMap.kt +++ b/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/model/ConversionMap.kt @@ -87,7 +87,7 @@ class ConversionMap { private fun getAllConversions(fromType: DataType?): List { val conversions: MutableList = ArrayList() var currentType = fromType - while (currentType != null) { + while (currentType != null && currentType != DataType.ANY) { conversions.addAll(getConversions(currentType)) currentType = currentType.baseType } @@ -339,7 +339,7 @@ class ConversionMap { if (result == null) { // NOTE: FHIRPath Implicit conversion from list to singleton - // If the from type is a list and the target type is a singleton (potentially with a + // If the fromType is a list and the target type is a singleton (potentially with a // compatible conversion), // Convert by invoking a singleton result = @@ -361,7 +361,7 @@ class ConversionMap { (allowPromotionAndDemotion || isIntervalPromotionEnabled) -> findIntervalPromotion(fromType, toType, operatorMap) - // If the from type is a choice, attempt to find a conversion from one of the + // If the fromType is a choice, attempt to find a conversion from one of the // choice // types fromType is ChoiceType -> diff --git a/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/model/ModelImporter.kt b/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/model/ModelImporter.kt index e8f689b9b..239010d1b 100644 --- a/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/model/ModelImporter.kt +++ b/Src/java/cql-to-elm/src/main/kotlin/org/cqframework/cql/cql2elm/model/ModelImporter.kt @@ -600,7 +600,7 @@ class ModelImporter(val modelInfo: ModelInfo, val modelManager: ModelManager?) { if (t is ProfileInfo) { ProfileType( qualifiedName, - resolveTypeNameOrSpecifier(t.baseType, t.baseTypeSpecifier) + resolveTypeNameOrSpecifier(t.baseType, t.baseTypeSpecifier) ?: DataType.ANY ) } else { // Added to support generic notation in ModelInfo file for class type names @@ -615,6 +615,7 @@ class ModelImporter(val modelInfo: ModelInfo, val modelManager: ModelManager?) { ClassType( qualifiedName, resolveTypeNameOrSpecifier(t.baseType, t.baseTypeSpecifier) + ?: DataType.ANY ) } } diff --git a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/NamespaceTests.java b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/NamespaceTests.java index c1fc947e1..d5bb42c1d 100644 --- a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/NamespaceTests.java +++ b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/NamespaceTests.java @@ -71,17 +71,20 @@ static void setup() { @Test void namespacePath() { assertThat( - NamespaceManager.getPath(defaultNamespaceInfo.getUri(), "Main"), is("http://cql.hl7.org/public/Main")); + NamespaceManager.Companion.getPath(defaultNamespaceInfo.getUri(), "Main"), + is("http://cql.hl7.org/public/Main")); } @Test void namespaceNamePart() { - assertThat(NamespaceManager.getNamePart("http://cql.hl7.org/public/Main"), is("Main")); + assertThat(NamespaceManager.Companion.getNamePart("http://cql.hl7.org/public/Main"), is("Main")); } @Test void namespaceUriPart() { - assertThat(NamespaceManager.getUriPart("http://cql.hl7.org/public/Main"), is("http://cql.hl7.org/public")); + assertThat( + NamespaceManager.Companion.getUriPart("http://cql.hl7.org/public/Main"), + is("http://cql.hl7.org/public")); } /* Ensure base functionality with a defaulted namespace uri */ diff --git a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/TestFhirModelInfoProvider.java b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/TestFhirModelInfoProvider.java index 92137293c..84c1c548f 100644 --- a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/TestFhirModelInfoProvider.java +++ b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/TestFhirModelInfoProvider.java @@ -1,6 +1,8 @@ package org.cqframework.cql.cql2elm; -import java.io.IOException; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + import org.hl7.cql.model.ModelIdentifier; import org.hl7.cql.model.ModelInfoProvider; import org.hl7.elm_modelinfo.r1.ModelInfo; @@ -18,15 +20,8 @@ public TestFhirModelInfoProvider(Class clazz) { public ModelInfo load(ModelIdentifier modelIdentifier) { if (modelIdentifier.getId().equals("FHIR")) { - try { - return ModelInfoReaderFactory.getReader("application/xml") - .read(clazz.getResourceAsStream("fhir-modelinfo-1.8.xml")); - } 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)); - } + var source = buffered(asSource(clazz.getResourceAsStream("fhir-modelinfo-1.8.xml"))); + return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(source); } return null; diff --git a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/TestModelInfoProvider.java b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/TestModelInfoProvider.java index c647a7171..23e199a5b 100644 --- a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/TestModelInfoProvider.java +++ b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/TestModelInfoProvider.java @@ -1,6 +1,8 @@ package org.cqframework.cql.cql2elm; -import java.io.IOException; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + import org.hl7.cql.model.ModelIdentifier; import org.hl7.cql.model.ModelInfoProvider; import org.hl7.elm_modelinfo.r1.ModelInfo; @@ -9,15 +11,8 @@ public class TestModelInfoProvider implements ModelInfoProvider { public ModelInfo load(ModelIdentifier modelIdentifier) { if (modelIdentifier.getId().equals("Test")) { - try { - return ModelInfoReaderFactory.getReader("application/xml") - .read(TestModelInfoProvider.class.getResourceAsStream("ModelTests/test-modelinfo.xml")); - } 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)); - } + var stream = TestModelInfoProvider.class.getResourceAsStream("ModelTests/test-modelinfo.xml"); + return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(stream))); } return null; diff --git a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/model/GentestModelInfoProvider.java b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/model/GentestModelInfoProvider.java index c580ba337..e443eddfa 100644 --- a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/model/GentestModelInfoProvider.java +++ b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/model/GentestModelInfoProvider.java @@ -1,6 +1,8 @@ package org.cqframework.cql.cql2elm.model; -import java.io.IOException; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + import java.io.InputStream; import org.hl7.cql.model.ModelIdentifier; import org.hl7.cql.model.ModelInfoProvider; @@ -11,16 +13,9 @@ public class GentestModelInfoProvider implements ModelInfoProvider { @Override public ModelInfo load(ModelIdentifier modelIdentifier) { if (modelIdentifier.getId().equals("GENTEST")) { - try { - InputStream is = GentestModelInfoProvider.class.getResourceAsStream( - "/org/cqframework/cql/cql2elm/ModelTests/test-modelinfowithgenerics-happy.xml"); - return ModelInfoReaderFactory.getReader("application/xml").read(is); - } catch (IOException e) { - e.printStackTrace(); - // Do not throw, allow other providers to resolve - // throw new IllegalArgumentException(String.format("Unknown version %s of the GENTEST model.", - // localVersion)); - } + InputStream is = GentestModelInfoProvider.class.getResourceAsStream( + "/org/cqframework/cql/cql2elm/ModelTests/test-modelinfowithgenerics-happy.xml"); + return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(is))); } return null; diff --git a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/model/GentestModelInfoProviderSad1.java b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/model/GentestModelInfoProviderSad1.java index 7c8418019..433eb273a 100644 --- a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/model/GentestModelInfoProviderSad1.java +++ b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/model/GentestModelInfoProviderSad1.java @@ -1,6 +1,8 @@ package org.cqframework.cql.cql2elm.model; -import java.io.IOException; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + import java.io.InputStream; import org.hl7.cql.model.ModelIdentifier; import org.hl7.cql.model.ModelInfoProvider; @@ -11,16 +13,9 @@ public class GentestModelInfoProviderSad1 implements ModelInfoProvider { @Override public ModelInfo load(ModelIdentifier modelIdentifier) { if (modelIdentifier.equals("GENTEST")) { - try { - InputStream is = GentestModelInfoProviderSad1.class.getResourceAsStream( - "/org/cqframework/cql/cql2elm/ModelTests/test-modelinfowithgenerics-sad1.xml"); - return ModelInfoReaderFactory.getReader("application/xml").read(is); - } catch (IOException e) { - e.printStackTrace(); - // Do not throw, allow other providers to resolve - // throw new IllegalArgumentException(String.format("Unknown version %s of the GENTEST model.", - // localVersion)); - } + InputStream is = GentestModelInfoProviderSad1.class.getResourceAsStream( + "/org/cqframework/cql/cql2elm/ModelTests/test-modelinfowithgenerics-sad1.xml"); + return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(is))); } return null; diff --git a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/operators/QueryTest.java b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/operators/QueryTest.java index e71444172..348f50841 100644 --- a/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/operators/QueryTest.java +++ b/Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/operators/QueryTest.java @@ -76,7 +76,7 @@ void mixedMultipleSourceQuery() { def, hasTypeAndResult( Query.class, - "list")); + "list")); } @Test diff --git a/Src/java/elm-fhir/src/main/java/org/cqframework/cql/elm/requirements/ElmRequirementsContext.java b/Src/java/elm-fhir/src/main/java/org/cqframework/cql/elm/requirements/ElmRequirementsContext.java index e7cae93ed..3c73ce8bc 100644 --- a/Src/java/elm-fhir/src/main/java/org/cqframework/cql/elm/requirements/ElmRequirementsContext.java +++ b/Src/java/elm-fhir/src/main/java/org/cqframework/cql/elm/requirements/ElmRequirementsContext.java @@ -714,8 +714,8 @@ public CompiledLibrary resolveLibrary(VersionedIdentifier libraryIdentifier, Str public CompiledLibrary resolveLibraryFromIncludeDef(IncludeDef includeDef) { VersionedIdentifier targetLibraryIdentifier = new VersionedIdentifier() - .withSystem(NamespaceManager.getUriPart(includeDef.getPath())) - .withId(NamespaceManager.getNamePart(includeDef.getPath())) + .withSystem(NamespaceManager.Companion.getUriPart(includeDef.getPath())) + .withId(NamespaceManager.Companion.getNamePart(includeDef.getPath())) .withVersion(includeDef.getVersion()); return resolveLibrary(targetLibraryIdentifier); diff --git a/Src/java/elm-fhir/src/main/java/org/cqframework/cql/elm/requirements/fhir/DataRequirementsProcessor.java b/Src/java/elm-fhir/src/main/java/org/cqframework/cql/elm/requirements/fhir/DataRequirementsProcessor.java index 17ae4df47..fbed43cb1 100644 --- a/Src/java/elm-fhir/src/main/java/org/cqframework/cql/elm/requirements/fhir/DataRequirementsProcessor.java +++ b/Src/java/elm-fhir/src/main/java/org/cqframework/cql/elm/requirements/fhir/DataRequirementsProcessor.java @@ -589,8 +589,8 @@ private org.hl7.fhir.r5.model.RelatedArtifact toRelatedArtifact( } private String getReferenceUrl(String path, String version) { - String uri = NamespaceManager.getUriPart(path); - String name = NamespaceManager.getNamePart(path); + String uri = NamespaceManager.Companion.getUriPart(path); + String name = NamespaceManager.Companion.getNamePart(path); if (uri != null) { // The translator has no way to correctly infer the namespace of the FHIRHelpers library, since it will diff --git a/Src/java/elm-xmlutil/build.gradle.kts b/Src/java/elm-xmlutil/build.gradle.kts index d093c6154..438f0aa83 100644 --- a/Src/java/elm-xmlutil/build.gradle.kts +++ b/Src/java/elm-xmlutil/build.gradle.kts @@ -7,6 +7,7 @@ dependencies { api(project(":model")) api(project(":elm")) + implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.6.0") implementation("io.github.pdvrieze.xmlutil:core:0.90.3") implementation("io.github.pdvrieze.xmlutil:serialization:0.90.3") implementation("io.github.pdvrieze.xmlutil:serialization-jvm:0.90.3") diff --git a/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/CqlEngine.java b/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/CqlEngine.java index 25d73f493..3fb6d50fd 100644 --- a/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/CqlEngine.java +++ b/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/CqlEngine.java @@ -303,8 +303,8 @@ private Library loadAndValidate(VersionedIdentifier libraryIdentifier) { if (library.getIncludes() != null && library.getIncludes().getDef() != null) { for (IncludeDef include : library.getIncludes().getDef()) { this.loadAndValidate(new VersionedIdentifier() - .withSystem(NamespaceManager.getUriPart(include.getPath())) - .withId(NamespaceManager.getNamePart(include.getPath())) + .withSystem(NamespaceManager.Companion.getUriPart(include.getPath())) + .withId(NamespaceManager.Companion.getNamePart(include.getPath())) .withVersion(include.getVersion())); } } diff --git a/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/Libraries.java b/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/Libraries.java index 11d68f27b..9658def52 100644 --- a/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/Libraries.java +++ b/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/Libraries.java @@ -142,8 +142,8 @@ public static List getFunctionDefs(final String name, final Library public static VersionedIdentifier toVersionedIdentifier(IncludeDef includeDef) { return new VersionedIdentifier() - .withSystem(NamespaceManager.getUriPart(includeDef.getPath())) - .withId(NamespaceManager.getNamePart(includeDef.getPath())) + .withSystem(NamespaceManager.Companion.getUriPart(includeDef.getPath())) + .withId(NamespaceManager.Companion.getNamePart(includeDef.getPath())) .withVersion(includeDef.getVersion()); } } diff --git a/Src/java/model-xmlutil/build.gradle.kts b/Src/java/model-xmlutil/build.gradle.kts index 0d6fd9f6a..c2200e9d2 100644 --- a/Src/java/model-xmlutil/build.gradle.kts +++ b/Src/java/model-xmlutil/build.gradle.kts @@ -5,6 +5,7 @@ plugins { dependencies { api(project(":model")) + implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.6.0") implementation("io.github.pdvrieze.xmlutil:core:0.90.3") implementation("io.github.pdvrieze.xmlutil:serialization:0.90.3") implementation("io.github.pdvrieze.xmlutil:serialization-jvm:0.90.3") diff --git a/Src/java/model-xmlutil/src/main/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/ModelInfoReaderProvider.kt b/Src/java/model-xmlutil/src/main/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/ModelInfoReaderProvider.kt index 4fe49b11c..4503966cf 100644 --- a/Src/java/model-xmlutil/src/main/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/ModelInfoReaderProvider.kt +++ b/Src/java/model-xmlutil/src/main/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/ModelInfoReaderProvider.kt @@ -6,11 +6,7 @@ import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReader import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReaderProvider class ModelInfoReaderProvider : ModelInfoReaderProvider { - override fun create(contentType: String): ModelInfoReader? { - var contentType: String? = contentType - if (contentType == null) { - contentType = "application/xml" - } + override fun create(contentType: String): ModelInfoReader { return when (contentType) { "application/xml" -> XmlModelInfoReader() else -> diff --git a/Src/java/model-xmlutil/src/main/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/XmlModelInfoReader.kt b/Src/java/model-xmlutil/src/main/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/XmlModelInfoReader.kt index a9cb32915..c85579790 100644 --- a/Src/java/model-xmlutil/src/main/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/XmlModelInfoReader.kt +++ b/Src/java/model-xmlutil/src/main/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/XmlModelInfoReader.kt @@ -2,11 +2,10 @@ package org.hl7.elm_modelinfo.r1.serializing.xmlutil -import java.io.File -import java.io.InputStream -import java.io.Reader -import java.net.URI -import java.net.URL +import kotlinx.io.Source +import kotlinx.io.asInputStream +import kotlinx.io.buffered +import nl.adaptivity.xmlutil.newReader import nl.adaptivity.xmlutil.serialization.XML import nl.adaptivity.xmlutil.xmlStreaming import org.hl7.elm_modelinfo.r1.* @@ -14,41 +13,15 @@ import org.hl7.elm_modelinfo.r1.ModelInfo import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReader class XmlModelInfoReader : ModelInfoReader { - override fun read(src: File): ModelInfo { - src.reader().use { - return read(it) - } - } - - override fun read(src: Reader): ModelInfo { + override fun read(source: Source): ModelInfo { val serializersModule = Serializer.createSerializer() val xml = XML(serializersModule) - val modelInfo = xml.decodeFromReader(ModelInfo.serializer(), xmlStreaming.newReader(src)) + val modelInfo = + xml.decodeFromReader( + ModelInfo.serializer(), + xmlStreaming.newReader(source.buffered().asInputStream()) + ) return modelInfo } - - override fun read(src: InputStream): ModelInfo { - src.reader().use { - return read(it) - } - } - - override fun read(url: URL): ModelInfo { - url.openStream().use { - return read(it) - } - } - - override fun read(uri: URI): ModelInfo { - uri.toURL().openStream().use { - return read(it) - } - } - - override fun read(string: String): ModelInfo { - string.reader().use { - return read(it) - } - } } diff --git a/Src/java/model-xmlutil/src/test/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/XmlutilModelInfoLoadingTests.java b/Src/java/model-xmlutil/src/test/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/XmlutilModelInfoLoadingTests.java index aea491e88..943ec8f99 100644 --- a/Src/java/model-xmlutil/src/test/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/XmlutilModelInfoLoadingTests.java +++ b/Src/java/model-xmlutil/src/test/java/org/hl7/elm_modelinfo/r1/serializing/xmlutil/XmlutilModelInfoLoadingTests.java @@ -1,159 +1,167 @@ package org.hl7.elm_modelinfo.r1.serializing.xmlutil; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + import org.cqframework.cql.cql2elm.qdm.QdmModelInfoProvider; import org.cqframework.cql.cql2elm.quick.FhirModelInfoProvider; import org.cqframework.cql.cql2elm.quick.QICoreModelInfoProvider; import org.cqframework.cql.cql2elm.quick.QuickFhirModelInfoProvider; import org.cqframework.cql.cql2elm.quick.QuickModelInfoProvider; import org.hl7.cql.model.SystemModelInfoProvider; +import org.hl7.elm_modelinfo.r1.ModelInfo; import org.junit.jupiter.api.Test; class XmlutilModelInfoLoadingTests { static XmlModelInfoReader reader = new XmlModelInfoReader(); + private ModelInfo read(Class clazz, String resource) { + var stream = clazz.getResourceAsStream(resource); + return reader.read(buffered(asSource(stream))); + } + @Test void system() { - reader.read(SystemModelInfoProvider.class.getResourceAsStream("/org/hl7/elm/r1/system-modelinfo.xml")); + read(SystemModelInfoProvider.class, "/org/hl7/elm/r1/system-modelinfo.xml"); } @Test void uSCore310() { - reader.read(QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/uscore-modelinfo-3.1.0.xml")); + read(QuickModelInfoProvider.class, "/org/hl7/fhir/uscore-modelinfo-3.1.0.xml"); } @Test void uSCore311() { - reader.read(QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/uscore-modelinfo-3.1.1.xml")); + read(QuickModelInfoProvider.class, "/org/hl7/fhir/uscore-modelinfo-3.1.1.xml"); } @Test void quickFhir301() { - reader.read( - QuickFhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/quickfhir-modelinfo-3.0.1.xml")); + read(QuickFhirModelInfoProvider.class, "/org/hl7/fhir/quickfhir-modelinfo-3.0.1.xml"); } @Test void quick330() { - reader.read(QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/quick-modelinfo-3.3.0.xml")); + read(QuickModelInfoProvider.class, "/org/hl7/fhir/quick-modelinfo-3.3.0.xml"); } @Test void quick300() { - reader.read(QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/quick-modelinfo-3.0.0.xml")); + read(QuickModelInfoProvider.class, "/org/hl7/fhir/quick-modelinfo-3.0.0.xml"); } @Test void quick() { - reader.read(QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/quick-modelinfo.xml")); + read(QuickModelInfoProvider.class, "/org/hl7/fhir/quick-modelinfo.xml"); } @Test void qICore400() { - reader.read(QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.0.0.xml")); + read(QICoreModelInfoProvider.class, "/org/hl7/fhir/qicore-modelinfo-4.0.0.xml"); } @Test void qICore410() { - reader.read(QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.1.0.xml")); + read(QICoreModelInfoProvider.class, "/org/hl7/fhir/qicore-modelinfo-4.1.0.xml"); } @Test void qICore411() { - reader.read(QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.1.1.xml")); + read(QICoreModelInfoProvider.class, "/org/hl7/fhir/qicore-modelinfo-4.1.1.xml"); } @Test void qdm() { - reader.read(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo.xml")); + read(QdmModelInfoProvider.class, "/gov/healthit/qdm/qdm-modelinfo.xml"); } @Test void qdm420() { - reader.read(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-4.2.xml")); + read(QdmModelInfoProvider.class, "/gov/healthit/qdm/qdm-modelinfo-4.2.xml"); } @Test void qdm430() { - reader.read(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-4.3.xml")); + read(QdmModelInfoProvider.class, "/gov/healthit/qdm/qdm-modelinfo-4.3.xml"); } @Test void qdm500() { - reader.read(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.xml")); + read(QdmModelInfoProvider.class, "/gov/healthit/qdm/qdm-modelinfo-5.0.xml"); } @Test void qdm501() { - reader.read(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.1.xml")); + read(QdmModelInfoProvider.class, "/gov/healthit/qdm/qdm-modelinfo-5.0.1.xml"); } @Test void qdm502() { - reader.read(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.2.xml")); + read(QdmModelInfoProvider.class, "/gov/healthit/qdm/qdm-modelinfo-5.0.2.xml"); } @Test void qdm530() { - reader.read(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.3.xml")); + read(QdmModelInfoProvider.class, "/gov/healthit/qdm/qdm-modelinfo-5.3.xml"); } @Test void qdm540() { - reader.read(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.4.xml")); + read(QdmModelInfoProvider.class, "/gov/healthit/qdm/qdm-modelinfo-5.4.xml"); } @Test void qdm550() { - reader.read(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.5.xml")); + read(QdmModelInfoProvider.class, "/gov/healthit/qdm/qdm-modelinfo-5.5.xml"); } @Test void qdm560() { - reader.read(QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.6.xml")); + read(QdmModelInfoProvider.class, "/gov/healthit/qdm/qdm-modelinfo-5.6.xml"); } @Test void fhirModelInfo102() { - reader.read(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.0.2.xml")); + read(FhirModelInfoProvider.class, "/org/hl7/fhir/fhir-modelinfo-1.0.2.xml"); } @Test void fhirModelInfo140() { - reader.read(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.4.xml")); + read(FhirModelInfoProvider.class, "/org/hl7/fhir/fhir-modelinfo-1.4.xml"); } @Test void fhirModelInfo160() { - reader.read(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.6.xml")); + read(FhirModelInfoProvider.class, "/org/hl7/fhir/fhir-modelinfo-1.6.xml"); } @Test void fhirModelInfo180() { - reader.read(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.8.xml")); + read(FhirModelInfoProvider.class, "/org/hl7/fhir/fhir-modelinfo-1.8.xml"); } @Test void fhirModelInfo300() { - reader.read(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.0.0.xml")); + read(FhirModelInfoProvider.class, "/org/hl7/fhir/fhir-modelinfo-3.0.0.xml"); } @Test void fhirModelInfo301() { - reader.read(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.0.1.xml")); + read(FhirModelInfoProvider.class, "/org/hl7/fhir/fhir-modelinfo-3.0.1.xml"); } @Test void fhirModelInfo320() { - reader.read(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.2.0.xml")); + read(FhirModelInfoProvider.class, "/org/hl7/fhir/fhir-modelinfo-3.2.0.xml"); } @Test void fhirModelInfo400() { - reader.read(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-4.0.0.xml")); + read(FhirModelInfoProvider.class, "/org/hl7/fhir/fhir-modelinfo-4.0.0.xml"); } @Test void fhirModelInfo401() { - reader.read(FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-4.0.1.xml")); + read(FhirModelInfoProvider.class, "/org/hl7/fhir/fhir-modelinfo-4.0.1.xml"); } } diff --git a/Src/java/model/build.gradle.kts b/Src/java/model/build.gradle.kts index 68f8a8bb4..ec2810536 100644 --- a/Src/java/model/build.gradle.kts +++ b/Src/java/model/build.gradle.kts @@ -5,9 +5,22 @@ plugins { kotlin { sourceSets { + commonMain { + dependencies { + api("org.jetbrains.kotlinx:kotlinx-io-core:0.6.0") + } + } jvmTest { dependencies { implementation(project(":model-xmlutil")) + implementation("org.hamcrest:hamcrest-all:1.3") + implementation("uk.co.datumedge:hamcrest-json:0.2") + implementation("org.slf4j:slf4j-simple:2.0.13") + + // These are JAXB dependencies excluded because the libraries need to work + // on Android. But for test purposes we use them pretty much everywhere. + runtimeOnly("org.eclipse.persistence:org.eclipse.persistence.moxy:4.0.2") + runtimeOnly("org.eclipse.parsson:parsson:1.1.5") } } } diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/BaseDataType.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/BaseDataType.kt index ff1328081..143da16d0 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/BaseDataType.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/BaseDataType.kt @@ -1,7 +1,8 @@ package org.hl7.cql.model -abstract class BaseDataType protected constructor(baseType: DataType? = DataType.ANY) : DataType { - override val baseType: DataType = baseType ?: DataType.ANY +abstract class BaseDataType protected constructor(private val base: DataType? = null) : DataType { + override val baseType + get() = base ?: DataType.ANY override fun toLabel(): String = toString() diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ClassType.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ClassType.kt index e6bb6777c..ce7b4c4ac 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ClassType.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ClassType.kt @@ -1,11 +1,7 @@ package org.hl7.cql.model -import java.util.* - @Suppress("TooManyFunctions", "ComplexCondition") -open class ClassType -@JvmOverloads -constructor( +open class ClassType( final override val name: String, baseType: DataType? = null, val elements: MutableList = mutableListOf(), @@ -18,6 +14,11 @@ constructor( */ var genericParameters: MutableList = mutableListOf() ) : BaseDataType(baseType), NamedType { + + // For Java compatibility. Can be deleted once tests are updated. + constructor( + name: String, + ) : this(name, null, mutableListOf(), mutableListOf()) init { require(name.isNotEmpty()) { "name can not be empty" } } @@ -144,10 +145,10 @@ constructor( val sortedElements: List get() = elements.sortedWith(compareBy { it.name }) - private var baseElementMap: LinkedHashMap? = null + private var baseElementMap: HashMap? = null get() { if (field == null) { - field = LinkedHashMap() + field = HashMap() if (baseType is ClassType) { (baseType as ClassType).gatherElements(field!!) } @@ -156,7 +157,7 @@ constructor( return field } - private fun gatherElements(elementMap: LinkedHashMap) { + private fun gatherElements(elementMap: HashMap) { if (baseType is ClassType) { (baseType as ClassType).gatherElements(elementMap) } @@ -169,7 +170,7 @@ constructor( val allElements: List get() { // Get the baseClass elements into a map by name - val elementMap = LinkedHashMap(baseElementMap) + val elementMap = baseElementMap?.toMutableMap() ?: mutableMapOf() // Add this class's elements, overwriting baseClass definitions where applicable for (el in elements) { diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ClassTypeElement.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ClassTypeElement.kt index b4d3070df..d6659730b 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ClassTypeElement.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ClassTypeElement.kt @@ -8,6 +8,9 @@ data class ClassTypeElement( val target: String? = null ) { + // For Java compatibility. Can be deleted once tests are updated. + constructor(name: String, type: DataType) : this(name, type, false, false, null) + init { require(name.isNotEmpty()) { "name can not be empty" } } diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/DataType.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/DataType.kt index 504c5c329..c640a4089 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/DataType.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/DataType.kt @@ -23,6 +23,6 @@ interface DataType { fun instantiate(context: InstantiationContext): DataType companion object { - @JvmField val ANY: SimpleType = SimpleType("System.Any") + val ANY: SimpleType = SimpleType("System.Any") } } diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/GenericClassSignatureParser.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/GenericClassSignatureParser.kt index 7d60b42ea..3447ae47b 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/GenericClassSignatureParser.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/GenericClassSignatureParser.kt @@ -15,7 +15,6 @@ class GenericClassSignatureParser( var baseType: String?, private val resolvedTypes: MutableMap ) { - @JvmOverloads constructor( genericSignature: String, resolvedTypes: MutableMap = HashMap() @@ -111,17 +110,17 @@ class GenericClassSignatureParser( /** * Identifies the data type for the named type. If the argument is null, the return type will be - * null. + * "Any". * * @param parameterType * @return The parameter's type */ - private fun resolveTypeName(parameterType: String?): DataType? { + private fun resolveTypeName(parameterType: String?): DataType { return if (isValidGenericSignature(parameterType)) { handleBoundType(parameterType!!) } else { if (parameterType == null) { - null + DataType.ANY } else { resolveType(parameterType) } @@ -270,7 +269,7 @@ class GenericClassSignatureParser( c == ',' && openBracketCount > 0 -> signatureCharArray[index] = '|' } } - return String(signatureCharArray) + return signatureCharArray.concatToString() } /** diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ListType.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ListType.kt index 7b56adf9b..f2c76b34e 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ListType.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ListType.kt @@ -1,7 +1,5 @@ package org.hl7.cql.model -import org.hl7.cql.model.DataType.Companion.ANY - data class ListType(val elementType: DataType) : BaseDataType() { override fun isSubTypeOf(other: DataType): Boolean { return if (other is ListType) { @@ -23,7 +21,7 @@ data class ListType(val elementType: DataType) : BaseDataType() { override fun isInstantiable(callType: DataType, context: InstantiationContext): Boolean { return when (callType) { - ANY -> elementType.isInstantiable(callType, context) + DataType.ANY -> elementType.isInstantiable(callType, context) is ListType -> elementType.isInstantiable(callType.elementType, context) else -> { val instantiableElements = diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/NamespaceManager.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/NamespaceManager.kt index d9a78d02d..fd603d935 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/NamespaceManager.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/NamespaceManager.kt @@ -44,12 +44,10 @@ class NamespaceManager { } companion object { - @JvmStatic fun getPath(namespaceUri: String?, name: String): String { return namespaceUri?.let { "$it/$name" } ?: name } - @JvmStatic fun getUriPart(namespaceQualifiedName: String?): String? { return namespaceQualifiedName ?.lastIndexOf('/') @@ -57,7 +55,6 @@ class NamespaceManager { ?.let { namespaceQualifiedName.substring(0, it) } } - @JvmStatic fun getNamePart(namespaceQualifiedName: String?): String? { return namespaceQualifiedName ?.lastIndexOf("/") diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ProfileType.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ProfileType.kt index c77641111..952cc678c 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ProfileType.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/ProfileType.kt @@ -20,11 +20,9 @@ package org.hl7.cql.model * NOTE: This behavior was subsequently changed due to the inconsistency it introduces between * retrieves and general-purpose expressions. QDM still defines a base type with profiles for * positive and negative aspects, but the retrieve will now return the profile type, not the base - * type. See github issue #131 for a detailed discussion of this change. + * type. See GitHub issue #131 for a detailed discussion of this change. */ -class ProfileType -@JvmOverloads -constructor( +class ProfileType( name: String, baseType: DataType?, elements: MutableList = mutableListOf() diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/SimpleType.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/SimpleType.kt index f6c5f559e..16adaf762 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/SimpleType.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/SimpleType.kt @@ -1,13 +1,13 @@ package org.hl7.cql.model -data class SimpleType -@JvmOverloads -constructor( +data class SimpleType( override val name: String, - private val base: DataType? = null, + val base: DataType? = null, override var target: String? = null ) : BaseDataType(base), NamedType { + constructor(name: String) : this(name, null, null) + init { require(name.isNotEmpty()) { "name can not be empty" } } diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/TupleType.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/TupleType.kt index 36aa49959..80b828147 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/TupleType.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/TupleType.kt @@ -1,20 +1,19 @@ package org.hl7.cql.model -import java.util.SortedSet +class TupleType private constructor(val elements: Set) : BaseDataType() { -class TupleType private constructor(val elements: SortedSet) : BaseDataType() { + constructor(elements: Iterable) : this(elements.toSet()) - constructor(elements: Iterable) : this(elements.sortedByName()) + val sortedElements = elements.sortedWith(compareBy { it.name }) override fun isSubTypeOf(other: DataType): Boolean { - return if (other is TupleType) { - elements.zipAll(other.elements) { a, b -> a.isSubTypeOf(b) } + return if (other is TupleType) { sortedElements.zipAll(other.sortedElements) { a, b -> a.isSubTypeOf(b) } } else super.isSubTypeOf(other) } override fun isSuperTypeOf(other: DataType): Boolean { return if (other is TupleType) { - elements.zipAll(other.elements) { a, b -> a.isSuperTypeOf(b) } + sortedElements.zipAll(other.sortedElements) { a, b -> a.isSuperTypeOf(b) } } else super.isSuperTypeOf(other) } @@ -35,7 +34,7 @@ class TupleType private constructor(val elements: SortedSet) : return when (callType) { DataType.ANY -> elements.all { it.type.isInstantiable(callType, context) } is TupleType -> { - elements.zipAll(callType.elements) { a, b -> + sortedElements.zipAll(callType.sortedElements) { a, b -> a.type.isInstantiable(b.type, context) } } @@ -48,7 +47,6 @@ class TupleType private constructor(val elements: SortedSet) : TupleType( elements .map { TupleTypeElement(it.name, it.type.instantiate(context), false) } - .sortedByName() ) } else this } @@ -68,9 +66,6 @@ class TupleType private constructor(val elements: SortedSet) : } companion object { - private fun Iterable.sortedByName(): SortedSet = - toSortedSet(compareBy { it.name }) - private fun Collection.zipAll( other: Collection, predicate: (a: TupleTypeElement, b: TupleTypeElement) -> Boolean diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/TypeParameter.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/TypeParameter.kt index 35ce32ed2..1f9a88c8c 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/TypeParameter.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/TypeParameter.kt @@ -1,12 +1,14 @@ package org.hl7.cql.model -data class TypeParameter -@JvmOverloads -constructor( +data class TypeParameter( val identifier: String, val constraint: TypeParameterConstraint = TypeParameterConstraint.NONE, val constraintType: DataType? = null ) : BaseDataType() { + + // For Java compatibility. Can be deleted once tests are updated. + constructor(identifier: String) : this(identifier, TypeParameterConstraint.NONE, null) + init { require(identifier.isNotEmpty()) { "identifier can not be empty" } if (constraint == TypeParameterConstraint.TYPE) { diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReader.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReader.kt index 2e30643ec..07b0bd2fd 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReader.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReader.kt @@ -1,25 +1,9 @@ @file:Suppress("PackageNaming") package org.hl7.elm_modelinfo.r1.serializing - -import java.io.File -import java.io.IOException -import java.io.InputStream -import java.io.Reader -import java.net.URI -import java.net.URL +import kotlinx.io.Source import org.hl7.elm_modelinfo.r1.ModelInfo interface ModelInfoReader { - @Throws(IOException::class) fun read(src: File): ModelInfo? - - @Throws(IOException::class) fun read(src: Reader): ModelInfo? - - @Throws(IOException::class) fun read(src: InputStream): ModelInfo? - - @Throws(IOException::class) fun read(url: URL): ModelInfo? - - @Throws(IOException::class) fun read(uri: URI): ModelInfo? - - @Throws(IOException::class) fun read(string: String): ModelInfo? + fun read(source: Source): ModelInfo } diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderProvider.kt b/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderProvider.kt index ece685b58..e7d33b90f 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderProvider.kt +++ b/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderProvider.kt @@ -3,5 +3,5 @@ package org.hl7.elm_modelinfo.r1.serializing interface ModelInfoReaderProvider { - fun create(contentType: String): ModelInfoReader? + fun create(contentType: String): ModelInfoReader } diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/SystemModelInfoProvider.kt b/Src/java/model/src/jvmMain/kotlin/org/hl7/cql/model/SystemModelInfoProvider.kt similarity index 84% rename from Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/SystemModelInfoProvider.kt rename to Src/java/model/src/jvmMain/kotlin/org/hl7/cql/model/SystemModelInfoProvider.kt index a24c62398..d389d47f1 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/cql/model/SystemModelInfoProvider.kt +++ b/Src/java/model/src/jvmMain/kotlin/org/hl7/cql/model/SystemModelInfoProvider.kt @@ -1,5 +1,7 @@ package org.hl7.cql.model +import kotlinx.io.asSource +import kotlinx.io.buffered import org.hl7.elm_modelinfo.r1.ModelInfo import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReaderFactory.getReader @@ -24,7 +26,8 @@ class SystemModelInfoProvider : ModelInfoProvider { val reader = getReader("application/xml") val stream = this::class.java.getResourceAsStream("/org/hl7/elm/r1/system-modelinfo.xml") - stream?.use { reader?.read(it) } + checkNotNull(stream) { "Could not find system model info" } + reader.read(stream.asSource().buffered()) } else null } } diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/BigDecimalSerializer.kt b/Src/java/model/src/jvmMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/BigDecimalSerializer.kt similarity index 95% rename from Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/BigDecimalSerializer.kt rename to Src/java/model/src/jvmMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/BigDecimalSerializer.kt index a6f1ea4a3..ab0065bde 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/BigDecimalSerializer.kt +++ b/Src/java/model/src/jvmMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/BigDecimalSerializer.kt @@ -1,5 +1,3 @@ -@file:Suppress("detekt:all") - package org.hl7.elm_modelinfo.r1.serializing import java.math.BigDecimal diff --git a/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderFactory.kt b/Src/java/model/src/jvmMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderFactory.kt similarity index 94% rename from Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderFactory.kt rename to Src/java/model/src/jvmMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderFactory.kt index 6f7fe29e7..1cfc18bfb 100644 --- a/Src/java/model/src/commonMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderFactory.kt +++ b/Src/java/model/src/jvmMain/kotlin/org/hl7/elm_modelinfo/r1/serializing/ModelInfoReaderFactory.kt @@ -15,8 +15,7 @@ object ModelInfoReaderFactory { } @Suppress("TooGenericExceptionThrown") - @JvmStatic - fun getReader(contentType: String): ModelInfoReader? { + fun getReader(contentType: String): ModelInfoReader { val providers = providers(false) if (providers.hasNext()) { val p = providers.next() diff --git a/Src/java/model/src/jvmTest/resources/org/hl7/cql/model/ChoiceTypeTests.java b/Src/java/model/src/jvmTest/java/org/hl7/cql/model/ChoiceTypeTests.java similarity index 99% rename from Src/java/model/src/jvmTest/resources/org/hl7/cql/model/ChoiceTypeTests.java rename to Src/java/model/src/jvmTest/java/org/hl7/cql/model/ChoiceTypeTests.java index 38608148a..f71034fec 100644 --- a/Src/java/model/src/jvmTest/resources/org/hl7/cql/model/ChoiceTypeTests.java +++ b/Src/java/model/src/jvmTest/java/org/hl7/cql/model/ChoiceTypeTests.java @@ -1,10 +1,11 @@ package org.hl7.cql.model; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; import java.util.Set; -import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; class ChoiceTypeTests { @@ -36,4 +37,4 @@ void choiceTypeIsNotCompatible() { assertFalse(first.isSubSetOf(second)); assertFalse(first.isSuperSetOf(second)); } -} +} \ No newline at end of file diff --git a/Src/java/model/src/jvmTest/resources/org/hl7/cql/model/GenericClassSignatureParserTest.java b/Src/java/model/src/jvmTest/java/org/hl7/cql/model/GenericClassSignatureParserTest.java similarity index 92% rename from Src/java/model/src/jvmTest/resources/org/hl7/cql/model/GenericClassSignatureParserTest.java rename to Src/java/model/src/jvmTest/java/org/hl7/cql/model/GenericClassSignatureParserTest.java index 3364b0ede..c973892f2 100644 --- a/Src/java/model/src/jvmTest/resources/org/hl7/cql/model/GenericClassSignatureParserTest.java +++ b/Src/java/model/src/jvmTest/java/org/hl7/cql/model/GenericClassSignatureParserTest.java @@ -12,7 +12,7 @@ class GenericClassSignatureParserTest { @Test void parseTest1() { - GenericClassSignatureParser genericClassSignatureParser = new GenericClassSignatureParser("MyType"); + GenericClassSignatureParser genericClassSignatureParser = new GenericClassSignatureParser("MyType", new HashMap<>()); if (genericClassSignatureParser.isValidGenericSignature()) { ClassType signature = genericClassSignatureParser.parseGenericSignature(); assertThat(signature.getName(), is("MyType")); @@ -26,7 +26,7 @@ void parseTest1() { @Test void parseTest2() { - ClassType collectionType = new ClassType("Collection", null); + ClassType collectionType = new ClassType("Collection"); Map resolvedTypes = new HashMap<>(); resolvedTypes.put("Collection", collectionType); GenericClassSignatureParser genericClassSignatureParser = @@ -50,8 +50,8 @@ void parseTest2() { @Test void parseTest3() { - ClassType collectionType = new ClassType("Collection", null); - ClassType objectType = new ClassType("Object", null); + ClassType collectionType = new ClassType("Collection"); + ClassType objectType = new ClassType("Object"); Map resolvedTypes = new HashMap<>(); resolvedTypes.put("Collection", collectionType); resolvedTypes.put("Object", objectType); @@ -82,7 +82,7 @@ void parseTest3() { @Test void parseTest4() { try { - ClassType collectionType = new ClassType("Collection", null); + ClassType collectionType = new ClassType("Collection"); Map resolvedTypes = new HashMap<>(); resolvedTypes.put("Collection", collectionType); GenericClassSignatureParser genericClassSignatureParser = @@ -96,8 +96,8 @@ void parseTest4() { @Test void parseTest5() { - ClassType objectType = new ClassType("Object", null); - ClassType listType = new ClassType("List", null); + ClassType objectType = new ClassType("Object"); + ClassType listType = new ClassType("List"); listType.addGenericParameter(new TypeParameter("T")); listType.getElements().add(new ClassTypeElement("elements", new TypeParameter("T"), false, false, null)); Map resolvedTypes = new HashMap<>(); @@ -125,12 +125,12 @@ void parseTest5() { @Test void parseTest6() { - ClassType objectType = new ClassType("Object", null); + ClassType objectType = new ClassType("Object"); SimpleType stringType = new SimpleType("String"); - ClassType listType = new ClassType("List", null); + ClassType listType = new ClassType("List"); listType.addGenericParameter(new TypeParameter("T")); listType.getElements().add(new ClassTypeElement("elements", new TypeParameter("T"), false, false, null)); - ClassType mapType = new ClassType("Map", null); + ClassType mapType = new ClassType("Map"); mapType.addGenericParameter(new TypeParameter("K")); mapType.addGenericParameter(new TypeParameter("V")); mapType.addElement(new ClassTypeElement("keys", new TypeParameter("K"), false, false, null)); @@ -164,14 +164,14 @@ void parseTest6() { void parseTest7() { SimpleType integerType = new SimpleType("Integer"); SimpleType stringType = new SimpleType("String"); - ClassType listType = new ClassType("List", null); + ClassType listType = new ClassType("List"); listType.addGenericParameter(new TypeParameter("T")); listType.getElements().add(new ClassTypeElement("elements", new TypeParameter("T"), false, false, null)); - ClassType mapType = new ClassType("Map", null); + ClassType mapType = new ClassType("Map"); mapType.addGenericParameter(new TypeParameter("K")); mapType.addGenericParameter(new TypeParameter("V")); - mapType.addElement(new ClassTypeElement("keys", new TypeParameter("K"), false, false, null)); - mapType.addElement(new ClassTypeElement("values", new TypeParameter("V"), false, false, null)); + mapType.addElement(new ClassTypeElement("keys", new TypeParameter("K", TypeParameter.TypeParameterConstraint.NONE, null), false, false, null)); + mapType.addElement(new ClassTypeElement("values", new TypeParameter("V", TypeParameter.TypeParameterConstraint.NONE, null), false, false, null)); Map resolvedTypes = new HashMap<>(); resolvedTypes.put("Integer", integerType); resolvedTypes.put("String", stringType); @@ -196,4 +196,4 @@ void parseTest7() { fail("Invalid generic class"); } } -} +} \ No newline at end of file diff --git a/Src/java/model/src/jvmTest/resources/org/hl7/cql/model/ModelInfoComparerTest.java b/Src/java/model/src/jvmTest/java/org/hl7/cql/model/ModelInfoComparerTest.java similarity index 91% rename from Src/java/model/src/jvmTest/resources/org/hl7/cql/model/ModelInfoComparerTest.java rename to Src/java/model/src/jvmTest/java/org/hl7/cql/model/ModelInfoComparerTest.java index 0f170f6f9..f75565680 100644 --- a/Src/java/model/src/jvmTest/resources/org/hl7/cql/model/ModelInfoComparerTest.java +++ b/Src/java/model/src/jvmTest/java/org/hl7/cql/model/ModelInfoComparerTest.java @@ -1,24 +1,29 @@ package org.hl7.cql.model; +import static kotlinx.io.JvmCoreKt.asSource; +import static kotlinx.io.CoreKt.buffered; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import java.io.IOException; import java.util.*; import java.util.stream.Collectors; import org.hl7.elm_modelinfo.r1.*; -import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReader; -import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReaderFactory; +import org.hl7.elm_modelinfo.r1.serializing.xmlutil.XmlModelInfoReader; import org.junit.jupiter.api.Test; public class ModelInfoComparerTest { - static ModelInfoReader reader = ModelInfoReaderFactory.getReader("application/xml"); + private ModelInfo readModelInfo(String resourceName) { + var reader = new XmlModelInfoReader(); + var stream = ModelInfoComparerTest.class.getResourceAsStream(resourceName); + return reader.read(buffered(asSource(stream))); + } @Test - void compareModelInfo() throws IOException { - ModelInfo a = reader.read(ModelInfoComparerTest.class.getResourceAsStream("a-modelinfo.xml")); - ModelInfo b = reader.read(ModelInfoComparerTest.class.getResourceAsStream("b-modelinfo.xml")); + void compareModelInfo() { + var a = readModelInfo("a-modelinfo.xml"); + var b = readModelInfo("b-modelinfo.xml"); + ModelInfoCompareContext differences = new ModelInfoCompareContext(); compareModelInfo(differences, a, b); @@ -37,20 +42,18 @@ void compareModelInfo() throws IOException { // @Test // Not an actual test, Used to determine differences between current and updated model info from the MAT team - public void compareMATModelInfo() throws IOException { - ModelInfo a = reader.read(ModelInfoComparerTest.class.getResourceAsStream("fhir-modelinfo-4.0.1.xml")); - ModelInfo b = reader.read(ModelInfoComparerTest.class.getResourceAsStream("mat-fhir-modelinfo-4.0.1.xml")); - + public void compareMATModelInfo() { + var a = readModelInfo("fhir-modelinfo-4.0.1.xml"); + var b = readModelInfo("mat-fhir-modelinfo-4.0.1.xml"); ModelInfoCompareContext differences = new ModelInfoCompareContext(); compareModelInfo(differences, a, b); assertThat(differences.length(), is(0)); } @Test - void compareNewModelInfo() throws IOException { - ModelInfo a = reader.read(ModelInfoComparerTest.class.getResourceAsStream("fhir-modelinfo-4.0.1.xml")); - ModelInfo b = reader.read(ModelInfoComparerTest.class.getResourceAsStream("new-fhir-modelinfo-4.0.1.xml")); - + void compareNewModelInfo() { + var a = readModelInfo("fhir-modelinfo-4.0.1.xml"); + var b = readModelInfo("new-fhir-modelinfo-4.0.1.xml"); ModelInfoCompareContext differences = new ModelInfoCompareContext(); compareModelInfo(differences, a, b); assertThat( @@ -66,11 +69,9 @@ void compareNewModelInfo() throws IOException { } @Test - void compareMetadataModelInfo() throws IOException { - ModelInfo a = reader.read(ModelInfoComparerTest.class.getResourceAsStream("fhir-modelinfo-4.0.1-1.5.1.xml")); - ModelInfo b = - reader.read(ModelInfoComparerTest.class.getResourceAsStream("fhir-modelinfo-4.0.1-with-metadata.xml")); - + void compareMetadataModelInfo() { + var a = readModelInfo("fhir-modelinfo-4.0.1-1.5.1.xml"); + var b = readModelInfo("fhir-modelinfo-4.0.1-with-metadata.xml"); ModelInfoCompareContext differences = new ModelInfoCompareContext(); compareModelInfo(differences, a, b); /* @@ -128,9 +129,9 @@ void compareMetadataModelInfo() throws IOException { "ModelInfo.positiveInt.Element value in right only%n"))); // redeclaration for metadata } - public class ModelInfoCompareContext { + public static class ModelInfoCompareContext { private StringBuilder differences = new StringBuilder(); - private List focusList = new ArrayList(); + private List focusList = new ArrayList<>(); public ModelInfoCompareContext() { pushFocus("ModelInfo"); @@ -145,7 +146,7 @@ public void pushFocus(String newFocus) { } public void popFocus() { - if (focusList.size() > 0) { + if (!focusList.isEmpty()) { focusList.remove(focusList.size() - 1); } } @@ -185,9 +186,9 @@ public static void compareModelInfo(ModelInfoCompareContext context, ModelInfo a // requiredModelInfo Map msa = - a.getRequiredModelInfo().stream().collect(Collectors.toMap(k -> k.getName(), v -> v)); + a.getRequiredModelInfo().stream().collect(Collectors.toMap(ModelSpecifier::getName, v -> v)); Map msb = - b.getRequiredModelInfo().stream().collect(Collectors.toMap(k -> k.getName(), v -> v)); + b.getRequiredModelInfo().stream().collect(Collectors.toMap(ModelSpecifier::getName, v -> v)); for (Map.Entry ms : msa.entrySet()) { ModelSpecifier msOther = msb.getOrDefault(ms.getKey(), null); @@ -227,9 +228,9 @@ public static void compareModelInfo(ModelInfoCompareContext context, ModelInfo a // conversionInfo Map cia = - a.getConversionInfo().stream().collect(Collectors.toMap(k -> k.getFromType(), v -> v)); + a.getConversionInfo().stream().collect(Collectors.toMap(ConversionInfo::getFromType, v -> v)); Map cib = - b.getConversionInfo().stream().collect(Collectors.toMap(k -> k.getFromType(), v -> v)); + b.getConversionInfo().stream().collect(Collectors.toMap(ConversionInfo::getFromType, v -> v)); for (Map.Entry ci : cia.entrySet()) { ConversionInfo ciOther = cib.getOrDefault(ci.getKey(), null); @@ -244,8 +245,8 @@ public static void compareModelInfo(ModelInfoCompareContext context, ModelInfo a } // contextInfo - Map cxa = a.getContextInfo().stream().collect(Collectors.toMap(k -> k.getName(), v -> v)); - Map cxb = b.getContextInfo().stream().collect(Collectors.toMap(k -> k.getName(), v -> v)); + Map cxa = a.getContextInfo().stream().collect(Collectors.toMap(ContextInfo::getName, v -> v)); + Map cxb = b.getContextInfo().stream().collect(Collectors.toMap(ContextInfo::getName, v -> v)); for (Map.Entry ci : cxa.entrySet()) { ContextInfo ciOther = cxb.getOrDefault(ci.getKey(), null); @@ -261,7 +262,7 @@ public static void compareModelInfo(ModelInfoCompareContext context, ModelInfo a } public static void compareAttribute(ModelInfoCompareContext context, String attributeName, String a, String b) { - if (a == null || b == null || !a.equals(b)) { + if (a == null || !a.equals(b)) { if (a == null && b == null) { return; } @@ -270,7 +271,7 @@ public static void compareAttribute(ModelInfoCompareContext context, String attr } public static void compareAttribute(ModelInfoCompareContext context, String attributeName, Boolean a, Boolean b) { - if (a == null || b == null || !a.equals(b)) { + if (a == null || !a.equals(b)) { if (a == null && b == null) { return; } @@ -437,7 +438,7 @@ public static String descriptor(TypeInfo typeInfo) { public static void compareTypeInfo(ModelInfoCompareContext context, SimpleTypeInfo a, SimpleTypeInfo b) { String descriptorA = descriptor(a); String descriptorB = descriptor(b); - if (descriptorA == null || descriptorB == null || !descriptorA.equals(descriptorB)) { + if (descriptorA == null || !descriptorA.equals(descriptorB)) { context.append(String.format("%s <> %s", descriptorA, descriptorB)); } } @@ -498,7 +499,7 @@ public static void compareClassInfoElement( public static void compareTypeInfo(ModelInfoCompareContext context, IntervalTypeInfo a, IntervalTypeInfo b) { String descriptorA = descriptor(a); String descriptorB = descriptor(b); - if (descriptorA == null || descriptorB == null || !descriptorA.equals(descriptorB)) { + if (descriptorA == null || !descriptorA.equals(descriptorB)) { context.append(String.format("%s <> %s", descriptorA, descriptorB)); } } @@ -506,7 +507,7 @@ public static void compareTypeInfo(ModelInfoCompareContext context, IntervalType public static void compareTypeInfo(ModelInfoCompareContext context, ListTypeInfo a, ListTypeInfo b) { String descriptorA = descriptor(a); String descriptorB = descriptor(b); - if (descriptorA == null || descriptorB == null || !descriptorA.equals(descriptorB)) { + if (descriptorA == null || !descriptorA.equals(descriptorB)) { context.append(String.format("%s <> %s", descriptorA, descriptorB)); } } @@ -514,7 +515,7 @@ public static void compareTypeInfo(ModelInfoCompareContext context, ListTypeInfo public static void compareTypeInfo(ModelInfoCompareContext context, TupleTypeInfo a, TupleTypeInfo b) { String descriptorA = descriptor(a); String descriptorB = descriptor(b); - if (descriptorA == null || descriptorB == null || !descriptorA.equals(descriptorB)) { + if (descriptorA == null || !descriptorA.equals(descriptorB)) { context.append(String.format("%s <> %s", descriptorA, descriptorB)); } } @@ -522,7 +523,7 @@ public static void compareTypeInfo(ModelInfoCompareContext context, TupleTypeInf public static void compareTypeInfo(ModelInfoCompareContext context, ChoiceTypeInfo a, ChoiceTypeInfo b) { String descriptorA = descriptor(a); String descriptorB = descriptor(b); - if (descriptorA == null || descriptorB == null || !descriptorA.equals(descriptorB)) { + if (descriptorA == null || !descriptorA.equals(descriptorB)) { context.append(String.format("%s <> %s", descriptorA, descriptorB)); } } @@ -596,4 +597,4 @@ public static void compareContextInfo(ModelInfoCompareContext context, ContextIn compareAttribute(context, "birthDateElement", a.getBirthDateElement(), b.getBirthDateElement()); } } -} +} \ No newline at end of file diff --git a/Src/java/qdm/src/main/java/org/cqframework/cql/cql2elm/qdm/QdmModelInfoProvider.java b/Src/java/qdm/src/main/java/org/cqframework/cql/cql2elm/qdm/QdmModelInfoProvider.java index e4438ea3e..1d51bcf95 100644 --- a/Src/java/qdm/src/main/java/org/cqframework/cql/cql2elm/qdm/QdmModelInfoProvider.java +++ b/Src/java/qdm/src/main/java/org/cqframework/cql/cql2elm/qdm/QdmModelInfoProvider.java @@ -1,6 +1,9 @@ package org.cqframework.cql.cql2elm.qdm; -import java.io.IOException; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + +import java.io.InputStream; import org.hl7.cql.model.ModelIdentifier; import org.hl7.cql.model.ModelInfoProvider; import org.hl7.cql.model.NamespaceAware; @@ -31,55 +34,38 @@ private boolean isQDMModelIdentifier(ModelIdentifier modelIdentifier) { public ModelInfo load(ModelIdentifier modelIdentifier) { if (isQDMModelIdentifier(modelIdentifier)) { String localVersion = modelIdentifier.getVersion() == null ? "" : modelIdentifier.getVersion(); - try { - switch (localVersion) { - case "4.1.2": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QdmModelInfoProvider.class.getResourceAsStream( - "/gov/healthit/qdm/qdm-modelinfo.xml")); - case "4.2": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QdmModelInfoProvider.class.getResourceAsStream( - "/gov/healthit/qdm/qdm-modelinfo-4.2.xml")); - case "4.3": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QdmModelInfoProvider.class.getResourceAsStream( - "/gov/healthit/qdm/qdm-modelinfo-4.3.xml")); - case "5.0": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QdmModelInfoProvider.class.getResourceAsStream( - "/gov/healthit/qdm/qdm-modelinfo-5.0.xml")); - case "5.0.1": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QdmModelInfoProvider.class.getResourceAsStream( - "/gov/healthit/qdm/qdm-modelinfo-5.0.1.xml")); - case "5.0.2": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QdmModelInfoProvider.class.getResourceAsStream( - "/gov/healthit/qdm/qdm-modelinfo-5.0.2.xml")); - case "5.3": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QdmModelInfoProvider.class.getResourceAsStream( - "/gov/healthit/qdm/qdm-modelinfo-5.3.xml")); - case "5.4": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QdmModelInfoProvider.class.getResourceAsStream( - "/gov/healthit/qdm/qdm-modelinfo-5.4.xml")); - case "5.5": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QdmModelInfoProvider.class.getResourceAsStream( - "/gov/healthit/qdm/qdm-modelinfo-5.5.xml")); - case "5.6": - case "": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QdmModelInfoProvider.class.getResourceAsStream( - "/gov/healthit/qdm/qdm-modelinfo-5.6.xml")); - } - } catch (IOException e) { - // Do not throw, allow other providers to resolve - } + var stream = getQdmResource(localVersion); + return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(stream))); } return null; } + + private InputStream getQdmResource(String localVersion) { + switch (localVersion) { + case "4.1.2": + return QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo.xml"); + case "4.2": + return QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-4.2.xml"); + case "4.3": + return QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-4.3.xml"); + case "5.0": + return QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.xml"); + case "5.0.1": + return QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.1.xml"); + case "5.0.2": + return QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.0.2.xml"); + case "5.3": + return QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.3.xml"); + case "5.4": + return QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.4.xml"); + case "5.5": + return QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.5.xml"); + case "5.6": + case "": + return QdmModelInfoProvider.class.getResourceAsStream("/gov/healthit/qdm/qdm-modelinfo-5.6.xml"); + default: + return null; + } + } } diff --git a/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/FhirModelInfoProvider.java b/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/FhirModelInfoProvider.java index 6c36f35a8..7f7d5fe0c 100644 --- a/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/FhirModelInfoProvider.java +++ b/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/FhirModelInfoProvider.java @@ -1,6 +1,9 @@ package org.cqframework.cql.cql2elm.quick; -import java.io.IOException; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + +import java.io.InputStream; import org.hl7.cql.model.ModelIdentifier; import org.hl7.cql.model.ModelInfoProvider; import org.hl7.cql.model.NamespaceAware; @@ -31,62 +34,42 @@ private boolean isFHIRModelIdentifier(ModelIdentifier modelIdentifier) { public ModelInfo load(ModelIdentifier modelIdentifier) { if (isFHIRModelIdentifier(modelIdentifier)) { String localVersion = modelIdentifier.getVersion() == null ? "" : modelIdentifier.getVersion(); - try { - switch (localVersion) { - case "1.0.2": - return ModelInfoReaderFactory.getReader("application/xml") - .read(FhirModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/fhir-modelinfo-1.0.2.xml")); - - case "1.4": - return ModelInfoReaderFactory.getReader("application/xml") - .read(FhirModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/fhir-modelinfo-1.4.xml")); - - case "1.6": - return ModelInfoReaderFactory.getReader("application/xml") - .read(FhirModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/fhir-modelinfo-1.6.xml")); - - case "1.8": - return ModelInfoReaderFactory.getReader("application/xml") - .read(FhirModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/fhir-modelinfo-1.8.xml")); - - case "3.0.0": - case "": - return ModelInfoReaderFactory.getReader("application/xml") - .read(FhirModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/fhir-modelinfo-3.0.0.xml")); - - case "3.0.1": - return ModelInfoReaderFactory.getReader("application/xml") - .read(FhirModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/fhir-modelinfo-3.0.1.xml")); - - case "3.2.0": - return ModelInfoReaderFactory.getReader("application/xml") - .read(FhirModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/fhir-modelinfo-3.2.0.xml")); - - case "4.0.0": - return ModelInfoReaderFactory.getReader("application/xml") - .read(FhirModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/fhir-modelinfo-4.0.0.xml")); - - case "4.0.1": - return ModelInfoReaderFactory.getReader("application/xml") - .read(FhirModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/fhir-modelinfo-4.0.1.xml")); - - // 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) { - // Do not throw, allow other providers to resolve - } + var stream = getResource(localVersion); + return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(stream))); + } + + return null; + } + + private InputStream getResource(String localVersion) { + switch (localVersion) { + case "1.0.2": + return FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.0.2.xml"); + + case "1.4": + return FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.4.xml"); + + case "1.6": + return FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.6.xml"); + + case "1.8": + return FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-1.8.xml"); + + case "3.0.0": + case "": + return FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.0.0.xml"); + + case "3.0.1": + return FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.0.1.xml"); + + case "3.2.0": + return FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-3.2.0.xml"); + + case "4.0.0": + return FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-4.0.0.xml"); + + case "4.0.1": + return FhirModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/fhir-modelinfo-4.0.1.xml"); } return null; diff --git a/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QICoreModelInfoProvider.java b/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QICoreModelInfoProvider.java index 2a84a9615..dd6e94c69 100644 --- a/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QICoreModelInfoProvider.java +++ b/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QICoreModelInfoProvider.java @@ -1,6 +1,9 @@ package org.cqframework.cql.cql2elm.quick; -import java.io.IOException; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + +import java.io.InputStream; import org.hl7.cql.model.ModelIdentifier; import org.hl7.cql.model.ModelInfoProvider; import org.hl7.cql.model.NamespaceManager; @@ -27,35 +30,26 @@ private boolean isQICoreModelIdentifier(ModelIdentifier modelIdentifier) { public ModelInfo load(ModelIdentifier modelIdentifier) { if (isQICoreModelIdentifier(modelIdentifier)) { String localVersion = modelIdentifier.getVersion() == null ? "" : modelIdentifier.getVersion(); - try { - switch (localVersion) { - case "4.0.0": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QICoreModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/qicore-modelinfo-4.0.0.xml")); - case "4.1.0": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QICoreModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/qicore-modelinfo-4.1.0.xml")); - case "4.1.1": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QICoreModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/qicore-modelinfo-4.1.1.xml")); - case "5.0.0": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QICoreModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/qicore-modelinfo-5.0.0.xml")); - case "6.0.0": - default: - return ModelInfoReaderFactory.getReader("application/xml") - .read(QICoreModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/qicore-modelinfo-6.0.0.xml")); - } - } catch (IOException e) { - // Do not throw, allow other providers to resolve - } + var stream = getResource(localVersion); + return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(stream))); } return null; } + + private InputStream getResource(String localVersion) { + switch (localVersion) { + case "4.0.0": + return QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.0.0.xml"); + case "4.1.0": + return QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.1.0.xml"); + case "4.1.1": + return QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-4.1.1.xml"); + case "5.0.0": + return QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-5.0.0.xml"); + case "6.0.0": + default: + return QICoreModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/qicore-modelinfo-6.0.0.xml"); + } + } } diff --git a/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QuickFhirModelInfoProvider.java b/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QuickFhirModelInfoProvider.java index d1ea631a3..706d63375 100644 --- a/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QuickFhirModelInfoProvider.java +++ b/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QuickFhirModelInfoProvider.java @@ -1,6 +1,9 @@ package org.cqframework.cql.cql2elm.quick; -import java.io.IOException; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + +import java.io.InputStream; import org.hl7.cql.model.ModelIdentifier; import org.hl7.cql.model.ModelInfoProvider; import org.hl7.cql.model.NamespaceManager; @@ -30,17 +33,18 @@ private boolean isQuickFhirModelIdentifier(ModelIdentifier modelIdentifier) { public ModelInfo load(ModelIdentifier modelIdentifier) { if (isQuickFhirModelIdentifier(modelIdentifier)) { String localVersion = modelIdentifier.getVersion() == null ? "" : modelIdentifier.getVersion(); - try { - switch (localVersion) { - case "3.0.1": - case "": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QuickFhirModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/quickfhir-modelinfo-3.0.1.xml")); - } - } catch (IOException e) { - // Do not throw, allow other providers to resolve - } + var stream = getResource(localVersion); + return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(stream))); + } + + return null; + } + + private InputStream getResource(String localVersion) { + switch (localVersion) { + case "3.0.1", "": + return QuickFhirModelInfoProvider.class.getResourceAsStream( + "/org/hl7/fhir/quickfhir-modelinfo-3.0.1.xml"); } return null; diff --git a/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QuickModelInfoProvider.java b/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QuickModelInfoProvider.java index 846d87c3c..6eec6203d 100644 --- a/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QuickModelInfoProvider.java +++ b/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/QuickModelInfoProvider.java @@ -1,6 +1,9 @@ package org.cqframework.cql.cql2elm.quick; -import java.io.IOException; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + +import java.io.InputStream; import org.hl7.cql.model.ModelIdentifier; import org.hl7.cql.model.ModelInfoProvider; import org.hl7.cql.model.NamespaceManager; @@ -27,27 +30,21 @@ private boolean isQuickModelIdentifier(ModelIdentifier modelIdentifier) { public ModelInfo load(ModelIdentifier modelIdentifier) { if (isQuickModelIdentifier(modelIdentifier)) { String localVersion = modelIdentifier.getVersion() == null ? "" : modelIdentifier.getVersion(); - - try { - switch (localVersion) { - case "3.3.0": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QuickModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/quick-modelinfo-3.3.0.xml")); - case "3.0.0": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QuickModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/quick-modelinfo-3.0.0.xml")); - default: - return ModelInfoReaderFactory.getReader("application/xml") - .read(QuickModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/quick-modelinfo.xml")); - } - } catch (IOException e) { - // Do not throw, allow other providers to resolve - } + var stream = getResource(localVersion); + return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(stream))); } return null; } + + private InputStream getResource(String localVersion) { + switch (localVersion) { + case "3.3.0": + return QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/quick-modelinfo-3.3.0.xml"); + case "3.0.0": + return QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/quick-modelinfo-3.0.0.xml"); + default: + return QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/quick-modelinfo.xml"); + } + } } diff --git a/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/UsCoreModelInfoProvider.java b/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/UsCoreModelInfoProvider.java index f3f6fb1bc..a5516b357 100644 --- a/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/UsCoreModelInfoProvider.java +++ b/Src/java/quick/src/main/java/org/cqframework/cql/cql2elm/quick/UsCoreModelInfoProvider.java @@ -1,6 +1,9 @@ package org.cqframework.cql.cql2elm.quick; -import java.io.IOException; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + +import java.io.InputStream; import org.hl7.cql.model.ModelIdentifier; import org.hl7.cql.model.ModelInfoProvider; import org.hl7.cql.model.NamespaceManager; @@ -27,28 +30,22 @@ private boolean isUSCoreModelIdentifier(ModelIdentifier modelIdentifier) { public ModelInfo load(ModelIdentifier modelIdentifier) { if (isUSCoreModelIdentifier(modelIdentifier)) { String localVersion = modelIdentifier.getVersion() == null ? "" : modelIdentifier.getVersion(); - - try { - switch (localVersion) { - case "3.1.0": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QuickModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/uscore-modelinfo-3.1.0.xml")); - case "3.1.1": - return ModelInfoReaderFactory.getReader("application/xml") - .read(QuickModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/uscore-modelinfo-3.1.1.xml")); - case "6.1.0": - default: - return ModelInfoReaderFactory.getReader("application/xml") - .read(QuickModelInfoProvider.class.getResourceAsStream( - "/org/hl7/fhir/uscore-modelinfo-6.1.0.xml")); - } - } catch (IOException e) { - // Do not throw, allow other providers to resolve - } + var stream = getResource(localVersion); + return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(stream))); } return null; } + + private InputStream getResource(String localVersion) { + switch (localVersion) { + case "3.1.0": + return QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/uscore-modelinfo-3.1.0.xml"); + case "3.1.1": + return QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/uscore-modelinfo-3.1.1.xml"); + case "6.1.0": + default: + return QuickModelInfoProvider.class.getResourceAsStream("/org/hl7/fhir/uscore-modelinfo-6.1.0.xml"); + } + } } diff --git a/Src/java/tools/xsd-to-modelinfo/build.gradle.kts b/Src/java/tools/xsd-to-modelinfo/build.gradle.kts index 25f591268..de19480bb 100644 --- a/Src/java/tools/xsd-to-modelinfo/build.gradle.kts +++ b/Src/java/tools/xsd-to-modelinfo/build.gradle.kts @@ -11,6 +11,7 @@ application { dependencies { api(project(":model")) implementation(project(":model-xmlutil")) + implementation("org.jetbrains.kotlinx:kotlinx-io-core-jvm:0.6.0") implementation("net.sf.jopt-simple:jopt-simple:4.7") implementation("org.apache.ws.xmlschema:xmlschema-core:2.2.5") implementation("org.apache.ws.xmlschema:xmlschema-walker:2.2.5") diff --git a/Src/java/tools/xsd-to-modelinfo/src/main/java/org/cqframework/cql/tools/xsd2modelinfo/Main.java b/Src/java/tools/xsd-to-modelinfo/src/main/java/org/cqframework/cql/tools/xsd2modelinfo/Main.java index fd2ce9f88..2154dadb9 100644 --- a/Src/java/tools/xsd-to-modelinfo/src/main/java/org/cqframework/cql/tools/xsd2modelinfo/Main.java +++ b/Src/java/tools/xsd-to-modelinfo/src/main/java/org/cqframework/cql/tools/xsd2modelinfo/Main.java @@ -1,5 +1,8 @@ package org.cqframework.cql.tools.xsd2modelinfo; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; + import java.io.*; import javax.xml.transform.stream.StreamSource; import joptsimple.OptionParser; @@ -77,7 +80,11 @@ public static void main(String[] args) throws IOException { if (configOpt != null) { File configFile = configOpt.value(options); if (configFile != null) { - config = ModelInfoReaderFactory.getReader("application/xml").read(configFile); + var stream = new FileInputStream(configFile); + var source = buffered(asSource(stream)); + config = ModelInfoReaderFactory.INSTANCE + .getReader("application/xml") + .read(source); } } diff --git a/Src/java/tools/xsd-to-modelinfo/src/main/java/org/cqframework/cql/tools/xsd2modelinfo/ModelImporter.java b/Src/java/tools/xsd-to-modelinfo/src/main/java/org/cqframework/cql/tools/xsd2modelinfo/ModelImporter.java index 44267ebf0..2de0bab68 100644 --- a/Src/java/tools/xsd-to-modelinfo/src/main/java/org/cqframework/cql/tools/xsd2modelinfo/ModelImporter.java +++ b/Src/java/tools/xsd-to-modelinfo/src/main/java/org/cqframework/cql/tools/xsd2modelinfo/ModelImporter.java @@ -1,5 +1,7 @@ package org.cqframework.cql.tools.xsd2modelinfo; +import static kotlinx.io.CoreKt.buffered; +import static kotlinx.io.JvmCoreKt.asSource; import static org.cqframework.cql.tools.xsd2modelinfo.ModelImporterOptions.ChoiceTypePolicy.USE_CHOICE; import java.io.IOException; @@ -17,8 +19,9 @@ public class ModelImporter { private static Map getSystemCatalog() { var reader = new XmlModelInfoReader(); - var systemModelInfo = - reader.read(ModelImporter.class.getResourceAsStream("/org/hl7/elm/r1/system-modelinfo.xml")); + var source = + buffered(asSource(ModelImporter.class.getResourceAsStream("/org/hl7/elm/r1/system-modelinfo.xml"))); + var systemModelInfo = reader.read(source); final Map map = new HashMap<>(); for (TypeInfo info : systemModelInfo.getTypeInfo()) { if (info instanceof SimpleTypeInfo) { @@ -338,7 +341,7 @@ private DataType resolveType(QName schemaTypeName) { DataType resultType = dataTypes.get(typeName); if (resultType == null) { if (mapping != null && mapping.getRelationship() == ModelImporterMapperValue.Relationship.EXTEND) { - resultType = new SimpleType(typeName, SYSTEM_CATALOG.get(mapping.getTargetSystemClass())); + resultType = new SimpleType(typeName, SYSTEM_CATALOG.get(mapping.getTargetSystemClass()), null); } else { resultType = new SimpleType(typeName); } @@ -397,7 +400,7 @@ private DataType resolveSimpleType(XmlSchemaSimpleType simpleType) { if (retypeToBase) { resultType = baseType; } else { - resultType = new SimpleType(typeName, baseType); + resultType = new SimpleType(typeName); dataTypes.put(typeName, resultType); } } @@ -445,7 +448,7 @@ private DataType resolveComplexType(XmlSchemaComplexType complexType) { } // Create and register the type - ClassType classType = new ClassType(typeName, baseType); + ClassType classType = new ClassType(typeName); dataTypes.put(typeName, classType); applyConfig(classType);