001package org.hl7.fhir.convertors.loaders.loaderR5; 002 003import com.google.gson.JsonSyntaxException; 004import lombok.Getter; 005import lombok.Setter; 006import lombok.experimental.Accessors; 007import org.hl7.fhir.exceptions.FHIRException; 008import org.hl7.fhir.r5.context.IWorkerContext.IContextResourceLoader; 009import org.hl7.fhir.r5.model.Resource; 010import org.hl7.fhir.utilities.VersionUtilities; 011import org.hl7.fhir.utilities.npm.NpmPackage; 012 013import java.io.IOException; 014 015@Accessors(chain = true) 016public abstract class BaseLoaderR5 implements IContextResourceLoader { 017 018 protected final String URL_BASE = "http://hl7.org/fhir/"; 019 protected final String URL_DSTU2 = "http://hl7.org/fhir/1.0/"; 020 protected final String URL_DSTU2016MAY = "http://hl7.org/fhir/1.4/"; 021 protected final String URL_DSTU3 = "http://hl7.org/fhir/3.0/"; 022 protected final String URL_R4 = "http://hl7.org/fhir/4.0/"; 023 protected final String URL_ELEMENT_DEF_NAMESPACE = "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"; 024 @Getter @Setter protected boolean patchUrls; 025 @Getter @Setter protected boolean killPrimitives; 026 @Getter protected String[] types; 027 protected ILoaderKnowledgeProviderR5 lkp; 028 029 public BaseLoaderR5(String[] types, ILoaderKnowledgeProviderR5 lkp) { 030 super(); 031 this.types = types; 032 this.lkp = lkp; 033 } 034 035 public String getResourcePath(Resource resource) { 036 return lkp.getResourcePath(resource); 037 } 038 039 public void setPath(Resource r) { 040 String path = lkp.getResourcePath(r); 041 if (lkp.getWebRoot() != null) { 042 r.setUserData("webroot", lkp.getWebRoot()); 043 } else { 044 r.setUserData("webroot", ""); 045 } 046 if (path != null) { 047 r.setUserData("path", path); 048 } 049 } 050 051 public IContextResourceLoader getNewLoader(NpmPackage npm) throws JsonSyntaxException, IOException { 052 BaseLoaderR5 ret = loaderFactory(npm); 053 ret.patchUrls = patchUrls; 054 ret.killPrimitives = killPrimitives; 055 return ret; 056 } 057 058 protected BaseLoaderR5 loaderFactory(NpmPackage npm) throws JsonSyntaxException, IOException { 059 if (VersionUtilities.isR5Ver(npm.fhirVersion())) { 060 return new R5ToR5Loader(types, lkp.forNewPackage(npm)); 061 } else if (VersionUtilities.isR4Ver(npm.fhirVersion())) { 062 return new R4ToR5Loader(types, lkp.forNewPackage(npm), npm.version()); 063 } else if (VersionUtilities.isR3Ver(npm.fhirVersion())) { 064 return new R3ToR5Loader(types, lkp.forNewPackage(npm)); 065 } else if (VersionUtilities.isR2Ver(npm.fhirVersion())) { 066 return new R2ToR5Loader(types, lkp.forNewPackage(npm)); 067 } else if (VersionUtilities.isR2BVer(npm.fhirVersion())) { 068 return new R2016MayToR5Loader(types, lkp.forNewPackage(npm)); 069 } else { 070 throw new FHIRException("Unsupported FHIR Version " + npm.fhirVersion()); 071 } 072 } 073 074}