44import java .io .InputStream ;
55import java .util .ArrayList ;
66import java .util .List ;
7- import java .util .stream .Collectors ;
87
98import com .google .gson .Gson ;
109
1110import org .hl7 .fhir .common .hapi .validation .support .CachingValidationSupport ;
1211import org .hl7 .fhir .common .hapi .validation .support .CommonCodeSystemsTerminologyService ;
1312import org .hl7 .fhir .common .hapi .validation .support .InMemoryTerminologyServerValidationSupport ;
14- import org .hl7 .fhir .common .hapi .validation .support .PrePopulatedValidationSupport ;
13+ import org .hl7 .fhir .common .hapi .validation .support .NpmPackageValidationSupport ;
1514import org .hl7 .fhir .common .hapi .validation .support .SnapshotGeneratingValidationSupport ;
1615import org .hl7 .fhir .common .hapi .validation .support .ValidationSupportChain ;
1716import org .hl7 .fhir .common .hapi .validation .validator .FhirInstanceValidator ;
1817import org .hl7 .fhir .instance .model .api .IBaseResource ;
1918import org .hl7 .fhir .r4 .model .CanonicalType ;
20- import org .hl7 .fhir .r4 .model .CodeSystem ;
2119import org .hl7 .fhir .r4 .model .ElementDefinition ;
22- import org .hl7 .fhir .r4 .model .Resource ;
2320import org .hl7 .fhir .r4 .model .StructureDefinition ;
24- import org .hl7 .fhir .r4 .model .ValueSet ;
2521import org .hl7 .fhir .utilities .npm .NpmPackage ;
2622
2723import ca .uhn .fhir .context .FhirContext ;
2824import ca .uhn .fhir .context .support .ConceptValidationOptions ;
2925import ca .uhn .fhir .context .support .DefaultProfileValidationSupport ;
3026import ca .uhn .fhir .context .support .IValidationSupport ;
3127import ca .uhn .fhir .context .support .ValidationSupportContext ;
32- import ca .uhn .fhir .parser .IParser ;
3328import ca .uhn .fhir .rest .server .exceptions .InternalErrorException ;
3429import ca .uhn .fhir .util .ClasspathUtil ;
3530import ca .uhn .fhir .validation .FhirValidator ;
@@ -53,44 +48,6 @@ public class ValidatorConfiguration {
5348
5449 Logger log = LogManager .getLogger (ValidatorConfiguration .class );
5550
56- private <T extends Resource > List <T > getResourcesOfType (NpmPackage npmPackage , T resourceType ) throws IOException {
57- IParser jsonParser = fhirContext .newJsonParser ();
58-
59- return npmPackage .listResources (resourceType .fhirType ()).stream ()
60- .map (t -> {
61- try {
62- return npmPackage .loadResource (t );
63- } catch (IOException e ) {
64- // TODO Auto-generated catch block
65- e .printStackTrace ();
66- }
67- return null ;
68- })
69- .map (jsonParser ::parseResource )
70- .filter (resource -> resourceType .getClass ().isInstance (resource ))
71- .map (resource -> (T ) resource )
72- .collect (Collectors .toList ());
73- }
74-
75- private PrePopulatedValidationSupport createPrePopulatedValidationSupport (InputStream inputStream ) throws IOException {
76- NpmPackage npmPackage = NpmPackage .fromPackage (inputStream );
77- PrePopulatedValidationSupport prePopulatedSupport = new PrePopulatedValidationSupport (fhirContext );
78-
79- for (StructureDefinition sd : getResourcesOfType (npmPackage , new StructureDefinition ())) {
80- prePopulatedSupport .addStructureDefinition (sd );
81- }
82-
83- for (CodeSystem cs : getResourcesOfType (npmPackage , new CodeSystem ())) {
84- prePopulatedSupport .addCodeSystem (cs );
85- }
86-
87- for (ValueSet vs : getResourcesOfType (npmPackage , new ValueSet ())) {
88- prePopulatedSupport .addValueSet (vs );
89- }
90-
91- return prePopulatedSupport ;
92- }
93-
9451 public ValidatorConfiguration (String _PROFILE_MANIFEST_FILE ) {
9552 PROFILE_MANIFEST_FILE = _PROFILE_MANIFEST_FILE ;
9653 fhirContext = FhirContext .forR4 ();
@@ -103,15 +60,13 @@ public ValidatorConfiguration(String _PROFILE_MANIFEST_FILE) {
10360 new SnapshotGeneratingValidationSupport (fhirContext ));
10461
10562 SimplifierPackage [] packages = getPackages ();
63+ NpmPackageValidationSupport npmPackageSupport = new NpmPackageValidationSupport (fhirContext );
10664
10765 try {
10866 for (SimplifierPackage individualPackage : packages ) {
109- String packagePath = String .format ("%s-%s.tgz" , individualPackage .packageName ,
67+ String packagePath = String .format ("classpath:package/ %s-%s.tgz" , individualPackage .packageName ,
11068 individualPackage .version );
111- try (InputStream is = ClasspathUtil .loadResourceAsStream (packagePath )) {
112- PrePopulatedValidationSupport foo = createPrePopulatedValidationSupport (is );
113- supportChain .addValidationSupport (foo );
114- }
69+ npmPackageSupport .loadPackageFromClasspath (packagePath );
11570 try (InputStream is = ClasspathUtil .loadResourceAsStream (packagePath )) {
11671 NpmPackage pkg = NpmPackage .fromPackage (is );
11772 npmPackages .add (pkg );
@@ -121,6 +76,7 @@ public ValidatorConfiguration(String _PROFILE_MANIFEST_FILE) {
12176 log .error (ex .getMessage (), ex );
12277 throw new RuntimeException ("error loading simplifier packages" , ex );
12378 }
79+ supportChain .addValidationSupport (npmPackageSupport );
12480 generateSnapshots (supportChain );
12581 supportChain .fetchCodeSystem ("http://snomed.info/sct" );
12682
@@ -227,7 +183,7 @@ public IValidationSupport.CodeValidationResult validateCodeInValueSet(
227183 String theCode ,
228184 String theDisplay ,
229185 IBaseResource theValueSet ) {
230- String valueSetUrl = CommonCodeSystemsTerminologyService .getValueSetUrl (theValueSet );
186+ String valueSetUrl = CommonCodeSystemsTerminologyService .getValueSetUrl (fhirContext , theValueSet );
231187
232188 if ("https://fhir.nhs.uk/ValueSet/NHSDigital-MedicationRequest-Code" .equals (valueSetUrl )
233189 || "https://fhir.nhs.uk/ValueSet/NHSDigital-MedicationDispense-Code" .equals (valueSetUrl )
0 commit comments