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;
}
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);
}
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);
}
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");
}
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());
}
}
Aggregations