Search in sources :

Example 1 with ValidationOutputRenderer

use of org.hl7.fhir.validation.cli.renderers.ValidationOutputRenderer 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)

Aggregations

FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 MemoryMXBean (java.lang.management.MemoryMXBean)1 ArrayList (java.util.ArrayList)1 ValidationRecord (org.hl7.fhir.validation.ValidationRecord)1 ValidationOutputRenderer (org.hl7.fhir.validation.cli.renderers.ValidationOutputRenderer)1