Search in sources :

Example 31 with TypeRef

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

the class XPathQueryGenerator method generateXpath.

public String generateXpath(List<String> list, String rn) throws Exception {
    StringBuilder b = new StringBuilder();
    for (String ppath : list) {
        String[] path = splitPath(rn != null ? ppath.replace("{{name}}", rn) : ppath);
        if (path[path.length - 1].endsWith("[x]")) {
            ElementDefn defn = definitions.getElementDefn(path[0]);
            ElementDefn ed = defn.getElementForPath(ppath, definitions, "Search parameter xpath generation", true, false);
            for (TypeRef tr : ed.getTypes()) {
                buildPath(b, path, ed.getName().substring(0, ed.getName().length() - 3) + Utilities.capitalize(tr.getName()));
            }
        } else
            buildPath(b, path, path[path.length - 1]);
    }
    return b.toString();
}
Also used : TypeRef(org.hl7.fhir.definitions.model.TypeRef) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn)

Example 32 with TypeRef

use of org.hl7.fhir.definitions.model.TypeRef 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 33 with TypeRef

use of org.hl7.fhir.definitions.model.TypeRef 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 34 with TypeRef

use of org.hl7.fhir.definitions.model.TypeRef 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 35 with TypeRef

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

the class XSDGenerator method generateElement.

private void generateElement(ElementDefn root, ElementDefn e) throws Exception {
    write("          ");
    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");
        String close = " minOccurs=\"0\">";
        if (!forCodeGeneration) {
            write("<xs:choice minOccurs=\"" + e.getMinCardinality().toString() + "\" maxOccurs=\"" + (e.unbounded() ? "unbounded" : "1") + "\" ");
            write(">\r\n");
            if (e.hasDefinition()) {
                write("            <xs:annotation>\r\n");
                write("              <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(e.getDefinition()) + "</xs:documentation>\r\n");
                write("            </xs:annotation>\r\n");
            }
            close = "/>";
        }
        if (e.getTypes().size() == 1)
            generateAny(root, e, e.getName().replace("[x]", ""), close);
        else
            for (TypeRef t : e.getTypes()) {
                String tn = encodeType(e, t, true);
                String n = e.getName().replace("[x]", nameForType(tn));
                if (t.getName().equals("Reference"))
                    n = e.getName().replace("[x]", "Reference");
                write("            <xs:element name=\"" + n + "\" type=\"" + encodeType(e, t, true) + "\"" + close + "\r\n");
                if (forCodeGeneration) {
                    write("              <xs:annotation>\r\n");
                    if (e.hasDefinition()) {
                        write("                <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(e.getDefinition()) + " (choose any one of " + e.getName().replace("[x]", "") + "*, but only one)</xs:documentation>\r\n");
                    } else {
                        write("                <xs:documentation xml:lang=\"en\">(choose any one of " + e.getName().replace("[x]", "") + "*, but only one)</xs:documentation>\r\n");
                    }
                    write("              </xs:annotation>\r\n");
                    write("             </xs:element>\r\n");
                }
            }
        if (!forCodeGeneration) {
            write("          </xs:choice>\r\n");
        }
    } else {
        String tn = null;
        if ("extension".equals(e.getName()))
            write("<xs:element name=\"" + e.getName() + "\" type=\"Extension\" ");
        else 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() + ")");
            write("<xs:element name=\"" + e.getName() + "\" type=\"" + rtn + "\" ");
        } else if (e.getTypes().size() == 0 && e.getElements().size() > 0) {
            write("<xs:element name=\"" + e.getName() + "\" type=\"" + types.get(e) + "\" ");
        } else if (e.getTypes().size() == 1) {
            write("<xs:element name=\"" + e.getName() + "\" ");
            tn = encodeType(e, e.getTypes().get(0), true);
            if (tn.equals("Narrative") && e.getName().equals("text") && root.getElements().contains(e))
                write("type=\"" + tn + "\" ");
        } else
            throw new Exception("how do we get here? " + e.getName() + " in " + root.getName() + " " + Integer.toString(e.getTypes().size()));
        write("minOccurs=\"" + e.getMinCardinality().toString() + "\"");
        if (e.unbounded())
            write(" maxOccurs=\"unbounded\"");
        else
            write(" maxOccurs=\"1\"");
        if (tn != null && !(tn.equals("Narrative") && e.getName().equals("text") && root.getElements().contains(e))) {
            write(" type=\"" + tn + "\"");
        }
        write(">\r\n");
        if (e.hasDefinition()) {
            write("            <xs:annotation>\r\n");
            write("              <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(e.getDefinition()) + "</xs:documentation>\r\n");
            write("           </xs:annotation>\r\n");
        }
        write("          </xs:element>\r\n");
        if (tn != null && !(tn.equals("Narrative") && e.getName().equals("text") && root.getElements().contains(e))) {
            if (tn.equals("FHIRDefinedType"))
                enums.put("FHIRDefinedType", definitions.getValuesets().get("http://hl7.org/fhir/ValueSet/defined-types"));
            else if (tn.equals("FHIRAllTypes"))
                enums.put("FHIRAllTypes", definitions.getValuesets().get("http://hl7.org/fhir/ValueSet/all-types"));
        }
    }
}
Also used : TypeRef(org.hl7.fhir.definitions.model.TypeRef) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

TypeRef (org.hl7.fhir.definitions.model.TypeRef)51 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)26 ArrayList (java.util.ArrayList)18 FHIRException (org.hl7.fhir.exceptions.FHIRException)13 IOException (java.io.IOException)10 ProfiledType (org.hl7.fhir.definitions.model.ProfiledType)9 URISyntaxException (java.net.URISyntaxException)8 TypeParser (org.hl7.fhir.definitions.parsers.TypeParser)8 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)7 File (java.io.File)6 FileNotFoundException (java.io.FileNotFoundException)6 Invariant (org.hl7.fhir.definitions.model.Invariant)6 CSFile (org.hl7.fhir.utilities.CSFile)6 IniFile (org.hl7.fhir.utilities.IniFile)6 TextFile (org.hl7.fhir.utilities.TextFile)6 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 TransformerException (javax.xml.transform.TransformerException)5 NotImplementedException (org.apache.commons.lang3.NotImplementedException)5 UcumException (org.fhir.ucum.UcumException)5 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)5