Search in sources :

Example 16 with RenderingContext

use of org.hl7.fhir.r4b.renderers.utils.RenderingContext in project org.hl7.fhir.core by hapifhir.

the class SnapShotGenerationTests method testGen.

private void testGen(boolean fail, TestDetails test, SnapShotGenerationTestsContext context) throws Exception {
    if (!Utilities.noString(test.register)) {
        List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
        ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), messages, null);
        pu.setNewSlicingProcessing(true);
        for (StructureDefinition sd : test.included) {
            pu.setIds(sd, false);
        }
        for (StructureDefinition sd : test.included) {
            if (!TestingUtilities.context().hasResource(StructureDefinition.class, sd.getUrl())) {
                TestingUtilities.context().cacheResource(sd);
            }
        }
        StructureDefinition base = TestingUtilities.context().fetchResource(StructureDefinition.class, test.included.get(0).getBaseDefinition());
        if (base != null) {
            pu.generateSnapshot(base, test.included.get(0), test.included.get(0).getUrl(), "http://test.org/profile", test.included.get(0).getName());
        }
        int ec = 0;
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                System.out.println(vm.summary());
                ec++;
            }
        }
        if (ec > 0)
            throw new FHIRException("register gen failed: " + messages.toString());
    }
    StructureDefinition base = getSD(test.getSource().getBaseDefinition(), context);
    if (!base.getUrl().equals(test.getSource().getBaseDefinition()))
        throw new Exception("URL mismatch on base: " + base.getUrl() + " wanting " + test.getSource().getBaseDefinition());
    StructureDefinition output = test.getSource().copy();
    ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), messages, new TestPKP());
    pu.setNewSlicingProcessing(test.isNewSliceProcessing());
    pu.setThrowException(false);
    pu.setDebug(test.isDebug());
    pu.setIds(test.getSource(), false);
    if (!TestingUtilities.context().hasPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER)) {
        NpmPackage npm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER);
        TestingUtilities.context().loadFromPackage(npm, new TestLoader(new String[] { "StructureDefinition" }), new String[] { "StructureDefinition" });
    }
    pu.setXver(new XVerExtensionManager(TestingUtilities.context()));
    if (test.isSort()) {
        List<String> errors = new ArrayList<String>();
        int lastCount = output.getDifferential().getElement().size();
        pu.sortDifferential(base, output, test.getSource().getName(), errors, false);
        if (errors.size() > 0)
            throw new FHIRException("Sort failed: " + errors.toString());
    }
    try {
        messages.clear();
        pu.generateSnapshot(base, output, test.getSource().getUrl(), "http://test.org/profile", test.getSource().getName());
        List<ValidationMessage> ml = new ArrayList<>();
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                ml.add(vm);
            }
        }
        if (ml.size() > 0) {
            throw new FHIRException("Snapshot Generation failed: " + ml.toString());
        }
    } catch (Throwable e) {
        System.out.println("\r\nException: " + e.getMessage());
        throw e;
    }
    if (output.getDifferential().hasElement()) {
        RenderingContext rc = new RenderingContext(TestingUtilities.context(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
        rc.setDestDir(Utilities.path("[tmp]", "snapshot"));
        rc.setProfileUtilities(new ProfileUtilities(TestingUtilities.context(), null, new TestPKP()));
        RendererFactory.factory(output, rc).render(output);
    }
    if (!fail) {
        test.output = output;
        TestingUtilities.context().cacheResource(output);
        File dst = new File(TestingUtilities.tempFile("snapshot", test.getId() + "-expected.xml"));
        if (dst.exists())
            dst.delete();
        IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "snapshot-generation", test.getId() + "-expected.xml"), new FileOutputStream(dst));
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.tempFile("snapshot", test.getId() + "-actual.xml")), output);
        StructureDefinition t1 = test.expected.copy();
        t1.setText(null);
        StructureDefinition t2 = test.output.copy();
        t2.setText(null);
        Assertions.assertTrue(t1.equalsDeep(t2), "Output does not match expected");
    }
}
Also used : FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) RenderingContext(org.hl7.fhir.r4b.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r4b.formats.XmlParser) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) ProfileUtilities(org.hl7.fhir.r4b.conformance.ProfileUtilities) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) XVerExtensionManager(org.hl7.fhir.r4b.utils.XVerExtensionManager) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 17 with RenderingContext

