Search in sources :

Example 6 with FHIRDefinedType

use of org.hl7.fhir.r4.model.Enumerations.FHIRDefinedType 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

UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Nonnull (javax.annotation.Nonnull)3 FHIRDefinedType (org.hl7.fhir.r4.model.Enumerations.FHIRDefinedType)2 ValueSet (org.hl7.fhir.r5.model.ValueSet)2 DatasetWithColumnMap (au.csiro.pathling.QueryHelpers.DatasetWithColumnMap)1 QueryHelpers.createColumns (au.csiro.pathling.QueryHelpers.createColumns)1 InvalidUserInputError (au.csiro.pathling.errors.InvalidUserInputError)1 FhirPath (au.csiro.pathling.fhirpath.FhirPath)1 NonLiteralPath (au.csiro.pathling.fhirpath.NonLiteralPath)1 ResourcePath (au.csiro.pathling.fhirpath.ResourcePath)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 AccessLevel (lombok.AccessLevel)1 Getter (lombok.Getter)1 Column (org.apache.spark.sql.Column)1 Dataset (org.apache.spark.sql.Dataset)1 Row (org.apache.spark.sql.Row)1