Search in sources :

Example 81 with ElementDefn

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

the class JsonGenerator method generateType.

private JsonObject generateType(ElementDefn root, String name, ElementDefn struc, boolean isResource, JsonObject base, boolean relative) throws IOException, Exception {
    String parent = isResource ? struc.typeCode() : "BackboneElement";
    JsonObject r = new JsonObject();
    name = name.replace(".", "_");
    base.add(name, r);
    r.addProperty("description", root.getDefinition());
    Set<String> required = new HashSet<String>();
    JsonObject props = new JsonObject();
    r.add("properties", props);
    if (isResource && definitions.hasResource(root.getName()) || "Parameters".equals(struc.getName())) {
        JsonObject rt = new JsonObject();
        props.add("resourceType", rt);
        rt.addProperty("description", "This is a " + root.getName() + " resource");
        rt.addProperty("const", root.getName());
        required.add("resourceType");
    }
    r.addProperty("additionalProperties", false);
    addInheritedProperties(root, parent, 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);
    }
    if (required.size() > 0) {
        JsonArray req = new JsonArray();
        r.add("required", req);
        for (String s : required) {
            req.add(new JsonPrimitive(s));
        }
    }
    return props;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) JsonObject(com.google.gson.JsonObject) HashSet(java.util.HashSet)

Example 82 with ElementDefn

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

the class TableGenerator method genElement.

