001package org.hl7.fhir.convertors.loaders;
002
003import org.hl7.fhir.exceptions.FHIRException;
004import org.hl7.fhir.r5.context.IWorkerContext.IContextResourceLoader;
005import org.hl7.fhir.r5.model.Bundle;
006import org.hl7.fhir.r5.model.CanonicalType;
007import org.hl7.fhir.r5.model.ElementDefinition;
008import org.hl7.fhir.r5.model.Resource;
009import org.hl7.fhir.r5.model.StructureDefinition;
010import org.hl7.fhir.utilities.VersionUtilities;
011import org.hl7.fhir.utilities.npm.NpmPackage;
012
013import com.google.gson.JsonSyntaxException;
014
015import java.io.IOException;
016import java.util.ArrayList;
017import java.util.List;
018
019public abstract class BaseLoaderR5 implements IContextResourceLoader  {
020
021  public interface ILoaderKnowledgeProvider {
022    /** 
023     * get the path for references to this resource.
024     * @param resource
025     * @return null if not tracking paths
026     */
027    String getResourcePath(Resource resource);
028    ILoaderKnowledgeProvider forNewPackage(NpmPackage npm) throws JsonSyntaxException, IOException;
029  }
030  
031  public static class NullLoaderKnowledgeProvider implements ILoaderKnowledgeProvider {
032    @Override
033    public String getResourcePath(Resource resource) {
034      return null;
035    }
036
037    @Override
038    public ILoaderKnowledgeProvider forNewPackage(NpmPackage npm) {
039      return this;
040    }
041  }
042  protected final String URL_BASE = "http://hl7.org/fhir/";
043  protected final String URL_DSTU2 = "http://hl7.org/fhir/1.0/";
044  protected final String URL_DSTU2016MAY = "http://hl7.org/fhir/1.4/";
045  protected final String URL_DSTU3 = "http://hl7.org/fhir/3.0/";
046  protected final String URL_R4 = "http://hl7.org/fhir/4.0/";
047
048  protected final String URL_ELEMENT_DEF_NAMESPACE = "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace";
049
050  protected boolean patchUrls;
051  protected boolean killPrimitives;;
052
053  protected String[] types;
054  protected ILoaderKnowledgeProvider lkp;
055
056  public BaseLoaderR5(String[] types, ILoaderKnowledgeProvider lkp) {
057    super();
058    this.types = types;
059    this.lkp = lkp;
060  }
061  
062  public String[] getTypes() {
063    return types;
064  }
065
066  public boolean isPatchUrls() {
067    return patchUrls;
068  }
069
070  public BaseLoaderR5 setPatchUrls(boolean patchUrls) {
071    this.patchUrls = patchUrls;
072    return this;
073  }
074
075  public boolean isKillPrimitives() {
076    return killPrimitives;
077  }
078
079  public BaseLoaderR5 setKillPrimitives(boolean killPrimitives) {
080    this.killPrimitives = killPrimitives;
081    return this;
082  }
083
084  public String getResourcePath(Resource resource) {
085    return lkp.getResourcePath(resource);
086  }
087
088  public void setPath(Resource r) {
089    String path = lkp.getResourcePath(r);
090    if (path != null) {
091      r.setUserData("path", path);
092    }
093  }
094
095  public IContextResourceLoader getNewLoader(NpmPackage npm) throws JsonSyntaxException, IOException {
096    BaseLoaderR5 ret = loaderFactory(npm);
097    ret.patchUrls = patchUrls;
098    ret.killPrimitives = killPrimitives;
099    return ret;
100  }
101
102  protected BaseLoaderR5 loaderFactory(NpmPackage npm) throws JsonSyntaxException, IOException {
103    if (VersionUtilities.isR5Ver(npm.fhirVersion())) {
104      return new R5ToR5Loader(types, lkp.forNewPackage(npm));
105    } else if (VersionUtilities.isR4Ver(npm.fhirVersion())) {
106      return new R4ToR5Loader(types, lkp.forNewPackage(npm));
107    } else if (VersionUtilities.isR3Ver(npm.fhirVersion())) {
108      return new R3ToR5Loader(types, lkp.forNewPackage(npm));
109    } else if (VersionUtilities.isR2Ver(npm.fhirVersion())) {
110      return new R2ToR5Loader(types, lkp.forNewPackage(npm));
111    } else if (VersionUtilities.isR2BVer(npm.fhirVersion())) {
112      return new R2016MayToR5Loader(types, lkp.forNewPackage(npm));
113    } else {
114      throw new FHIRException("Unsupported FHIR Version "+npm.fhirVersion());
115    }
116  }
117
118
119}