Search in sources :

Example 1 with ScanOutputItem

use of org.hl7.fhir.validation.cli.model.ScanOutputItem in project org.hl7.fhir.core by hapifhir.

the class Scanner method validateScan.

public void validateScan(String output, List<String> sources) throws Exception {
    if (Utilities.noString(output))
        throw new Exception("Output parameter required when scanning");
    if (!(new File(output).isDirectory()))
        throw new Exception("Output '" + output + "' must be a directory when scanning");
    System.out.println("  .. scan " + sources + " against loaded IGs");
    Set<String> urls = new HashSet<>();
    for (ImplementationGuide ig : getContext().allImplementationGuides()) {
        if (ig.getUrl().contains("/ImplementationGuide") && !ig.getUrl().equals("http://hl7.org/fhir/ImplementationGuide/fhir"))
            urls.add(ig.getUrl());
    }
    List<ScanOutputItem> res = validateScan(sources, urls);
    genScanOutput(output, res);
    System.out.println("Done. output in " + Utilities.path(output, "scan.html"));
}
Also used : ImplementationGuide(org.hl7.fhir.r5.model.ImplementationGuide) ScanOutputItem(org.hl7.fhir.validation.cli.model.ScanOutputItem) TextFile(org.hl7.fhir.utilities.TextFile) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 2 with ScanOutputItem

use of org.hl7.fhir.validation.cli.model.ScanOutputItem in project org.hl7.fhir.core by hapifhir.

the class Scanner method genScanOutput.

