Search in sources :

Example 16 with XhtmlParser

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

the class DataRenderer method addMarkdown.

// -- 2. Markdown support -------------------------------------------------------
protected 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 = getContext().getWorker().fetchResource(StructureDefinition.class, parts[0]);
            if (p == null)
                p = getContext().getWorker().fetchTypeDefinition(parts[0]);
            if (p == null)
                p = getContext().getWorker().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 = getContext().getMarkdown().process(Utilities.escapeXml(text), "narrative generator");
        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.r5.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) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 17 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, Resource r) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
    LiquidEngine engine = new LiquidEngine(context.getWorker(), context.getServices());
    XhtmlNode xn;
    try {
        engine.setIncludeResolver(new LiquidRendererIncludeResolver(context));
        LiquidDocument doc = engine.parse(liquidTemplate, "template");
        String html = engine.evaluate(doc, r, 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 18 with XhtmlParser

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

the class XhtmlNode method innerHTML.

public void innerHTML(String html) throws IOException {
    if (html != null) {
        XhtmlParser p = new XhtmlParser();
        XhtmlNode m;
        try {
            m = p.parse("<div>" + html + "</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)

Example 19 with XhtmlParser

use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project kindling by HL7.

the class PageProcessor method checkFormat.

private void checkFormat(String filename, String res, ResourceDefn r) throws FHIRException {
    XhtmlNode doc;
    try {
        doc = new XhtmlParser().parse("<div>" + res + "</div>", null).getFirstElement();
        if (doc.getFirstElement() == null || !doc.getFirstElement().getName().equals("div"))
            log("file \"" + filename + "\": root element should be 'div'", LogMessageType.Error);
        else if (doc.getFirstElement() == null) {
            log("file \"" + filename + "\": there is no 'Scope and Usage'", LogMessageType.Error);
        } else {
            XhtmlNode scope = null;
            XhtmlNode context = null;
            for (XhtmlNode x : doc.getChildNodes()) {
                if (x.getNodeType() == NodeType.Element) {
                    if (!x.getName().equals("div")) {
                        log("file \"" + filename + "\": all child elements of the root div should be 'div's too (found '" + x.getName() + "')", LogMessageType.Error);
                        return;
                    } else if (x.getChildNodes().isEmpty()) {
                        log("file \"" + filename + "\": div/div[" + Integer.toString(doc.getChildNodes().indexOf(x)) + "] must have at least an h2", LogMessageType.Error);
                        return;
                    } else if (!isFirstChildElementH2(x)) {
                        log("file \"" + filename + "\": div/div[" + Integer.toString(doc.getChildNodes().indexOf(x)) + "] must start with an h2", LogMessageType.Error);
                        return;
                    } else {
                        XhtmlNode fn = getH2Element(x);
                        String s = fn.allText();
                        if (!((s.equals("Scope and Usage")) || (s.equals("Boundaries and Relationships")) || (s.equals("Background and Context")))) {
                            log("file \"" + filename + "\": div/div[" + Integer.toString(doc.getChildNodes().indexOf(x)) + "]/h2 must be either 'Scope and Usage', 'Boundaries and Relationships', or 'Background and Context'", LogMessageType.Error);
                            return;
                        } else {
                            if (scope == null) {
                                if (s.equals("Scope and Usage")) {
                                    scope = x;
                                    if (r != null)
                                        r.setRequirements(new XhtmlComposer(XhtmlComposer.HTML).composePlainText(x));
                                } else {
                                    log("file \"" + filename + "\": 'Scope and Usage' must come first", LogMessageType.Error);
                                    return;
                                }
                                if (s.equals("Boundaries and Relationships")) {
                                    if (context != null) {
                                        log("file \"" + filename + "\": 'Boundaries and Relationships' must come first before 'Background and Context'", LogMessageType.Error);
                                        return;
                                    }
                                }
                                if (s.equals("Background and Context"))
                                    context = x;
                            }
                        }
                        boolean found = false;
                        for (XhtmlNode n : x.getChildNodes()) {
                            if (!found)
                                found = n == fn;
                            else {
                                if ("h1".equals(n.getName()) || "h2".equals(n.getName())) {
                                    log("file \"" + filename + "\": content of a <div> inner section cannot contain h1 or h2 headings", LogMessageType.Error);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
        List<String> allowed = Arrays.asList("div", "h2", "h3", "h4", "h5", "i", "b", "code", "pre", "blockquote", "p", "a", "img", "table", "thead", "tbody", "tr", "th", "td", "ol", "ul", "li", "br", "span", "em", "strong");
        iterateAllChildNodes(doc, allowed);
    } catch (Exception e) {
        throw new FHIRException("Error processing " + filename + ": " + e.getMessage(), e);
    }
}
Also used : XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) FHIRException(org.hl7.fhir.exceptions.FHIRException) UcumException(org.fhir.ucum.UcumException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) URISyntaxException(java.net.URISyntaxException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FileNotFoundException(java.io.FileNotFoundException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 20 with XhtmlParser

use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project kindling by HL7.

the class SourceParser method loadMappingSpaces.

private void loadMappingSpaces() throws Exception {
    FileInputStream is = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        is = new FileInputStream(Utilities.path(srcDir, "mappingSpaces.xml"));
        Document doc = builder.parse(is);
        Element e = XMLUtil.getFirstChild(doc.getDocumentElement());
        while (e != null) {
            MappingSpace m = new MappingSpace(XMLUtil.getNamedChild(e, "columnName").getTextContent(), XMLUtil.getNamedChild(e, "title").getTextContent(), XMLUtil.getNamedChild(e, "id").getTextContent(), Integer.parseInt(XMLUtil.getNamedChild(e, "sort").getTextContent()), isTrue(XMLUtil.getNamedChild(e, "publish")), isTrue(XMLUtil.getNamedChild(e, "sparse")), isTrue(XMLUtil.getNamedChild(e, "pattern")), XMLUtil.getNamedChild(e, "link") != null ? XMLUtil.getNamedChild(e, "link").getTextContent() : XMLUtil.getNamedChild(e, "url").getTextContent());
            definitions.getMapTypes().put(XMLUtil.getNamedChild(e, "url").getTextContent(), m);
            Element p = XMLUtil.getNamedChild(e, "preamble");
            if (p != null)
                m.setPreamble(new XhtmlParser().parseHtmlNode(p).setName("div"));
            e = XMLUtil.getNextSibling(e);
        }
    } catch (Exception e) {
        throw new Exception("Error processing mappingSpaces.xml: " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}
Also used : MappingSpace(org.hl7.fhir.definitions.model.MappingSpace) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

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