Search in sources :

Example 16 with DefinedCode

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

the class XSDBaseGenerator method genPrimitives.

private void genPrimitives() throws Exception {
    for (DefinedCode cd : definitions.getPrimitives().values()) {
        if (cd instanceof PrimitiveType) {
            PrimitiveType pt = (PrimitiveType) cd;
            // two very special cases due to schema weirdness
            if (cd.getCode().equals("date")) {
                write("  <xs:simpleType name=\"date-primitive\">\r\n");
                write("    <xs:restriction>\r\n");
                write("      <xs:simpleType>\r\n");
                write("        <xs:union memberTypes=\"xs:gYear xs:gYearMonth xs:date\"/>\r\n");
                write("      </xs:simpleType>\r\n");
                write("      <xs:pattern value=\"([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1]))?)?\"/>\r\n");
                write("    </xs:restriction>\r\n");
                write("  </xs:simpleType>\r\n");
            } else if (cd.getCode().equals("dateTime")) {
                write("  <xs:simpleType name=\"dateTime-primitive\">\r\n");
                write("    <xs:restriction>\r\n");
                write("      <xs:simpleType>\r\n");
                write("        <xs:union memberTypes=\"xs:gYear xs:gYearMonth xs:date xs:dateTime\"/>\r\n");
                write("      </xs:simpleType>\r\n");
                write("      <xs:pattern value=\"([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]+)?(Z|(\\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)?\"/>\r\n");
                write("    </xs:restriction>\r\n");
                write("  </xs:simpleType>\r\n");
            } else if (cd.getCode().equals("time")) {
                write("  <xs:simpleType name=\"time-primitive\">\r\n");
                write("    <xs:restriction base=\"xs:time\">\r\n");
                write("      <xs:pattern value=\"([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]+)?\"/>\r\n");
                write("    </xs:restriction>\r\n");
                write("  </xs:simpleType>\r\n");
            } else {
                write("  <xs:simpleType name=\"" + pt.getCode() + "-primitive\">\r\n");
                if (pt.getSchemaType().contains(",")) {
                    write("    <xs:union memberTypes=\"" + pt.getSchemaType().replace(",", "") + "\"/>\r\n");
                } else if (pt.getSchemaType().equals("string")) {
                    write("    <xs:restriction base=\"xs:" + pt.getSchemaType() + "\">\r\n");
                    write("      <xs:minLength value=\"1\"/>\r\n");
                    write("    </xs:restriction>\r\n");
                } else if (!Utilities.noString(pt.getRegex())) {
                    write("    <xs:restriction base=\"xs:" + pt.getSchemaType() + "\">\r\n");
                    write("      <xs:pattern value=\"" + pt.getRegex() + "\"/>\r\n");
                    write("    </xs:restriction>\r\n");
                } else {
                    write("    <xs:restriction base=\"xs:" + pt.getSchemaType() + "\"/>\r\n");
                }
                write("  </xs:simpleType>\r\n");
            }
            write("  <xs:complexType name=\"" + pt.getCode() + "\">\r\n");
            write("    <xs:annotation>\r\n");
            write("      <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(pt.getDefinition()) + "</xs:documentation>\r\n");
            if (!Utilities.noString(pt.getComment()))
                write("      <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(pt.getComment()) + "</xs:documentation>\r\n");
            write("      <xs:documentation xml:lang=\"en\">If the element is present, it must have either a @value, an @id, or extensions</xs:documentation>\r\n");
            write("    </xs:annotation>\r\n");
            write("    <xs:complexContent>\r\n");
            write("      <xs:extension base=\"Element\">\r\n");
            write("        <xs:attribute name=\"value\" type=\"" + pt.getCode() + "-primitive\" use=\"optional\"/>\r\n");
            write("      </xs:extension>\r\n");
            write("    </xs:complexContent>\r\n");
            write("  </xs:complexType>\r\n");
        } else {
            DefinedStringPattern sp = (DefinedStringPattern) cd;
            write("  <xs:simpleType name=\"" + sp.getCode() + "-primitive\">\r\n");
            if (sp.getSchema().endsWith("+")) {
                write("    <xs:restriction base=\"" + sp.getSchema().substring(0, sp.getSchema().length() - 1) + "\">\r\n");
                write("      <xs:pattern value=\"" + sp.getRegex() + "\"/>\r\n");
                write("      <xs:minLength value=\"1\"/>\r\n");
                if (sp.getCode().equals("id"))
                    write("      <xs:maxLength value=\"64\"/>\r\n");
                write("    </xs:restriction>\r\n");
                write("  </xs:simpleType>\r\n");
            } else {
                write("    <xs:restriction base=\"" + sp.getSchema() + "\">\r\n");
                if (!sp.getSchema().contains("Integer")) {
                    write("      <xs:minLength value=\"1\"/>\r\n");
                }
                if (!Utilities.noString(sp.getRegex())) {
                    write("      <xs:pattern value=\"" + sp.getRegex() + "\"/>\r\n");
                }
                write("    </xs:restriction>\r\n");
                write("  </xs:simpleType>\r\n");
            }
            write("  <xs:complexType name=\"" + sp.getCode() + "\">\r\n");
            write("    <xs:annotation>\r\n");
            write("      <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(sp.getDefinition()) + "</xs:documentation>\r\n");
            if (!Utilities.noString(sp.getComment()))
                write("      <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(sp.getComment()) + "</xs:documentation>\r\n");
            write("      <xs:documentation xml:lang=\"en\">If the element is present, it must have either a @value, 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=\"Element\">\r\n");
            write("        <xs:attribute name=\"value\" type=\"" + sp.getCode() + "-primitive\"/>\r\n");
            write("      </xs:extension>\r\n");
            write("    </xs:complexContent>\r\n");
            write("  </xs:complexType>\r\n");
        }
    }
}
Also used : DefinedStringPattern(org.hl7.fhir.definitions.model.DefinedStringPattern) DefinedCode(org.hl7.fhir.definitions.model.DefinedCode) PrimitiveType(org.hl7.fhir.definitions.model.PrimitiveType)

Example 17 with DefinedCode

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

the class SvgGenerator method determineMetrics.

private Point determineMetrics(ElementDefn e, ClassItem source, String path, boolean isRoot, DefinedCode primitive) throws Exception {
    String t = e.getName();
    if (isRoot) {
        t = t + " {" + e.typeCodeNoParams() + ")";
    }
    if (definitions.getBaseResources().containsKey(e.getName()) && definitions.getBaseResources().get(e.getName()).isInterface()) {
        t = t + " «Interface»";
    }
    double width = textWidth(t) * 1.8;
    // double width = textWidth(e.getName()) * 1.8 + (isRoot ? textWidth(" (Resource)") : 0);
    double height;
    if (attributes) {
        if (primitive != null) {
            if (primitive instanceof PrimitiveType) {
                height = HEADER_HEIGHT + GAP_HEIGHT * 2 + LINE_HEIGHT + GAP_HEIGHT * 2;
                width = textWidth("value : " + getXsi(primitive) + " 0..1");
            } else
                height = HEADER_HEIGHT + GAP_HEIGHT * 2;
        } else {
            int i = 0;
            for (ElementDefn c : e.getElements()) if (isAttribute(c)) {
                String[] texts = textForAttribute(c);
                i = i + texts.length;
                double w = textWidth(texts[0]);
                for (int j = 1; j < texts.length; j++) w = Math.max(w, textWidth(texts[j]));
                if (w > width)
                    width = w;
            }
            height = HEADER_HEIGHT + GAP_HEIGHT * 2 + LINE_HEIGHT * i + GAP_HEIGHT * 2;
        }
    } else
        height = HEADER_HEIGHT + GAP_HEIGHT * 2;
    if (ini != null) {
        String uml = ini.getStringProperty("directions", path);
        if (!Utilities.noString(uml) && uml.contains(";")) {
            String[] svg = uml.split("\\;");
            e.setSvgLeft(Integer.parseInt(svg[0]));
            e.setSvgTop(Integer.parseInt(svg[1]));
        } else if (!Utilities.noString(uml) && uml.contains(",")) {
            String[] svg = uml.split("\\,");
            e.setSvgLeft(Integer.parseInt(svg[0]));
            e.setSvgTop(Integer.parseInt(svg[1]));
        } else {
            e.setUmlDir(uml);
        }
    }
    Point p = new Point(e.getSvgLeft(), e.getSvgTop(), PointKind.unknown);
    if (p.y == ElementDefn.MAX_NEG || p.x == ElementDefn.MAX_NEG) {
        if ("left".equals(e.getUmlDir())) {
            p.x = source.left - 120 - width;
            p.y = source.centerV() - height / 2;
            p = findEmptyPlace(p, width, height, 0, 80);
        } else if ("right".equals(e.getUmlDir())) {
            p.x = source.right() + 120;
            p.y = source.centerV() - height / 2;
            p = findEmptyPlace(p, width, height, 0, 80);
        } else if ("up".equals(e.getUmlDir())) {
            p.x = source.centerH() - width / 2;
            p.y = source.top - height - 80;
            p = findEmptyPlace(p, width, height, 80, 0);
        } else if ("down".equals(e.getUmlDir())) {
            p.x = source.centerH() - width / 2;
            p.y = source.bottom() + 80;
            p = findEmptyPlace(p, width, height, +80, 0);
        } else {
            p.y = 0;
            p.x = 0;
            p = findEmptyPlace(p, width, height, 80, 0);
        }
    }
    miny = Math.min(miny, p.y);
    minx = Math.min(minx, p.x);
    ClassItem item = new ClassItem(p.x, p.y, width, height, e.getPath());
    classes.put(e, item);
    double x = item.right() + MARGIN_X;
    double y = item.bottom() + MARGIN_Y;
    if (attributes) {
        for (ElementDefn c : e.getElements()) {
            if (!isAttribute(c) && (Utilities.noString(c.typeCode()) || !c.typeCode().startsWith("@"))) {
                p = determineMetrics(c, item, path + "." + c.getName(), false, null);
                x = Math.max(x, p.x + MARGIN_X);
                y = Math.max(y, p.y + MARGIN_Y);
            }
        }
    }
    return new Point(x, y, PointKind.unknown);
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) PrimitiveType(org.hl7.fhir.definitions.model.PrimitiveType)

Example 18 with DefinedCode

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

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

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

the class BindingSpecification method processCode.

private void processCode(CodeSystem cs, ConceptDefinitionComponent c, String system, String parent) {
    DefinedCode code = new DefinedCode();
    code.setCode(c.getCode());
    code.setDisplay(c.getDisplay());
    code.setComment(ToolingExtensions.getCSComment(c));
    code.setDefinition(c.getDefinition());
    code.setParent(parent);
    code.setSystem(system);
    code.setAbstract(CodeSystemUtilities.isNotSelectable(cs, c));
    allCodes.add(code);
    for (ConceptDefinitionComponent cc : c.getConcept()) processCode(cs, cc, system, c.getCode());
}
Also used : ConceptDefinitionComponent(org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent)

Aggregations

DefinedCode (org.hl7.fhir.definitions.model.DefinedCode)19 ArrayList (java.util.ArrayList)8 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)7 IOException (java.io.IOException)6 ResourceDefn (org.hl7.fhir.definitions.model.ResourceDefn)6 PrimitiveType (org.hl7.fhir.definitions.model.PrimitiveType)5 FHIRException (org.hl7.fhir.exceptions.FHIRException)5 JsonObject (com.google.gson.JsonObject)4 DefinedStringPattern (org.hl7.fhir.definitions.model.DefinedStringPattern)4 TypeDefn (org.hl7.fhir.definitions.model.TypeDefn)4 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)4 FileNotFoundException (java.io.FileNotFoundException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ProfiledType (org.hl7.fhir.definitions.model.ProfiledType)3 TypeRef (org.hl7.fhir.definitions.model.TypeRef)3 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)3 CSFile (org.hl7.fhir.utilities.CSFile)3 File (java.io.File)2 TransformerException (javax.xml.transform.TransformerException)2 ConstraintStructure (org.hl7.fhir.definitions.model.ConstraintStructure)2