protected void genScanOutput(String folder, List<ScanOutputItem> items) throws IOException, FHIRException, EOperationOutcome {
    String f = Utilities.path(folder, "comparison.zip");
    download("http://fhir.org/archive/comparison.zip", f);
    unzip(f, folder);
    for (int i = 0; i < items.size(); i++) {
        items.get(i).setId("c" + i);
        genScanOutputItem(items.get(i), Utilities.path(folder, items.get(i).getId() + ".html"));
    }
    StringBuilder b = new StringBuilder();
    b.append("<html>");
    b.append("<head>");
    b.append("<title>Implementation Guide Scan</title>");
    b.append("<link rel=\"stylesheet\" href=\"fhir.css\"/>\r\n");
    b.append("<style>\r\n");
    b.append("th \r\n");
    b.append("{\r\n");
    b.append("  vertical-align: bottom;\r\n");
    b.append("  text-align: center;\r\n");
    b.append("}\r\n");
    b.append("\r\n");
    b.append("th span\r\n");
    b.append("{\r\n");
    b.append("  -ms-writing-mode: tb-rl;\r\n");
    b.append("  -webkit-writing-mode: vertical-rl;\r\n");
    b.append("  writing-mode: vertical-rl;\r\n");
    b.append("  transform: rotate(180deg);\r\n");
    b.append("  white-space: nowrap;\r\n");
    b.append("}\r\n");
    b.append("</style>\r\n");
    b.append("</head>");
    b.append("<body>");
    b.append("<h2>Implementation Guide Scan</h2>");
    // organise
    Set<String> refs = new HashSet<>();
    Set<String> igs = new HashSet<>();
    Map<String, Set<String>> profiles = new HashMap<>();
    for (ScanOutputItem item : items) {
        refs.add(item.getRef());
        if (item.getIg() != null) {
            igs.add(item.getIg().getUrl());
            if (!profiles.containsKey(item.getIg().getUrl())) {
                profiles.put(item.getIg().getUrl(), new HashSet<>());
            }
            if (item.getProfile() != null)
                profiles.get(item.getIg().getUrl()).add(item.getProfile().getUrl());
        }
    }
    b.append("<h2>By reference</h2>\r\n");
    b.append("<table class=\"grid\">");
    b.append("<tr><th></th><th></th>");
    for (String s : sort(igs)) {
        ImplementationGuide ig = getContext().fetchResource(ImplementationGuide.class, s);
        b.append("<th colspan=\"" + Integer.toString(profiles.get(s).size() + 1) + "\"><b title=\"" + s + "\">" + ig.present() + "</b></th>");
    }
    b.append("</tr>\r\n");
    b.append("<tr><th><b>Source</b></th><th><span>Core Spec</span></th>");
    for (String s : sort(igs)) {
        ImplementationGuide ig = getContext().fetchResource(ImplementationGuide.class, s);
        b.append("<th><span>Global</span></th>");
        for (String sp : sort(profiles.get(s))) {
            StructureDefinition sd = getContext().fetchResource(StructureDefinition.class, sp);
            b.append("<th><b title=\"" + sp + "\"><span>" + sd.present() + "</span></b></th>");
        }
    }
    b.append("</tr>\r\n");
    for (String s : sort(refs)) {
        b.append("<tr>");
        b.append("<td>" + s + "</td>");
        b.append(genOutcome(items, s, null, null));
        for (String si : sort(igs)) {
            ImplementationGuide ig = getContext().fetchResource(ImplementationGuide.class, si);
            b.append(genOutcome(items, s, si, null));
            for (String sp : sort(profiles.get(ig.getUrl()))) {
                b.append(genOutcome(items, s, si, sp));
            }
        }
        b.append("</tr>\r\n");
    }
    b.append("</table>\r\n");
    b.append("<h2>By IG</h2>\r\n");
    b.append("<table class=\"grid\">");
    b.append("<tr><th></th><th></th>");
    for (String s : sort(refs)) {
        b.append("<th><span>" + s + "</span></th>");
    }
    b.append("</tr>\r\n");
    b.append("<tr><td></td><td>Core Spec</td>");
    for (String s : sort(refs)) {
        b.append(genOutcome(items, s, null, null));
    }
    b.append("</tr>\r\n");
    for (String si : sort(igs)) {
        b.append("<tr>");
        ImplementationGuide ig = getContext().fetchResource(ImplementationGuide.class, si);
        b.append("<td><b title=\"" + si + "\">" + ig.present() + "</b></td>");
        b.append("<td>Global</td>");
        for (String s : sort(refs)) {
            b.append(genOutcome(items, s, si, null));
        }
        b.append("</tr>\r\n");
        for (String sp : sort(profiles.get(ig.getUrl()))) {
            b.append("<tr>");
            StructureDefinition sd = getContext().fetchResource(StructureDefinition.class, sp);
            b.append("<td></td><td><b title=\"" + sp + "\">" + sd.present() + "</b></td>");
            for (String s : sort(refs)) {
                b.append(genOutcome(items, s, si, sp));
            }
            b.append("</tr>\r\n");
        }
    }
    b.append("</table>\r\n");
    b.append("</body>");
    b.append("</html>");
    TextFile.stringToFile(b.toString(), Utilities.path(folder, "scan.html"));
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ImplementationGuide(org.hl7.fhir.r5.model.ImplementationGuide) ScanOutputItem(org.hl7.fhir.validation.cli.model.ScanOutputItem)

Example 3 with ScanOutputItem

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

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

Example 5 with ScanOutputItem

use of org.hl7.fhir.validation.cli.model.ScanOutputItem in project org.hl7.fhir.core by hapifhir.

the class Scanner method genOutcome.

protected String genOutcome(List<ScanOutputItem> items, String src, String ig, String profile) {
    ScanOutputItem item = null;
    for (ScanOutputItem t : items) {
        boolean match = true;
        if (!t.getRef().equals(src))
            match = false;
        if (!((ig == null && t.getIg() == null) || (ig != null && t.getIg() != null && ig.equals(t.getIg().getUrl()))))
            match = false;
        if (!((profile == null && t.getProfile() == null) || (profile != null && t.getProfile() != null && profile.equals(t.getProfile().getUrl()))))
            match = false;
        if (match) {
            item = t;
            break;
        }
    }
    if (item == null)
        return "<td></td>";
    boolean ok = true;
    for (OperationOutcome.OperationOutcomeIssueComponent iss : item.getOutcome().getIssue()) {
        if (iss.getSeverity() == OperationOutcome.IssueSeverity.ERROR || iss.getSeverity() == OperationOutcome.IssueSeverity.FATAL) {
            ok = false;
        }
    }
    if (ok)
        return "<td style=\"background-color: #e6ffe6\"><a href=\"" + item.getId() + ".html\">\u2714</a></td>";
    else
        return "<td style=\"background-color: #ffe6e6\"><a href=\"" + item.getId() + ".html\">\u2716</a></td>";
}
Also used : ScanOutputItem(org.hl7.fhir.validation.cli.model.ScanOutputItem) OperationOutcome(org.hl7.fhir.r5.model.OperationOutcome) EOperationOutcome(org.hl7.fhir.r5.utils.EOperationOutcome)

Aggregations

ScanOutputItem (org.hl7.fhir.validation.cli.model.ScanOutputItem)4 ImplementationGuide (org.hl7.fhir.r5.model.ImplementationGuide)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)2 Element (org.hl7.fhir.r5.elementmodel.Element)1 OperationOutcome (org.hl7.fhir.r5.model.OperationOutcome)1 RenderingContext (org.hl7.fhir.r5.renderers.utils.RenderingContext)1 EOperationOutcome (org.hl7.fhir.r5.utils.EOperationOutcome)1 TextFile (org.hl7.fhir.utilities.TextFile)1 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)1 XhtmlComposer (org.hl7.fhir.utilities.xhtml.XhtmlComposer)1