Search in sources :

Example 76 with ElementDefn

use of org.hl7.fhir.definitions.model.ElementDefn in project kindling by HL7.

the class XmlSpecGenerator method generateInner.

private void generateInner(ElementDefn root, boolean resource, boolean isAbstract) throws IOException, Exception {
    String rn;
    if (root.getName().equals("Extension"))
        rn = "extension|modifierExtension";
    else if (root.getName().equals("Meta"))
        rn = "meta";
    else if (root.getTypes().size() > 0 && (root.getTypes().get(0).getName().equals("Type") || (root.getTypes().get(0).getName().equals("Structure"))) || isAbstract)
        rn = "[name]";
    else
        rn = root.getName();
    write("<");
    if (defPage == null)
        write("<span title=\"" + Utilities.escapeXml(root.getDefinition()) + "\"><b>");
    else
        write("<a href=\"" + (defPage + "#" + root.getName()) + "\" title=\"" + Utilities.escapeXml(root.getDefinition()) + "\" class=\"dict\"><b>");
    write(rn);
    if ((defPage == null))
        write("</b></span>");
    else
        write("</b></a>");
    boolean hasXmlLang = false;
    for (ElementDefn elem : root.getElements()) {
        hasXmlLang = hasXmlLang || elem.typeCode().equals("xml:lang");
    }
    if (hasXmlLang)
        write(" xml:lang?");
    if (resource)
        write(" xmlns=\"http://hl7.org/fhir\"");
    else
        write(" xmlns=\"http://hl7.org/fhir\"");
    for (ElementDefn elem : root.getElements()) {
        if (elem.isXmlAttribute()) {
            generateAttribute(elem);
        }
    }
    if (resource) {
        write("&gt; <span style=\"float: right\"><a title=\"Documentation for this format\" href=\"" + prefix + "xml.html\"><img src=\"" + prefix + "help.png\" alt=\"doco\"/></a></span>\r\n");
    } else
        write("&gt;\r\n");
    if (rn.equals(root.getName()) && resource) {
        if (!Utilities.noString(root.typeCode())) {
            write(" &lt;!-- from <a href=\"" + prefix + "resource.html\">Resource</a>: <a href=\"" + prefix + "resource.html#id\">id</a>, <a href=\"" + prefix + "resource.html#meta\">meta</a>, <a href=\"" + prefix + "resource.html#implicitRules\">implicitRules</a>, and <a href=\"" + prefix + "resource.html#language\">language</a> -->\r\n");
            if (root.typeCode().equals("DomainResource"))
                write(" &lt;!-- from <a href=\"" + prefix + "domainresource.html\">DomainResource</a>: <a href=\"" + prefix + "narrative.html#Narrative\">text</a>, <a href=\"" + prefix + "references.html#contained\">contained</a>, <a href=\"" + prefix + "extensibility.html\">extension</a>, and <a href=\"" + prefix + "extensibility.html#modifierExtension\">modifierExtension</a> -->\r\n");
        }
    } else if (root.typeCode().equals("BackboneElement")) {
        write(" &lt;!-- from BackboneElement: <a href=\"" + prefix + "extensibility.html\">extension</a>, <a href=\"" + prefix + "extensibility.html\">modifierExtension</a> -->\r\n");
    } else {
        write(" &lt;!-- from Element: <a href=\"" + prefix + "extensibility.html\">extension</a> -->\r\n");
    }
    for (ElementDefn elem : root.getElements()) {
        if (!elem.typeCode().equals("xml:lang") && !elem.isXmlAttribute())
            generateCoreElem(elem, 1, rn, root.getName(), rn.equals(root.getName()) && resource);
    }
    write("&lt;/");
    write(rn);
    write("&gt;\r\n");
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn)

Example 77 with ElementDefn

use of org.hl7.fhir.definitions.model.ElementDefn in project kindling by HL7.

the class JsonGenerator method generate.

public JsonObject generate(ElementDefn root, String version, String genDate, JsonObject mainSchema) throws Exception {
    enums.clear();
    enumDefs.clear();
    JsonObject schema = mainSchema;
    JsonObject definitions;
    if (schema == null) {
        schema = new JsonObject();
        schema.addProperty("$schema", "http://json-schema.org/draft-06/schema#");
        schema.addProperty("id", "http://hl7.org/fhir/json-schema/" + root.getName());
        schema.addProperty("$ref", "#/definitions/" + root.getName().replace(".", "_"));
        schema.addProperty("description", "see http://hl7.org/fhir/json.html#schema for information about the FHIR Json Schemas");
        definitions = new JsonObject();
        schema.add("definitions", definitions);
    } else
        definitions = schema.getAsJsonObject("definitions");
    scanTypes(root, root);
    generateType(root, root.getName(), root, true, definitions, mainSchema != null);
    for (ElementDefn e : structures) {
        generateType(root, types.get(e), e, false, definitions, mainSchema != null);
    }
    return schema;
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) JsonObject(com.google.gson.JsonObject)

