Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include validation of local files #1647

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Element convert(Resource ig) throws IOException, FHIRException {
ByteArrayInputStream bi = new ByteArrayInputStream(bs.toByteArray());
List<ValidatedFragment> list = new JsonParser(context).parse(bi);
if (list.size() != 1) {
throw new FHIRException("Unable to convert because the source contains multieple resources");
throw new FHIRException("Unable to convert because the source contains multiple resources");
}
return list.get(0).getElement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -800,10 +800,13 @@ else if (fn.endsWith(".txt") || fn.endsWith(".map") || fn.endsWith(".fml"))
r = VersionConvertorFactory_30_50.convertResource(res);
} else if (fhirVersion.startsWith("4.0")) {
org.hl7.fhir.r4.model.Resource res;
String str = new String( content );
if (fn.endsWith(".xml") && !fn.endsWith("template.xml"))
res = new org.hl7.fhir.r4.formats.XmlParser().parse(new ByteArrayInputStream(content));
else if (fn.endsWith(".json") && !fn.endsWith("template.json"))
res = new org.hl7.fhir.r4.formats.JsonParser().parse(new ByteArrayInputStream(content));
else if (fn.endsWith(".json") && !fn.endsWith("template.json")) {
// res = new org.hl7.fhir.r4.formats.JsonParser().parse(new ByteArrayInputStream(content));
res = new org.hl7.fhir.r4.formats.JsonParser().parse(str);
}
else if (fn.endsWith(".txt") || fn.endsWith(".map") || fn.endsWith(".fml"))
res = new org.hl7.fhir.r4.utils.StructureMapUtilities(org.hl7.fhir.r4.context.SimpleWorkerContext.fromNothing()).parse(new String(content), fn);
else
Expand Down Expand Up @@ -861,8 +864,9 @@ private void log(String s) {
}

@Override
public void load(Content cnt) throws FHIRException, IOException {
public Resource load(Content cnt) throws FHIRException, IOException {
Resource res = loadResourceByVersion(version, cnt.getFocus().getBytes(), cnt.getExampleFileName());
context.cacheResource(res);
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.fhir.ucum.UcumEssenceService;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_50;
Expand Down Expand Up @@ -71,12 +70,9 @@
import org.hl7.fhir.r5.utils.validation.IResourceValidator;
import org.hl7.fhir.r5.utils.validation.IValidationPolicyAdvisor;
import org.hl7.fhir.r5.utils.validation.IValidatorResourceFetcher;
import org.hl7.fhir.r5.utils.validation.IValidationPolicyAdvisor.AdditionalBindingPurpose;
import org.hl7.fhir.r5.utils.validation.IValidationPolicyAdvisor.CodedContentValidationAction;
import org.hl7.fhir.r5.utils.validation.constants.BestPracticeWarningLevel;
import org.hl7.fhir.r5.utils.validation.constants.BindingKind;
import org.hl7.fhir.r5.utils.validation.constants.CheckDisplayOption;
import org.hl7.fhir.r5.utils.validation.constants.CodedContentValidationPolicy;
import org.hl7.fhir.r5.utils.validation.constants.ContainedReferenceValidationPolicy;
import org.hl7.fhir.r5.utils.validation.constants.IdStatus;
import org.hl7.fhir.r5.utils.validation.constants.ReferenceValidationPolicy;
Expand Down Expand Up @@ -107,6 +103,7 @@
import org.hl7.fhir.validation.cli.utils.ValidationLevel;
import org.hl7.fhir.validation.instance.BasePolicyAdvisorForFullValidation;
import org.hl7.fhir.validation.instance.InstanceValidator;
import org.hl7.fhir.validation.instance.R5EvaluationContext;
import org.hl7.fhir.validation.instance.utils.ValidationContext;
import org.hl7.fhir.utilities.ByteProvider;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -192,7 +189,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP

public interface IValidationEngineLoader {

void load(Content cnt) throws FHIRException, IOException;
Resource load(Content cnt) throws FHIRException, IOException;

}

Expand Down Expand Up @@ -558,7 +555,7 @@ public void loadProfile(String src) throws FHIRException, IOException {
// testing entry point
public OperationOutcome validate(FhirFormat format, InputStream stream, List<String> profiles) throws FHIRException, IOException, EOperationOutcome {
List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
InstanceValidator validator = getValidator(format);
InstanceValidator validator = getValidator(format );
validator.validate(null, messages, stream, format, asSdList(profiles));
return ValidatorUtils.messagesToOutcome(messages, context, fhirPathEngine);
}
Expand Down Expand Up @@ -600,7 +597,7 @@ public Resource validate(List<String> sources, List<String> profiles, List<Sourc
} else if (!first && delay != 0) {
Thread.sleep(delay);
}

// round one: try to read them all natively
// Ignore if it fails.The purpose of this is to make dependencies
// available for other resources to depend on. if it fails to load, there'll be an error if there's
Expand All @@ -610,7 +607,9 @@ public Resource validate(List<String> sources, List<String> profiles, List<Sourc
ref.setCnt(igLoader.loadContent(ref.getRef(), "validate", false, first));
if (loader != null && ref.getCnt() != null) {
try {
loader.load(ref.getCnt());
Resource res = loader.load(ref.getCnt());
ref.setResourceType( res.getResourceType() );
ref.setId( res.getIdElement().getIdPart() );
} catch (Throwable t) {
if (debug) {
System.out.println("Error during round 1 scanning: "+t.getMessage());
Expand All @@ -619,15 +618,17 @@ public Resource validate(List<String> sources, List<String> profiles, List<Sourc
}
}
}


R5EvaluationContext r5EvaluationContext = new R5EvaluationContext(context, refs);

for (SourceFile ref : refs) {
if ((ref.isProcess() || all) && ref.getCnt() != null) {
TimeTracker.Session tts = context.clock().start("validation");
context.clock().milestone();
System.out.println(" Validate " + ref.getRef());

try {
OperationOutcome outcome = validate(ref.getRef(), ref.getCnt().getFocus(), ref.getCnt().getCntType(), profiles, record);
OperationOutcome outcome = validate( ref.getRef(), ref.getCnt().getFocus(), ref.getCnt().getCntType(), profiles, record, r5EvaluationContext );
ToolingExtensions.addStringExtension(outcome, ToolingExtensions.EXT_OO_FILE, ref.getRef());
System.out.println(" " + context.clock().milestone());
results.addEntry().setResource(outcome);
Expand All @@ -648,25 +649,25 @@ public Resource validate(List<String> sources, List<String> profiles, List<Sourc


public ValidatedFragments validateAsFragments(byte[] source, FhirFormat cntType, List<String> profiles, List<ValidationMessage> messages) throws FHIRException, IOException, EOperationOutcome {
InstanceValidator validator = getValidator(cntType);
InstanceValidator validator = getValidator(cntType );
validator.validate(null, messages, new ByteArrayInputStream(source), cntType, asSdList(profiles));
return new ValidatedFragments(validator.validatedContent,
ValidationTime.fromTimeTracker(validator.timeTracker));
}

public OperationOutcome validate(byte[] source, FhirFormat cntType, List<String> profiles, List<ValidationMessage> messages) throws FHIRException, IOException, EOperationOutcome {
InstanceValidator validator = getValidator(cntType);
InstanceValidator validator = getValidator( cntType );

validator.validate(null, messages, new ByteArrayInputStream(source), cntType, asSdList(profiles));
return ValidatorUtils.messagesToOutcome(messages, context, fhirPathEngine);
}

public OperationOutcome validate(String location, ByteProvider 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, R5EvaluationContext r5EvaluationContext) throws FHIRException, IOException, EOperationOutcome, SAXException {
List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
if (doNative) {
SchemaValidator.validateSchema(location, cntType, messages);
}
InstanceValidator validator = getValidator(cntType);
InstanceValidator validator = getValidator( cntType, r5EvaluationContext );
validator.validate(null, messages, new ByteArrayInputStream(source.getBytes()), cntType, asSdList(profiles));
if (showTimes) {
System.out.println(location + ": " + validator.reportTimes());
Expand Down Expand Up @@ -812,7 +813,7 @@ public void convert(String source, String output) throws FHIRException, IOExcept

public String evaluateFhirPath(String source, String expression) throws FHIRException, IOException {
Content cnt = igLoader.loadContent(source, "validate", false, true);
FHIRPathEngine fpe = this.getValidator(null).getFHIRPathEngine();
FHIRPathEngine fpe = this.getValidator(null ).getFHIRPathEngine();
Element e = Manager.parseSingle(context, new ByteArrayInputStream(cnt.getFocus().getBytes()), cnt.getCntType());
ExpressionNode exp = fpe.parse(expression);
return fpe.evaluateToString(new ValidationContext(context, e), e, e, e, exp);
Expand Down Expand Up @@ -849,7 +850,11 @@ public void dropResource(String type, String id) {
}

public InstanceValidator getValidator(FhirFormat format) throws FHIRException, IOException {
InstanceValidator validator = new InstanceValidator(context, null, null);
return getValidator( format, new R5EvaluationContext() );
}

public InstanceValidator getValidator(FhirFormat format, R5EvaluationContext evaluationContext) throws FHIRException, IOException {
InstanceValidator validator = new InstanceValidator(context, evaluationContext, null);
context.getTxClientManager().setUsage("validation");
validator.setHintAboutNonMustSupport(hintAboutNonMustSupport);
validator.setAnyExtensionsAllowed(anyExtensionsAllowed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hl7.fhir.r5.context.SimpleWorkerContext;
import org.hl7.fhir.r5.fhirpath.FHIRPathEngine;
import org.hl7.fhir.r5.model.OperationOutcome;
import org.hl7.fhir.r5.model.ResourceType;
import org.hl7.fhir.r5.renderers.RendererFactory;
import org.hl7.fhir.r5.renderers.utils.RenderingContext;
import org.hl7.fhir.r5.renderers.utils.RenderingContext.GenerationRules;
Expand All @@ -49,7 +50,9 @@ public static class SourceFile {
private long date;
private boolean process;
private Content cnt;

private ResourceType resourceType;
private String id;

public boolean isProcess() {
return process;
}
Expand All @@ -72,6 +75,15 @@ public void setCnt(Content cnt) {
public boolean isKnownToBeMissing () {
return date == 0; // File::lastModified() returns 0 if the file is missing
}

public void setResourceType(ResourceType resourceType ){
this.resourceType = resourceType;
}
public ResourceType getResourceType(){return this.resourceType;}
public void setId( String id ){
this.id = id;
}
public String getId(){return this.id;}
}

protected static void grabNatives(Map<String, ByteProvider> source, Map<String, ByteProvider> binaries, String prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void printHelp(PrintStream out) {

@Override
public void executeTask(ValidationService validationService, ValidationEngine validationEngine, CliContext cliContext, String[] args, TimeTracker tt, TimeTracker.Session tts) throws Exception {
Scanner validationScanner = new Scanner(validationEngine.getContext(), validationEngine.getValidator(null), validationEngine.getIgLoader(), validationEngine.getFhirPathEngine());
Scanner validationScanner = new Scanner(validationEngine.getContext(), validationEngine.getValidator(null ), validationEngine.getIgLoader(), validationEngine.getFhirPathEngine());
validationScanner.validateScan(cliContext.getOutput(), cliContext.getSources());
}

Expand Down
Loading
Loading