rework package loaders for automatic loading of package dependencies

This commit is contained in:
Grahame Grieve 2020-07-25 08:33:16 +10:00
parent 42c49509cc
commit fee1708fa2
12 changed files with 233 additions and 9 deletions

View File

@ -0,0 +1,84 @@
package org.hl7.fhir.convertors.loaders;
import org.hl7.fhir.dstu3.context.SimpleWorkerContext.IContextResourceLoader;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.ElementDefinition;
import org.hl7.fhir.dstu3.model.Resource;
import org.hl7.fhir.dstu3.model.StructureDefinition;
import org.hl7.fhir.utilities.cache.NpmPackage;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseLoaderR3 implements IContextResourceLoader {
public interface ILoaderKnowledgeProvider {
/**
* get the path for references to this resource.
* @param resource
* @return null if not tracking paths
*/
String getResourcePath(Resource resource);
}
public static class NullLoaderKnowledgeProvider implements ILoaderKnowledgeProvider {
@Override
public String getResourcePath(Resource resource) {
return null;
}
}
protected final String URL_BASE = "http://hl7.org/fhir/";
protected final String URL_DSTU2 = "http://hl7.org/fhir/1.0/";
protected final String URL_DSTU2016MAY = "http://hl7.org/fhir/1.4/";
protected final String URL_DSTU3 = "http://hl7.org/fhir/3.0/";
protected final String URL_R4 = "http://hl7.org/fhir/4.0/";
protected final String URL_ELEMENT_DEF_NAMESPACE = "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace";
protected boolean patchUrls;
protected boolean killPrimitives;;
private String[] types;
private ILoaderKnowledgeProvider lkp;
public BaseLoaderR3(String[] types, ILoaderKnowledgeProvider lkp) {
super();
this.types = types;
this.lkp = lkp;
}
public String[] getTypes() {
return types;
}
public boolean isPatchUrls() {
return patchUrls;
}
public BaseLoaderR3 setPatchUrls(boolean patchUrls) {
this.patchUrls = patchUrls;
return this;
}
public boolean isKillPrimitives() {
return killPrimitives;
}
public BaseLoaderR3 setKillPrimitives(boolean killPrimitives) {
this.killPrimitives = killPrimitives;
return this;
}
public String getResourcePath(Resource resource) {
return lkp.getResourcePath(resource);
}
public void setPath(Resource r) {
String path = lkp.getResourcePath(r);
if (path != null) {
r.setUserData("path", path);
}
}
}

View File

@ -0,0 +1,85 @@
package org.hl7.fhir.convertors.loaders;
import org.hl7.fhir.r4.context.SimpleWorkerContext.IContextResourceLoader;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.CanonicalType;
import org.hl7.fhir.r4.model.ElementDefinition;
import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.model.StructureDefinition;
import org.hl7.fhir.utilities.cache.NpmPackage;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseLoaderR4 implements IContextResourceLoader {
public interface ILoaderKnowledgeProvider {
/**
* get the path for references to this resource.
* @param resource
* @return null if not tracking paths
*/
String getResourcePath(Resource resource);
}
public static class NullLoaderKnowledgeProvider implements ILoaderKnowledgeProvider {
@Override
public String getResourcePath(Resource resource) {
return null;
}
}
protected final String URL_BASE = "http://hl7.org/fhir/";
protected final String URL_DSTU2 = "http://hl7.org/fhir/1.0/";
protected final String URL_DSTU2016MAY = "http://hl7.org/fhir/1.4/";
protected final String URL_DSTU3 = "http://hl7.org/fhir/3.0/";
protected final String URL_R4 = "http://hl7.org/fhir/4.0/";
protected final String URL_ELEMENT_DEF_NAMESPACE = "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace";
protected boolean patchUrls;
protected boolean killPrimitives;;
private String[] types;
private ILoaderKnowledgeProvider lkp;
public BaseLoaderR4(String[] types, ILoaderKnowledgeProvider lkp) {
super();
this.types = types;
this.lkp = lkp;
}
public String[] getTypes() {
return types;
}
public boolean isPatchUrls() {
return patchUrls;
}
public BaseLoaderR4 setPatchUrls(boolean patchUrls) {
this.patchUrls = patchUrls;
return this;
}
public boolean isKillPrimitives() {
return killPrimitives;
}
public BaseLoaderR4 setKillPrimitives(boolean killPrimitives) {
this.killPrimitives = killPrimitives;
return this;
}
public String getResourcePath(Resource resource) {
return lkp.getResourcePath(resource);
}
public void setPath(Resource r) {
String path = lkp.getResourcePath(r);
if (path != null) {
r.setUserData("path", path);
}
}
}