Example 78 with ElementDefn

use of org.hl7.fhir.definitions.model.ElementDefn in project kindling by HL7.

the class JsonGenerator method generateElement.

private void generateElement(ElementDefn root, ElementDefn e, Set<String> required, JsonObject props, boolean relative) throws Exception {
    if (e.getTypes().size() > 1 || (e.getTypes().size() == 1 && e.getTypes().get(0).isWildcardType())) {
        if (!e.getName().contains("[x]"))
            throw new Exception("Element " + e.getName() + " in " + root.getName() + " has multiple types as a choice doesn't have a [x] in the element name");
        if (e.getTypes().size() == 1)
            generateAny(root, e, e.getName().replace("[x]", ""), props, relative);
        else {
            for (TypeRef t : e.getTypes()) {
                JsonObject property = new JsonObject();
                JsonObject property_ = null;
                TypeDefn td = null;
                if (definitions.getConstraints().containsKey(t.getName()))
                    td = definitions.getElementDefn(definitions.getConstraints().get(t.getName()).getBaseType());
                else if (definitions.hasElementDefn(t.getName()))
                    td = definitions.getElementDefn(t.getName());
                String en = e.getName().replace("[x]", "");
                props.add(en + upFirst(td == null ? t.getName() : td.getName()), property);
                property.addProperty("description", e.getDefinition());
                String tref = null;
                String type = null;
                String pattern = null;
                if (definitions.getPrimitives().containsKey(t.getName())) {
                    DefinedCode def = definitions.getPrimitives().get(t.getName());
                    type = def.getJsonType();
                    pattern = def.getRegex();
                    if (!Utilities.noString(pattern))
                        property.addProperty("pattern", "^" + pattern + "$");
                    property.addProperty("type", type);
                    property_ = new JsonObject();
                    props.add("_" + en + upFirst(t.getName()), property_);
                    property_.addProperty("description", "Extensions for " + en + upFirst(t.getName()));
                    tref = (relative ? "#" : "Element.schema.json#") + "/definitions/Element";
                    property_.addProperty("$ref", tref);
                } else {
                    String tn = encodeType(e, t, true);
                    tref = (relative ? "#" : tn.replace(".", "_") + ".schema.json#") + "/definitions/" + tn.replace(".", "_");
                    property.addProperty("$ref", tref);
                }
            }
        }
    } else {
        JsonObject property = new JsonObject();
        JsonObject property_ = null;
        props.add(e.getName(), property);
        property.addProperty("description", e.getDefinition());
        String tref = null;
        String type = null;
        String pattern = null;
        if (e.usesCompositeType()) /* && types.containsKey(root.getElementByName(e.typeCode().substring(1)))*/
        {
            ElementDefn ref = root.getElementByName(definitions, e.typeCode().substring(1), true, false, null);
            String rtn = types.get(ref);
            if (rtn == null)
                throw new Exception("logic error in schema generator (null composite reference in " + types.toString() + ")");
            if (rtn == "Type")
                rtn = "Element";
            type = rtn;
            tref = "#/definitions/" + rtn.replace(".", "_");
        } else if (e.getTypes().size() == 0 && e.getElements().size() > 0) {
            tref = "#/definitions/" + types.get(e).replace(".", "_");
            type = types.get(e).replace(".", "_");
        } else if (e.getTypes().size() == 1) {
            String tn = encodeType(e, e.getTypes().get(0), true);
            tref = "#/definitions/" + tn;
            if (definitions.getPrimitives().containsKey(e.typeCode())) {
                DefinedCode def = definitions.getPrimitives().get(e.typeCode());
                if (e.getName().equals("id")) {
                    tref = (relative ? "#" : tn.replace(".", "_") + ".schema.json#") + "/definitions/" + tn.replace(".", "_");
                    property.addProperty("$ref", tref);
                } else {
                    property_ = new JsonObject();
                    props.add("_" + e.getName(), property_);
                    property_.addProperty("description", "Extensions for " + e.getName());
                    BindingSpecification cd = e.getBinding();
                    if (cd != null && (cd.getBinding() == BindingSpecification.BindingMethod.CodeList)) {
                        ValueSet vs = cd.getValueSet();
                        if (vs != null) {
                            ValueSet ex = workerContext.expandVS(vs, true, false).getValueset();
                            JsonArray enums = new JsonArray();
                            for (ValueSetExpansionContainsComponent cc : ex.getExpansion().getContains()) {
                                enums.add(new JsonPrimitive(cc.getCode()));
                            }
                            property.add("enum", enums);
                            pattern = null;
                        }
                    } else {
                        property.addProperty("$ref", tref);
                    }
                    tref = (relative ? "#" : "Element.schema.json#") + "/definitions/Element";
                }
            } else {
                tref = (relative ? "#" : tn.replace(".", "_") + ".schema.json#") + "/definitions/" + tn.replace(".", "_");
            }
        } else
            throw new Exception("how do we get here? " + e.getName() + " in " + root.getName() + " " + Integer.toString(e.getTypes().size()));
        if (property_ != null) {
            if (!Utilities.noString(type))
                property.addProperty("type", type);
            if (!Utilities.noString(pattern))
                property.addProperty("pattern", "^" + pattern + "$");
            if (!Utilities.noString(tref))
                property_.addProperty("$ref", tref);
        } else if ("div".equals(e.getName()) && "xhtml".equals(type)) {
            // Is there a better type, or ref for html?
            property.addProperty("type", "string");
        } else {
            property.addProperty("$ref", tref);
        }
        if (e.unbounded()) {
            makeArray(property);
            if (property_ != null)
                makeArray(property_);
        }
        if (e.getMinCardinality() > 0 && property_ == null)
            required.add(e.getName());
    }
}
Also used : TypeDefn(org.hl7.fhir.definitions.model.TypeDefn) JsonArray(com.google.gson.JsonArray) ValueSetExpansionContainsComponent(org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent) JsonPrimitive(com.google.gson.JsonPrimitive) TypeRef(org.hl7.fhir.definitions.model.TypeRef) DefinedCode(org.hl7.fhir.definitions.model.DefinedCode) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) BindingSpecification(org.hl7.fhir.definitions.model.BindingSpecification) JsonObject(com.google.gson.JsonObject) ValueSet(org.hl7.fhir.r5.model.ValueSet) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 79 with ElementDefn

