Merge pull request #1249 from hapifhir/do-20230503-add-r5-conversion-to-cli

Add r5 + r4b conversion support in CLI
This commit is contained in:
Grahame Grieve 2023-05-17 13:31:38 +10:00 committed by GitHub
commit 8a3fce9824
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 7 deletions

View File

@ -908,7 +908,11 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
return VersionConvertor.convertVersionNativeR3(targetVer, cnt, format);
} else if (VersionUtilities.isR4Ver(version)) {
return VersionConvertor.convertVersionNativeR4(targetVer, cnt, format);
} else {
} else if (VersionUtilities.isR4BVer(version)) {
return VersionConvertor.convertVersionNativeR4b(targetVer, cnt, format);
} else if (VersionUtilities.isR5Ver(version)) {
return VersionConvertor.convertVersionNativeR5(targetVer, cnt, format);
}else {
throw new FHIRException("Source version not supported yet: " + version);
}
} catch (Exception e) {
@ -947,6 +951,13 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
if (VersionUtilities.isR3Ver(targetVer)) {
return "http://hl7.org/fhir/StructureMap/" + type + "4to3";
}
else if (VersionUtilities.isR5Ver(targetVer)) {
return "http://hl7.org/fhir/StructureMap/" + type + "4to5";
}
} else if (VersionUtilities.isR5Ver(version)) {
if (VersionUtilities.isR4Ver(targetVer)) {
return "http://hl7.org/fhir/StructureMap/" + type + "5to4";
}
}
throw new FHIRException("Source/Target version not supported: " + version + " -> " + targetVer);
}

View File

