Search in sources :

Example 1 with CDANarrativeFormat

use of org.hl7.fhir.utilities.xhtml.CDANarrativeFormat 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) NamedElement(org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement) SpecialElement(org.hl7.fhir.r4b.elementmodel.Element.SpecialElement) ArrayList(java.util.ArrayList) List(java.util.List) CDANarrativeFormat(org.hl7.fhir.utilities.xhtml.CDANarrativeFormat)

Example 2 with CDANarrativeFormat

use of org.hl7.fhir.utilities.xhtml.CDANarrativeFormat 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 3 with CDANarrativeFormat

use of org.hl7.fhir.utilities.xhtml.CDANarrativeFormat 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 = convertForDateFormatFromExternal(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 {
                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(context) && attr.getLocalName().equals("type") && FormatUtilities.NS_XSI.equals(attr.getNamespaceURI()));
                if (!ok)
                    logError(line(node), col(node), path, IssueType.STRUCTURE, "Undefined attribute '@" + attr.getNodeName() + "' on " + node.getNodeName() + " for type " + context.fhirType() + " (properties = " + properties + ")", 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;
                    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);
                    context.getChildren().add(new Element(property.getName(), property, "xhtml", new XhtmlComposer(XhtmlComposer.XML, 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 (Utilities.noString(xsiType)) {
                                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);
                                n.setExplicitType(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.r4.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 4 with CDANarrativeFormat

use of org.hl7.fhir.utilities.xhtml.CDANarrativeFormat 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(elementName);
        xml.text(element.getValue());
        xml.exit(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, element.getXhtml());
            } else
                xml.escapedText(rawXhtml);
        } else if (isText(element.getProperty())) {
            if (linkResolver != null)
                xml.link(linkResolver.resolveProperty(element.getProperty()));
            xml.text(element.getValue());
        } else {
            if (isTypeAttr(element.getProperty()) && !Utilities.noString(element.getType())) {
                xml.attribute("xsi:type", element.getType());
            }
            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(elementName);
                for (Element child : element.getChildren()) composeElement(xml, child, child.getName(), false);
                xml.exit(elementName);
            } else
                xml.element(elementName);
        }
    } else {
        if (isTypeAttr(element.getProperty()) && !Utilities.noString(element.getType())) {
            xml.attribute("xsi:type", element.getType());
        }
        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.getName(), av);
            }
        }
        if (linkResolver != null)
            xml.link(linkResolver.resolveProperty(element.getProperty()));
        xml.enter(elementName);
        if (!root && element.getSpecial() != null) {
            if (linkResolver != null)
                xml.link(linkResolver.resolveProperty(element.getProperty()));
            xml.enter(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.getType());
        xml.exit(elementName);
    }
}
Also used : ElementDecoration(org.hl7.fhir.utilities.ElementDecoration) SpecialElement(org.hl7.fhir.r4.elementmodel.Element.SpecialElement) ArrayList(java.util.ArrayList) List(java.util.List) CDANarrativeFormat(org.hl7.fhir.utilities.xhtml.CDANarrativeFormat)

Example 5 with CDANarrativeFormat

use of org.hl7.fhir.utilities.xhtml.CDANarrativeFormat 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)

Aggregations

CDANarrativeFormat (org.hl7.fhir.utilities.xhtml.CDANarrativeFormat)6 XhtmlParser (org.hl7.fhir.utilities.xhtml.XhtmlParser)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ElementDecoration (org.hl7.fhir.utilities.ElementDecoration)3 XhtmlComposer (org.hl7.fhir.utilities.xhtml.XhtmlComposer)3 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)3 Node (org.w3c.dom.Node)3 SpecialElement (org.hl7.fhir.r4.elementmodel.Element.SpecialElement)2 SpecialElement (org.hl7.fhir.r4b.elementmodel.Element.SpecialElement)2 NamedElement (org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement)2 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)2 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)2