use of org.hl7.fhir.r4b.renderers.utils.RenderingContext in project org.hl7.fhir.core by hapifhir.

the class ResourceRoundTripTests method test.

@Test
public void test() throws IOException, FHIRException, EOperationOutcome {
    DomainResource res = (DomainResource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "unicode.xml"));
    RenderingContext rc = new RenderingContext(TestingUtilities.context(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
    RendererFactory.factory(res, rc).render(res);
    IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "unicode.xml"), new FileOutputStream(TestingUtilities.tempFile("gen", "unicode.xml")));
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.tempFile("gen", "unicode.out.xml")), res);
}
Also used : XmlParser(org.hl7.fhir.r4b.formats.XmlParser) RenderingContext(org.hl7.fhir.r4b.renderers.utils.RenderingContext) DomainResource(org.hl7.fhir.r4b.model.DomainResource) FileOutputStream(java.io.FileOutputStream) Test(org.junit.jupiter.api.Test)

Example 18 with RenderingContext

use of org.hl7.fhir.r4b.renderers.utils.RenderingContext in project org.hl7.fhir.core by hapifhir.

the class SnapShotGenerationXTests method testGen.

private void testGen(boolean fail, TestDetails test) throws Exception {
    if (!Utilities.noString(test.register)) {
        List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
        ProfileUtilities pu = new ProfileUtilities(UtilitiesXTests.context(version), messages, null);
        pu.setNewSlicingProcessing(true);
        pu.setIds(test.included, false);
        StructureDefinition base = UtilitiesXTests.context(version).fetchResource(StructureDefinition.class, test.included.getBaseDefinition());
        if (base != null) {
            pu.generateSnapshot(base, test.included, test.included.getUrl(), "http://test.org/profile", test.included.getName());
        }
        if (!UtilitiesXTests.context(version).hasResource(StructureDefinition.class, test.included.getUrl()))
            UtilitiesXTests.context(version).cacheResource(test.included);
        int ec = 0;
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                System.out.println(vm.summary());
                ec++;
            }
        }
        if (ec > 0)
            throw new FHIRException("register gen failed: " + messages.toString());
    }
    StructureDefinition base = getSD(test.getSource().getBaseDefinition());
    if (!base.getUrl().equals(test.getSource().getBaseDefinition()))
        throw new Exception("URL mismatch on base: " + base.getUrl() + " wanting " + test.getSource().getBaseDefinition());
    StructureDefinition output = test.getSource().copy();
    ProfileUtilities pu = new ProfileUtilities(UtilitiesXTests.context(version), messages, new TestPKP());
    pu.setNewSlicingProcessing(test.isNewSliceProcessing());
    pu.setThrowException(false);
    pu.setDebug(test.isDebug());
    pu.setIds(test.getSource(), false);
    if (test.isSort()) {
        List<String> errors = new ArrayList<String>();
        int lastCount = output.getDifferential().getElement().size();
        pu.sortDifferential(base, output, test.getSource().getName(), errors, false);
        if (errors.size() > 0)
            throw new FHIRException("Sort failed: " + errors.toString());
    }
    try {
        messages.clear();
        pu.generateSnapshot(base, output, test.getSource().getUrl(), "http://test.org/profile", test.getSource().getName());
        List<ValidationMessage> ml = new ArrayList<>();
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                ml.add(vm);
            }
        }
        if (ml.size() > 0) {
            throw new FHIRException("Snapshot Generation failed: " + ml.toString());
        }
    } catch (Throwable e) {
        System.out.println("\r\nException: " + e.getMessage());
        throw e;
    }
    if (output.getDifferential().hasElement()) {
        RenderingContext rc = new RenderingContext(TestingUtilities.getSharedWorkerContext(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
        rc.setDestDir(makeTempDir());
        rc.setProfileUtilities(new ProfileUtilities(TestingUtilities.getSharedWorkerContext(), null, new TestPKP()));
        RendererFactory.factory(output, rc).render(output);
    }
    if (!fail) {
        test.output = output;
        UtilitiesXTests.context(version).cacheResource(output);
        File dst = new File(UtilitiesXTests.tempFile("snapshot", test.getId() + "-expected.xml"));
        if (dst.exists())
            dst.delete();
        IOUtils.copy(UtilitiesXTests.loadTestResourceStream("rX", "snapshot-generation", test.getId() + "-expected.xml"), new FileOutputStream(dst));
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(UtilitiesXTests.tempFile("snapshot", test.getId() + "-actual.xml")), output);
        StructureDefinition t1 = test.expected.copy();
        t1.setText(null);
        StructureDefinition t2 = test.output.copy();
        t2.setText(null);
        Assertions.assertTrue(t1.equalsDeep(t2), "Output does not match expected");
    }
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r5.formats.XmlParser) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 19 with RenderingContext

