Search in sources :

Example 31 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 element) throws FHIRFormatError, FHIRException, IOException, DefinitionException {
    // this parsing routine retains the original order in a the XML file, to support validation
    reapComments(node, element);
    List<Property> properties = element.getProperty().getChildProperties(element.getName(), XMLUtil.getXsiType(node));
    String text = XMLUtil.getDirectText(node).trim();
    int line = line(node);
    int col = col(node);
    if (!Utilities.noString(text)) {
        Property property = getTextProp(properties);
        if (property != null) {
            if ("ED.data[x]".equals(property.getDefinition().getId()) || (property.getDefinition() != null && property.getDefinition().getBase() != null && "ED.data[x]".equals(property.getDefinition().getBase().getPath()))) {
                if ("B64".equals(node.getAttribute("representation"))) {
                    Element n = new Element("dataBase64Binary", property, "base64Binary", text).markLocation(line, col);
                    n.setPath(element.getPath() + "." + property.getName());
                    element.getChildren().add(n);
                } else {
                    Element n = new Element("dataString", property, "string", text).markLocation(line, col);
                    n.setPath(element.getPath() + "." + property.getName());
                    element.getChildren().add(n);
                }
            } else {
                Element n = new Element(property.getName(), property, property.getType(), text).markLocation(line, col);
                n.setPath(element.getPath() + "." + property.getName());
                element.getChildren().add(n);
            }
        } else {
            Node n = node.getFirstChild();
            while (n != null) {
                if (n.getNodeType() == Node.TEXT_NODE && !Utilities.noString(n.getTextContent().trim())) {
                    while (n.getNextSibling() != null && n.getNodeType() != Node.ELEMENT_NODE) {
                        n = n.getNextSibling();
                    }
                    while (n.getPreviousSibling() != null && n.getNodeType() != Node.ELEMENT_NODE) {
                        n = n.getPreviousSibling();
                    }
                    line = line(n);
                    col = col(n);
                    logError(line, col, path, IssueType.STRUCTURE, context.formatMessage(I18nConstants.TEXT_SHOULD_NOT_BE_PRESENT, text), IssueSeverity.ERROR);
                }
                n = n.getNextSibling();
            }
        }
    }
    for (int i = 0; i < node.getAttributes().getLength(); i++) {
        Node attr = node.getAttributes().item(i);
        String value = attr.getNodeValue();
        if (!validAttrValue(value)) {
            logError(line, col, path, IssueType.STRUCTURE, context.formatMessage(I18nConstants.XML_ATTR_VALUE_INVALID, attr.getNodeName()), IssueSeverity.ERROR);
        }
        if (!(attr.getNodeName().equals("xmlns") || attr.getNodeName().startsWith("xmlns:"))) {
            Property property = getAttrProp(properties, attr.getLocalName(), attr.getNamespaceURI());
            if (property != null) {
                String av = attr.getNodeValue();
                if (ToolingExtensions.hasExtension(property.getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat"))
                    av = convertForDateFormatFromExternal(ToolingExtensions.readStringExtension(property.getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat"), av);
                if (property.getName().equals("value") && element.isPrimitive())
                    element.setValue(av);
                else {
                    Element n = new Element(property.getName(), property, property.getType(), av).markLocation(line, col);
                    n.setPath(element.getPath() + "." + property.getName());
                    element.getChildren().add(n);
                }
            } else {
                boolean ok = false;
                if (FormatUtilities.FHIR_NS.equals(node.getNamespaceURI())) {
                    if (attr.getLocalName().equals("schemaLocation") && FormatUtilities.NS_XSI.equals(attr.getNamespaceURI())) {
                        ok = ok || allowXsiLocation;
                    }
                } else
                    // xsi:schemalocation allowed for non FHIR content
                    ok = ok || (attr.getLocalName().equals("schemaLocation"));
                // xsi:type allowed if element says so
                ok = ok || (hasTypeAttr(element) && attr.getLocalName().equals("type") && FormatUtilities.NS_XSI.equals(attr.getNamespaceURI()));
                if (!ok)
                    logError(line, col, path, IssueType.STRUCTURE, context.formatMessage(I18nConstants.UNDEFINED_ATTRIBUTE__ON__FOR_TYPE__PROPERTIES__, attr.getNodeName(), node.getNodeName(), element.fhirType(), properties), IssueSeverity.ERROR);
            }
        }
    }
    String lastName = null;
    int repeatCount = 0;
    Node child = node.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Property property = getElementProp(properties, child.getLocalName(), child.getNamespaceURI());
            if (property != null) {
                if (property.getName().equals(lastName)) {
                    repeatCount++;
                } else {
                    lastName = property.getName();
                    repeatCount = 0;
                }
                if (!property.isChoice() && "xhtml".equals(property.getType())) {
                    XhtmlNode xhtml;
                    if (property.getDefinition().hasRepresentation(PropertyRepresentation.CDATEXT))
                        xhtml = new CDANarrativeFormat().convert((org.w3c.dom.Element) child);
                    else
                        xhtml = new XhtmlParser().setValidatorMode(true).parseHtmlNode((org.w3c.dom.Element) child);
                    Element n = new Element(property.getName(), property, "xhtml", new XhtmlComposer(XhtmlComposer.XML, false).compose(xhtml)).setXhtml(xhtml).markLocation(line(child), col(child));
                    n.setPath(element.getPath() + "." + property.getName());
                    element.getChildren().add(n);
                } else {
                    String npath = path + "/" + pathPrefix(child.getNamespaceURI()) + child.getLocalName();
                    Element n = new Element(child.getLocalName(), property).markLocation(line(child), col(child));
                    if (property.isList()) {
                        n.setPath(element.getPath() + "." + property.getName() + "[" + repeatCount + "]");
                    } else {
                        n.setPath(element.getPath() + "." + property.getName());
                    }
                    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 (Utilities.noString(xsiType)) {
                                if (ToolingExtensions.hasExtension(property.getDefinition(), "http://hl7.org/fhir/StructureDefinition/elementdefinition-defaulttype")) {
                                    xsiType = ToolingExtensions.readStringExtension(property.getDefinition(), "http://hl7.org/fhir/StructureDefinition/elementdefinition-defaulttype");
                                    n.setType(xsiType);
                                } else {
                                    logError(line(child), col(child), path, IssueType.STRUCTURE, context.formatMessage(I18nConstants.NO_TYPE_FOUND_ON_, child.getLocalName()), IssueSeverity.ERROR);
                                    ok = false;
                                }
                            } else {
                                if (xsiType.contains(":"))
                                    xsiType = xsiType.substring(xsiType.indexOf(":") + 1);
                                n.setType(xsiType);
                                n.setExplicitType(xsiType);
                            }
                        } else
                            n.setType(n.getType());
                    }
                    element.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, context.formatMessage(I18nConstants.UNDEFINED_ELEMENT_, child.getLocalName()), IssueSeverity.ERROR);
        } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
            logError(line(child), col(child), path, IssueType.STRUCTURE, context.formatMessage(I18nConstants.CDATA_IS_NOT_ALLOWED), IssueSeverity.ERROR);
        } else if (!Utilities.existsInList(child.getNodeType(), 3, 8)) {
            logError(line(child), col(child), path, IssueType.STRUCTURE, context.formatMessage(I18nConstants.NODE_TYPE__IS_NOT_ALLOWED, Integer.toString(child.getNodeType())), IssueSeverity.ERROR);
        }
        child = child.getNextSibling();
    }
}
Also used : XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) NamedElement(org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement) SpecialElement(org.hl7.fhir.r4b.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) CDANarrativeFormat(org.hl7.fhir.utilities.xhtml.CDANarrativeFormat)