@ -3,11 +3,7 @@ package org.hl7.fhir.validation;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_30;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_40;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_14_30;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_14_40;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40;
import org.hl7.fhir.convertors.factory.*;
import org.hl7.fhir.dstu2016may.model.Resource;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.elementmodel.Manager;
@ -37,6 +33,8 @@ public class VersionConvertor {
return getBytesDstu3(cnt, format, VersionConvertorFactory_10_30.convertResource(r2));
} else if (VersionUtilities.isR4Ver(targetVer)) {
return getBytesR4(cnt, format, VersionConvertorFactory_10_40.convertResource(r2));
} else if (VersionUtilities.isR5Ver(targetVer)) {
return getBytesR5(cnt, format, VersionConvertorFactory_10_50.convertResource(r2));
} else {
throw new FHIRException("Target Version not supported yet: " + targetVer);
}
@ -64,6 +62,8 @@ public class VersionConvertor {
return getBytesDstu3(cnt, format, VersionConvertorFactory_14_30.convertResource(r2b));
} else if (VersionUtilities.isR4Ver(targetVer)) {
return getBytesR4(cnt, format, VersionConvertorFactory_14_40.convertResource(r2b));
} else if (VersionUtilities.isR5Ver(targetVer)) {
return getBytesR5(cnt, format, VersionConvertorFactory_14_50.convertResource(r2b));
} else {
throw new FHIRException("Target Version not supported yet: " + targetVer);
}
@ -89,6 +89,8 @@ public class VersionConvertor {
return getBytesDstu3(cnt, format, r3);
} else if (VersionUtilities.isR4Ver(targetVer)) {
return getBytesR4(cnt, format, VersionConvertorFactory_30_40.convertResource(r3));
} else if (VersionUtilities.isR5Ver(targetVer)) {
return getBytesR5(cnt, format, VersionConvertorFactory_30_50.convertResource(r3));
} else {
throw new FHIRException("Target Version not supported yet: " + targetVer);
}
@ -114,11 +116,68 @@ public class VersionConvertor {
return getBytesDstu3(cnt, format, VersionConvertorFactory_30_40.convertResource(r4));
} else if (VersionUtilities.isR4Ver(targetVer)) {
return getBytesR4(cnt, format, r4);
} else {
} else if (VersionUtilities.isR5Ver(targetVer)) {
return getBytesR5(cnt, format, VersionConvertorFactory_40_50.convertResource(r4));
}
else {
throw new FHIRException("Target Version not supported yet: " + targetVer);
}
}
public static byte[] convertVersionNativeR4b(String targetVer, Content cnt, Manager.FhirFormat format) throws IOException, Exception {
org.hl7.fhir.r4b.model.Resource r4b;
switch (cnt.getCntType()) {
case JSON:
r4b = new org.hl7.fhir.r4b.formats.JsonParser().parse(cnt.getFocus());
break;
case XML:
r4b = new org.hl7.fhir.r4b.formats.XmlParser().parse(cnt.getFocus());
break;
default:
throw new FHIRException("Unsupported input format: " + cnt.getCntType().toString());
}
if (VersionUtilities.isR4BVer(targetVer)) {
return getBytesR4B(cnt, format, r4b);
} else if (VersionUtilities.isR5Ver(targetVer)) {
return getBytesR5(cnt, format, VersionConvertorFactory_43_50.convertResource(r4b));
}
else {
throw new FHIRException("Target Version not supported yet: " + targetVer);
}
}
public static byte[] convertVersionNativeR5(String targetVer, Content cnt, Manager.FhirFormat format) throws IOException, Exception {
org.hl7.fhir.r5.model.Resource r5;
switch (cnt.getCntType()) {
case JSON:
r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt.getFocus());
break;
case XML:
r5 = new org.hl7.fhir.r5.formats.XmlParser().parse(cnt.getFocus());
break;
default:
throw new FHIRException("Unsupported input format: " + cnt.getCntType().toString());
}
if (VersionUtilities.isR2Ver(targetVer)) {
return getBytesDstu2(cnt, format, VersionConvertorFactory_10_50.convertResource(r5));
} else if (VersionUtilities.isR2BVer(targetVer)) {
return getBytesDstu2016(cnt, format, VersionConvertorFactory_14_50.convertResource(r5));
} else if (VersionUtilities.isR3Ver(targetVer)) {
return getBytesDstu3(cnt, format, VersionConvertorFactory_30_50.convertResource(r5));
}
else if (VersionUtilities.isR4BVer(targetVer)) {
return getBytesR4B(cnt, format, VersionConvertorFactory_43_50.convertResource(r5));
}
else if (VersionUtilities.isR4Ver(targetVer)) {
return getBytesR4(cnt, format, VersionConvertorFactory_40_50.convertResource(r5));
}
else if (VersionUtilities.isR5Ver(targetVer)) {
return getBytesR5(cnt, format, r5);
}
else {
throw new FHIRException("Target Version not supported yet: " + targetVer);
}
}
private static byte[] getBytesDstu2(Content cnt, Manager.FhirFormat format, org.hl7.fhir.dstu2.model.Resource r2) throws IOException {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
switch (format) {
@ -174,4 +233,32 @@ public class VersionConvertor {
throw new FHIRException("Unsupported output format: " + cnt.getCntType().toString());
}
}
private static byte[] getBytesR4B(Content cnt, Manager.FhirFormat format, org.hl7.fhir.r4b.model.Resource r4b) throws IOException {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
switch (format) {
case JSON:
new org.hl7.fhir.r4b.formats.JsonParser().compose(bs, r4b);
return bs.toByteArray();
case XML:
new org.hl7.fhir.r4b.formats.XmlParser().compose(bs, r4b);
return bs.toByteArray();
default:
throw new FHIRException("Unsupported output format: " + cnt.getCntType().toString());
}
}
private static byte[] getBytesR5(Content cnt, Manager.FhirFormat format, org.hl7.fhir.r5.model.Resource r5) throws IOException {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
switch (format) {
case JSON:
new org.hl7.fhir.r5.formats.JsonParser().compose(bs, r5);
return bs.toByteArray();
case XML:
new org.hl7.fhir.r5.formats.XmlParser().compose(bs, r5);
return bs.toByteArray();
default:
throw new FHIRException("Unsupported output format: " + cnt.getCntType().toString());
}
}
}