Search in sources :

Example 6 with NarrativeGenerator

use of org.hl7.fhir.r4.utils.NarrativeGenerator in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireBuilder method build.

public void build() throws FHIRException {
    if (profile == null)
        throw new DefinitionException("QuestionnaireBuilder.build: no profile found");
    if (resource != null)
        if (!profile.getType().equals(resource.getResourceType().toString()))
            throw new DefinitionException("Wrong Type");
    if (prebuiltQuestionnaire != null)
        questionnaire = prebuiltQuestionnaire;
    else
        questionnaire = new Questionnaire();
    if (resource != null)
        response = new QuestionnaireResponse();
    processMetadata();
    List<ElementDefinition> list = new ArrayList<ElementDefinition>();
    List<QuestionnaireResponse.QuestionnaireResponseItemComponent> answerGroups = new ArrayList<QuestionnaireResponse.QuestionnaireResponseItemComponent>();
    if (resource != null)
        answerGroups.addAll(response.getItem());
    if (prebuiltQuestionnaire != null) {
        // give it a fake group to build
        Questionnaire.QuestionnaireItemComponent group = new Questionnaire.QuestionnaireItemComponent();
        group.setType(QuestionnaireItemType.GROUP);
        buildGroup(group, profile, profile.getSnapshot().getElement().get(0), list, answerGroups);
    } else
        buildGroup(questionnaire.getItem().get(0), profile, profile.getSnapshot().getElement().get(0), list, answerGroups);
// 
// NarrativeGenerator ngen = new NarrativeGenerator(context);
// ngen.generate(result);
// 
// if FResponse <> nil then
// FResponse.collapseAllContained;
}
Also used : QuestionnaireItemComponent(org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemComponent) Questionnaire(org.hl7.fhir.r5.model.Questionnaire) ArrayList(java.util.ArrayList) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) QuestionnaireResponse(org.hl7.fhir.r5.model.QuestionnaireResponse) QuestionnaireItemComponent(org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemComponent)

Example 7 with NarrativeGenerator

use of org.hl7.fhir.r4.utils.NarrativeGenerator in project org.hl7.fhir.core by hapifhir.

the class ResourceRoundTripTests method test.

@Test
public void test() throws FileNotFoundException, IOException, FHIRException, EOperationOutcome {
    if (TestingUtilities.context == null)
        TestingUtilities.context = SimpleWorkerContext.fromPack("C:\\work\\org.hl7.fhir\\build\\publish\\definitions.xml.zip");
    Resource res = new XmlParser().parse(new FileInputStream("C:\\work\\org.hl7.fhir\\build\\tests\\resources\\unicode.xml"));
    new NarrativeGenerator("", "", TestingUtilities.context).generate((DomainResource) res);
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream("C:\\work\\org.hl7.fhir\\build\\tests\\resources\\unicode.out.xml"), res);
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) NarrativeGenerator(org.hl7.fhir.dstu3.utils.NarrativeGenerator) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.model.Resource) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) FileInputStream(java.io.FileInputStream) Test(org.junit.jupiter.api.Test)

Example 8 with NarrativeGenerator

use of org.hl7.fhir.r4.utils.NarrativeGenerator in project org.hl7.fhir.core by hapifhir.

the class ResourceRoundTripTests method test.

@Test
@Disabled
public void test() throws FileNotFoundException, IOException, FHIRException, EOperationOutcome, UcumException {
    Resource res = new XmlParser().parse(new FileInputStream(TestingUtilities.resourceNameToFile("unicode.xml")));
    new NarrativeGenerator("", "", TestingUtilities.context()).generate((DomainResource) res, null);
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.resourceNameToFile("gen", "unicode.out.xml")), res);
}
Also used : XmlParser(org.hl7.fhir.r4.formats.XmlParser) NarrativeGenerator(org.hl7.fhir.r4.utils.NarrativeGenerator) Resource(org.hl7.fhir.r4.model.Resource) DomainResource(org.hl7.fhir.r4.model.DomainResource) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 9 with NarrativeGenerator

use of org.hl7.fhir.r4.utils.NarrativeGenerator in project org.hl7.fhir.core by hapifhir.

the class SnapShotGenerationTests method testGen.

