From 8094604da00a9c5e18457d4c06518a71e7090d71 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 8 Aug 2023 19:57:55 +1000 Subject: [PATCH 1/5] rework CPT import --- .../hl7/fhir/convertors/misc/CPTImporter.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CPTImporter.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CPTImporter.java index 36e70b339..7467b47a0 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CPTImporter.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CPTImporter.java @@ -38,7 +38,6 @@ public class CPTImporter { } - private Connection con; private void doImport(String src, String version, String dst) throws FHIRException, FileNotFoundException, IOException, ClassNotFoundException, SQLException { @@ -85,8 +84,15 @@ public class CPTImporter { System.out.println(k); new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dst), cs); + produceDB(Utilities.changeFileExt(dst, ".db"), cs); - connect(Utilities.changeFileExt(dst, ".db")); + cs.getConcept().removeIf(cc -> !Utilities.existsInList(cc.getCode(), "metadata-kinds", "metadata-designations", "99202", "99203", "25", "P1")); + new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.changeFileExt(dst, "-fragment.json")), cs); + produceDB(Utilities.changeFileExt(dst, "-fragment.db"), cs); + } + + private void produceDB(String path, CodeSystem cs) throws ClassNotFoundException, SQLException { + Connection con = connect(path); Statement stmt = con.createStatement(); stmt.execute("insert into Information (name, value) values ('version', "+cs.getVersion()+")"); @@ -95,7 +101,7 @@ public class CPTImporter { stmt.execute("insert into Concepts (code, modifier) values ('"+cc.getCode()+"', "+isModifier(cc)+")"); int i = 0; if (cc.hasDisplay()) { - stmt.execute("insert into Designations (code, type, sequence, value) values ('"+cc.getCode()+"', 0, 0, '"+Utilities.escapeSql(cc.getDisplay())+"')"); + stmt.execute("insert into Designations (code, type, sequence, value) values ('"+cc.getCode()+"', 'display', 0, '"+Utilities.escapeSql(cc.getDisplay())+"')"); i++; } for (ConceptDefinitionDesignationComponent d : cc.getDesignation()) { @@ -111,9 +117,7 @@ public class CPTImporter { } } } - cs.getConcept().removeIf(cc -> !Utilities.existsInList(cc.getCode(), "metadata-kinds", "metadata-designations", "99202", "25")); - new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dst+"-fragment"), cs); } private String isModifier(ConceptDefinitionComponent cc) { @@ -126,19 +130,19 @@ public class CPTImporter { } - private void connect(String dest) throws SQLException, ClassNotFoundException { + private Connection connect(String dest) throws SQLException, ClassNotFoundException { // Class.forName("com.mysql.jdbc.Driver"); // con = DriverManager.getConnection("jdbc:mysql://localhost:3306/omop?useSSL=false","root",{pwd}); new File(dest).delete(); - con = DriverManager.getConnection("jdbc:sqlite:"+dest); - makeMetadataTable(); - makeConceptsTable(); - makeDesignationsTable(); - makePropertiesTable(); - + Connection con = DriverManager.getConnection("jdbc:sqlite:"+dest); + makeMetadataTable(con); + makeConceptsTable(con); + makeDesignationsTable(con); + makePropertiesTable(con); + return con; } - private void makeDesignationsTable() throws SQLException { + private void makeDesignationsTable(Connection con) throws SQLException { Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE Designations (\r\n"+ "`code` varchar(15) NOT NULL,\r\n"+ @@ -149,7 +153,7 @@ public class CPTImporter { } - private void makePropertiesTable() throws SQLException { + private void makePropertiesTable(Connection con) throws SQLException { Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE Properties (\r\n"+ @@ -162,7 +166,7 @@ public class CPTImporter { } - private void makeConceptsTable() throws SQLException { + private void makeConceptsTable(Connection con) throws SQLException { Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE Concepts (\r\n"+ @@ -173,7 +177,7 @@ public class CPTImporter { } - private void makeMetadataTable() throws SQLException { + private void makeMetadataTable(Connection con) throws SQLException { Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE Information (\r\n"+ From 4c132852c0b7ca8453a216759e34e76ecc5c1e1e Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 8 Aug 2023 19:58:27 +1000 Subject: [PATCH 2/5] include ExampleScenario renderer in factory and refactor --- .../fhir/r5/renderers/RendererFactory.java | 133 +++++------------- 1 file changed, 35 insertions(+), 98 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/RendererFactory.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/RendererFactory.java index 2e12c38e0..8991a5cda 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/RendererFactory.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/RendererFactory.java @@ -17,89 +17,35 @@ public class RendererFactory { return new LiquidRenderer(context, liquidTemplate); } } - - if ("CodeSystem".equals(resourceName)) { - return new CodeSystemRenderer(context); + switch (resourceName) { + case "ActorDefinition": return new ActorDefinitionRenderer(context); + case "Bundle": return new BundleRenderer(context); + case "CapabilityStatement": return new CapabilityStatementRenderer(context); + case "CodeSystem": return new CodeSystemRenderer(context); + case "CompartmentDefinition": return new CompartmentDefinitionRenderer(context); + case "ConceptMap": return new ConceptMapRenderer(context); + case "DiagnosticReport": return new DiagnosticReportRenderer(context); + case "Encounter": return new EncounterRenderer(context); + case "ExampleScenario": return new ExampleScenarioRenderer(context); + case "ImplementationGuide": return new ImplementationGuideRenderer(context); + case "Library": return new LibraryRenderer(context); + case "List": return new ListRenderer(context); + case "NamingSystem": return new NamingSystemRenderer(context); + case "OperationDefinition": return new OperationDefinitionRenderer(context); + case "OperationOutcome": return new OperationOutcomeRenderer(context); + case "Parameters": return new ParametersRenderer(context); + case "Patient": return new PatientRenderer(context); + case "Provenance": return new ProvenanceRenderer(context); + case "Questionnaire": return new QuestionnaireRenderer(context); + case "QuestionnaireResponse": return new QuestionnaireResponseRenderer(context); + case "Requirements": return new RequirementsRenderer(context); + case "SearchParameter": return new SearchParameterRenderer(context); + case "StructureDefinition": return new StructureDefinitionRenderer(context); + case "StructureMap": return new StructureMapRenderer(context); + case "SubscriptionTopic": return new SubscriptionTopicRenderer(context); + case "TestPlan": return new TestPlanRenderer(context); + case "ValueSet": return new ValueSetRenderer(context); } - if ("ValueSet".equals(resourceName)) { - return new ValueSetRenderer(context); - } - if ("ConceptMap".equals(resourceName)) { - return new ConceptMapRenderer(context); - } - - if ("CapabilityStatement".equals(resourceName)) { - return new CapabilityStatementRenderer(context); - } - if ("StructureDefinition".equals(resourceName)) { - return new StructureDefinitionRenderer(context); - } - if ("OperationDefinition".equals(resourceName)) { - return new OperationDefinitionRenderer(context); - } - if ("SearchParameter".equals(resourceName)) { - return new SearchParameterRenderer(context); - } - if ("CompartmentDefinition".equals(resourceName)) { - return new CompartmentDefinitionRenderer(context); - } - if ("ImplementationGuide".equals(resourceName)) { - return new ImplementationGuideRenderer(context); - } - if ("NamingSystem".equals(resourceName)) { - return new NamingSystemRenderer(context); - } - if ("Questionnaire".equals(resourceName)) { - return new QuestionnaireRenderer(context); - } - - if ("QuestionnaireResponse".equals(resourceName)) { - return new QuestionnaireResponseRenderer(context); - } - - if ("Patient".equals(resourceName)) { - return new PatientRenderer(context); - } - if ("Encounter".equals(resourceName)) { - return new EncounterRenderer(context); - } - if ("Library".equals(resourceName)) { - return new LibraryRenderer(context); - } - if ("List".equals(resourceName)) { - return new ListRenderer(context); - } - if ("DiagnosticReport".equals(resourceName)) { - return new DiagnosticReportRenderer(context); - } - - if ("Provenance".equals(resourceName)) { - return new ProvenanceRenderer(context); - } - if ("OperationOutcome".equals(resourceName)) { - return new OperationOutcomeRenderer(context); - } - if ("Parameters".equals(resourceName)) { - return new ParametersRenderer(context); - } - if ("Bundle".equals(resourceName)) { - return new BundleRenderer(context); - } - if ("ActorDefinition".equals(resourceName)) { - return new ActorDefinitionRenderer(context); - } - if ("Requirements".equals(resourceName)) { - return new RequirementsRenderer(context); - } - if ("SubscriptionTopic".equals(resourceName)) { - return new SubscriptionTopicRenderer(context); - } - if ("StructureMap".equals(resourceName)) { - return new StructureMapRenderer(context); - } - if ("TestPlan".equals(resourceName)) { - return new TestPlanRenderer(context); - } return new ProfileDrivenRenderer(context); } @@ -123,21 +69,12 @@ public class RendererFactory { return new LiquidRenderer(context, liquidTemplate); } } - - if ("List".equals(resource.getName())) { - return new ListRenderer(context); - } - if ("Library".equals(resource.getName())) { - return new LibraryRenderer(context); - } - if ("Patient".equals(resource.getName())) { - return new PatientRenderer(context); - } - if ("DiagnosticReport".equals(resource.getName())) { - return new DiagnosticReportRenderer(context); - } - if ("QuestionnaireResponse".equals(resource.getName())) { - return new QuestionnaireResponseRenderer(context); + switch (resource.getName()) { + case "DiagnosticReport": return new DiagnosticReportRenderer(context); + case "Library": return new LibraryRenderer(context); + case "List": return new ListRenderer(context); + case "Patient": return new PatientRenderer(context); + case "QuestionnaireResponse": return new QuestionnaireResponseRenderer(context); } return new ProfileDrivenRenderer(context, resourceContext); @@ -152,7 +89,7 @@ public class RendererFactory { return Utilities.existsInList(rt, "CodeSystem", "ValueSet", "ConceptMap", "CapabilityStatement", "CompartmentDefinition", "ImplementationGuide", "Library", "NamingSystem", "OperationDefinition", - "Questionnaire", "SearchParameter", "StructureDefinition", "ActorDefinition", "Requirements", "TestPlan"); + "Questionnaire", "SearchParameter", "StructureDefinition", "ActorDefinition", "Requirements", "TestPlan", "ExampleScenario"); } /** From 752f0e1983296de19eff32533b81c6f7e91adf7e Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 8 Aug 2023 19:58:51 +1000 Subject: [PATCH 3/5] fix bug in warning about No valid Display Names found --- .../main/java/org/hl7/fhir/r5/renderers/ValueSetRenderer.java | 3 ++- org.hl7.fhir.utilities/src/main/resources/Messages.properties | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ValueSetRenderer.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ValueSetRenderer.java index 6e1269d8c..ccc11cc96 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ValueSetRenderer.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ValueSetRenderer.java @@ -76,6 +76,7 @@ public class ValueSetRenderer extends TerminologyRenderer { return render(x, (ValueSet) dr, false); } + public boolean render(XhtmlNode x, ValueSet vs, boolean header) throws FHIRFormatError, DefinitionException, IOException { List maps = findReleventMaps(vs); @@ -1130,10 +1131,10 @@ public class ValueSetRenderer extends TerminologyRenderer { boolean hasExtensions = false; XhtmlNode li; li = ul.li(); - CodeSystem e = getContext().getWorker().fetchCodeSystem(inc.getSystem()); Map definitions = new HashMap<>(); if (inc.hasSystem()) { + CodeSystem e = getContext().getWorker().fetchCodeSystem(inc.getSystem()); if (inc.getConcept().size() == 0 && inc.getFilter().size() == 0) { li.addText(type+" all codes defined in "); addCsRef(inc, li, e); diff --git a/org.hl7.fhir.utilities/src/main/resources/Messages.properties b/org.hl7.fhir.utilities/src/main/resources/Messages.properties index 5de2104e7..c77643f3d 100644 --- a/org.hl7.fhir.utilities/src/main/resources/Messages.properties +++ b/org.hl7.fhir.utilities/src/main/resources/Messages.properties @@ -925,8 +925,8 @@ UNKNOWN_CODESYSTEM = The CodeSystem {0} is unknown UNKNOWN_CODESYSTEM_VERSION = The CodeSystem {0} version {1} is unknown. Valid versions: {2} UNABLE_TO_INFER_CODESYSTEM = The System URI could not be determined for the code {0} in the ValueSet {1} VALUESET_TOO_COSTLY = The value set {0} has too many codes to display ({1}) -NO_VALID_DISPLAY_FOUND_one = No valid Display Names found for {1}#{2} in the language {3} -NO_VALID_DISPLAY_FOUND_other = No valid Display Names found for {1}#{2} in the languages {3} +NO_VALID_DISPLAY_FOUND_one = No valid Display Names found for {0}#{1} in the language {3} +NO_VALID_DISPLAY_FOUND_other = No valid Display Names found for {0}#{1} in the languages {3} SD_NO_CONTEXT_WHEN_NOT_EXTENSION = The type is {0} so an extension context should not be specified SD_NO_CONTEXT_INV_WHEN_NOT_EXTENSION = The type is {0} so an extension context invariants should not be specified SD_CONTEXT_SHOULD_NOT_BE_ELEMENT = Review the extension type: extensions should not have a context of {0} unless it''s really intended that they can be used anywhere From abcad6db381fddabe5a9053d01a8f08cd8de1fa8 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 8 Aug 2023 19:59:34 +1000 Subject: [PATCH 4/5] update tests dependency --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 254bb68b6..e4581f7c5 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ 32.0.1-jre 6.4.1 - 1.3.25 + 1.3.26-SNAPSHOT 2.15.2 5.9.2 1.8.2 From 4b006f82ab84f5411be4042d525fcd9cb2ec12ab Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 8 Aug 2023 21:36:46 +1000 Subject: [PATCH 5/5] delay loading content when loading IGs --- .../fhir/r5/context/SimpleWorkerContext.java | 5 +- .../org/hl7/fhir/utilities/ByteProvider.java | 59 ++++++++++++++ .../hl7/fhir/utilities/npm/NpmPackage.java | 36 +++++++++ .../java/org/hl7/fhir/validation/Content.java | 7 +- .../org/hl7/fhir/validation/IgLoader.java | 80 ++++++++++--------- .../java/org/hl7/fhir/validation/Scanner.java | 6 +- .../hl7/fhir/validation/ValidationEngine.java | 29 +++---- .../hl7/fhir/validation/ValidatorUtils.java | 5 +- .../hl7/fhir/validation/VersionConvertor.java | 24 +++--- .../cli/services/ValidationService.java | 4 +- .../fhir/r5/test/StructureMappingTests.java | 7 +- .../hl7/fhir/validation/IgLoaderTests.java | 9 ++- 12 files changed, 187 insertions(+), 84 deletions(-) create mode 100644 org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/ByteProvider.java diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java index ba5a5dca9..002d4dadb 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java @@ -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 source, IContextResourceLoader loader, PackageInformation pi) throws IOException, FHIRException { + public SimpleWorkerContext fromDefinitions(Map 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); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/ByteProvider.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/ByteProvider.java new file mode 100644 index 000000000..6532a2fe4 --- /dev/null +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/ByteProvider.java @@ -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); + } + + } +} diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java index df3f36382..992e76969 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java @@ -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) { diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Content.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Content.java index 1d1ff614e..f81fcee8f 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Content.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Content.java @@ -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) { diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java index ef39e2750..5b1930752 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java @@ -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 igs, - Map binaries, + Map 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 source = loadIgSource(srcPackage, recursive, true); + Map 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 t : source.entrySet()) { + for (Map.Entry 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 s = loadIgSource(source, false, asIg); + Map 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 t : s.entrySet()) { + for (Map.Entry 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 loadIgSource(String src, + public Map 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 res = loadPackage(stream, src, false); + Map 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 res = new HashMap(); - res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), TextFile.fileToBytesNCS(src)); + Map res = new HashMap(); + 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 source = loadIgSourceForVersion(src, recursive, true, versions); + Map 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 readZip(InputStream stream) throws IOException { - Map res = new HashMap<>(); + protected Map readZip(InputStream stream) throws IOException { + Map 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 loadIgSourceForVersion(String src, + private Map 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 res = new HashMap(); - res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), TextFile.fileToBytesNCS(src)); + Map res = new HashMap(); + 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 fetchByPackage(String src, boolean loadInContext) throws FHIRException, IOException { + private Map 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 loadPackage(InputStream stream, String name, boolean loadInContext) throws FHIRException, IOException { + private Map loadPackage(InputStream stream, String name, boolean loadInContext) throws FHIRException, IOException { return loadPackage(NpmPackage.fromPackage(stream), loadInContext); } - public Map loadPackage(NpmPackage pi, boolean loadInContext) throws FHIRException, IOException { - Map res = new HashMap(); + public Map loadPackage(NpmPackage pi, boolean loadInContext) throws FHIRException, IOException { + Map res = new HashMap(); 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 resolvePackage(String id, String v, boolean loadInContext) throws FHIRException, IOException { + private Map 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 fetchVersionFromUrl(String src, + private Map 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 res = new HashMap(); - res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), cnt); + Map res = new HashMap(); + 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 fetchFromUrl(String src, boolean explore) throws FHIRException, IOException { + private Map 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 res = new HashMap<>(); - res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), cnt); + Map 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 scanDirectory(File f, boolean recursive) throws IOException { - Map res = new HashMap<>(); + private Map scanDirectory(File f, boolean recursive) throws IOException { + Map 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 t, String fn) { + protected Resource loadFileWithErrorChecking(String version, Map.Entry 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); } } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Scanner.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Scanner.java index 9a6f6351f..0db64f87f 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Scanner.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Scanner.java @@ -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))); diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java index 445847a34..4ab08508a 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java @@ -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 binaries = new HashMap<>(); + @Getter @Setter private Map 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 source = igLoader.loadIgSource(src, recursive, true); + Map 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 source) { + private String getVersionFromPack(Map 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 profiles, List record) throws FHIRException, IOException, EOperationOutcome, SAXException { + public OperationOutcome validate(String location, ByteProvider source, FhirFormat cntType, List profiles, List record) throws FHIRException, IOException, EOperationOutcome, SAXException { List messages = new ArrayList(); 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 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)) { diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidatorUtils.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidatorUtils.java index c724b8a73..b989ae189 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidatorUtils.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidatorUtils.java @@ -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 source, Map binaries, String prefix) { - for (Map.Entry e : source.entrySet()) { + protected static void grabNatives(Map source, Map binaries, String prefix) { + for (Map.Entry e : source.entrySet()) { if (e.getKey().endsWith(".zip")) binaries.put(prefix + "#" + e.getKey(), e.getValue()); } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/VersionConvertor.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/VersionConvertor.java index c7b0f2199..578218a93 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/VersionConvertor.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/VersionConvertor.java @@ -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()); diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java index 3223c5cdd..67c16c4af 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java @@ -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); diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/r5/test/StructureMappingTests.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/r5/test/StructureMappingTests.java index 511df6c47..bec2dbb95 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/r5/test/StructureMappingTests.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/r5/test/StructureMappingTests.java @@ -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()), diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/IgLoaderTests.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/IgLoaderTests.java index dd964a7d3..3b7b856c5 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/IgLoaderTests.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/IgLoaderTests.java @@ -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 dummyMap = new HashMap<>(); - dummyMap.put(dummyKey, dummyBytes); + final Map 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 map = igLoader.readZip(IgLoaderTests.class.getResourceAsStream("/zip-slip/zip-normal.zip")); + Map 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); } }