use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project org.hl7.fhir.core by hapifhir.
the class GeneratorTestFragments method test.
private void test() throws FHIRFormatError, IOException {
Observation res = new Observation();
res.setId("example");
Narrative n = new Narrative();
res.setText(n);
n.setStatus(NarrativeStatus.GENERATED);
n.setDiv(new XhtmlParser().parse("<div xmlns=\"http://www.w3.org/1999/xhtml\"><p><b>Generated Narrative with Details</b></p><p><b>id</b>: example</p><p><b>status</b>: final</p><p><b>category</b>: Vital Signs <span>(Details : {http://hl7.org/fhir/observation-category code 'vital-signs' = 'Vital Signs', given as 'Vital Signs'})</span></p><p><b>code</b>: Body Weight <span>(Details : {LOINC code '29463-7' = 'Body weight', given as 'Body Weight'}; {LOINC code '3141-9' = 'Body weight Measured', given as 'Body weight Measured'}; {SNOMED CT code '27113001' = 'Body weight', given as 'Body weight'}; {http://acme.org/devices/clinical-codes code 'body-weight' = 'body-weight', given as 'Body Weight'})</span></p><p><b>subject</b>: <a>Patient/example</a></p><p><b>context</b>: <a>Encounter/example</a></p><p><b>effective</b>: 28/03/2016</p><p><b>value</b>: 185 lbs<span> (Details: UCUM code [lb_av] = 'lb_av')</span></p></div>", "div"));
res.setStatus(ObservationStatus.FINAL);
CodeableConcept cc = res.addCategory();
Coding c = cc.addCoding();
c.setSystem("http://hl7.org/fhir/observation-category");
c.setCode("vital-signs");
c.setDisplay("Vital Signs");
cc = new CodeableConcept();
res.setCode(cc);
c = cc.addCoding();
c.setSystem("http://loinc.org");
c.setCode("29463-7");
c.setDisplay("Body Weight");
c = cc.addCoding();
c.setSystem("http://loinc.org");
c.setCode("3141-9");
c.setDisplay("Body weight Measured");
c = cc.addCoding();
c.setSystem("http://snomed.info/sct");
c.setCode("27113001");
c.setDisplay("Body weight");
c = cc.addCoding();
c.setSystem("http://acme.org/devices/clinical-codes");
c.setCode("body-weight");
c.setDisplay("Body Weight");
Reference r = new Reference();
res.setSubject(r);
r.setReference("Patient/example");
r = new Reference();
res.setEncounter(r);
r.setReference("Encounter/example");
res.setEffective(new DateTimeType("2016-03-28"));
Quantity q = new Quantity();
res.setValue(q);
q.setValue(185);
q.setUnit("lbs");
q.setSystem("http://unitsofmeasure.org");
q.setCode("[lb_av]");
}
use of org.hl7.fhir.utilities.xhtml.XhtmlParser 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 = p.parse("<div>" + s + "</div>", "div");
x.getChildNodes().addAll(m.getChildNodes());
}
}
use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project org.hl7.fhir.core by hapifhir.
the class XmlParser method parseChildren.
private void parseChildren(String path, org.w3c.dom.Element node, Element context) throws Exception {
// this parsing routine retains the original order in a the XML file, to support validation
reapComments(node, context);
List<Property> properties = getChildProperties(context.getProperty(), context.getName(), XMLUtil.getXsiType(node));
String text = XMLUtil.getDirectText(node).trim();
if (!Utilities.noString(text)) {
Property property = getTextProp(properties);
if (property != null) {
context.getChildren().add(new Element(property.getName(), property, property.getType(), text).markLocation(line(node), col(node)));
} else {
logError(line(node), col(node), path, IssueType.STRUCTURE, "Text should not be present", IssueSeverity.ERROR);
}
}
for (int i = 0; i < node.getAttributes().getLength(); i++) {
Node attr = node.getAttributes().item(i);
if (!(attr.getNodeName().equals("xmlns") || attr.getNodeName().startsWith("xmlns:"))) {
Property property = getAttrProp(properties, attr.getNodeName());
if (property != null) {
String av = attr.getNodeValue();
if (ToolingExtensions.hasExtension(property.getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat"))
av = convertForDateFormat(ToolingExtensions.readStringExtension(property.getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat"), av);
if (property.getName().equals("value") && context.isPrimitive())
context.setValue(av);
else
context.getChildren().add(new Element(property.getName(), property, property.getType(), av).markLocation(line(node), col(node)));
} else {
logError(line(node), col(node), path, IssueType.STRUCTURE, "Undefined attribute '@" + attr.getNodeName() + "'", IssueSeverity.ERROR);
}
}
}
Node child = node.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.ELEMENT_NODE) {
Property property = getElementProp(properties, child.getLocalName());
if (property != null) {
if (!property.isChoice() && "xhtml".equals(property.getType())) {
XhtmlNode xhtml = new XhtmlParser().setValidatorMode(true).parseHtmlNode((org.w3c.dom.Element) child);
context.getChildren().add(new Element("div", property, "xhtml", new XhtmlComposer(true, false).compose(xhtml)).setXhtml(xhtml).markLocation(line(child), col(child)));
} else {
String npath = path + "/" + pathPrefix(child.getNamespaceURI()) + child.getLocalName();
Element n = new Element(child.getLocalName(), property).markLocation(line(child), col(child));
checkElement((org.w3c.dom.Element) child, npath, n.getProperty());
boolean ok = true;
if (property.isChoice()) {
if (property.getDefinition().hasRepresentation(PropertyRepresentation.TYPEATTR)) {
String xsiType = ((org.w3c.dom.Element) child).getAttributeNS(FormatUtilities.NS_XSI, "type");
if (xsiType == null) {
logError(line(child), col(child), path, IssueType.STRUCTURE, "No type found on '" + child.getLocalName() + '"', IssueSeverity.ERROR);
ok = false;
} else {
if (xsiType.contains(":"))
xsiType = xsiType.substring(xsiType.indexOf(":") + 1);
n.setType(xsiType);
}
} else
n.setType(n.getType());
}
context.getChildren().add(n);
if (ok) {
if (property.isResource())
parseResource(npath, (org.w3c.dom.Element) child, n);
else
parseChildren(npath, (org.w3c.dom.Element) child, n);
}
}
} else
logError(line(child), col(child), path, IssueType.STRUCTURE, "Undefined element '" + child.getLocalName() + "'", IssueSeverity.ERROR);
} else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
logError(line(child), col(child), path, IssueType.STRUCTURE, "CDATA is not allowed", IssueSeverity.ERROR);
} else if (!Utilities.existsInList(child.getNodeType(), 3, 8)) {
logError(line(child), col(child), path, IssueType.STRUCTURE, "Node type " + Integer.toString(child.getNodeType()) + " is not allowed", IssueSeverity.ERROR);
}
child = child.getNextSibling();
}
}
use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project org.hl7.fhir.core by hapifhir.
the class XmlParser method parseChildren.
private void parseChildren(String path, org.w3c.dom.Element node, Element context) throws FHIRFormatError, FHIRException, IOException, DefinitionException {
// this parsing routine retains the original order in a the XML file, to support validation
reapComments(node, context);
List<Property> properties = context.getProperty().getChildProperties(context.getName(), XMLUtil.getXsiType(node));
String text = XMLUtil.getDirectText(node).trim();
if (!Utilities.noString(text)) {
Property property = getTextProp(properties);
if (property != null) {
context.getChildren().add(new Element(property.getName(), property, property.getType(), text).markLocation(line(node), col(node)));
} else {
logError(line(node), col(node), path, IssueType.STRUCTURE, "Text should not be present", IssueSeverity.ERROR);
}
}
for (int i = 0; i < node.getAttributes().getLength(); i++) {
Node attr = node.getAttributes().item(i);
if (!(attr.getNodeName().equals("xmlns") || attr.getNodeName().startsWith("xmlns:"))) {
Property property = getAttrProp(properties, attr.getNodeName());
if (property != null) {
String av = attr.getNodeValue();
if (ToolingExtensions.hasExtension(property.getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat"))
av = convertForDateFormat(ToolingExtensions.readStringExtension(property.getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat"), av);
if (property.getName().equals("value") && context.isPrimitive())
context.setValue(av);
else
context.getChildren().add(new Element(property.getName(), property, property.getType(), av).markLocation(line(node), col(node)));
} else if (!allowXsiLocation || !attr.getNodeName().endsWith(":schemaLocation")) {
logError(line(node), col(node), path, IssueType.STRUCTURE, "Undefined attribute '@" + attr.getNodeName() + "' on " + node.getNodeName(), IssueSeverity.ERROR);
}
}
}
Node child = node.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.ELEMENT_NODE) {
Property property = getElementProp(properties, child.getLocalName());
if (property != null) {
if (!property.isChoice() && "xhtml".equals(property.getType())) {
XhtmlNode xhtml = new XhtmlParser().setValidatorMode(true).parseHtmlNode((org.w3c.dom.Element) child);
context.getChildren().add(new Element("div", property, "xhtml", new XhtmlComposer(XhtmlComposer.XML).compose(xhtml)).setXhtml(xhtml).markLocation(line(child), col(child)));
} else {
String npath = path + "/" + pathPrefix(child.getNamespaceURI()) + child.getLocalName();
Element n = new Element(child.getLocalName(), property).markLocation(line(child), col(child));
checkElement((org.w3c.dom.Element) child, npath, n.getProperty());
boolean ok = true;
if (property.isChoice()) {
if (property.getDefinition().hasRepresentation(PropertyRepresentation.TYPEATTR)) {
String xsiType = ((org.w3c.dom.Element) child).getAttributeNS(FormatUtilities.NS_XSI, "type");
if (xsiType == null) {
logError(line(child), col(child), path, IssueType.STRUCTURE, "No type found on '" + child.getLocalName() + '"', IssueSeverity.ERROR);
ok = false;
} else {
if (xsiType.contains(":"))
xsiType = xsiType.substring(xsiType.indexOf(":") + 1);
n.setType(xsiType);
}
} else
n.setType(n.getType());
}
context.getChildren().add(n);
if (ok) {
if (property.isResource())
parseResource(npath, (org.w3c.dom.Element) child, n, property);
else
parseChildren(npath, (org.w3c.dom.Element) child, n);
}
}
} else
logError(line(child), col(child), path, IssueType.STRUCTURE, "Undefined element '" + child.getLocalName() + "'", IssueSeverity.ERROR);
} else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
logError(line(child), col(child), path, IssueType.STRUCTURE, "CDATA is not allowed", IssueSeverity.ERROR);
} else if (!Utilities.existsInList(child.getNodeType(), 3, 8)) {
logError(line(child), col(child), path, IssueType.STRUCTURE, "Node type " + Integer.toString(child.getNodeType()) + " is not allowed", IssueSeverity.ERROR);
}
child = child.getNextSibling();
}
}
use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project org.hl7.fhir.core by hapifhir.
the class Factory method newNarrative.
public static Narrative newNarrative(NarrativeStatus status, String html) throws IOException, FHIRException {
Narrative n = new Narrative();
n.setStatus(status);
try {
n.setDiv(new XhtmlParser().parseFragment("<div>" + Utilities.escapeXml(html) + "</div>"));
} catch (org.hl7.fhir.exceptions.FHIRException e) {
throw new FHIRException(e.getMessage(), e);
}
return n;
}
Aggregations