private void testGen(TestDetails test, SnapShotGenerationTestsContext context) throws Exception {
    if (!Utilities.noString(test.register)) {
        ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), null, null);
        pu.setNewSlicingProcessing(true);
        List<String> errors = new ArrayList<String>();
        pu.setIds(test.included, false);
        StructureDefinition base = TestingUtilities.context().fetchResource(StructureDefinition.class, test.included.getBaseDefinition());
        pu.generateSnapshot(base, test.included, test.included.getUrl(), "http://test.org/profile", test.included.getName());
        TestingUtilities.context().cacheResource(test.included);
    }
    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(true);
    pu.setThrowException(true);
    pu.setDebug(true);
    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);
        if (errors.size() > 0)
            throw new FHIRException("Sort failed: " + errors.toString());
    }
    try {
        pu.generateSnapshot(base, output, test.getSource().getUrl(), "http://test.org/profile", test.getSource().getName());
    } catch (Throwable e) {
        System.out.println("\r\nException: " + e.getMessage());
        throw e;
    }
    if (output.getDifferential().hasElement())
        new NarrativeGenerator("", "http://hl7.org/fhir", TestingUtilities.context()).setPkp(new TestPKP()).generate(output, null);
    test.output = output;
    TestingUtilities.context().cacheResource(output);
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.resourceNameToFile("snapshot-generation", 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 : XmlParser(org.hl7.fhir.r4.formats.XmlParser) 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) NarrativeGenerator(org.hl7.fhir.r4.utils.NarrativeGenerator) StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) ProfileUtilities(org.hl7.fhir.r4.conformance.ProfileUtilities) FileOutputStream(java.io.FileOutputStream)

Example 10 with NarrativeGenerator

use of org.hl7.fhir.r4.utils.NarrativeGenerator in project org.hl7.fhir.core by hapifhir.

the class NarrativeGenerator method addMarkdown.

private void addMarkdown(XhtmlNode x, String text) throws FHIRFormatError, IOException, DefinitionException {
    if (text != null) {
        // 1. custom FHIR extensions
        while (text.contains("[[[")) {
            String left = text.substring(0, text.indexOf("[[["));
            String link = text.substring(text.indexOf("[[[") + 3, text.indexOf("]]]"));
            String right = text.substring(text.indexOf("]]]") + 3);
            String url = link;
            String[] parts = link.split("\\#");
            StructureDefinition p = context.fetchResource(StructureDefinition.class, parts[0]);
            if (p == null)
                p = context.fetchTypeDefinition(parts[0]);
            if (p == null)
                p = context.fetchResource(StructureDefinition.class, link);
            if (p != null) {
                url = p.getUserString("path");
                if (url == null)
                    url = p.getUserString("filename");
            } else
                throw new DefinitionException("Unable to resolve markdown link " + link);
            text = left + "[" + link + "](" + url + ")" + right;
        }
        // 2. markdown
        String s = new MarkDownProcessor(Dialect.DARING_FIREBALL).process(Utilities.escapeXml(text), "NarrativeGenerator");
        XhtmlParser p = new XhtmlParser();
        XhtmlNode m;
        try {
            m = p.parse("<div>" + s + "</div>", "div");
        } catch (org.hl7.fhir.exceptions.FHIRFormatError e) {
            throw new FHIRFormatError(e.getMessage(), e);
        }
        x.getChildNodes().addAll(m.getChildNodes());
    }
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) MarkDownProcessor(org.hl7.fhir.utilities.MarkDownProcessor) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Aggregations

DefinitionException (org.hl7.fhir.exceptions.DefinitionException)10 ArrayList (java.util.ArrayList)7 NarrativeGenerator (org.hl7.fhir.dstu3.utils.NarrativeGenerator)3 MarkDownProcessor (org.hl7.fhir.utilities.MarkDownProcessor)3 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)3 XhtmlParser (org.hl7.fhir.utilities.xhtml.XhtmlParser)3 FileOutputStream (java.io.FileOutputStream)2 XmlParser (org.hl7.fhir.dstu3.formats.XmlParser)2 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)2 XmlParser (org.hl7.fhir.r4.formats.XmlParser)2 NarrativeGenerator (org.hl7.fhir.r4.utils.NarrativeGenerator)2 Test (org.junit.jupiter.api.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 NotImplementedException (org.apache.commons.lang3.NotImplementedException)1 ElementDefinition (org.hl7.fhir.dstu2.model.ElementDefinition)1 Questionnaire (org.hl7.fhir.dstu2.model.Questionnaire)1