use of org.hl7.fhir.definitions.model.ElementDefn in project kindling by HL7.

the class JsonGenerator method generateAny.

private void generateAny(ElementDefn root, ElementDefn e, String prefix, JsonObject props, boolean relative) throws Exception {
    List<String> allTypes = TypesUtilities.wildcardTypes(version);
    for (String s : allTypes) {
        JsonObject property = new JsonObject();
        JsonObject property_ = null;
        String en = e.getName().replace("[x]", "");
        props.add(en + upFirst(s), property);
        property.addProperty("description", e.getDefinition());
        String tref = null;
        String type = null;
        String pattern = null;
        if (definitions.getPrimitives().containsKey(s)) {
            DefinedCode def = definitions.getPrimitives().get(s);
            type = def.getJsonType();
            pattern = def.getRegex();
            if (!Utilities.noString(pattern))
                property.addProperty("pattern", "^" + pattern + "$");
            property.addProperty("type", type);
            property_ = new JsonObject();
            props.add("_" + en + upFirst(s), property_);
            property_.addProperty("description", "Extensions for " + en + upFirst(s));
            tref = (relative ? "#" : "Element.schema.json#") + "/definitions/Element";
            property_.addProperty("$ref", tref);
        } else {
            String tn = encodeType(e, datatypes.get(s), true);
            tref = (relative ? "#" : tn.replace(".", "_") + ".schema.json#") + "/definitions/" + tn.replace(".", "_");
            property.addProperty("$ref", tref);
        }
    }
}
Also used : DefinedCode(org.hl7.fhir.definitions.model.DefinedCode) JsonObject(com.google.gson.JsonObject)

Example 80 with ElementDefn

use of org.hl7.fhir.definitions.model.ElementDefn in project kindling by HL7.

the class JsonGenerator method addInheritedProperties.

private void addInheritedProperties(ElementDefn root, String typeCode, Set<String> required, JsonObject props, boolean relative) throws Exception {
    if (Utilities.noString(typeCode))
        return;
    ElementDefn struc = definitions.getElementDefn(typeCode);
    addInheritedProperties(root, struc.typeCode(), required, props, relative);
    for (ElementDefn e : struc.getElements()) {
        if (e.getName().equals("[type]"))
            generateAny(root, e, "", props, relative);
        else
            generateElement(root, e, required, props, relative);
    }
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn)

Aggregations

ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)100 TypeRef (org.hl7.fhir.definitions.model.TypeRef)35 ArrayList (java.util.ArrayList)28 FHIRException (org.hl7.fhir.exceptions.FHIRException)28 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)21 ResourceDefn (org.hl7.fhir.definitions.model.ResourceDefn)19 Invariant (org.hl7.fhir.definitions.model.Invariant)16 IOException (java.io.IOException)14 BindingSpecification (org.hl7.fhir.definitions.model.BindingSpecification)11 FileNotFoundException (java.io.FileNotFoundException)10 URISyntaxException (java.net.URISyntaxException)10 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)10 ProfiledType (org.hl7.fhir.definitions.model.ProfiledType)9 TransformerException (javax.xml.transform.TransformerException)8 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)8 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)7 NotImplementedException (org.apache.commons.lang3.NotImplementedException)7 UcumException (org.fhir.ucum.UcumException)7 TypeParser (org.hl7.fhir.definitions.parsers.TypeParser)7 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)7