protected Row genElement(ElementDefn e, HierarchicalTableGenerator gen, boolean resource, String path, boolean isProfile, String prefix, RenderMode mode, boolean isRoot, StandardsStatus rootStatus, boolean isAbstract, boolean isInterface) throws Exception {
    Row row = gen.new Row();
    row.setAnchor(path);
    boolean isProfiledExtension = isProfile && (e.getName().equals("extension") || e.getName().equals("modifierExtension"));
    row.getCells().add(gen.new Cell(null, dictLinks() ? pageName + "#" + path.replace("[", "_").replace("]", "_") : null, e.getName(), path + " : " + e.getDefinition(), null));
    Cell gc = gen.new Cell();
    row.getCells().add(gc);
    if (e.hasMustSupport() && e.isMustSupport()) {
        gc.addStyledText("This element must be supported", "S", "white", "red", prefix + "conformance-rules.html#mustSupport", false);
    }
    if (e.isModifier()) {
        gc.addStyledText("This element is a modifier element", "?!", null, null, prefix + "conformance-rules.html#isModifier", false);
    }
    if (e.isSummary()) {
        gc.addStyledText("This element is included in summaries", "\u03A3", null, null, prefix + "elementdefinition-definitions.html#ElementDefinition.isSummary", false);
    }
    if (!isRoot && (!e.getInvariants().isEmpty() || !e.getStatedInvariants().isEmpty())) {
        gc.addStyledText("This element has or is affected by some invariants", "I", null, null, prefix + "conformance-rules.html#constraints", false);
    }
    if (isInterface) {
        gc.addStyledText("This is an abstract type", "«A»", null, null, prefix + "uml.html#abstract", false);
    } else if (isAbstract) {
        gc.addStyledText("This is an interface resource", "«I»", null, null, prefix + "uml.html#interface", false);
    }
    if (rootStatus != null)
        gc.addStyledText("Standards Status = " + rootStatus.toDisplay(), rootStatus.getAbbrev(), "black", rootStatus.getColor(), prefix + "versions.html#std-process", true);
    else if (e.getStandardsStatus() != null)
        gc.addStyledText("Standards Status = " + e.getStandardsStatus().toDisplay(), e.getStandardsStatus().getAbbrev(), "black", e.getStandardsStatus().getColor(), prefix + "versions.html#std-process", true);
    if (resource) {
        row.getCells().add(gen.new Cell());
        row.setIcon("icon_resource.png", HierarchicalTableGenerator.TEXT_ICON_RESOURCE);
        if (Utilities.noString(e.typeCode()))
            row.getCells().add(gen.new Cell(null, null, "n/a", null, null));
        else if ("Logical".equals(e.typeCode()))
            row.getCells().add(gen.new Cell(null, prefix + "structuredefinition.html#logical", e.typeCode(), null, null));
        else if ("Base".equals(e.typeCode()))
            row.getCells().add(gen.new Cell(null, prefix + definitions.getSrcFile("Base") + ".html#" + e.typeCode(), e.typeCode(), null, null));
        else
            row.getCells().add(gen.new Cell(null, prefix + e.typeCode().toLowerCase() + ".html", e.typeCode(), null, null));
    // todo: base elements
    } else {
        if (!e.getElements().isEmpty()) {
            row.getCells().add(gen.new Cell(null, null, path.contains(".") ? e.describeCardinality() : "", null, null));
            row.setIcon("icon_element.gif", HierarchicalTableGenerator.TEXT_ICON_ELEMENT);
            if (mode == RenderMode.RESOURCE)
                row.getCells().add(gen.new Cell(null, prefix + definitions.getBackboneLink(), "BackboneElement", null, null));
            else if (e.getName().equals("Type"))
                row.getCells().add(gen.new Cell(null, null, "", null, null));
            else if (e.getName().equals("Element")) {
                if (version.isR4B()) {
                    row.getCells().add(gen.new Cell(null, prefix + definitions.getElementLink(), "Element", null, null));
                } else {
                    row.getCells().add(gen.new Cell(null, prefix + definitions.getBaseLink(), "Base", null, null));
                }
            } else if (e.typeCode().equals("BackboneElement"))
                row.getCells().add(gen.new Cell(null, prefix + definitions.getBackboneLink(), "BackBoneElement", null, null));
            else
                row.getCells().add(gen.new Cell(null, prefix + definitions.getElementLink(), "Element", null, null));
        } else if (e.getTypes().size() == 1) {
            row.getCells().add(gen.new Cell(null, null, path.contains(".") ? e.describeCardinality() : "", null, null));
            String t = e.getTypes().get(0).getName();
            Cell c;
            if (t.startsWith("@")) {
                row.setIcon("icon_reuse.png", HierarchicalTableGenerator.TEXT_ICON_REUSE);
                c = gen.new Cell("see ", "#" + t.substring(1), t.substring(t.lastIndexOf(".") + 1), t.substring(1), null);
            } else if (isReference(t)) {
                row.setIcon("icon_reference.png", HierarchicalTableGenerator.TEXT_ICON_REFERENCE);
                c = gen.new Cell();
                if (ADD_REFERENCE_TO_TABLE) {
                    c.getPieces().add(gen.new Piece(prefix + definitions.getSrcFile(t) + ".html#" + t, t, null));
                    c.getPieces().add(gen.new Piece(null, "(", null));
                }
                boolean first = true;
                for (String rt : e.getTypes().get(0).getParams()) {
                    if (!first)
                        c.getPieces().add(gen.new Piece(null, " | ", null));
                    if (first && isProfile && e.getTypes().get(0).getProfile() != null)
                        c.getPieces().add(gen.new Piece(null, e.getTypes().get(0).getProfile(), null));
                    else
                        c.getPieces().add(gen.new Piece(prefix + findPage(rt) + ".html", rt, null));
                    first = false;
                }
                if (ADD_REFERENCE_TO_TABLE)
                    c.getPieces().add(gen.new Piece(null, ")", null));
            } else if (definitions.getPrimitives().containsKey(t)) {
                row.setIcon("icon_primitive.png", HierarchicalTableGenerator.TEXT_ICON_PRIMITIVE);
                c = gen.new Cell(null, prefix + "datatypes.html#" + t, t, null, null);
            } else {
                if (t.equals("Extension"))
                    row.setIcon("icon_extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION);
                else
                    row.setIcon("icon_datatype.gif", HierarchicalTableGenerator.TEXT_ICON_DATATYPE);
                c = gen.new Cell(null, prefix + definitions.getSrcFile(t) + ".html#" + t.replace("*", "open"), t, null, null);
            }
            row.getCells().add(c);
        } else {
            row.getCells().add(gen.new Cell(null, null, isRoot ? "" : e.describeCardinality(), null, null));
            row.setIcon("icon_choice.gif", HierarchicalTableGenerator.TEXT_ICON_CHOICE);
            row.getCells().add(gen.new Cell(null, null, "", null, null));
        }
    }
    Cell cc = gen.new Cell(null, e.getShortDefn() != null && Utilities.isURL(e.getShortDefn()) ? e.getShortDefn() : null, e.getShortDefn(), null, null);
    row.getCells().add(cc);
    // constraints
    if (isProfiledExtension) {
        cc.addPiece(gen.new Piece("br"));
        cc.getPieces().add(gen.new Piece(null, e.getTypes().get(0).getProfile(), null));
    }
    if (e.hasBinding() && e.getBinding() != null && e.getBinding().getBinding() != BindingMethod.Unbound) {
        if (cc.getPieces().size() == 1)
            cc.addPiece(gen.new Piece("br"));
        cc.getPieces().add(gen.new Piece(getBindingLink(prefix, e), e.getBinding().getValueSet() != null ? e.getBinding().getValueSet().present() : e.getBinding().getName(), e.getBinding().getDefinition()));
        cc.getPieces().add(gen.new Piece(null, " (", null));
        BindingSpecification b = e.getBinding();
        if (b.hasMax()) {
            cc.getPieces().add(gen.new Piece(prefix + "terminologies.html#" + b.getStrength().toCode(), b.getStrength().getDisplay(), b.getStrength().getDefinition()));
            cc.getPieces().add(gen.new Piece(null, " but limited to ", null));
            ValueSet vs = b.getMaxValueSet();
            if (vs == null)
                cc.getPieces().add(gen.new Piece(b.getMaxReference(), b.getMaxReference(), null));
            else
                cc.getPieces().add(gen.new Piece(vs.hasUserData("external.url") ? vs.getUserString("external.url") : vs.getUserString("path"), vs.getName(), null));
        } else
            cc.getPieces().add(gen.new Piece(prefix + "terminologies.html#" + b.getStrength().toCode(), b.getStrength().getDisplay(), b.getStrength().getDefinition()));
        cc.getPieces().add(gen.new Piece(null, ")", null));
    }
    List<String> invs = new ArrayList<String>(e.getInvariants().keySet());
    Collections.sort(invs, new ConstraintsSorter());
    for (String name : invs) {
        Invariant inv = e.getInvariants().get(name);
        cc.addPiece(gen.new Piece("br"));
        cc.getPieces().add(gen.new Piece(null, "+ " + presentLevel(inv) + ": " + inv.getEnglish(), inv.getId()).setStyle("font-style: italic"));
    }
    if (e.unbounded() && !isRoot) {
        if (cc.getPieces().size() > 0)
            cc.addPiece(gen.new Piece("br"));
        if (Utilities.noString(e.getOrderMeaning())) {
        // don't show this, this it's important: cc.getPieces().add(gen.new Piece(null, "This repeating element has no defined order", null));
        } else {
            cc.getPieces().add(gen.new Piece(null, "This repeating element order: " + e.getOrderMeaning(), null));
        }
    }
    if (isRoot && !Utilities.noString(e.typeCode()) && !"Logical".equals(e.typeCode())) {
        List<ElementDefn> ancestors = new ArrayList<ElementDefn>();
        ElementDefn f = definitions.getElementDefn(e.typeCode());
        while (f != null) {
            ancestors.add(0, f);
            f = Utilities.noString(f.typeCode()) || "Logical".equals(f.typeCode()) ? null : definitions.getElementDefn(f.typeCode());
        }
        cc.getPieces().add(gen.new Piece("br"));
        cc.getPieces().add(gen.new Piece(null, "Elements defined in Ancestors: ", null));
        boolean first = true;
        for (ElementDefn fi : ancestors) {
            for (ElementDefn fc : fi.getElements()) {
                if (first)
                    first = false;
                else
                    cc.getPieces().add(gen.new Piece(null, ", ", null));
                cc.getPieces().add(gen.new Piece(definitions.getSrcFile(fi.getName()) + ".html#" + fi.getName(), fc.getName(), fc.getDefinition()));
            }
        }
    }
    if (mode == RenderMode.LOGICAL) {
        String logical = e.getMappings().get("http://hl7.org/fhir/logical");
        Cell c = gen.new Cell();
        row.getCells().add(c);
        if (logical != null)
            presentLogicalMapping(gen, c, logical, prefix);
    }
    if (e.getTypes().size() > 1) {
        // create a child for each choice
        for (TypeRef tr : e.getTypes()) {
            Row choicerow = gen.new Row();
            String t = tr.getName();
            if (isReference(t)) {
                choicerow.getCells().add(gen.new Cell(null, null, e.getName().replace("[x]", Utilities.capitalize(t)), null, null));
                choicerow.getCells().add(gen.new Cell());
                choicerow.getCells().add(gen.new Cell(null, null, "", null, null));
                choicerow.setIcon("icon_reference.png", HierarchicalTableGenerator.TEXT_ICON_REFERENCE);
                Cell c = gen.new Cell();
                choicerow.getCells().add(c);
                if (ADD_REFERENCE_TO_TABLE) {
                    if (tr.getName().equals("canonical"))
                        c.getPieces().add(gen.new Piece(prefix + "datatypes.html#canonical", "canonical", null));
                    else
                        c.getPieces().add(gen.new Piece(prefix + "references.html#Reference", "Reference", null));
                    c.getPieces().add(gen.new Piece(null, "(", null));
                }
                boolean first = true;
                for (String rt : tr.getParams()) {
                    if (!first)
                        c.getPieces().add(gen.new Piece(null, " | ", null));
                    c.getPieces().add(gen.new Piece(prefix + findPage(rt) + ".html", rt, null));
                    first = false;
                }
                if (ADD_REFERENCE_TO_TABLE)
                    c.getPieces().add(gen.new Piece(null, ")", null));
            } else if (definitions.getPrimitives().containsKey(t)) {
                choicerow.getCells().add(gen.new Cell(null, null, e.getName().replace("[x]", Utilities.capitalize(t)), definitions.getPrimitives().get(t).getDefinition(), null));
                choicerow.getCells().add(gen.new Cell());
                choicerow.getCells().add(gen.new Cell(null, null, "", null, null));
                choicerow.setIcon("icon_primitive.png", HierarchicalTableGenerator.TEXT_ICON_PRIMITIVE);
                choicerow.getCells().add(gen.new Cell(null, prefix + "datatypes.html#" + t, t, null, null));
            } else if (definitions.getConstraints().containsKey(t)) {
                ProfiledType pt = definitions.getConstraints().get(t);
                choicerow.getCells().add(gen.new Cell(null, null, e.getName().replace("[x]", Utilities.capitalize(pt.getBaseType())), definitions.getTypes().containsKey(t) ? definitions.getTypes().get(t).getDefinition() : null, null));
                choicerow.getCells().add(gen.new Cell());
                choicerow.getCells().add(gen.new Cell(null, null, "", null, null));
                choicerow.setIcon("icon_datatype.gif", HierarchicalTableGenerator.TEXT_ICON_DATATYPE);
                choicerow.getCells().add(gen.new Cell(null, definitions.getSrcFile(t) + ".html#" + t.replace("*", "open"), t, null, null));
            } else {
                choicerow.getCells().add(gen.new Cell(null, null, e.getName().replace("[x]", Utilities.capitalize(t)), definitions.getTypes().containsKey(t) ? definitions.getTypes().get(t).getDefinition() : null, null));
                choicerow.getCells().add(gen.new Cell());
                choicerow.getCells().add(gen.new Cell(null, null, "", null, null));
                choicerow.setIcon("icon_datatype.gif", HierarchicalTableGenerator.TEXT_ICON_DATATYPE);
                choicerow.getCells().add(gen.new Cell(null, definitions.getSrcFile(t) + ".html#" + t.replace("*", "open"), t, null, null));
            }
            choicerow.getCells().add(gen.new Cell());
            // choicerow.getCells().add(gen.new Cell());
            row.getSubRows().add(choicerow);
        }
    } else
        for (ElementDefn c : e.getElements()) {
            row.getSubRows().add(genElement(c, gen, false, path + '.' + c.getName(), isProfile, prefix, mode, false, null, false, false));
        }
    return row;
}
Also used : Invariant(org.hl7.fhir.definitions.model.Invariant) ProfiledType(org.hl7.fhir.definitions.model.ProfiledType) TypeRef(org.hl7.fhir.definitions.model.TypeRef) ArrayList(java.util.ArrayList) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) BindingSpecification(org.hl7.fhir.definitions.model.BindingSpecification) Row(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Row) Cell(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 83 with ElementDefn

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

the class TurtleSpecGenerator method getTypes.

private List<TypeRef> getTypes(ElementDefn elem) {
    if (elem.getTypes().size() == 1 && elem.getTypes().get(0).isWildcardType()) {
        List<TypeRef> res = new ArrayList<TypeRef>();
        for (String t : TypesUtilities.wildcardTypes(version)) {
            TypeRef tr = new TypeRef();
            tr.setName(t);
            res.add(tr);
        }
        return res;
    } else
        return elem.getTypes();
}
Also used : TypeRef(org.hl7.fhir.definitions.model.TypeRef) ArrayList(java.util.ArrayList)

Example 84 with ElementDefn

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

the class XSDBaseGenerator method genType.

private void genType(ElementDefn elem) throws Exception {
    enums.clear();
    enumDefs.clear();
    String name = elem.getName();
    write("  <xs:complexType name=\"" + name + "\">\r\n");
    write("    <xs:annotation>\r\n");
    write("      <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(elem.getDefinition()) + "</xs:documentation>\r\n");
    write("      <xs:documentation xml:lang=\"en\">If the element is present, it must have a value for at least one of the defined elements, an @id referenced from the Narrative, or extensions</xs:documentation>\r\n");
    write("    </xs:annotation>\r\n");
    write("    <xs:complexContent>\r\n");
    write("      <xs:extension base=\"" + getParentType(elem) + "\">\r\n");
    write("        <xs:sequence>\r\n");
    for (ElementDefn e : elem.getElements()) {
        if (e.typeCode().equals("xml:lang")) {
        // do nothing here
        } else if (e.getName().equals("[type]"))
            generateAny(elem, e, null, null);
        else
            generateElement(elem, e, null, null);
    }
    write("        </xs:sequence>\r\n");
    for (ElementDefn e : elem.getElements()) {
        if (e.typeCode().equals("xml:lang")) {
            write("        <xs:attribute ref=\"xml:lang\"/>\r\n");
        }
    }
    write("      </xs:extension>\r\n");
    write("    </xs:complexContent>\r\n");
    write("  </xs:complexType>\r\n");
    // write("  <xs:complexType name=\""+name+"\">\r\n");
    // write("    <xs:complexContent>\r\n");
    // write("      <xs:extension base=\"Core"+name+"\">\r\n");
    // write("        <xs:attribute name=\"id\" type=\"id-primitive\"/>\r\n");
    // write("      </xs:extension>\r\n");
    // write("    </xs:complexContent>\r\n");
    // write("  </xs:complexType>\r\n");
    genRegex();
    while (!structures.isEmpty()) {
        String s = structures.keySet().iterator().next();
        generateType(elem, s, structures.get(s));
        structures.remove(s);
    }
    for (BindingSpecification en : enums) {
        generateEnum(en);
    }
    genRegex();
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) BindingSpecification(org.hl7.fhir.definitions.model.BindingSpecification)

Example 85 with ElementDefn

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

the class XSDGenerator method generate.

public void generate(ElementDefn root, String version, String genDate, boolean outer) throws Exception {
    enums.clear();
    enumDefs.clear();
    if (outer) {
        write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        write("<!-- \r\n");
        write(Config.FULL_LICENSE_CODE);
        write("\r\n");
        write("  Generated on " + genDate + " for FHIR v" + version + " \r\n");
        write("\r\n");
        write("  Note: the schemas &amp; schematrons do not contain all of the rules about what makes resources\r\n");
        write("  valid. Implementers will still need to be familiar with the content of the specification and with\r\n");
        write("  any profiles that apply to the resources in order to make a conformant implementation.\r\n");
        write("\r\n");
        write("-->\r\n");
        write("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://hl7.org/fhir\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\" " + "targetNamespace=\"http://hl7.org/fhir\" elementFormDefault=\"qualified\" version=\"1.0\">\r\n");
        write("  <xs:include schemaLocation=\"fhir-base.xsd\"/>\r\n");
    }
    write("  <xs:element name=\"" + root.getName() + "\" type=\"" + root.getName() + "\">\r\n");
    write("    <xs:annotation>\r\n");
    write("      <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(root.getDefinition()) + "</xs:documentation>\r\n");
    write("    </xs:annotation>\r\n");
    write("  </xs:element>\r\n");
    scanTypes(root, root);
    generateType(root, root.getName(), root, true);
    for (ElementDefn e : structures) {
        generateType(root, types.get(e), e, false);
    }
    for (String en : enums.keySet()) {
        generateEnum(en);
    }
    if (outer) {
        write("</xs:schema>\r\n");
        writer.flush();
    }
}
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