Search in sources :

Example 96 with Property

use of org.hl7.fhir.r4b.model.Property in project org.hl7.fhir.core by hapifhir.

the class Property method getChildProperties.

protected List<Property> getChildProperties(TypeDetails type) throws DefinitionException {
    ElementDefinition ed = definition;
    StructureDefinition sd = structure;
    List<ElementDefinition> children = profileUtilities.getChildMap(sd, ed);
    if (children.isEmpty()) {
        // ok, find the right definitions
        String t = null;
        if (ed.getType().size() == 1)
            t = ed.getType().get(0).getCode();
        else if (ed.getType().size() == 0)
            throw new Error("types == 0, and no children found");
        else {
            t = ed.getType().get(0).getCode();
            boolean all = true;
            for (TypeRefComponent tr : ed.getType()) {
                if (!tr.getCode().equals(t)) {
                    all = false;
                    break;
                }
            }
            if (!all) {
                // ok, it's polymorphic
                t = type.getType();
            }
        }
        if (!"xhtml".equals(t)) {
            sd = context.fetchResource(StructureDefinition.class, t);
            if (sd == null)
                throw new DefinitionException("Unable to find class '" + t + "' for name '" + ed.getPath() + "' on property " + definition.getPath());
            children = profileUtilities.getChildMap(sd, sd.getSnapshot().getElement().get(0));
        }
    }
    List<Property> properties = new ArrayList<Property>();
    for (ElementDefinition child : children) {
        properties.add(new Property(context, child, sd, this.profileUtilities));
    }
    return properties;
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.r4b.model.ElementDefinition.TypeRefComponent) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.r4b.model.ElementDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 97 with Property

use of org.hl7.fhir.r4b.model.Property in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method funcResolve.

private List<Base> funcResolve(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException {
    List<Base> result = new ArrayList<Base>();
    Base refContext = null;
    for (Base item : focus) {
        String s = convertToString(item);
        if (item.fhirType().equals("Reference")) {
            refContext = item;
            Property p = item.getChildByName("reference");
            if (p != null && p.hasValues()) {
                s = convertToString(p.getValues().get(0));
            } else {
                // a reference without any valid actual reference (just identifier or display, but we can't resolve it)
                s = null;
            }
        }
        if (item.fhirType().equals("canonical")) {
            s = item.primitiveValue();
            refContext = item;
        }
        if (s != null) {
            Base res = null;
            if (s.startsWith("#")) {
                Property p = context.rootResource.getChildByName("contained");
                if (p != null) {
                    for (Base c : p.getValues()) {
                        if (chompHash(s).equals(chompHash(c.getIdBase()))) {
                            res = c;
                            break;
                        }
                    }
                }
            } else if (hostServices != null) {
                res = hostServices.resolveReference(context.appInfo, s, refContext);
            }
            if (res != null) {
                result.add(res);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Property(org.hl7.fhir.r4b.model.Property) Base(org.hl7.fhir.r4b.model.Base)

Example 98 with Property

use of org.hl7.fhir.r4b.model.Property in project org.hl7.fhir.core by hapifhir.

the class XmlParser method parse.

public Element parse(org.w3c.dom.Element base, String type) throws Exception {
    StructureDefinition sd = getDefinition(0, 0, FormatUtilities.FHIR_NS, type);
    Element result = new Element(base.getLocalName(), new Property(context, sd.getSnapshot().getElement().get(0), sd));
    result.setPath(base.getLocalName());
    String path = "/" + pathPrefix(base.getNamespaceURI()) + base.getLocalName();
    checkElement(base, path, result.getProperty());
    result.setType(base.getLocalName());
    parseChildren(path, base, result);
    result.numberChildren();
    return result;
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)

Example 99 with Property

use of org.hl7.fhir.r4b.model.Property 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 100 with Property

use of org.hl7.fhir.r4b.model.Property in project org.hl7.fhir.core by hapifhir.

the class XmlParser method parseResource.

private void parseResource(String string, org.w3c.dom.Element container, Element parent, Property elementProperty) throws FHIRFormatError, DefinitionException, FHIRException, IOException {
    org.w3c.dom.Element res = XMLUtil.getFirstChild(container);
    String name = res.getLocalName();
    StructureDefinition sd = context.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(name, context.getOverrideVersionNs()));
    if (sd == null)
        throw new FHIRFormatError(context.formatMessage(I18nConstants.CONTAINED_RESOURCE_DOES_NOT_APPEAR_TO_BE_A_FHIR_RESOURCE_UNKNOWN_NAME_, res.getLocalName()));
    parent.updateProperty(new Property(context, sd.getSnapshot().getElement().get(0), sd), SpecialElement.fromProperty(parent.getProperty()), elementProperty);
    parent.setType(name);
    parseChildren(res.getLocalName(), res, parent);
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError)

Aggregations

ArrayList (java.util.ArrayList)35 FHIRException (org.hl7.fhir.exceptions.FHIRException)35 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)30 JsonElement (com.google.gson.JsonElement)23 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)21 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)15 JsonObject (com.google.gson.JsonObject)14 List (java.util.List)14 StructureDefinition (org.hl7.fhir.r4.model.StructureDefinition)14 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)13 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)13 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)13 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)11 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Collectors (java.util.stream.Collectors)8 ElementDefinition (org.hl7.fhir.r4.model.ElementDefinition)8 SpecialElement (org.hl7.fhir.r4b.elementmodel.Element.SpecialElement)8 NamedElement (org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement)8 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)8