001package org.hl7.fhir.validation; 002 003import java.io.ByteArrayOutputStream; 004import java.io.IOException; 005 006import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_30; 007import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_40; 008import org.hl7.fhir.convertors.factory.VersionConvertorFactory_14_30; 009import org.hl7.fhir.convertors.factory.VersionConvertorFactory_14_40; 010import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40; 011import org.hl7.fhir.dstu2016may.model.Resource; 012import org.hl7.fhir.exceptions.FHIRException; 013import org.hl7.fhir.r5.elementmodel.Manager; 014import org.hl7.fhir.utilities.VersionUtilities; 015 016public class VersionConvertor { 017 018 public static byte[] convertVersionNativeR2(String targetVer, Content cnt, Manager.FhirFormat format) throws IOException, Exception { 019 org.hl7.fhir.dstu2.model.Resource r2; 020 switch (cnt.cntType) { 021 case JSON: 022 r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(cnt.focus); 023 break; 024 case XML: 025 r2 = new org.hl7.fhir.dstu2.formats.XmlParser().parse(cnt.focus); 026 break; 027 default: 028 throw new FHIRException("Unsupported input format: " + cnt.cntType.toString()); 029 } 030 if (VersionUtilities.isR2Ver(targetVer)) { 031 return getBytesDstu2(cnt, format, r2); 032 } else if (VersionUtilities.isR2BVer(targetVer)) { 033 org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_10_30.convertResource(r2); 034 org.hl7.fhir.dstu2016may.model.Resource r2b = VersionConvertorFactory_14_30.convertResource(r3); 035 return getBytesDstu2016(cnt, format, r2b); 036 } else if (VersionUtilities.isR3Ver(targetVer)) { 037 return getBytesDstu3(cnt, format, VersionConvertorFactory_10_30.convertResource(r2)); 038 } else if (VersionUtilities.isR4Ver(targetVer)) { 039 return getBytesR4(cnt, format, VersionConvertorFactory_10_40.convertResource(r2)); 040 } else { 041 throw new FHIRException("Target Version not supported yet: " + targetVer); 042 } 043 } 044 045 public static byte[] convertVersionNativeR2b(String targetVer, Content cnt, Manager.FhirFormat format) throws IOException, Exception { 046 org.hl7.fhir.dstu2016may.model.Resource r2b; 047 switch (cnt.cntType) { 048 case JSON: 049 r2b = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(cnt.focus); 050 break; 051 case XML: 052 r2b = new org.hl7.fhir.dstu2016may.formats.XmlParser().parse(cnt.focus); 053 break; 054 default: 055 throw new FHIRException("Unsupported input format: " + cnt.cntType.toString()); 056 } 057 if (VersionUtilities.isR2Ver(targetVer)) { 058 org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_14_30.convertResource(r2b); 059 org.hl7.fhir.dstu2.model.Resource r2 = VersionConvertorFactory_10_30.convertResource(r3); 060 return getBytesDstu2(cnt, format, r2); 061 } else if (VersionUtilities.isR2BVer(targetVer)) { 062 return getBytesDstu2016(cnt, format, r2b); 063 } else if (VersionUtilities.isR3Ver(targetVer)) { 064 return getBytesDstu3(cnt, format, VersionConvertorFactory_14_30.convertResource(r2b)); 065 } else if (VersionUtilities.isR4Ver(targetVer)) { 066 return getBytesR4(cnt, format, VersionConvertorFactory_14_40.convertResource(r2b)); 067 } else { 068 throw new FHIRException("Target Version not supported yet: " + targetVer); 069 } 070 } 071 072 public static byte[] convertVersionNativeR3(String targetVer, Content cnt, Manager.FhirFormat format) throws IOException, Exception { 073 org.hl7.fhir.dstu3.model.Resource r3; 074 switch (cnt.cntType) { 075 case JSON: 076 r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt.focus); 077 break; 078 case XML: 079 r3 = new org.hl7.fhir.dstu3.formats.XmlParser().parse(cnt.focus); 080 break; 081 default: 082 throw new FHIRException("Unsupported input format: " + cnt.cntType.toString()); 083 } 084 if (VersionUtilities.isR2Ver(targetVer)) { 085 return getBytesDstu2(cnt, format, VersionConvertorFactory_10_30.convertResource(r3)); 086 } else if (VersionUtilities.isR2BVer(targetVer)) { 087 return getBytesDstu2016(cnt, format, VersionConvertorFactory_14_30.convertResource(r3)); 088 } else if (VersionUtilities.isR3Ver(targetVer)) { 089 return getBytesDstu3(cnt, format, r3); 090 } else if (VersionUtilities.isR4Ver(targetVer)) { 091 return getBytesR4(cnt, format, VersionConvertorFactory_30_40.convertResource(r3)); 092 } else { 093 throw new FHIRException("Target Version not supported yet: " + targetVer); 094 } 095 } 096 097 public static byte[] convertVersionNativeR4(String targetVer, Content cnt, Manager.FhirFormat format) throws IOException, Exception { 098 org.hl7.fhir.r4.model.Resource r4; 099 switch (cnt.cntType) { 100 case JSON: 101 r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt.focus); 102 break; 103 case XML: 104 r4 = new org.hl7.fhir.r4.formats.XmlParser().parse(cnt.focus); 105 break; 106 default: 107 throw new FHIRException("Unsupported input format: " + cnt.cntType.toString()); 108 } 109 if (VersionUtilities.isR2Ver(targetVer)) { 110 return getBytesDstu2(cnt, format, VersionConvertorFactory_10_40.convertResource(r4)); 111 } else if (VersionUtilities.isR2BVer(targetVer)) { 112 return getBytesDstu2016(cnt, format, VersionConvertorFactory_14_40.convertResource(r4)); 113 } else if (VersionUtilities.isR3Ver(targetVer)) { 114 return getBytesDstu3(cnt, format, VersionConvertorFactory_30_40.convertResource(r4)); 115 } else if (VersionUtilities.isR4Ver(targetVer)) { 116 return getBytesR4(cnt, format, r4); 117 } else { 118 throw new FHIRException("Target Version not supported yet: " + targetVer); 119 } 120 } 121 122 private static byte[] getBytesDstu2(Content cnt, Manager.FhirFormat format, org.hl7.fhir.dstu2.model.Resource r2) throws IOException { 123 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 124 switch (format) { 125 case JSON: 126 new org.hl7.fhir.dstu2.formats.JsonParser().compose(bs, r2); 127 return bs.toByteArray(); 128 case XML: 129 new org.hl7.fhir.dstu2.formats.XmlParser().compose(bs, r2); 130 return bs.toByteArray(); 131 default: 132 throw new FHIRException("Unsupported output format: " + cnt.cntType.toString()); 133 } 134 } 135 136 private static byte[] getBytesDstu2016(Content cnt, Manager.FhirFormat format, Resource r2b) throws IOException { 137 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 138 switch (format) { 139 case JSON: 140 new org.hl7.fhir.dstu2016may.formats.JsonParser().compose(bs, r2b); 141 return bs.toByteArray(); 142 case XML: 143 new org.hl7.fhir.dstu2016may.formats.XmlParser().compose(bs, r2b); 144 return bs.toByteArray(); 145 default: 146 throw new FHIRException("Unsupported output format: " + cnt.cntType.toString()); 147 } 148 } 149 150 private static byte[] getBytesDstu3(Content cnt, Manager.FhirFormat format, org.hl7.fhir.dstu3.model.Resource r3) throws IOException { 151 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 152 switch (format) { 153 case JSON: 154 new org.hl7.fhir.dstu3.formats.JsonParser().compose(bs, r3); 155 return bs.toByteArray(); 156 case XML: 157 new org.hl7.fhir.dstu3.formats.XmlParser().compose(bs, r3); 158 return bs.toByteArray(); 159 default: 160 throw new FHIRException("Unsupported output format: " + cnt.cntType.toString()); 161 } 162 } 163 164 private static byte[] getBytesR4(Content cnt, Manager.FhirFormat format, org.hl7.fhir.r4.model.Resource r4) throws IOException { 165 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 166 switch (format) { 167 case JSON: 168 new org.hl7.fhir.r4.formats.JsonParser().compose(bs, r4); 169 return bs.toByteArray(); 170 case XML: 171 new org.hl7.fhir.r4.formats.XmlParser().compose(bs, r4); 172 return bs.toByteArray(); 173 default: 174 throw new FHIRException("Unsupported output format: " + cnt.cntType.toString()); 175 } 176 } 177}