001package org.hl7.fhir.convertors.loaders;
002
003import java.io.IOException;
004import java.io.InputStream;
005
006import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_50;
007import org.hl7.fhir.convertors.factory.VersionConvertorFactory_14_50;
008import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_50;
009import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
010import org.hl7.fhir.exceptions.FHIRException;
011import org.hl7.fhir.exceptions.FHIRFormatError;
012import org.hl7.fhir.r5.model.Resource;
013import org.hl7.fhir.utilities.Utilities;
014import org.hl7.fhir.utilities.VersionUtilities;
015
016public class XVersionLoader {
017
018  public static Resource loadXml(String version, InputStream stream) throws FHIRFormatError, IOException {
019    if (Utilities.noString(version)) {
020      return new org.hl7.fhir.r5.formats.XmlParser().parse(stream);
021    }
022    switch (VersionUtilities.getMajMin(version)) {
023      case "1.0":
024        return VersionConvertorFactory_10_50.convertResource(new org.hl7.fhir.dstu2.formats.XmlParser().parse(stream));
025      case "1.4":
026        return VersionConvertorFactory_14_50.convertResource(new org.hl7.fhir.dstu2016may.formats.XmlParser().parse(stream));
027      case "3.0":
028        return VersionConvertorFactory_30_50.convertResource(new org.hl7.fhir.dstu3.formats.XmlParser().parse(stream));
029      case "4.0":
030        return VersionConvertorFactory_40_50.convertResource(new org.hl7.fhir.r4.formats.XmlParser().parse(stream));
031      case "5.0":
032        return new org.hl7.fhir.r5.formats.XmlParser().parse(stream);
033    }
034    throw new FHIRException("Unknown version " + version + " loading resource");
035  }
036
037  public static Resource loadJson(String version, InputStream stream) throws FHIRException, IOException {
038    if (Utilities.noString(version)) {
039      return new org.hl7.fhir.r5.formats.JsonParser().parse(stream);
040    }
041    switch (VersionUtilities.getMajMin(version)) {
042      case "1.0":
043        return VersionConvertorFactory_10_50.convertResource(new org.hl7.fhir.dstu2.formats.JsonParser().parse(stream));
044      case "1.4":
045        return VersionConvertorFactory_14_50.convertResource(new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(stream));
046      case "3.0":
047        return VersionConvertorFactory_30_50.convertResource(new org.hl7.fhir.dstu3.formats.JsonParser().parse(stream));
048      case "4.0":
049        return VersionConvertorFactory_40_50.convertResource(new org.hl7.fhir.r4.formats.JsonParser().parse(stream));
050      case "5.0":
051        return new org.hl7.fhir.r5.formats.JsonParser().parse(stream);
052    }
053    throw new FHIRException("Unknown version " + version + " loading resource");
054  }
055
056}