001package org.hl7.fhir.common.hapi.validation.support; 002 003import ca.uhn.fhir.context.FhirContext; 004import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 005import ca.uhn.fhir.util.ClasspathUtil; 006import org.hl7.fhir.instance.model.api.IBaseResource; 007import org.hl7.fhir.r4.model.ValueSet; 008import org.hl7.fhir.utilities.npm.NpmPackage; 009 010import javax.annotation.Nonnull; 011import java.io.IOException; 012import java.io.InputStream; 013import java.nio.charset.StandardCharsets; 014import java.util.Locale; 015 016/** 017 * This interceptor loads and parses FHIR NPM Conformance Packages, and makes the 018 * artifacts foudn within them available to the FHIR validator. 019 * 020 * @since 5.5.0 021 */ 022public class NpmPackageValidationSupport extends PrePopulatedValidationSupport { 023 024 /** 025 * Constructor 026 */ 027 public NpmPackageValidationSupport(@Nonnull FhirContext theFhirContext) { 028 super(theFhirContext); 029 } 030 031 /** 032 * Load an NPM package using a classpath specification, e.g. <code>/path/to/resource/my_package.tgz</code>. The 033 * classpath spec can optionally be prefixed with the string <code>classpath:</code> 034 * 035 * @throws InternalErrorException If the classpath file can't be found 036 */ 037 public void loadPackageFromClasspath(String theClasspath) throws IOException { 038 try (InputStream is = ClasspathUtil.loadResourceAsStream(theClasspath)) { 039 NpmPackage pkg = NpmPackage.fromPackage(is); 040 if (pkg.getFolders().containsKey("package")) { 041 NpmPackage.NpmPackageFolder packageFolder = pkg.getFolders().get("package"); 042 043 for (String nextFile : packageFolder.listFiles()) { 044 if (nextFile.toLowerCase(Locale.US).endsWith(".json")) { 045 String input = new String(packageFolder.getContent().get(nextFile), StandardCharsets.UTF_8); 046 IBaseResource resource = getFhirContext().newJsonParser().parseResource(input); 047 super.addResource(resource); 048 } 049 } 050 051 } 052 } 053 } 054 055}