Search in sources :

Example 61 with EOperationOutcome

use of org.hl7.fhir.r5.utils.EOperationOutcome in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method validate.

public OperationOutcome validate(String location, byte[] 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));
    if (showTimes) {
        System.out.println(location + ": " + validator.reportTimes());
    }
    if (record != null) {
        record.add(new ValidationRecord(location, messages));
    }
    return ValidatorUtils.messagesToOutcome(messages, context, fhirPathEngine);
}
Also used : InstanceValidator(org.hl7.fhir.validation.instance.InstanceValidator) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

Example 62 with EOperationOutcome

use of org.hl7.fhir.r5.utils.EOperationOutcome in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method generate.

public Resource generate(String source, String version) throws FHIRException, IOException, EOperationOutcome {
    Content cnt = igLoader.loadContent(source, "validate", false);
    Resource res = igLoader.loadResourceByVersion(version, cnt.focus, source);
    RenderingContext rc = new RenderingContext(context, null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
    genResource(res, rc);
    return (Resource) res;
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext)

Example 63 with EOperationOutcome

use of org.hl7.fhir.r5.utils.EOperationOutcome in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method validate.

public OperationOutcome validate(String location, byte[] source, FhirFormat cntType, List<String> profiles, IdStatus resourceIdRule, boolean anyExtensionsAllowed, BestPracticeWarningLevel bpWarnings, CheckDisplayOption displayOption) throws FHIRException, IOException, EOperationOutcome, SAXException {
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    if (doNative) {
        SchemaValidator.validateSchema(location, cntType, messages);
    }
    InstanceValidator validator = getValidator(cntType);
    validator.setResourceIdRule(resourceIdRule);
    validator.setBestPracticeWarningLevel(bpWarnings);
    validator.setCheckDisplay(displayOption);
    validator.validate(null, messages, new ByteArrayInputStream(source), cntType, asSdList(profiles));
    return ValidatorUtils.messagesToOutcome(messages, context, fhirPathEngine);
}
Also used : InstanceValidator(org.hl7.fhir.validation.instance.InstanceValidator) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

Example 64 with EOperationOutcome

use of org.hl7.fhir.r5.utils.EOperationOutcome in project org.hl7.fhir.core by hapifhir.

the class Scanner method validateScan.

protected List<ScanOutputItem> validateScan(List<String> sources, Set<String> guides) throws FHIRException, IOException, EOperationOutcome {
    List<String> refs = new ArrayList<>();
    ValidatorUtils.parseSources(sources, refs, getContext());
    List<ScanOutputItem> res = new ArrayList();
    for (String ref : refs) {
        Content cnt = getIgLoader().loadContent(ref, "validate", false);
        List<ValidationMessage> messages = new ArrayList<>();
        Element e = null;
        try {
            System.out.println("Validate " + ref);
            messages.clear();
            e = getValidator().validate(null, messages, new ByteArrayInputStream(cnt.focus), cnt.cntType);
            res.add(new ScanOutputItem(ref, null, null, ValidatorUtils.messagesToOutcome(messages, getContext(), getFhirPathEngine())));
        } catch (Exception ex) {
            res.add(new ScanOutputItem(ref, null, null, exceptionToOutcome(ex)));
        }
        if (e != null) {
            String rt = e.fhirType();
            for (String u : guides) {
                ImplementationGuide ig = getContext().fetchResource(ImplementationGuide.class, u);
                System.out.println("Check Guide " + ig.getUrl());
                String canonical = ig.getUrl().contains("/Impl") ? ig.getUrl().substring(0, ig.getUrl().indexOf("/Impl")) : ig.getUrl();
                String url = getGlobal(ig, rt);
                if (url != null) {
                    try {
                        System.out.println("Validate " + ref + " against " + ig.getUrl());
                        messages.clear();
                        getValidator().validate(null, messages, new ByteArrayInputStream(cnt.focus), cnt.cntType, url);
                        res.add(new ScanOutputItem(ref, ig, null, ValidatorUtils.messagesToOutcome(messages, getContext(), getFhirPathEngine())));
                    } catch (Exception ex) {
                        res.add(new ScanOutputItem(ref, ig, null, exceptionToOutcome(ex)));
                    }
                }
                Set<String> done = new HashSet<>();
                for (StructureDefinition sd : getContext().allStructures()) {
                    if (!done.contains(sd.getUrl())) {
                        done.add(sd.getUrl());
                        if (sd.getUrl().startsWith(canonical) && rt.equals(sd.getType())) {
                            try {
                                System.out.println("Validate " + ref + " against " + sd.getUrl());
                                messages.clear();
                                validator.validate(null, messages, new ByteArrayInputStream(cnt.focus), cnt.cntType, Collections.singletonList(sd));
                                res.add(new ScanOutputItem(ref, ig, sd, ValidatorUtils.messagesToOutcome(messages, getContext(), getFhirPathEngine())));
                            } catch (Exception ex) {
                                res.add(new ScanOutputItem(ref, ig, sd, exceptionToOutcome(ex)));
                            }
                        }
                    }
                }
            }
        }
    }
    return res;
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ImplementationGuide(org.hl7.fhir.r5.model.ImplementationGuide) Element(org.hl7.fhir.r5.elementmodel.Element) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ScanOutputItem(org.hl7.fhir.validation.cli.model.ScanOutputItem)

Example 65 with EOperationOutcome

use of org.hl7.fhir.r5.utils.EOperationOutcome in project org.hl7.fhir.core by hapifhir.

the class Scanner method genScanOutputItem.

protected void genScanOutputItem(ScanOutputItem item, String filename) throws IOException, FHIRException, EOperationOutcome {
    RenderingContext rc = new RenderingContext(getContext(), null, null, "http://hl7.org/fhir", "", null, RenderingContext.ResourceRendererMode.END_USER);
    rc.setNoSlowLookup(true);
    RendererFactory.factory(item.getOutcome(), rc).render(item.getOutcome());
    String s = new XhtmlComposer(XhtmlComposer.HTML).compose(item.getOutcome().getText().getDiv());
    String title = item.getTitle();
    StringBuilder b = new StringBuilder();
    b.append("<html>");
    b.append("<head>");
    b.append("<title>" + title + "</title>");
    b.append("<link rel=\"stylesheet\" href=\"fhir.css\"/>\r\n");
    b.append("</head>");
    b.append("<body>");
    b.append("<h2>" + title + "</h2>");
    b.append(s);
    b.append("</body>");
    b.append("</html>");
    TextFile.stringToFile(b.toString(), filename);
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer)

Aggregations

XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)52 FHIRException (org.hl7.fhir.exceptions.FHIRException)38 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)37 ArrayList (java.util.ArrayList)35 ProfileUtilities (org.hl7.fhir.dstu3.conformance.ProfileUtilities)14 ElementDefinition (org.hl7.fhir.dstu2.model.ElementDefinition)13 StructureDefinition (org.hl7.fhir.dstu2.model.StructureDefinition)13 ProfileUtilities (org.hl7.fhir.dstu2.utils.ProfileUtilities)13 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)13 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)13 FileOutputStream (java.io.FileOutputStream)10 Test (org.junit.jupiter.api.Test)10 IOException (java.io.IOException)9 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)9 DomainResource (org.hl7.fhir.r5.model.DomainResource)7 DomainResource (org.hl7.fhir.r4b.model.DomainResource)6 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)6 BaseWrapper (org.hl7.fhir.r4b.renderers.utils.BaseWrappers.BaseWrapper)6 BaseWrapper (org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper)6 ElementDefinition (org.hl7.fhir.r4b.model.ElementDefinition)5