use of org.hl7.fhir.r4b.renderers.utils.RenderingContext in project kindling by HL7.

the class PageProcessor method renderCodeSystemWithLangs.

private String renderCodeSystemWithLangs(Set<String> langs, CodeSystem cs, String prefix) throws Exception {
    Narrative n = cs.getText();
    StringBuilder b = new StringBuilder();
    b.append("<div id=\"tabs\" style=\"border-right-style: none;\">\r\n");
    b.append("<ul>\r\n");
    b.append("<li><a href=\"#tabs-all\">All Languages</a></li>\r\n");
    b.append("<li><a href=\"#tabs-en\">English</a></li>\r\n");
    for (String l : sorted(langs)) b.append("<li><a href=\"#tabs-" + l + "\">" + langDisplay(l) + "</a></li>\r\n");
    b.append("</ul>\r\n");
    b.append("<div id=\"tabs-all\">\r\n");
    cs.setText(null);
    RenderingContext lrc = rc.copy().setLang("*");
    RendererFactory.factory(cs, lrc).render(cs);
    b.append(new XhtmlComposer(XhtmlComposer.HTML).compose(cs.getText().getDiv()));
    b.append("</div>\r\n");
    b.append("<div id=\"tabs-en\">\r\n");
    cs.setText(null);
    lrc = rc.copy().setLang("en");
    RendererFactory.factory(cs, lrc).render(cs);
    b.append(new XhtmlComposer(XhtmlComposer.HTML).compose(cs.getText().getDiv()));
    b.append("</div>\r\n");
    for (String l : sorted(langs)) {
        b.append("<div id=\"tabs-" + l + "\">\r\n");
        String desc = cs.getDescriptionElement().getTranslation(l);
        if (!Utilities.noString(desc))
            b.append(processMarkdown("RenderingCodeSystem", workerContext.translator().translate("render-cs", "Definition", l) + ": " + desc, prefix));
        cs.setText(null);
        lrc = rc.copy().setLang(l);
        RendererFactory.factory(cs, lrc).render(cs);
        b.append(new XhtmlComposer(XhtmlComposer.HTML).compose(cs.getText().getDiv()));
        b.append("</div>\r\n");
    }
    b.append("</div>\r\n");
    cs.setText(n);
    return b.toString();
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) Narrative(org.hl7.fhir.r5.model.Narrative) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer)

Example 20 with RenderingContext

use of org.hl7.fhir.r4b.renderers.utils.RenderingContext in project kindling by HL7.

the class PageProcessor method makeRenderingContext.

public void makeRenderingContext() {
    rc = new RenderingContext(workerContext, processor, ValidationOptions.defaults(), "", "", null, ResourceRendererMode.IG);
    rc.setParser(this);
    rc.setResolver(this);
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext)

Aggregations

RenderingContext (org.hl7.fhir.r5.renderers.utils.RenderingContext)22 FHIRException (org.hl7.fhir.exceptions.FHIRException)14 FileOutputStream (java.io.FileOutputStream)13 XmlParser (org.hl7.fhir.r5.formats.XmlParser)13 XhtmlComposer (org.hl7.fhir.utilities.xhtml.XhtmlComposer)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 FileNotFoundException (java.io.FileNotFoundException)9 JsonParser (org.hl7.fhir.r5.formats.JsonParser)8 Cell (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell)8 TransformerException (javax.xml.transform.TransformerException)6 NotImplementedException (org.apache.commons.lang3.NotImplementedException)6 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)6 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)6 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)6 Piece (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece)6 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)5 ValueSet (org.hl7.fhir.r5.model.ValueSet)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 IParser (org.hl7.fhir.r5.formats.IParser)4