Search in sources :

Example 1 with ValidationRecord

use of org.hl7.fhir.validation.ValidationRecord in project org.hl7.fhir.core by hapifhir.

the class HTMLOutputGenerator method generate.

public String generate(long time) {
    StringBuilder b = new StringBuilder();
    b.append(genHeader(time));
    int i = 0;
    for (ValidationRecord f : records) {
        i++;
        b.append(genSummaryRow(i, f));
    }
    b.append("</table>\r\n");
    i = 0;
    int id = 0;
    for (ValidationRecord f : records) {
        i++;
        b.append(genStart(i, f));
        if (f.getMessages().size() > 0) {
            b.append(" <table class=\"grid\">\r\n" + "   <tr>\r\n" + "     <td><b>Path</b></td><td><b>Severity</b></td><td><b>Message</b></td>\r\n" + "   </tr>\r\n");
            for (ValidationMessage vm : f.getMessages()) {
                id++;
                b.append(genDetails(vm, "m" + id));
            }
            b.append("</table>\r\n");
        } else {
            b.append("<p>No Issues detected</p>\r\n");
        }
    }
    return b.toString();
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ValidationRecord(org.hl7.fhir.validation.ValidationRecord)

Example 2 with ValidationRecord

use of org.hl7.fhir.validation.ValidationRecord in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method validate.

public Resource validate(List<String> sources, List<String> profiles, List<ValidationRecord> record) throws FHIRException, IOException {
    if (profiles.size() > 0) {
        System.out.println("  Profiles: " + profiles);
    }
    List<String> refs = new ArrayList<String>();
    boolean asBundle = ValidatorUtils.parseSources(sources, refs, context);
    Bundle results = new Bundle();
    results.setType(Bundle.BundleType.COLLECTION);
    for (String ref : refs) {
        TimeTracker.Session tts = context.clock().start("validation");
        context.clock().milestone();
        System.out.print("  Validate " + ref);
        Content cnt = igLoader.loadContent(ref, "validate", false);
        try {
            OperationOutcome outcome = validate(ref, cnt.focus, cnt.cntType, profiles, record);
            ToolingExtensions.addStringExtension(outcome, ToolingExtensions.EXT_OO_FILE, ref);
            System.out.println(" " + context.clock().milestone());
            results.addEntry().setResource(outcome);
            tts.end();
        } catch (Exception e) {
            System.out.println("Validation Infrastructure fail validating " + ref + ": " + e.getMessage());
            tts.end();
            throw new FHIRException(e);
        }
    }
    if (asBundle)
        return results;
    else
        return results.getEntryFirstRep().getResource();
}
Also used : TimeTracker(org.hl7.fhir.utilities.TimeTracker) EOperationOutcome(org.hl7.fhir.r5.utils.EOperationOutcome) FHIRException(org.hl7.fhir.exceptions.FHIRException) URISyntaxException(java.net.URISyntaxException) SAXException(org.xml.sax.SAXException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 3 with ValidationRecord

use of org.hl7.fhir.validation.ValidationRecord 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 4 with ValidationRecord

use of org.hl7.fhir.validation.ValidationRecord in project org.hl7.fhir.core by hapifhir.

the class ValidationService method validateSources.

public void validateSources(CliContext cliContext, ValidationEngine validator) throws Exception {
    long start = System.currentTimeMillis();
    List<ValidationRecord> records = new ArrayList<>();
    Resource r = validator.validate(cliContext.getSources(), cliContext.getProfiles(), records);
    MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
    System.out.println("Done. " + validator.getContext().clock().report() + ". Memory = " + Utilities.describeSize(mbean.getHeapMemoryUsage().getUsed() + mbean.getNonHeapMemoryUsage().getUsed()));
    System.out.println();
    PrintStream dst = null;
    if (cliContext.getOutput() == null) {
        dst = System.out;
    } else {
        dst = new PrintStream(new FileOutputStream(cliContext.getOutput()));
    }
    ValidationOutputRenderer renderer = makeValidationOutputRenderer(cliContext);
    renderer.setOutput(dst);
    renderer.setCrumbTrails(validator.isCrumbTrails());
    int ec = 0;
    if (r instanceof Bundle) {
        if (renderer.handlesBundleDirectly()) {
            renderer.render((Bundle) r);
        } else {
            renderer.start(((Bundle) r).getEntry().size() > 1);
            for (Bundle.BundleEntryComponent e : ((Bundle) r).getEntry()) {
                OperationOutcome op = (OperationOutcome) e.getResource();
                ec = ec + countErrors(op);
                renderer.render(op);
            }
            renderer.finish();
        }
    } else if (r == null) {
        ec = ec + 1;
        System.out.println("No output from validation - nothing to validate");
    } else {
        renderer.start(false);
        OperationOutcome op = (OperationOutcome) r;
        ec = countErrors(op);
        renderer.render((OperationOutcome) r);
        renderer.finish();
    }
    if (cliContext.getOutput() != null) {
        dst.close();
    }
    if (cliContext.getHtmlOutput() != null) {
        String html = new HTMLOutputGenerator(records).generate(System.currentTimeMillis() - start);
        TextFile.stringToFile(html, cliContext.getHtmlOutput());
        System.out.println("HTML Summary in " + cliContext.getHtmlOutput());
    }
    System.exit(ec > 0 ? 1 : 0);
}
Also used : PrintStream(java.io.PrintStream) ValidationOutputRenderer(org.hl7.fhir.validation.cli.renderers.ValidationOutputRenderer) ArrayList(java.util.ArrayList) MemoryMXBean(java.lang.management.MemoryMXBean) FileOutputStream(java.io.FileOutputStream) ValidationRecord(org.hl7.fhir.validation.ValidationRecord)

Example 5 with ValidationRecord

use of org.hl7.fhir.validation.ValidationRecord in project org.hl7.fhir.core by hapifhir.

the class HTMLOutputGenerator method genHeader.

private String genHeader(long time) {
    int err = 0;
    int warn = 0;
    int info = 0;
    for (ValidationRecord f : records) {
        err = err + f.getErr();
        warn = warn + f.getWarn();
        info = info + f.getInfo();
    }
    return "<!DOCTYPE HTML>\r\n" + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\r\n" + "<head>\r\n" + "  <title>Validation Results</title>\r\n" + "  <link href=\"http://hl7.org/fhir/fhir.css\" rel=\"stylesheet\"/>\r\n" + "  <style>\r\n" + "    span.flip  { background-color: #4CAF50; color: white; border: solid 1px #a6d8a8; padding: 2px }\r\n" + "  </style>\r\n" + "  <script>\r\n" + "    function flip(id) {\r\n" + "      var span = document.getElementById('s'+id);\r\n" + "      var div = document.getElementById(id);\r\n" + "      if (document.getElementById('s'+id).innerHTML == 'Show Reasoning') {\r\n" + "        div.style.display = 'block';\r\n" + "        span.innerHTML = 'Hide Reasoning';\r\n" + "      } else {\r\n" + "        div.style.display = 'none';\r\n" + "        span.innerHTML = 'Show Reasoning';\r\n" + "      }\r\n" + "    }\r\n" + "  </script>\r\n" + "</head>\r\n" + "<body style=\"margin: 20px; background-color: #ffffff\">\r\n" + " <h1>Validation Results</h1>\r\n" + " <p>" + err + " " + Utilities.pluralize("error", err) + ", " + warn + " " + Utilities.pluralize("warning", warn) + ", " + info + " " + Utilities.pluralize("hint", info) + ". Generated " + now() + " by Validator " + VersionUtil.getVersionString() + " (" + time + "ms)</p>\r\n" + " <table class=\"grid\">\r\n" + "   <tr>\r\n" + "     <td><b>Filename</b></td><td><b>Errors</b></td><td><b>Warnings</b></td><td><b>Hints</b></td>\r\n" + "   </tr>\r\n";
}
Also used : ValidationRecord(org.hl7.fhir.validation.ValidationRecord)

Aggregations

ValidationRecord (org.hl7.fhir.validation.ValidationRecord)3 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)2 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 MemoryMXBean (java.lang.management.MemoryMXBean)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)1 FHIRException (org.hl7.fhir.exceptions.FHIRException)1 EOperationOutcome (org.hl7.fhir.r5.utils.EOperationOutcome)1 TimeTracker (org.hl7.fhir.utilities.TimeTracker)1 ValidationOutputRenderer (org.hl7.fhir.validation.cli.renderers.ValidationOutputRenderer)1 InstanceValidator (org.hl7.fhir.validation.instance.InstanceValidator)1 SAXException (org.xml.sax.SAXException)1