Example 32 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)

Example 33 with XhtmlParser

use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project org.hl7.fhir.core by hapifhir.

the class LiquidRenderer method render.

@Override
public boolean render(XhtmlNode x, ResourceWrapper r) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
    LiquidEngine engine = new LiquidEngine(context.getWorker(), context.getServices());
    XhtmlNode xn;
    try {
        LiquidDocument doc = engine.parse(liquidTemplate, "template");
        String html = engine.evaluate(doc, r.getBase(), rcontext);
        xn = new XhtmlParser().parseFragment(html);
        if (!x.getName().equals("div"))
            throw new FHIRException("Error in template: Root element is not 'div'");
    } catch (FHIRException | IOException e) {
        xn = new XhtmlNode(NodeType.Element, "div");
        xn.para().b().style("color: maroon").tx("Exception generating Narrative: " + e.getMessage());
    }
    x.getChildNodes().addAll(xn.getChildNodes());
    return true;
}
Also used : LiquidDocument(org.hl7.fhir.r5.utils.LiquidEngine.LiquidDocument) XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) IOException(java.io.IOException) LiquidEngine(org.hl7.fhir.r5.utils.LiquidEngine) FHIRException(org.hl7.fhir.exceptions.FHIRException) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 34 with XhtmlParser

use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project org.hl7.fhir.core by hapifhir.

the class XhtmlNode method markdown.

public void markdown(String md, String source) throws IOException {
    if (md != null) {
        String s = new MarkDownProcessor(Dialect.COMMON_MARK).process(md, source);
        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);
        }
        getChildNodes().addAll(m.getChildNodes());
    }
}
Also used : FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) MarkDownProcessor(org.hl7.fhir.utilities.MarkDownProcessor)

Example 35 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;
        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

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