Search in sources :

Example 36 with XhtmlParser

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

the class NarrativeGenerator method generate.

public boolean generate(ResourceContext rcontext, CompartmentDefinition cpd) {
    StringBuilder in = new StringBuilder();
    StringBuilder out = new StringBuilder();
    for (CompartmentDefinitionResourceComponent cc : cpd.getResource()) {
        CommaSeparatedStringBuilder rules = new CommaSeparatedStringBuilder();
        if (!cc.hasParam()) {
            out.append(" <li><a href=\"").append(cc.getCode().toLowerCase()).append(".html\">").append(cc.getCode()).append("</a></li>\r\n");
        } else if (!rules.equals("{def}")) {
            for (StringType p : cc.getParam()) rules.append(p.asStringValue());
            in.append(" <tr><td><a href=\"").append(cc.getCode().toLowerCase()).append(".html\">").append(cc.getCode()).append("</a></td><td>").append(rules.toString()).append("</td></tr>\r\n");
        }
    }
    XhtmlNode x;
    try {
        x = new XhtmlParser().parseFragment("<div><p>\r\nThe following resources may be in this compartment:\r\n</p>\r\n" + "<table class=\"grid\">\r\n" + " <tr><td><b>Resource</b></td><td><b>Inclusion Criteria</b></td></tr>\r\n" + in.toString() + "</table>\r\n" + "<p>\r\nA resource is in this compartment if the nominated search parameter (or chain) refers to the patient resource that defines the compartment.\r\n</p>\r\n" + "<p>\r\n\r\n</p>\r\n" + "<p>\r\nThe following resources are never in this compartment:\r\n</p>\r\n" + "<ul>\r\n" + out.toString() + "</ul></div>\r\n");
        inject(cpd, x, NarrativeStatus.GENERATED);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) CompartmentDefinitionResourceComponent(org.hl7.fhir.dstu3.model.CompartmentDefinition.CompartmentDefinitionResourceComponent) StringType(org.hl7.fhir.dstu3.model.StringType) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 37 with XhtmlParser

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

the class NarrativeGenerator method generate.

public boolean generate(ResourceContext rcontext, CompartmentDefinition cpd) {
    StringBuilder in = new StringBuilder();
    StringBuilder out = new StringBuilder();
    for (CompartmentDefinitionResourceComponent cc : cpd.getResource()) {
        CommaSeparatedStringBuilder rules = new CommaSeparatedStringBuilder();
        if (!cc.hasParam()) {
            out.append(" <li><a href=\"").append(cc.getCode().toLowerCase()).append(".html\">").append(cc.getCode()).append("</a></li>\r\n");
        } else if (!rules.equals("{def}")) {
            for (StringType p : cc.getParam()) rules.append(p.asStringValue());
            in.append(" <tr><td><a href=\"").append(cc.getCode().toLowerCase()).append(".html\">").append(cc.getCode()).append("</a></td><td>").append(rules.toString()).append("</td></tr>\r\n");
        }
    }
    XhtmlNode x;
    try {
        x = new XhtmlParser().parseFragment("<div><p>\r\nThe following resources may be in this compartment:\r\n</p>\r\n" + "<table class=\"grid\">\r\n" + " <tr><td><b>Resource</b></td><td><b>Inclusion Criteria</b></td></tr>\r\n" + in.toString() + "</table>\r\n" + "<p>\r\nA resource is in this compartment if the nominated search parameter (or chain) refers to the patient resource that defines the compartment.\r\n</p>\r\n" + "<p>\r\n\r\n</p>\r\n" + "<p>\r\nThe following resources are never in this compartment:\r\n</p>\r\n" + "<ul>\r\n" + out.toString() + "</ul></div>\r\n");
        inject(cpd, x, NarrativeStatus.GENERATED);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) CompartmentDefinitionResourceComponent(org.hl7.fhir.r4.model.CompartmentDefinition.CompartmentDefinitionResourceComponent) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) ParseException(java.text.ParseException) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 38 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 = markdown.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 : 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 39 with XhtmlParser

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

the class XmlParser method composeElement.

private void composeElement(IXMLWriter xml, Element element, String elementName, boolean root) throws IOException, FHIRException {
    if (showDecorations) {
        @SuppressWarnings("unchecked") List<ElementDecoration> decorations = (List<ElementDecoration>) element.getUserData("fhir.decorations");
        if (decorations != null)
            for (ElementDecoration d : decorations) xml.decorate(d);
    }
    for (String s : element.getComments()) {
        xml.comment(s, true);
    }
    if (isText(element.getProperty())) {
        if (linkResolver != null)
            xml.link(linkResolver.resolveProperty(element.getProperty()));
        xml.enter(element.getProperty().getXmlNamespace(), elementName);
        xml.text(element.getValue());
        xml.exit(element.getProperty().getXmlNamespace(), elementName);
    } else if (!element.hasChildren() && !element.hasValue()) {
        if (element.getExplicitType() != null)
            xml.attribute("xsi:type", element.getExplicitType());
        xml.element(elementName);
    } else if (element.isPrimitive() || (element.hasType() && isPrimitive(element.getType()))) {
        if (element.getType().equals("xhtml")) {
            String rawXhtml = element.getValue();
            if (isCdaText(element.getProperty())) {
                new CDANarrativeFormat().convert(xml, new XhtmlParser().parseFragment(rawXhtml));
            } else {
                xml.escapedText(rawXhtml);
                xml.anchor("end-xhtml");
            }
        } else if (isText(element.getProperty())) {
            if (linkResolver != null)
                xml.link(linkResolver.resolveProperty(element.getProperty()));
            xml.text(element.getValue());
        } else {
            setXsiTypeIfIsTypeAttr(xml, element);
            if (element.hasValue()) {
                if (linkResolver != null)
                    xml.link(linkResolver.resolveType(element.getType()));
                xml.attribute("value", element.getValue());
            }
            if (linkResolver != null)
                xml.link(linkResolver.resolveProperty(element.getProperty()));
            if (element.hasChildren()) {
                xml.enter(element.getProperty().getXmlNamespace(), elementName);
                for (Element child : element.getChildren()) composeElement(xml, child, child.getName(), false);
                xml.exit(element.getProperty().getXmlNamespace(), elementName);
            } else
                xml.element(elementName);
        }
    } else {
        setXsiTypeIfIsTypeAttr(xml, element);
        for (Element child : element.getChildren()) {
            if (isAttr(child.getProperty())) {
                if (linkResolver != null)
                    xml.link(linkResolver.resolveType(child.getType()));
                String av = child.getValue();
                if (ToolingExtensions.hasExtension(child.getProperty().getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat"))
                    av = convertForDateFormatToExternal(ToolingExtensions.readStringExtension(child.getProperty().getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat"), av);
                xml.attribute(child.getProperty().getXmlNamespace(), child.getProperty().getXmlName(), av);
            }
        }
        if (linkResolver != null)
            xml.link(linkResolver.resolveProperty(element.getProperty()));
        xml.enter(element.getProperty().getXmlNamespace(), elementName);
        if (!root && element.getSpecial() != null) {
            if (linkResolver != null)
                xml.link(linkResolver.resolveProperty(element.getProperty()));
            xml.enter(element.getProperty().getXmlNamespace(), element.getType());
        }
        for (Element child : element.getChildren()) {
            if (isText(child.getProperty())) {
                if (linkResolver != null)
                    xml.link(linkResolver.resolveProperty(element.getProperty()));
                xml.text(child.getValue());
            } else if (!isAttr(child.getProperty()))
                composeElement(xml, child, child.getName(), false);
        }
        if (!root && element.getSpecial() != null)
            xml.exit(element.getProperty().getXmlNamespace(), element.getType());
        xml.exit(element.getProperty().getXmlNamespace(), elementName);
    }
}
Also used : XhtmlParser(org.hl7.fhir.utilities.xhtml.XhtmlParser) ElementDecoration(org.hl7.fhir.utilities.ElementDecoration) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) ArrayList(java.util.ArrayList) List(java.util.List) CDANarrativeFormat(org.hl7.fhir.utilities.xhtml.CDANarrativeFormat)

Example 40 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.dstu2016may.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