delay loading content when loading IGs

This commit is contained in:
Grahame Grieve 2023-08-08 21:36:46 +10:00
parent abcad6db38
commit 4b006f82ab
12 changed files with 187 additions and 84 deletions

View File

@ -71,6 +71,7 @@ import org.hl7.fhir.r5.terminologies.client.ITerminologyClient;
import org.hl7.fhir.r5.utils.validation.IResourceValidator;
import org.hl7.fhir.r5.utils.R5Hacker;
import org.hl7.fhir.r5.utils.XVerExtensionManager;
import org.hl7.fhir.utilities.ByteProvider;
import org.hl7.fhir.utilities.CSFileInputStream;
import org.hl7.fhir.utilities.MagicResources;
import org.hl7.fhir.utilities.TextFile;
@ -291,11 +292,11 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon
return build(context);
}
public SimpleWorkerContext fromDefinitions(Map<String, byte[]> source, IContextResourceLoader loader, PackageInformation pi) throws IOException, FHIRException {
public SimpleWorkerContext fromDefinitions(Map<String, ByteProvider> source, IContextResourceLoader loader, PackageInformation pi) throws IOException, FHIRException {
SimpleWorkerContext context = getSimpleWorkerContextInstance();
for (String name : source.keySet()) {
try {
context.loadDefinitionItem(name, new ByteArrayInputStream(source.get(name)), loader, null, pi);
context.loadDefinitionItem(name, new ByteArrayInputStream(source.get(name).getBytes()), loader, null, pi);
} catch (Exception e) {
System.out.println("Error loading "+name+": "+e.getMessage());
throw new FHIRException("Error loading "+name+": "+e.getMessage(), e);

View File

@ -0,0 +1,59 @@
package org.hl7.fhir.utilities;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public abstract class ByteProvider {
public abstract byte[] getBytes() throws FileNotFoundException, IOException;
// this one needs to be deprecated - or try to to use it - get to the source
public static ByteProvider forStream(InputStream stream) throws IOException {
return new ByteProviderBytes(TextFile.streamToBytes(stream));
}
public static ByteProvider forBytes(byte[] bytes) {
return new ByteProviderBytes(bytes);
}
public static ByteProvider forFile(File ff) {
return new ByteProviderFile(ff);
}
public static ByteProvider forFile(String src) {
return new ByteProviderFile(new File(src));
}
private static class ByteProviderBytes extends ByteProvider {
private byte[] cnt;
protected ByteProviderBytes(byte[] cnt) {
this.cnt = cnt;
}
@Override
public byte[] getBytes() {
return cnt;
}
}
private static class ByteProviderFile extends ByteProvider {
private File file;
protected ByteProviderFile(File file) {
this.file = file;
}
@Override
public byte[] getBytes() throws FileNotFoundException, IOException {
return TextFile.fileToBytes(file);
}
}
}

View File

@ -65,6 +65,7 @@ import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipParameters;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.ByteProvider;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
@ -252,6 +253,19 @@ public class NpmPackage {
}
}
public ByteProvider getProvider(String file) throws FileNotFoundException, IOException {
if (folder != null) {
File f = new File(Utilities.path(folder.getAbsolutePath(), file));
if (f.exists()) {
return ByteProvider.forFile(f);
} else {
return null;
}
} else {
return ByteProvider.forBytes(content.get(file));
}
}
public boolean hasFile(String file) throws IOException {
if (folder != null) {
return new File(Utilities.path(folder.getAbsolutePath(), file)).exists();
@ -794,6 +808,7 @@ public class NpmPackage {
public InputStream load(String file) throws IOException {
return load("package", file);
}
/**
* get a stream that contains the contents of one of the files in a folder
*
@ -814,6 +829,27 @@ public class NpmPackage {
}
}
/**
* get a stream that contains the contents of one of the files in a folder
*
* @param folder
* @param file
* @return
* @throws IOException
*/
public ByteProvider getProvider(String folder, String file) throws IOException {
NpmPackageFolder f = folders.get(folder);
if (f == null) {
f = folders.get(Utilities.path("package", folder));
}
if (f != null && f.hasFile(file)) {
return f.getProvider(file);
} else {
throw new IOException("Unable to find the file "+folder+"/"+file+" in the package "+name());
}
}
public boolean hasFile(String folder, String file) throws IOException {
NpmPackageFolder f = folders.get(folder);
if (f == null) {

View File

@ -1,18 +1,19 @@
package org.hl7.fhir.validation;
import org.hl7.fhir.r5.elementmodel.Manager;
import org.hl7.fhir.utilities.ByteProvider;
public class Content {
private byte[] focus = null;
private ByteProvider focus = null;
private Manager.FhirFormat cntType = null;
public byte[] getFocus() {
public ByteProvider getFocus() {
return focus;
}
public Manager.FhirFormat getCntType() {
return cntType;
}
public void setFocus(byte[] focus) {
public void setFocus(ByteProvider focus) {
this.focus = focus;
}
public void setCntType(Manager.FhirFormat cntType) {

View File

@ -30,6 +30,7 @@ import org.hl7.fhir.r5.model.Constants;
import org.hl7.fhir.r5.model.ImplementationGuide;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities;
import org.hl7.fhir.utilities.ByteProvider;
import org.hl7.fhir.utilities.IniFile;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
@ -86,7 +87,7 @@ public class IgLoader implements IValidationEngineLoader {
* @see IgLoader#loadIgSource(String, boolean, boolean) loadIgSource for detailed description of the src parameter
*/
public void loadIg(List<ImplementationGuide> igs,
Map<String, byte[]> binaries,
Map<String, ByteProvider> binaries,
String src,
boolean recursive) throws IOException, FHIRException {
@ -124,7 +125,7 @@ public class IgLoader implements IValidationEngineLoader {
System.out.print(" Load " + srcPackage);
String canonical = null;
int count = 0;
Map<String, byte[]> source = loadIgSource(srcPackage, recursive, true);
Map<String, ByteProvider> source = loadIgSource(srcPackage, recursive, true);
String version = Constants.VERSION;
if (getVersion() != null) {
version = getVersion();
@ -136,7 +137,7 @@ public class IgLoader implements IValidationEngineLoader {
version = explicitFhirVersion;
}
for (Map.Entry<String, byte[]> t : source.entrySet()) {
for (Map.Entry<String, ByteProvider> t : source.entrySet()) {
String fn = t.getKey();
if (!exemptFile(fn)) {
Resource r = loadFileWithErrorChecking(version, t, fn);
@ -175,14 +176,14 @@ public class IgLoader implements IValidationEngineLoader {
*/
public Content loadContent(String source, String opName, boolean asIg, boolean mustLoad) throws FHIRException, IOException {
Map<String, byte[]> s = loadIgSource(source, false, asIg);
Map<String, ByteProvider> s = loadIgSource(source, false, asIg);
Content res = new Content();
if (!mustLoad && s.size() == 0) {
return null;
}
if (s.size() != 1)
throw new FHIRException("Unable to find resource " + source + " to " + opName);
for (Map.Entry<String, byte[]> t : s.entrySet()) {
for (Map.Entry<String, ByteProvider> t : s.entrySet()) {
res.setFocus(t.getValue());
if (t.getKey().endsWith(".json"))
res.setCntType(Manager.FhirFormat.JSON);
@ -216,7 +217,7 @@ public class IgLoader implements IValidationEngineLoader {
* @throws FHIRException
* @throws IOException
*/
public Map<String, byte[]> loadIgSource(String src,
public Map<String, ByteProvider> loadIgSource(String src,
boolean recursive,
boolean explore) throws FHIRException, IOException {
//
@ -265,7 +266,7 @@ public class IgLoader implements IValidationEngineLoader {
FileInputStream stream = new FileInputStream(src);
try {
if (src.endsWith(".tgz")) {
Map<String, byte[]> res = loadPackage(stream, src, false);
Map<String, ByteProvider> res = loadPackage(stream, src, false);
return res;
}
if (src.endsWith(".pack")) {
@ -280,8 +281,8 @@ public class IgLoader implements IValidationEngineLoader {
Manager.FhirFormat fmt = ResourceChecker.checkIsResource(getContext(), isDebug(), TextFile.fileToBytes(f), src, true);
if (fmt != null) {
Map<String, byte[]> res = new HashMap<String, byte[]>();
res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), TextFile.fileToBytesNCS(src));
Map<String, ByteProvider> res = new HashMap<String, ByteProvider>();
res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), ByteProvider.forFile(src));
return res;
}
} else if ((src.matches(FilesystemPackageCacheManager.PACKAGE_REGEX) || src.matches(FilesystemPackageCacheManager.PACKAGE_VERSION_REGEX)) && !src.endsWith(".zip") && !src.endsWith(".tgz")) {
@ -293,12 +294,12 @@ public class IgLoader implements IValidationEngineLoader {
public void scanForIgVersion(String src,
boolean recursive,
VersionSourceInformation versions) throws Exception {
Map<String, byte[]> source = loadIgSourceForVersion(src, recursive, true, versions);
Map<String, ByteProvider> source = loadIgSourceForVersion(src, recursive, true, versions);
if (source != null) {
if (source.containsKey("version.info")) {
versions.see(readInfoVersion(source.get("version.info")), "version.info in " + src);
} else if (source.size() == 1) {
for (byte[] v : source.values()) {
for (ByteProvider v : source.values()) {
scanForFhirVersion(versions, src, v);
}
}
@ -314,7 +315,8 @@ public class IgLoader implements IValidationEngineLoader {
}
}
private void scanForFhirVersion(VersionSourceInformation versions, String ref, byte[] cnt) throws IOException {
private void scanForFhirVersion(VersionSourceInformation versions, String ref, ByteProvider bp) throws IOException {
byte[] cnt = bp.getBytes();
String s = TextFile.bytesToString(cnt.length > SCAN_HEADER_SIZE ? Arrays.copyOfRange(cnt, 0, SCAN_HEADER_SIZE) : cnt).trim();
try {
int i = s.indexOf("fhirVersion");
@ -377,8 +379,8 @@ public class IgLoader implements IValidationEngineLoader {
return i == s.length() ? -1 : i;
}
protected Map<String, byte[]> readZip(InputStream stream) throws IOException {
Map<String, byte[]> res = new HashMap<>();
protected Map<String, ByteProvider> readZip(InputStream stream) throws IOException {
Map<String, ByteProvider> res = new HashMap<>();
ZipInputStream zip = new ZipInputStream(stream);
ZipEntry zipEntry;
while ((zipEntry = zip.getNextEntry()) != null) {
@ -392,7 +394,7 @@ public class IgLoader implements IValidationEngineLoader {
while ((n = ((InputStream) zip).read(buf, 0, 1024)) > -1) {
b.write(buf, 0, n);
}
res.put(entryName, b.toByteArray());
res.put(entryName, ByteProvider.forBytes(b.toByteArray()));
zip.closeEntry();
}
zip.close();
@ -417,7 +419,7 @@ public class IgLoader implements IValidationEngineLoader {
}
}
private Map<String, byte[]> loadIgSourceForVersion(String src,
private Map<String, ByteProvider> loadIgSourceForVersion(String src,
boolean recursive,
boolean explore,
VersionSourceInformation versions) throws FHIRException, IOException {
@ -458,8 +460,8 @@ public class IgLoader implements IValidationEngineLoader {
return readZip(new FileInputStream(src));
Manager.FhirFormat fmt = ResourceChecker.checkIsResource(getContext(), isDebug(), TextFile.fileToBytes(f), src, true);
if (fmt != null) {
Map<String, byte[]> res = new HashMap<String, byte[]>();
res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), TextFile.fileToBytesNCS(src));
Map<String, ByteProvider> res = new HashMap<String, ByteProvider>();
res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), ByteProvider.forFile(src));
return res;
}
} else if ((src.matches(FilesystemPackageCacheManager.PACKAGE_REGEX) || src.matches(FilesystemPackageCacheManager.PACKAGE_VERSION_REGEX)) && !src.endsWith(".zip") && !src.endsWith(".tgz")) {
@ -470,7 +472,7 @@ public class IgLoader implements IValidationEngineLoader {
}
private Map<String, byte[]> fetchByPackage(String src, boolean loadInContext) throws FHIRException, IOException {
private Map<String, ByteProvider> fetchByPackage(String src, boolean loadInContext) throws FHIRException, IOException {
String id = src;
String version = null;
if (src.contains("#")) {
@ -493,12 +495,12 @@ public class IgLoader implements IValidationEngineLoader {
return loadPackage(pi, loadInContext);
}
private Map<String, byte[]> loadPackage(InputStream stream, String name, boolean loadInContext) throws FHIRException, IOException {
private Map<String, ByteProvider> loadPackage(InputStream stream, String name, boolean loadInContext) throws FHIRException, IOException {
return loadPackage(NpmPackage.fromPackage(stream), loadInContext);
}
public Map<String, byte[]> loadPackage(NpmPackage pi, boolean loadInContext) throws FHIRException, IOException {
Map<String, byte[]> res = new HashMap<String, byte[]>();
public Map<String, ByteProvider> loadPackage(NpmPackage pi, boolean loadInContext) throws FHIRException, IOException {
Map<String, ByteProvider> res = new HashMap<String, ByteProvider>();
for (String s : pi.dependencies()) {
if (s.endsWith(".x") && s.length() > 2) {
String packageMajorMinor = s.substring(0, s.length() - 2);
@ -526,23 +528,23 @@ public class IgLoader implements IValidationEngineLoader {
getContext().loadFromPackage(pi, ValidatorUtils.loaderForVersion(pi.fhirVersion()));
}
for (String s : pi.listResources("CodeSystem", "ConceptMap", "ImplementationGuide", "CapabilityStatement", "SearchParameter", "Conformance", "StructureMap", "ValueSet", "StructureDefinition")) {
res.put(s, TextFile.streamToBytes(pi.load("package", s)));
res.put(s, pi.getProvider("package", s));
}
}
String ini = "[FHIR]\r\nversion=" + pi.fhirVersion() + "\r\n";
res.put("version.info", ini.getBytes());
res.put("version.info", ByteProvider.forBytes(ini.getBytes()));
return res;
}
private Map<String, byte[]> resolvePackage(String id, String v, boolean loadInContext) throws FHIRException, IOException {
private Map<String, ByteProvider> resolvePackage(String id, String v, boolean loadInContext) throws FHIRException, IOException {
NpmPackage pi = getPackageCacheManager().loadPackage(id, v);
if (pi != null && v == null)
System.out.println(" ... Using version " + pi.version());
return loadPackage(pi, loadInContext);
}
private String readInfoVersion(byte[] bs) throws IOException {
String is = TextFile.bytesToString(bs);
private String readInfoVersion(ByteProvider bs) throws IOException {
String is = TextFile.bytesToString(bs.getBytes());
is = is.trim();
IniFile ini = new IniFile(new ByteArrayInputStream(TextFile.stringToBytes(is, false)));
return ini.getStringProperty("FHIR", "version");
@ -572,7 +574,7 @@ public class IgLoader implements IValidationEngineLoader {
}
}
private Map<String, byte[]> fetchVersionFromUrl(String src,
private Map<String, ByteProvider> fetchVersionFromUrl(String src,
boolean explore,
VersionSourceInformation versions) throws FHIRException, IOException {
if (src.endsWith(".tgz")) {
@ -611,8 +613,8 @@ public class IgLoader implements IValidationEngineLoader {
Manager.FhirFormat fmt = ResourceChecker.checkIsResource(getContext(), isDebug(), cnt, src, true);
if (fmt != null) {
Map<String, byte[]> res = new HashMap<String, byte[]>();
res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), cnt);
Map<String, ByteProvider> res = new HashMap<String, ByteProvider>();
res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), ByteProvider.forBytes(cnt));
return res;
}
String fn = Utilities.path("[tmp]", "fetch-resource-error-content.bin");
@ -647,7 +649,7 @@ public class IgLoader implements IValidationEngineLoader {
}
}
private Map<String, byte[]> fetchFromUrl(String src, boolean explore) throws FHIRException, IOException {
private Map<String, ByteProvider> fetchFromUrl(String src, boolean explore) throws FHIRException, IOException {
if (src.endsWith(".tgz"))
return loadPackage(fetchFromUrlSpecific(src, false), src, false);
if (src.endsWith(".pack"))
@ -688,8 +690,8 @@ public class IgLoader implements IValidationEngineLoader {
}
Manager.FhirFormat fmt = checkFormat(cnt, src);
if (fmt != null) {
Map<String, byte[]> res = new HashMap<>();
res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), cnt);
Map<String, ByteProvider> res = new HashMap<>();
res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), ByteProvider.forBytes(cnt));
return res;
}
throw new FHIRException("Unable to read content from " + src + ": cannot determine format");
@ -702,15 +704,15 @@ public class IgLoader implements IValidationEngineLoader {
return Utilities.existsInList(Utilities.getFileExtension(ff.getName()).toLowerCase(), IGNORED_EXTENSIONS);
}
private Map<String, byte[]> scanDirectory(File f, boolean recursive) throws IOException {
Map<String, byte[]> res = new HashMap<>();
private Map<String, ByteProvider> scanDirectory(File f, boolean recursive) throws IOException {
Map<String, ByteProvider> res = new HashMap<>();
for (File ff : f.listFiles()) {
if (ff.isDirectory() && recursive) {
res.putAll(scanDirectory(ff, true));
} else if (!ff.isDirectory() && !isIgnoreFile(ff)) {
Manager.FhirFormat fmt = ResourceChecker.checkIsResource(getContext(), isDebug(), TextFile.fileToBytes(ff), ff.getAbsolutePath(), true);
if (fmt != null) {
res.put(Utilities.changeFileExt(ff.getName(), "." + fmt.getExtension()), TextFile.fileToBytes(ff.getAbsolutePath()));
res.put(Utilities.changeFileExt(ff.getName(), "." + fmt.getExtension()), ByteProvider.forFile(ff));
}
}
}
@ -764,11 +766,11 @@ public class IgLoader implements IValidationEngineLoader {
return Utilities.existsInList(fn, EXEMPT_FILES);
}
protected Resource loadFileWithErrorChecking(String version, Map.Entry<String, byte[]> t, String fn) {
protected Resource loadFileWithErrorChecking(String version, Map.Entry<String, ByteProvider> t, String fn) {
log("* load file: " + fn);
Resource r = null;
try {
r = loadResourceByVersion(version, t.getValue(), fn);
r = loadResourceByVersion(version, t.getValue().getBytes(), fn);
log(" .. success");
} catch (Exception e) {
if (!isDebug()) {
@ -858,7 +860,7 @@ public class IgLoader implements IValidationEngineLoader {
@Override
public void load(Content cnt) throws FHIRException, IOException {
Resource res = loadResourceByVersion(version, cnt.getFocus(), cnt.getExampleFileName());
Resource res = loadResourceByVersion(version, cnt.getFocus().getBytes(), cnt.getExampleFileName());
context.cacheResource(res);
}
}

View File

@ -88,7 +88,7 @@ public class Scanner {
try {
System.out.println("Validate " + ref);
messages.clear();
e = getValidator().validate(null, messages, new ByteArrayInputStream(cnt.getFocus()), cnt.getCntType());
e = getValidator().validate(null, messages, new ByteArrayInputStream(cnt.getFocus().getBytes()), cnt.getCntType());
res.add(new ScanOutputItem(ref.getRef(), null, null, ValidatorUtils.messagesToOutcome(messages, getContext(), getFhirPathEngine())));
} catch (Exception ex) {
res.add(new ScanOutputItem(ref.getRef(), null, null, exceptionToOutcome(ex)));
@ -104,7 +104,7 @@ public class Scanner {
try {
System.out.println("Validate " + ref + " against " + ig.getUrl());
messages.clear();
getValidator().validate(null, messages, new ByteArrayInputStream(cnt.getFocus()), cnt.getCntType(), url);
getValidator().validate(null, messages, new ByteArrayInputStream(cnt.getFocus().getBytes()), cnt.getCntType(), url);
res.add(new ScanOutputItem(ref.getRef(), ig, null, ValidatorUtils.messagesToOutcome(messages, getContext(), getFhirPathEngine())));
} catch (Exception ex) {
res.add(new ScanOutputItem(ref.getRef(), ig, null, exceptionToOutcome(ex)));
@ -118,7 +118,7 @@ public class Scanner {
try {
System.out.println("Validate " + ref + " against " + sd.getUrl());
messages.clear();
validator.validate(null, messages, new ByteArrayInputStream(cnt.getFocus()), cnt.getCntType(), Collections.singletonList(sd));
validator.validate(null, messages, new ByteArrayInputStream(cnt.getFocus().getBytes()), cnt.getCntType(), Collections.singletonList(sd));
res.add(new ScanOutputItem(ref.getRef(), ig, sd, ValidatorUtils.messagesToOutcome(messages, getContext(), getFhirPathEngine())));
} catch (Exception ex) {
res.add(new ScanOutputItem(ref.getRef(), ig, sd, exceptionToOutcome(ex)));

View File

@ -101,6 +101,7 @@ import org.hl7.fhir.validation.cli.utils.SchemaValidator;
import org.hl7.fhir.validation.cli.utils.ValidationLevel;
import org.hl7.fhir.validation.instance.InstanceValidator;
import org.hl7.fhir.validation.instance.utils.ValidatorHostContext;
import org.hl7.fhir.utilities.ByteProvider;
import org.xml.sax.SAXException;
import lombok.Getter;
@ -189,7 +190,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
}
@Getter @Setter private SimpleWorkerContext context;
@Getter @Setter private Map<String, byte[]> binaries = new HashMap<>();
@Getter @Setter private Map<String, ByteProvider> binaries = new HashMap<>();
@Getter @Setter private boolean doNative;
@Getter @Setter private boolean noInvariantChecks;
@Getter @Setter private boolean displayWarnings;
@ -445,7 +446,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
}
context = contextBuilder.fromPackage(npm, ValidatorUtils.loaderForVersion(version), false);
} else {
Map<String, byte[]> source = igLoader.loadIgSource(src, recursive, true);
Map<String, ByteProvider> source = igLoader.loadIgSource(src, recursive, true);
if (version == null) {
version = getVersionFromPack(source);
}
@ -485,9 +486,9 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
this.fhirPathEngine.setAllowDoubleQuotes(false);
}
private String getVersionFromPack(Map<String, byte[]> source) {
private String getVersionFromPack(Map<String, ByteProvider> source) throws FileNotFoundException, IOException {
if (source.containsKey("version.info")) {
IniFile vi = new IniFile(new ByteArrayInputStream(removeBom(source.get("version.info"))));
IniFile vi = new IniFile(new ByteArrayInputStream(removeBom(source.get("version.info").getBytes())));
return vi.getStringProperty("FHIR", "version");
} else {
throw new Error("Missing version.info?");
@ -640,13 +641,13 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
return ValidatorUtils.messagesToOutcome(messages, context, fhirPathEngine);
}
public OperationOutcome validate(String location, byte[] source, FhirFormat cntType, List<String> profiles, List<ValidationRecord> record) throws FHIRException, IOException, EOperationOutcome, SAXException {
public OperationOutcome validate(String location, ByteProvider source, FhirFormat cntType, List<String> profiles, List<ValidationRecord> record) throws FHIRException, IOException, EOperationOutcome, SAXException {
List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
if (doNative) {
SchemaValidator.validateSchema(location, cntType, messages);
}
InstanceValidator validator = getValidator(cntType);
validator.validate(null, messages, new ByteArrayInputStream(source), cntType, asSdList(profiles));
validator.validate(null, messages, new ByteArrayInputStream(source.getBytes()), cntType, asSdList(profiles));
if (showTimes) {
System.out.println(location + ": " + validator.reportTimes());
}
@ -688,7 +689,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
return map;
}
public org.hl7.fhir.r5.elementmodel.Element transform(byte[] source, FhirFormat cntType, String mapUri) throws FHIRException, IOException {
public org.hl7.fhir.r5.elementmodel.Element transform(ByteProvider source, FhirFormat cntType, String mapUri) throws FHIRException, IOException {
List<Base> outputs = new ArrayList<>();
StructureMapUtilities scu = new StructureMapUtilities(context, new TransformSupportServices(outputs, mapLog, context));
StructureMap map = context.fetchResource(StructureMap.class, mapUri);
@ -699,7 +700,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
if (sourceSD.getKind() == StructureDefinition.StructureDefinitionKind.LOGICAL) {
parser.setLogical(sourceSD);
}
org.hl7.fhir.r5.elementmodel.Element src = parser.parseSingle(new ByteArrayInputStream(source));
org.hl7.fhir.r5.elementmodel.Element src = parser.parseSingle(new ByteArrayInputStream(source.getBytes()));
scu.transform(null, src, map, resource);
resource.populatePaths(null);
return resource;
@ -764,7 +765,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
public Resource generate(String source, String version) throws FHIRException, IOException, EOperationOutcome {
Content cnt = igLoader.loadContent(source, "validate", false, true);
Resource res = igLoader.loadResourceByVersion(version, cnt.getFocus(), source);
Resource res = igLoader.loadResourceByVersion(version, cnt.getFocus().getBytes(), source);
RenderingContext rc = new RenderingContext(context, null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER, GenerationRules.VALID_RESOURCE);
genResource(res, rc);
return (Resource) res;
@ -785,21 +786,21 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
public void convert(String source, String output) throws FHIRException, IOException {
Content cnt = igLoader.loadContent(source, "validate", false, true);
Element e = Manager.parseSingle(context, new ByteArrayInputStream(cnt.getFocus()), cnt.getCntType());
Element e = Manager.parseSingle(context, new ByteArrayInputStream(cnt.getFocus().getBytes()), cnt.getCntType());
Manager.compose(context, e, new FileOutputStream(output), (output.endsWith(".json") ? FhirFormat.JSON : FhirFormat.XML), OutputStyle.PRETTY, null);
}
public String evaluateFhirPath(String source, String expression) throws FHIRException, IOException {
Content cnt = igLoader.loadContent(source, "validate", false, true);
FHIRPathEngine fpe = this.getValidator(null).getFHIRPathEngine();
Element e = Manager.parseSingle(context, new ByteArrayInputStream(cnt.getFocus()), cnt.getCntType());
Element e = Manager.parseSingle(context, new ByteArrayInputStream(cnt.getFocus().getBytes()), cnt.getCntType());
ExpressionNode exp = fpe.parse(expression);
return fpe.evaluateToString(new ValidatorHostContext(context, e), e, e, e, exp);
}
public StructureDefinition snapshot(String source, String version) throws FHIRException, IOException {
Content cnt = igLoader.loadContent(source, "validate", false, true);
Resource res = igLoader.loadResourceByVersion(version, cnt.getFocus(), Utilities.getFileNameForName(source));
Resource res = igLoader.loadResourceByVersion(version, cnt.getFocus().getBytes(), Utilities.getFileNameForName(source));
if (!(res instanceof StructureDefinition))
throw new FHIRException("Require a StructureDefinition for generating a snapshot");
@ -812,7 +813,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
public CanonicalResource loadCanonicalResource(String source, String version) throws FHIRException, IOException {
Content cnt = igLoader.loadContent(source, "validate", false, true);
Resource res = igLoader.loadResourceByVersion(version, cnt.getFocus(), Utilities.getFileNameForName(source));
Resource res = igLoader.loadResourceByVersion(version, cnt.getFocus().getBytes(), Utilities.getFileNameForName(source));
if (!(res instanceof CanonicalResource))
throw new FHIRException("Require a CanonicalResource");
@ -959,7 +960,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
public byte[] transformVersion(String source, String targetVer, FhirFormat format, Boolean canDoNative) throws FHIRException, IOException, Exception {
Content cnt = igLoader.loadContent(source, "validate", false, true);
org.hl7.fhir.r5.elementmodel.Element src = Manager.parseSingle(context, new ByteArrayInputStream(cnt.getFocus()), cnt.getCntType());
org.hl7.fhir.r5.elementmodel.Element src = Manager.parseSingle(context, new ByteArrayInputStream(cnt.getFocus().getBytes()), cnt.getCntType());
// if the src has a url, we try to use the java code
if ((canDoNative == null && src.hasChild("url")) || (canDoNative != null && canDoNative)) {

View File

@ -30,6 +30,7 @@ import org.hl7.fhir.r5.renderers.utils.RenderingContext.GenerationRules;
import org.hl7.fhir.r5.utils.EOperationOutcome;
import org.hl7.fhir.r5.utils.FHIRPathEngine;
import org.hl7.fhir.r5.utils.OperationOutcomeUtilities;
import org.hl7.fhir.utilities.ByteProvider;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.i18n.I18nConstants;
@ -72,8 +73,8 @@ public class ValidatorUtils {
}
}
protected static void grabNatives(Map<String, byte[]> source, Map<String, byte[]> binaries, String prefix) {
for (Map.Entry<String, byte[]> e : source.entrySet()) {
protected static void grabNatives(Map<String, ByteProvider> source, Map<String, ByteProvider> binaries, String prefix) {
for (Map.Entry<String, ByteProvider> e : source.entrySet()) {
if (e.getKey().endsWith(".zip"))
binaries.put(prefix + "#" + e.getKey(), e.getValue());
}

View File

@ -24,10 +24,10 @@ public class VersionConvertor {
org.hl7.fhir.dstu2.model.Resource r2;
switch (cnt.getCntType()) {
case JSON:
r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(cnt.getFocus());
r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(cnt.getFocus().getBytes());
break;
case XML:
r2 = new org.hl7.fhir.dstu2.formats.XmlParser().parse(cnt.getFocus());
r2 = new org.hl7.fhir.dstu2.formats.XmlParser().parse(cnt.getFocus().getBytes());
break;
default:
throw new FHIRException("Unsupported input format: " + cnt.getCntType().toString());
@ -53,10 +53,10 @@ public class VersionConvertor {
org.hl7.fhir.dstu2016may.model.Resource r2b;
switch (cnt.getCntType()) {
case JSON:
r2b = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(cnt.getFocus());
r2b = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(cnt.getFocus().getBytes());
break;
case XML:
r2b = new org.hl7.fhir.dstu2016may.formats.XmlParser().parse(cnt.getFocus());
r2b = new org.hl7.fhir.dstu2016may.formats.XmlParser().parse(cnt.getFocus().getBytes());
break;
default:
throw new FHIRException("Unsupported input format: " + cnt.getCntType().toString());
@ -82,10 +82,10 @@ public class VersionConvertor {
org.hl7.fhir.dstu3.model.Resource r3;
switch (cnt.getCntType()) {
case JSON:
r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt.getFocus());
r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt.getFocus().getBytes());
break;
case XML:
r3 = new org.hl7.fhir.dstu3.formats.XmlParser().parse(cnt.getFocus());
r3 = new org.hl7.fhir.dstu3.formats.XmlParser().parse(cnt.getFocus().getBytes());
break;
default:
throw new FHIRException("Unsupported input format: " + cnt.getCntType().toString());
@ -109,10 +109,10 @@ public class VersionConvertor {
org.hl7.fhir.r4.model.Resource r4;
switch (cnt.getCntType()) {
case JSON:
r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt.getFocus());
r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt.getFocus().getBytes());
break;
case XML:
r4 = new org.hl7.fhir.r4.formats.XmlParser().parse(cnt.getFocus());
r4 = new org.hl7.fhir.r4.formats.XmlParser().parse(cnt.getFocus().getBytes());
break;
default:
throw new FHIRException("Unsupported input format: " + cnt.getCntType().toString());
@ -137,10 +137,10 @@ public class VersionConvertor {
org.hl7.fhir.r4b.model.Resource r4b;
switch (cnt.getCntType()) {
case JSON:
r4b = new org.hl7.fhir.r4b.formats.JsonParser().parse(cnt.getFocus());
r4b = new org.hl7.fhir.r4b.formats.JsonParser().parse(cnt.getFocus().getBytes());
break;
case XML:
r4b = new org.hl7.fhir.r4b.formats.XmlParser().parse(cnt.getFocus());
r4b = new org.hl7.fhir.r4b.formats.XmlParser().parse(cnt.getFocus().getBytes());
break;
default:
throw new FHIRException("Unsupported input format: " + cnt.getCntType().toString());
@ -159,10 +159,10 @@ public class VersionConvertor {
org.hl7.fhir.r5.model.Resource r5;
switch (cnt.getCntType()) {
case JSON:
r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt.getFocus());
r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt.getFocus().getBytes());
break;
case XML:
r5 = new org.hl7.fhir.r5.formats.XmlParser().parse(cnt.getFocus());
r5 = new org.hl7.fhir.r5.formats.XmlParser().parse(cnt.getFocus().getBytes() );
break;
default:
throw new FHIRException("Unsupported input format: " + cnt.getCntType().toString());

View File

@ -579,7 +579,7 @@ public class ValidationService {
for (SourceFile ref : refs) {
System.out.println(" Extract Translations from " + ref);
org.hl7.fhir.validation.Content cnt = validator.getIgLoader().loadContent(ref.getRef(), "translate", false, true);
Element e = Manager.parseSingle(validator.getContext(), new ByteArrayInputStream(cnt.getFocus()), cnt.getCntType());
Element e = Manager.parseSingle(validator.getContext(), new ByteArrayInputStream(cnt.getFocus().getBytes()), cnt.getCntType());
LanguageProducerSession ps = po.startSession(e.fhirType()+"-"+e.getIdBase(), cliContext.getSrcLang());
LanguageProducerLanguageSession psl = ps.forLang(cliContext.getTgtLang());
new LanguageUtils(validator.getContext()).generateTranslations(e, psl);
@ -614,7 +614,7 @@ public class ValidationService {
for (SourceFile ref : refs) {
System.out.println(" Inject Translations into " + ref);
org.hl7.fhir.validation.Content cnt = validator.getIgLoader().loadContent(ref.getRef(), "translate", false, true);
Element e = Manager.parseSingle(validator.getContext(), new ByteArrayInputStream(cnt.getFocus()), cnt.getCntType());
Element e = Manager.parseSingle(validator.getContext(), new ByteArrayInputStream(cnt.getFocus().getBytes()), cnt.getCntType());
t = t + new LanguageUtils(validator.getContext()).importFromTranslations(e, translations);
Manager.compose(validator.getContext(), e, new FileOutputStream(Utilities.path(dst, new File(ref.getRef()).getName())), cnt.getCntType(),
OutputStyle.PRETTY, null);

View File

@ -25,6 +25,7 @@ import org.hl7.fhir.r5.model.StructureMap;
import org.hl7.fhir.r5.test.utils.CompareUtilities;
import org.hl7.fhir.r5.test.utils.TestingUtilities;
import org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities;
import org.hl7.fhir.utilities.ByteProvider;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.xml.XMLUtil;
@ -95,7 +96,7 @@ public class StructureMappingTests {
@MethodSource("data")
public void test(String name, String source, String map, String output) throws Exception {
byte[] byteSource = TestingUtilities.loadTestResourceBytes("r5", "structure-mapping", source);
ByteProvider byteSource = ByteProvider.forBytes(TestingUtilities.loadTestResourceBytes("r5", "structure-mapping", source));
String outputJson = TestingUtilities.loadTestResource("r5", "structure-mapping", output);
String fileOutputRes = TestingUtilities.tempFile("structure-mapping", output) + ".out";
@ -144,7 +145,7 @@ public class StructureMappingTests {
try {
r = new StructureMapUtilities(context).parse(map, "cda2qr");
context.cacheResource(r);
byte[] byteSource = "{}".getBytes();
ByteProvider byteSource = ByteProvider.forBytes("{}".getBytes());
org.hl7.fhir.r5.elementmodel.Element element = validationEngine.transform(byteSource, FhirFormat.JSON, r.getUrl());
Assertions.assertNotNull(element);
} finally {
@ -164,7 +165,7 @@ public class StructureMappingTests {
StructureDefinition structureDefinition = new StructureDefinition().setUrl("testGetSourceResourceFromStructureMapDefinitionExceptionTarget");
structureDefinition.getSnapshot().addElement().setPath("testGetSourceResourceFromStructureMapDefinitionExceptionTarget");
context.cacheResource(structureDefinition);
byte[] byteSource = "testGetSourceResourceFromStructureMapDefinitionException".getBytes();
ByteProvider byteSource = ByteProvider.forBytes("testGetSourceResourceFromStructureMapDefinitionException".getBytes());
DefinitionException thrown = assertThrows(
DefinitionException.class,
() -> validationEngine.transform(byteSource, FhirFormat.JSON, r.getUrl()),

View File

@ -21,6 +21,7 @@ import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.context.SimpleWorkerContext;
import org.hl7.fhir.r5.model.ImplementationGuide;
import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
import org.hl7.fhir.utilities.ByteProvider;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -63,8 +64,8 @@ public class IgLoaderTests {
final byte[] dummyBytes = {};
final String dummyKey = "dummyKey";
final Map<String, byte[]> dummyMap = new HashMap<>();
dummyMap.put(dummyKey, dummyBytes);
final Map<String, ByteProvider> dummyMap = new HashMap<>();
dummyMap.put(dummyKey, ByteProvider.forBytes(dummyBytes));
IgLoader igLoader = Mockito.spy(new IgLoader(
@ -136,10 +137,10 @@ public class IgLoaderTests {
simpleWorkerContext,
"4.0.1"
));
Map<String, byte[]> map = igLoader.readZip(IgLoaderTests.class.getResourceAsStream("/zip-slip/zip-normal.zip"));
Map<String, ByteProvider> map = igLoader.readZip(IgLoaderTests.class.getResourceAsStream("/zip-slip/zip-normal.zip"));
final String testPath = "zip-normal/depth1/test.txt";
assertTrue(map.containsKey(testPath));
String testFileContent = new String(map.get(testPath), StandardCharsets.UTF_8);
String testFileContent = new String(map.get(testPath).getBytes(), StandardCharsets.UTF_8);
Assertions.assertEquals("dummy file content", testFileContent);
}
}