View File

@ -1,16 +1,22 @@
package org.hl7.fhir.convertors.loaders;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.context.IWorkerContext.IContextResourceLoader;
import org.hl7.fhir.r5.model.Bundle;
import org.hl7.fhir.r5.model.CanonicalType;
import org.hl7.fhir.r5.model.ElementDefinition;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.cache.NpmPackage;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseLoaderR5 {
public abstract class BaseLoaderR5 implements IContextResourceLoader {
public interface ILoaderKnowledgeProvider {
/**
@ -19,6 +25,7 @@ public abstract class BaseLoaderR5 {
* @return null if not tracking paths
*/
String getResourcePath(Resource resource);
ILoaderKnowledgeProvider forNewPackage(NpmPackage npm) throws JsonSyntaxException, IOException;
}
public static class NullLoaderKnowledgeProvider implements ILoaderKnowledgeProvider {
@ -26,6 +33,11 @@ public abstract class BaseLoaderR5 {
public String getResourcePath(Resource resource) {
return null;
}
@Override
public ILoaderKnowledgeProvider forNewPackage(NpmPackage npm) {
return this;
}
}
protected final String URL_BASE = "http://hl7.org/fhir/";
protected final String URL_DSTU2 = "http://hl7.org/fhir/1.0/";
@ -38,8 +50,8 @@ public abstract class BaseLoaderR5 {
protected boolean patchUrls;
protected boolean killPrimitives;;
private String[] types;
private ILoaderKnowledgeProvider lkp;
protected String[] types;
protected ILoaderKnowledgeProvider lkp;
public BaseLoaderR5(String[] types, ILoaderKnowledgeProvider lkp) {
super();
@ -80,4 +92,28 @@ public abstract class BaseLoaderR5 {
}
}
public IContextResourceLoader getNewLoader(NpmPackage npm) throws JsonSyntaxException, IOException {
BaseLoaderR5 ret = loaderFactory(npm);
ret.patchUrls = patchUrls;
ret.killPrimitives = killPrimitives;
return ret;
}
protected BaseLoaderR5 loaderFactory(NpmPackage npm) throws JsonSyntaxException, IOException {
if (VersionUtilities.isR5Ver(npm.fhirVersion())) {
return new R5ToR5Loader(types, lkp.forNewPackage(npm));
} else if (VersionUtilities.isR4Ver(npm.fhirVersion())) {
return new R4ToR5Loader(types, lkp.forNewPackage(npm));
} else if (VersionUtilities.isR3Ver(npm.fhirVersion())) {
return new R3ToR5Loader(types, lkp.forNewPackage(npm));
} else if (VersionUtilities.isR2Ver(npm.fhirVersion())) {
return new R2ToR5Loader(types, lkp.forNewPackage(npm));
} else if (VersionUtilities.isR2BVer(npm.fhirVersion())) {
return new R2016MayToR5Loader(types, lkp.forNewPackage(npm));
} else {
throw new FHIRException("Unsupported FHIR Version "+npm.fhirVersion());
}
}
}

View File

@ -55,7 +55,7 @@ import org.hl7.fhir.r4.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.r4.model.UriType;
import org.hl7.fhir.r4.model.ValueSet;
public class R2016MayToR4Loader extends BaseLoaderR5 implements IContextResourceLoader, VersionConvertorAdvisor40 {
public class R2016MayToR4Loader extends BaseLoaderR4 implements IContextResourceLoader, VersionConvertorAdvisor40 {
private List<CodeSystem> cslist = new ArrayList<>();

View File

@ -57,8 +57,11 @@ import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.r5.model.UriType;
import org.hl7.fhir.r5.model.ValueSet;
import org.hl7.fhir.utilities.cache.NpmPackage;
public class R2016MayToR5Loader extends BaseLoaderR5 implements IContextResourceLoader, VersionConvertorAdvisor50 {
import com.google.gson.JsonSyntaxException;
public class R2016MayToR5Loader extends BaseLoaderR5 implements VersionConvertorAdvisor50 {
public R2016MayToR5Loader(String[] types, ILoaderKnowledgeProvider lkp) {
super(types, lkp);
@ -193,4 +196,6 @@ public class R2016MayToR5Loader extends BaseLoaderR5 implements IContextResource
return null;
}
}

View File

@ -54,7 +54,7 @@ import org.hl7.fhir.dstu3.model.UriType;
import org.hl7.fhir.dstu3.model.ValueSet;
import org.hl7.fhir.exceptions.FHIRException;
public class R2ToR3Loader extends BaseLoaderR5 implements IContextResourceLoader, VersionConvertorAdvisor30 {
public class R2ToR3Loader extends BaseLoaderR3 implements VersionConvertorAdvisor30 {
private List<CodeSystem> cslist = new ArrayList<>();

View File

@ -55,7 +55,7 @@ import org.hl7.fhir.r4.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.r4.model.UriType;
import org.hl7.fhir.r4.model.ValueSet;
public class R2ToR4Loader extends BaseLoaderR5 implements IContextResourceLoader, VersionConvertorAdvisor40 {
public class R2ToR4Loader extends BaseLoaderR4 implements VersionConvertorAdvisor40 {
private List<CodeSystem> cslist = new ArrayList<>();

View File

@ -58,6 +58,9 @@ import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.r5.model.UriType;
import org.hl7.fhir.r5.model.ValueSet;
import org.hl7.fhir.utilities.cache.NpmPackage;
import com.google.gson.JsonSyntaxException;
public class R2ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader, VersionConvertorAdvisor50 {
@ -192,4 +195,5 @@ public class R2ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader
return null;
}
}

View File

@ -51,7 +51,7 @@ import org.hl7.fhir.r4.model.Bundle.BundleType;
import org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent;
import org.hl7.fhir.r4.model.StructureDefinition.StructureDefinitionKind;
public class R3ToR4Loader extends BaseLoaderR5 implements IContextResourceLoader, VersionConvertorAdvisor40 {
public class R3ToR4Loader extends BaseLoaderR4 implements IContextResourceLoader, VersionConvertorAdvisor40 {
private List<CodeSystem> cslist = new ArrayList<>();

View File

@ -58,6 +58,9 @@ import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.r5.model.UriType;
import org.hl7.fhir.r5.model.ValueSet;
import org.hl7.fhir.utilities.cache.NpmPackage;
import com.google.gson.JsonSyntaxException;
public class R3ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader, VersionConvertorAdvisor50 {
@ -195,4 +198,5 @@ public class R3ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader
public org.hl7.fhir.r4.model.Resource convertR4(org.hl7.fhir.r5.model.Resource resource) throws FHIRException {
return null;
}
}

View File

@ -58,6 +58,9 @@ import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.r5.model.UriType;
import org.hl7.fhir.r5.model.ValueSet;
import org.hl7.fhir.utilities.cache.NpmPackage;
import com.google.gson.JsonSyntaxException;
public class R4ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader, VersionConvertorAdvisor50 {

View File

@ -57,8 +57,11 @@ import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.r5.model.UriType;
import org.hl7.fhir.r5.model.ValueSet;
import org.hl7.fhir.utilities.cache.NpmPackage;
public class R5ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader, VersionConvertorAdvisor50 {
import com.google.gson.JsonSyntaxException;
public class R5ToR5Loader extends BaseLoaderR5 implements VersionConvertorAdvisor50 {
public R5ToR5Loader(String[] types, ILoaderKnowledgeProvider lkp) {
super(types, lkp);