Search in sources :

Example 26 with XhtmlParser

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 &#39;vital-signs&#39; = &#39;Vital Signs&#39;, given as &#39;Vital Signs&#39;})</span></p><p><b>code</b>: Body Weight <span>(Details : {LOINC code &#39;29463-7&#39; = &#39;Body weight&#39;, given as &#39;Body Weight&#39;}; {LOINC code &#39;3141-9&#39; = &#39;Body weight Measured&#39;, given as &#39;Body weight Measured&#39;}; {SNOMED CT code &#39;27113001&#39; = &#39;Body weight&#39;, given as &#39;Body weight&#39;}; {http://acme.org/devices/clinical-codes code &#39;body-weight&#39; = &#39;body-weight&#39;, given as &#39;Body Weight&#39;})</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] = &#39;lb_av&#39;)</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]");
}
Also used : DateTimeType(org.hl7.fhir.r5.model.DateTimeType) XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) Narrative(org.hl7.fhir.r5.model.Narrative) Coding(org.hl7.fhir.r5.model.Coding) Reference(org.hl7.fhir.r5.model.Reference) Observation(org.hl7.fhir.r5.model.Observation) Quantity(org.hl7.fhir.r5.model.Quantity) CodeableConcept(org.hl7.fhir.r5.model.CodeableConcept)

Example 27 with XhtmlParser

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());
    }
}
Also used : StructureDefinition(org.hl7.fhir.dstu2.model.StructureDefinition) XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) MarkDownProcessor(org.hl7.fhir.utilities.MarkDownProcessor) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 28 with XhtmlParser

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();
    }
}
Also used : XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) SpecialElement(org.hl7.fhir.dstu2016may.metamodel.Element.SpecialElement) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) Node(org.w3c.dom.Node) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer)

Example 29 with XhtmlParser

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();
    }
}
Also used : XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) SpecialElement(org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) Node(org.w3c.dom.Node) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer)

Example 30 with XhtmlParser

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;
}
Also used : XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) FHIRException(org.hl7.fhir.exceptions.FHIRException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

XhtmlParser (org.hl7.fhir.utilities.xhtml.XhtmlParser)40 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)25 FHIRException (org.hl7.fhir.exceptions.FHIRException)19 IOException (java.io.IOException)14 XhtmlComposer (org.hl7.fhir.utilities.xhtml.XhtmlComposer)11 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)10 FileNotFoundException (java.io.FileNotFoundException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)7 XhtmlDocument (org.hl7.fhir.utilities.xhtml.XhtmlDocument)6 TransformerException (javax.xml.transform.TransformerException)5 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)5 CDANarrativeFormat (org.hl7.fhir.utilities.xhtml.CDANarrativeFormat)5 Node (org.w3c.dom.Node)5 NotImplementedException (org.apache.commons.lang3.NotImplementedException)4 MarkDownProcessor (org.hl7.fhir.utilities.MarkDownProcessor)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3