Search in sources :

Example 46 with XhtmlComposer

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

the class NarrativeGenerator method generateByProfile.

private String generateByProfile(Element er, StructureDefinition profile, boolean showCodeDetails) throws IOException, org.hl7.fhir.exceptions.FHIRException {
    XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
    x.para().b().tx("Generated Narrative" + (showCodeDetails ? " with Details" : ""));
    try {
        generateByProfile(er, profile, er, profile.getSnapshot().getElement(), profile.getSnapshot().getElement().get(0), getChildrenForPath(profile.getSnapshot().getElement(), er.getLocalName()), x, er.getLocalName(), showCodeDetails);
    } catch (Exception e) {
        e.printStackTrace();
        x.para().b().setAttribute("style", "color: maroon").tx("Exception generating Narrative: " + e.getMessage());
    }
    inject(er, x, NarrativeStatus.GENERATED);
    String b = new XhtmlComposer(XhtmlComposer.XML, pretty).compose(x);
    return b;
}
Also used : XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) 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 47 with XhtmlComposer

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

the class ValueSetExpansionCache method loadCache.

private void loadCache() throws FHIRFormatError, IOException {
    File[] files = new File(cacheFolder).listFiles();
    for (File f : files) {
        if (f.getName().endsWith(".xml")) {
            final FileInputStream is = new FileInputStream(f);
            try {
                Resource r = context.newXmlParser().setOutputStyle(OutputStyle.PRETTY).parse(is);
                if (r instanceof OperationOutcome) {
                    OperationOutcome oo = (OperationOutcome) r;
                    expansions.put(ToolingExtensions.getExtension(oo, VS_ID_EXT).getValue().toString(), new ValueSetExpansionOutcome(new XhtmlComposer(XhtmlComposer.XML, false).composePlainText(oo.getText().getDiv()), TerminologyServiceErrorClass.UNKNOWN));
                } else if (r instanceof ValueSet) {
                    ValueSet vs = (ValueSet) r;
                    if (vs.hasExpansion())
                        expansions.put(vs.getUrl(), new ValueSetExpansionOutcome(vs));
                    else {
                        canonicals.put(vs.getUrl(), vs);
                        if (vs.hasVersion())
                            canonicals.put(vs.getUrl() + "|" + vs.getVersion(), vs);
                    }
                } else if (r instanceof CanonicalResource) {
                    CanonicalResource md = (CanonicalResource) r;
                    canonicals.put(md.getUrl(), md);
                    if (md.hasVersion())
                        canonicals.put(md.getUrl() + "|" + md.getVersion(), md);
                }
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    }
}
Also used : OperationOutcome(org.hl7.fhir.r4b.model.OperationOutcome) CanonicalResource(org.hl7.fhir.r4b.model.CanonicalResource) Resource(org.hl7.fhir.r4b.model.Resource) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) ValueSetExpansionOutcome(org.hl7.fhir.r4b.terminologies.ValueSetExpander.ValueSetExpansionOutcome) File(java.io.File) ValueSet(org.hl7.fhir.r4b.model.ValueSet) CanonicalResource(org.hl7.fhir.r4b.model.CanonicalResource) FileInputStream(java.io.FileInputStream)

Example 48 with XhtmlComposer

use of org.hl7.fhir.utilities.xhtml.XhtmlComposer 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) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) 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 49 with XhtmlComposer

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

the class PageProcessor method renderCodeSystemWithLangs.

private String renderCodeSystemWithLangs(Set<String> langs, CodeSystem cs, String prefix) throws Exception {
    Narrative n = cs.getText();
    StringBuilder b = new StringBuilder();
    b.append("<div id=\"tabs\" style=\"border-right-style: none;\">\r\n");
    b.append("<ul>\r\n");
    b.append("<li><a href=\"#tabs-all\">All Languages</a></li>\r\n");
    b.append("<li><a href=\"#tabs-en\">English</a></li>\r\n");
    for (String l : sorted(langs)) b.append("<li><a href=\"#tabs-" + l + "\">" + langDisplay(l) + "</a></li>\r\n");
    b.append("</ul>\r\n");
    b.append("<div id=\"tabs-all\">\r\n");
    cs.setText(null);
    RenderingContext lrc = rc.copy().setLang("*");
    RendererFactory.factory(cs, lrc).render(cs);
    b.append(new XhtmlComposer(XhtmlComposer.HTML).compose(cs.getText().getDiv()));
    b.append("</div>\r\n");
    b.append("<div id=\"tabs-en\">\r\n");
    cs.setText(null);
    lrc = rc.copy().setLang("en");
    RendererFactory.factory(cs, lrc).render(cs);
    b.append(new XhtmlComposer(XhtmlComposer.HTML).compose(cs.getText().getDiv()));
    b.append("</div>\r\n");
    for (String l : sorted(langs)) {
        b.append("<div id=\"tabs-" + l + "\">\r\n");
        String desc = cs.getDescriptionElement().getTranslation(l);
        if (!Utilities.noString(desc))
            b.append(processMarkdown("RenderingCodeSystem", workerContext.translator().translate("render-cs", "Definition", l) + ": " + desc, prefix));
        cs.setText(null);
        lrc = rc.copy().setLang(l);
        RendererFactory.factory(cs, lrc).render(cs);
        b.append(new XhtmlComposer(XhtmlComposer.HTML).compose(cs.getText().getDiv()));
        b.append("</div>\r\n");
    }
    b.append("</div>\r\n");
    cs.setText(n);
    return b.toString();
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) Narrative(org.hl7.fhir.r5.model.Narrative) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer)

Example 50 with XhtmlComposer

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

the class PageProcessor method genDependencyGraph.

private String genDependencyGraph(ResourceDefn resource, String prefix) throws Exception {
    ElementDefn e = resource.getRoot();
    ResourceDependencyGenerator gen = new ResourceDependencyGenerator(folders.dstDir, this, resource.getName() + "-definitions.html", false, resource.getFmmLevel(), resource.getStatus());
    return "<p>Dependency Graph for " + resource.getName() + " FMM level " + resource.getFmmLevel() + "</p>" + new XhtmlComposer(XhtmlComposer.HTML).compose(gen.generate(e, prefix));
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) ResourceDependencyGenerator(org.hl7.fhir.definitions.generators.specification.ResourceDependencyGenerator)

Aggregations

XhtmlComposer (org.hl7.fhir.utilities.xhtml.XhtmlComposer)78 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)31 FHIRException (org.hl7.fhir.exceptions.FHIRException)15 IOException (java.io.IOException)14 FileOutputStream (java.io.FileOutputStream)13 NotImplementedException (org.apache.commons.lang3.NotImplementedException)11 XhtmlParser (org.hl7.fhir.utilities.xhtml.XhtmlParser)11 File (java.io.File)10 FileNotFoundException (java.io.FileNotFoundException)10 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)10 RenderingContext (org.hl7.fhir.r5.renderers.utils.RenderingContext)10 FileInputStream (java.io.FileInputStream)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 HashMap (java.util.HashMap)7 TransformerException (javax.xml.transform.TransformerException)7 XmlParser (org.hl7.fhir.r5.formats.XmlParser)7 ValueSet (org.hl7.fhir.r5.model.ValueSet)7 CanonicalResource (org.hl7.fhir.r5.model.CanonicalResource)6 URISyntaxException (java.net.URISyntaxException)5 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5