build core packages in other versions

This commit is contained in:
Grahame Grieve 2022-11-11 12:29:24 +11:00
parent 655fe59e94
commit aa6daa3ea0
14 changed files with 656 additions and 17 deletions

View File

@ -449,10 +449,12 @@ public class ElementDefinition10_40 {
ConversionContext10_40.INSTANCE.getVersionConvertor_10_40().copyElement(src, tgt);
if (src.hasStrength()) tgt.setStrengthElement(convertBindingStrength(src.getStrengthElement()));
if (src.hasDescriptionElement()) tgt.setDescriptionElement(String10_40.convertString(src.getDescriptionElement()));
org.hl7.fhir.r4.model.Type vs = ConversionContext10_40.INSTANCE.getVersionConvertor_10_40().convertType(src.getValueSet());
if (vs != null) {
tgt.setValueSet(vs instanceof org.hl7.fhir.r4.model.Reference ? ((org.hl7.fhir.r4.model.Reference) vs).getReference() : vs.primitiveValue());
tgt.setValueSet(VersionConvertorConstants.refToVS(tgt.getValueSet()));
if (src.hasValueSet()) {
org.hl7.fhir.r4.model.Type vs = ConversionContext10_40.INSTANCE.getVersionConvertor_10_40().convertType(src.getValueSet());
if (vs != null) {
tgt.setValueSet(vs instanceof org.hl7.fhir.r4.model.Reference ? ((org.hl7.fhir.r4.model.Reference) vs).getReference() : vs.primitiveValue());
tgt.setValueSet(VersionConvertorConstants.refToVS(tgt.getValueSet()));
}
}
return tgt;
}

View File

@ -311,7 +311,7 @@ public class ConceptMap14_30 {
static public ConceptMapGroupComponent getGroup(ConceptMap map, String srcs, String tgts) {
for (ConceptMapGroupComponent grp : map.getGroup()) {
if (grp.getSource().equals(srcs) && grp.getTarget().equals(tgts))
if (grp.hasSource() && grp.getSource().equals(srcs) && grp.getTarget().equals(tgts))
return grp;
}
ConceptMapGroupComponent grp = map.addGroup();

View File

@ -360,9 +360,10 @@ public class ElementDefinition14_40 {
if (src.hasTarget()) {
for (org.hl7.fhir.r4.model.UriType u : src.getTargetProfile()) {
tgt.addProfile(u.getValue());
String baseName = u.getValue().toLowerCase();
if (baseName.contains("reference") && !baseName.contains("documentreference"))
throw new Error("2016May Target profile contains the word 'reference':" + u);
// disabled 2022-11-11 - gdg - I don't see what value this is bringing anywhere?
// String baseName = u.getValue().toLowerCase();
// if (baseName.contains("reference") && !baseName.contains("documentreference"))
// throw new Error("2016May Target profile contains the word 'reference':" + u);
}
for (org.hl7.fhir.r4.model.UriType u : src.getProfile()) {
if (src.getCode().equals("Reference")) {

View File

@ -320,7 +320,7 @@ public class ConceptMap14_40 {
static public ConceptMapGroupComponent getGroup(ConceptMap map, String srcs, String tgts) {
for (ConceptMapGroupComponent grp : map.getGroup()) {
if (grp.getSource().equals(srcs) && grp.getTarget().equals(tgts))
if (grp.hasSource() && grp.getSource().equals(srcs) && grp.getTarget().equals(tgts))
return grp;
}
ConceptMapGroupComponent grp = map.addGroup();

View File

@ -320,7 +320,7 @@ public class ImplementationGuide14_40 {
static public org.hl7.fhir.dstu2016may.model.ImplementationGuide.ImplementationGuidePackageComponent findPackage(List<ImplementationGuide.ImplementationGuidePackageComponent> definition, String id) {
for (org.hl7.fhir.dstu2016may.model.ImplementationGuide.ImplementationGuidePackageComponent t : definition)
if (t.getId().equals(id))
if (t.hasId() && t.getId().equals(id))
return t;
org.hl7.fhir.dstu2016may.model.ImplementationGuide.ImplementationGuidePackageComponent t = new org.hl7.fhir.dstu2016may.model.ImplementationGuide.ImplementationGuidePackageComponent();
t.setName("Default Package");

View File

@ -169,6 +169,8 @@ public class Type14_50 {
return UnsignedInt14_50.convertUnsignedInt((org.hl7.fhir.r5.model.UnsignedIntType) src);
if (src instanceof org.hl7.fhir.r5.model.IntegerType)
return Integer14_50.convertInteger((org.hl7.fhir.r5.model.IntegerType) src);
if (src instanceof org.hl7.fhir.r5.model.Integer64Type)
return Integer14_50.convertInteger64((org.hl7.fhir.r5.model.Integer64Type) src);
if (src instanceof org.hl7.fhir.r5.model.UuidType)
return Uuid14_50.convertUuid((org.hl7.fhir.r5.model.UuidType) src);
if (src instanceof org.hl7.fhir.r5.model.UriType) return Uri14_50.convertUri((org.hl7.fhir.r5.model.UriType) src);

View File

@ -15,4 +15,25 @@ public class Integer14_50 {
ConversionContext14_50.INSTANCE.getVersionConvertor_14_50().copyElement(src, tgt);
return tgt;
}
public static org.hl7.fhir.r5.model.Integer64Type convertInteger64(org.hl7.fhir.dstu2016may.model.IntegerType src) throws FHIRException {
org.hl7.fhir.r5.model.Integer64Type tgt = src.hasValue() ? new org.hl7.fhir.r5.model.Integer64Type(src.getValueAsString()) : new org.hl7.fhir.r5.model.Integer64Type();
ConversionContext14_50.INSTANCE.getVersionConvertor_14_50().copyElement(src, tgt);
return tgt;
}
public static org.hl7.fhir.dstu2016may.model.IntegerType convertInteger64(org.hl7.fhir.r5.model.Integer64Type src) throws FHIRException {
org.hl7.fhir.dstu2016may.model.IntegerType tgt = new org.hl7.fhir.dstu2016may.model.IntegerType();
if (src.hasValue()) {
try {
tgt.setValueAsString(src.getValueAsString());
} catch (Exception e) {
// nothing?
tgt.setValueAsString("0");
}
}
ConversionContext14_50.INSTANCE.getVersionConvertor_14_50().copyElement(src, tgt);
return tgt;
}
}

View File

@ -329,7 +329,7 @@ public class ConceptMap14_50 {
static public ConceptMapGroupComponent getGroup(ConceptMap map, String srcs, String tgts) {
for (ConceptMapGroupComponent grp : map.getGroup()) {
if (grp.getSource().equals(srcs) && grp.getTarget().equals(tgts))
if (grp.hasSource() && grp.getSource().equals(srcs) && grp.getTarget().equals(tgts))
return grp;
}
ConceptMapGroupComponent grp = map.addGroup();

View File

@ -321,7 +321,7 @@ public class ImplementationGuide14_50 {
static public org.hl7.fhir.dstu2016may.model.ImplementationGuide.ImplementationGuidePackageComponent findPackage(List<ImplementationGuide.ImplementationGuidePackageComponent> definition, String id) {
for (org.hl7.fhir.dstu2016may.model.ImplementationGuide.ImplementationGuidePackageComponent t : definition)
if (t.getId().equals(id))
if (t.hasId() && t.getId().equals(id))
return t;
org.hl7.fhir.dstu2016may.model.ImplementationGuide.ImplementationGuidePackageComponent t = new org.hl7.fhir.dstu2016may.model.ImplementationGuide.ImplementationGuidePackageComponent();
t.setName("Default Package");

View File

@ -286,7 +286,7 @@ public class ImplementationGuide30_40 {
static public org.hl7.fhir.dstu3.model.ImplementationGuide.ImplementationGuidePackageComponent findPackage(List<ImplementationGuide.ImplementationGuidePackageComponent> definition, String id) {
for (org.hl7.fhir.dstu3.model.ImplementationGuide.ImplementationGuidePackageComponent t : definition)
if (t.getId().equals(id))
if (t.hasId() && t.getId().equals(id))
return t;
org.hl7.fhir.dstu3.model.ImplementationGuide.ImplementationGuidePackageComponent t1 = new org.hl7.fhir.dstu3.model.ImplementationGuide.ImplementationGuidePackageComponent();
t1.setName("Default Package");

View File

@ -41,8 +41,6 @@ public class CompartmentDefinition30_50 {
tgt.setPurposeElement(MarkDown30_50.convertMarkdown(src.getPurposeElement()));
for (org.hl7.fhir.dstu3.model.UsageContext t : src.getUseContext())
tgt.addUseContext(UsageContext30_50.convertUsageContext(t));
for (org.hl7.fhir.dstu3.model.CodeableConcept t : src.getJurisdiction())
tgt.addJurisdiction(CodeableConcept30_50.convertCodeableConcept(t));
if (src.hasCode())
tgt.setCodeElement(convertCompartmentType(src.getCodeElement()));
if (src.hasSearch())
@ -79,8 +77,6 @@ public class CompartmentDefinition30_50 {
tgt.setPurposeElement(MarkDown30_50.convertMarkdown(src.getPurposeElement()));
for (org.hl7.fhir.r5.model.UsageContext t : src.getUseContext())
tgt.addUseContext(UsageContext30_50.convertUsageContext(t));
for (org.hl7.fhir.r5.model.CodeableConcept t : src.getJurisdiction())
tgt.addJurisdiction(CodeableConcept30_50.convertCodeableConcept(t));
if (src.hasCode())
tgt.setCodeElement(convertCompartmentType(src.getCodeElement()));
if (src.hasSearch())

View File

@ -222,6 +222,8 @@ public class Type40_50 {
return UnsignedInt40_50.convertUnsignedInt((org.hl7.fhir.r5.model.UnsignedIntType) src);
if (src instanceof org.hl7.fhir.r5.model.IntegerType)
return Integer40_50.convertInteger((org.hl7.fhir.r5.model.IntegerType) src);
if (src instanceof org.hl7.fhir.r5.model.Integer64Type)
return Integer40_50.convertInteger64((org.hl7.fhir.r5.model.Integer64Type) src);
if (src instanceof org.hl7.fhir.r5.model.UrlType) return Url40_50.convertUrl((org.hl7.fhir.r5.model.UrlType) src);
if (src instanceof org.hl7.fhir.r5.model.UuidType)
return Uuid40_50.convertUuid((org.hl7.fhir.r5.model.UuidType) src);

View File

@ -15,4 +15,25 @@ public class Integer40_50 {
ConversionContext40_50.INSTANCE.getVersionConvertor_40_50().copyElement(src, tgt);
return tgt;
}
public static org.hl7.fhir.r5.model.Integer64Type convertInteger64(org.hl7.fhir.r4.model.IntegerType src) throws FHIRException {
org.hl7.fhir.r5.model.Integer64Type tgt = src.hasValue() ? new org.hl7.fhir.r5.model.Integer64Type(src.getValueAsString()) : new org.hl7.fhir.r5.model.Integer64Type();
ConversionContext40_50.INSTANCE.getVersionConvertor_40_50().copyElement(src, tgt);
return tgt;
}
public static org.hl7.fhir.r4.model.IntegerType convertInteger64(org.hl7.fhir.r5.model.Integer64Type src) throws FHIRException {
org.hl7.fhir.r4.model.IntegerType tgt = new org.hl7.fhir.r4.model.IntegerType();
if (src.hasValue()) {
try {
tgt.setValueAsString(src.getValueAsString());
} catch (Exception e) {
// nothing?
tgt.setValueAsString("0");
}
}
ConversionContext40_50.INSTANCE.getVersionConvertor_40_50().copyElement(src, tgt);
return tgt;
}
}

View File

@ -0,0 +1,594 @@
package org.hl7.fhir.convertors.misc.xver;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
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_10_50;
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_14_50;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_50;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
import org.hl7.fhir.convertors.misc.xver.CorePackageVersionConvertor.BaseConvertor;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.json.JsonTrackingParser;
import org.hl7.fhir.utilities.json.JsonUtilities;
import org.hl7.fhir.utilities.npm.NpmPackage;
import org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public class CorePackageVersionConvertor {
public interface IContentConvertor {
byte[] convert(byte[] cnt) throws IOException;
}
public class BaseConvertor {
protected String version;
public BaseConvertor(String version) {
super();
this.version = version;
}
}
public class ContentConverter3to4 extends BaseConvertor implements IContentConvertor {
public ContentConverter3to4(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt);
if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_30_40.convertResource(r3);
return new org.hl7.fhir.r4.formats.JsonParser().composeBytes(r4);
}
}
public class ContentConverter2to4 extends BaseConvertor implements IContentConvertor {
public ContentConverter2to4(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.dstu2.model.Resource r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(cnt);
org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_10_40.convertResource(r2);
if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
return new org.hl7.fhir.r4.formats.JsonParser().composeBytes(r4);
}
}
public class ContentConverter2Bto4 extends BaseConvertor implements IContentConvertor {
public ContentConverter2Bto4(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.dstu2016may.model.Resource r2 = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(cnt);
org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_14_40.convertResource(r2);
if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
return new org.hl7.fhir.r4.formats.JsonParser().composeBytes(r4);
}
}
public class ContentConverter5to4 extends BaseConvertor implements IContentConvertor {
public ContentConverter5to4(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.r5.model.Resource r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt);
if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_40_50.convertResource(r5);
return new org.hl7.fhir.r4.formats.JsonParser().composeBytes(r4);
}
}
public class ContentConverter4to3 extends BaseConvertor implements IContentConvertor {
public ContentConverter4to3(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt);
if (r4 instanceof org.hl7.fhir.r4.model.MessageDefinition) {
return null;
}
if (r4 instanceof org.hl7.fhir.r4.model.GraphDefinition) {
return null;
}
if (r4 instanceof org.hl7.fhir.r4.model.TerminologyCapabilities) {
return null;
}
if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_30_40.convertResource(r4);
return new org.hl7.fhir.dstu3.formats.JsonParser().composeBytes(r3);
}
}
public class ContentConverter2to3 extends BaseConvertor implements IContentConvertor {
public ContentConverter2to3(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.dstu2.model.Resource r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(cnt);
org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_10_30.convertResource(r2);
if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
return new org.hl7.fhir.dstu3.formats.JsonParser().composeBytes(r3);
}
}
public class ContentConverter2Bto3 extends BaseConvertor implements IContentConvertor {
public ContentConverter2Bto3(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.dstu2016may.model.Resource r2 = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(cnt);
if (r2 instanceof org.hl7.fhir.dstu2016may.model.StructureMap) {
return null;
}
org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_14_30.convertResource(r2);
if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
return new org.hl7.fhir.dstu3.formats.JsonParser().composeBytes(r3);
}
}
public class ContentConverter5to3 extends BaseConvertor implements IContentConvertor {
public ContentConverter5to3(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.r5.model.Resource r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt);
if (r5 instanceof org.hl7.fhir.r5.model.TerminologyCapabilities) {
return null;
}
if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_30_50.convertResource(r5);
return new org.hl7.fhir.dstu3.formats.JsonParser().composeBytes(r3);
}
}
public class ContentConverter3to5 extends BaseConvertor implements IContentConvertor {
public ContentConverter3to5(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt);
if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_30_50.convertResource(r3);
return new org.hl7.fhir.r5.formats.JsonParser().composeBytes(r5);
}
}
public class ContentConverter2to5 extends BaseConvertor implements IContentConvertor {
public ContentConverter2to5(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.dstu2.model.Resource r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(cnt);
org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_10_50.convertResource(r2);
if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
return new org.hl7.fhir.r5.formats.JsonParser().composeBytes(r5);
}
}
public class ContentConverter2Bto5 extends BaseConvertor implements IContentConvertor {
public ContentConverter2Bto5(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.dstu2016may.model.Resource r2 = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(cnt);
org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_14_50.convertResource(r2);
if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
return new org.hl7.fhir.r5.formats.JsonParser().composeBytes(r5);
}
}
public class ContentConverter4to5 extends BaseConvertor implements IContentConvertor {
public ContentConverter4to5(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt);
if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_40_50.convertResource(r4);
return new org.hl7.fhir.r5.formats.JsonParser().composeBytes(r5);
}
}
public class ContentConverter5to2 extends BaseConvertor implements IContentConvertor {
public ContentConverter5to2(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.r5.model.Resource r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt);
if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.dstu2.model.Resource r2 = VersionConvertorFactory_10_50.convertResource(r5);
return new org.hl7.fhir.dstu2.formats.JsonParser().composeBytes(r2);
}
}
public class ContentConverter5to2B extends BaseConvertor implements IContentConvertor {
public ContentConverter5to2B(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.r5.model.Resource r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt);
if (r5 instanceof org.hl7.fhir.r5.model.MessageDefinition) {
return null;
}
if (r5 instanceof org.hl7.fhir.r5.model.GraphDefinition) {
return null;
}
if (r5 instanceof org.hl7.fhir.r5.model.TerminologyCapabilities) {
return null;
}
if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.dstu2016may.model.Resource r2 = VersionConvertorFactory_14_50.convertResource(r5);
return new org.hl7.fhir.dstu2016may.formats.JsonParser().composeBytes(r2);
}
}
public class ContentConverter4to2 extends BaseConvertor implements IContentConvertor {
public ContentConverter4to2(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt);
if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.dstu2.model.Resource r2 = VersionConvertorFactory_10_40.convertResource(r4);
return new org.hl7.fhir.dstu2.formats.JsonParser().composeBytes(r2);
}
}
public class ContentConverter4to2B extends BaseConvertor implements IContentConvertor {
public ContentConverter4to2B(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt);
if (r4 instanceof org.hl7.fhir.r4.model.MessageDefinition) {
return null;
}
if (r4 instanceof org.hl7.fhir.r4.model.GraphDefinition) {
return null;
}
if (r4 instanceof org.hl7.fhir.r4.model.TerminologyCapabilities) {
return null;
}
if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.dstu2016may.model.Resource r2 = VersionConvertorFactory_14_40.convertResource(r4);
return new org.hl7.fhir.dstu2016may.formats.JsonParser().composeBytes(r2);
}
}
public class ContentConverter3to2 extends BaseConvertor implements IContentConvertor {
public ContentConverter3to2(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt);
if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.dstu2.model.Resource r2 = VersionConvertorFactory_10_30.convertResource(r3);
return new org.hl7.fhir.dstu2.formats.JsonParser().composeBytes(r2);
}
}
public class ContentConverter3to2B extends BaseConvertor implements IContentConvertor {
public ContentConverter3to2B(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt);
if (r3 instanceof org.hl7.fhir.dstu3.model.StructureMap) {
return null;
}
if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
org.hl7.fhir.dstu2016may.model.Resource r2 = VersionConvertorFactory_14_30.convertResource(r3);
return new org.hl7.fhir.dstu2016may.formats.JsonParser().composeBytes(r2);
}
}
public class ContentConverter4to4 extends BaseConvertor implements IContentConvertor {
public ContentConverter4to4(String version) {
super(version);
}
@Override
public byte[] convert(byte[] cnt) throws IOException {
org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt);
if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
if (!cr.hasVersion()) {
cr.setVersion(version);
}
}
return new org.hl7.fhir.r4.formats.JsonParser().composeBytes(r4);
}
}
public static void main(String[] args) throws Exception {
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r2b.core.tgz"), "3.0.1");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r2b.core.tgz"), "4.0.1");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r2b.core.tgz"), "4.3.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r2b.core.tgz"), "5.0.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r3.core.tgz"), "1.4.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r3.core.tgz"), "4.0.1");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r3.core.tgz"), "4.3.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r3.core.tgz"), "5.0.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4.core.tgz"), "1.4.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4.core.tgz"), "3.0.1");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4.core.tgz"), "4.3.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4.core.tgz"), "5.0.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4b.core.tgz"), "1.4.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4b.core.tgz"), "3.0.1");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4b.core.tgz"), "4.0.1");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4b.core.tgz"), "5.0.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r5.core.tgz"), "1.4.0");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r5.core.tgz"), "3.0.1");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r5.core.tgz"), "4.0.1");
new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r5.core.tgz"), "4.3.0");
}
public void execute(String packageSource, String versionTarget) throws Exception {
// open the existing package
// chose the version converter
// build a new package
System.out.println("Convert "+packageSource+" to "+versionTarget);
NpmPackage src = NpmPackage.fromPackage(new FileInputStream(packageSource));
IContentConvertor conv = contentConvertorFactory(src.fhirVersion(), versionTarget);
NpmPackage dst = NpmPackage.empty();
for (NpmPackageFolder folder : src.getFolders().values()) {
for (String s : folder.getContent().keySet()) {
byte[] cnt = folder.getContent().get(s);
if (s.endsWith(".json")) {
if (s.equals("package.json")) {
cnt = convertPackage(cnt, versionTarget);
} else {
String sJ = new String(cnt);
if (sJ.contains("\"resourceType\"") && !s.equals(".index.json") && !s.endsWith(".schema.json")) {
try {
cnt = conv.convert(cnt);
} catch (Exception e) {
throw new Exception("Error processing "+folder.getName()+"/"+s+": "+e.getMessage(), e);
}
} else {
// nothing
}
}
if (cnt != null) {
dst.addFile(folder.getName(), s, cnt, null);
}
} else {
dst.addFile(folder.getName(), s, cnt, null);
}
}
}
dst.save(new FileOutputStream(Utilities.changeFileExt(packageSource, ".as."+VersionUtilities.getNameForVersion(versionTarget).toLowerCase()+".tgz")));
}
private IContentConvertor contentConvertorFactory(String fhirVersion, String versionTarget) throws Exception {
if (VersionUtilities.isR3Ver(fhirVersion)) {
if (VersionUtilities.isR4Ver(versionTarget) || VersionUtilities.isR4BVer(versionTarget)) {
return new ContentConverter3to4(fhirVersion);
} else if (VersionUtilities.isR5Ver(versionTarget)) {
return new ContentConverter3to5(fhirVersion);
} else if (VersionUtilities.isR2Ver(versionTarget)) {
return new ContentConverter3to2(fhirVersion);
} else if (VersionUtilities.isR2BVer(versionTarget)) {
return new ContentConverter3to2B(fhirVersion);
}
} else if (VersionUtilities.isR2Ver(fhirVersion)) {
if (VersionUtilities.isR4Ver(versionTarget) || VersionUtilities.isR4BVer(versionTarget)) {
return new ContentConverter2to4(fhirVersion);
} else if (VersionUtilities.isR5Ver(versionTarget)) {
return new ContentConverter2to5(fhirVersion);
} else if (VersionUtilities.isR3Ver(versionTarget)) {
return new ContentConverter2to3(fhirVersion);
}
} else if (VersionUtilities.isR2BVer(fhirVersion)) {
if (VersionUtilities.isR4Ver(versionTarget) || VersionUtilities.isR4BVer(versionTarget)) {
return new ContentConverter2Bto4(fhirVersion);
} else if (VersionUtilities.isR5Ver(versionTarget)) {
return new ContentConverter2Bto5(fhirVersion);
} else if (VersionUtilities.isR3Ver(versionTarget)) {
return new ContentConverter2Bto3(fhirVersion);
}
} else if (VersionUtilities.isR4Ver(fhirVersion)) {
if (VersionUtilities.isR3Ver(versionTarget)) {
return new ContentConverter4to3(fhirVersion);
} else if (VersionUtilities.isR5Ver(versionTarget)) {
return new ContentConverter4to5(fhirVersion);
} else if (VersionUtilities.isR2Ver(versionTarget)) {
return new ContentConverter4to2(fhirVersion);
} else if (VersionUtilities.isR4BVer(versionTarget)) {
return new ContentConverter4to4(fhirVersion);
} else if (VersionUtilities.isR2BVer(versionTarget)) {
return new ContentConverter4to2B(fhirVersion);
}
} else if (VersionUtilities.isR4BVer(fhirVersion)) {
if (VersionUtilities.isR3Ver(versionTarget)) {
return new ContentConverter4to3(fhirVersion);
} else if (VersionUtilities.isR5Ver(versionTarget)) {
return new ContentConverter4to5(fhirVersion);
} else if (VersionUtilities.isR4Ver(versionTarget)) {
return new ContentConverter4to4(fhirVersion);
} else if (VersionUtilities.isR2Ver(versionTarget)) {
return new ContentConverter4to2(fhirVersion);
} else if (VersionUtilities.isR2BVer(versionTarget)) {
return new ContentConverter4to2B(fhirVersion);
}
} else if (VersionUtilities.isR5Ver(fhirVersion)) {
if (VersionUtilities.isR4Ver(versionTarget) || VersionUtilities.isR4BVer(versionTarget)) {
return new ContentConverter5to4(fhirVersion);
} else if (VersionUtilities.isR3Ver(versionTarget)) {
return new ContentConverter5to3(fhirVersion);
} else if (VersionUtilities.isR2Ver(versionTarget)) {
return new ContentConverter5to2(fhirVersion);
} else if (VersionUtilities.isR2BVer(versionTarget)) {
return new ContentConverter5to2B(fhirVersion);
}
}
throw new Exception("Unable to convert from "+fhirVersion+" to "+versionTarget);
}
private byte[] convertPackage(byte[] cnt, String version) throws IOException {
JsonObject json = JsonTrackingParser.parseJson(cnt);
json.remove("fhir-version-list");
JsonArray vl = new JsonArray();
json.add("fhirVersions", vl);
vl.add(version);
json.addProperty("name", JsonUtilities.str(json, "name")+".as."+VersionUtilities.getNameForVersion(version).toLowerCase());
json.addProperty("title", JsonUtilities.str(json, "title")+" (as Version "+VersionUtilities.getNameForVersion(version).toLowerCase()+")");
return JsonTrackingParser.write(json).getBytes(StandardCharsets.UTF_8);
}
}