Search in sources :

Example 11 with TypeRef

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

the class TurtleSpecGenerator method renderType.

private int renderType(int indent, int w, TypeRef t) throws IOException {
    if (t.isXhtml())
        write("fhir:value \"[escaped xhtml]\"^^xsd:string");
    else if (t.getName().startsWith("@"))
        write("<a href=\"#ttl-" + t.getName().substring(1) + "\"><span style=\"color: DarkViolet\">See " + t.getName().substring(1) + "</span></a>");
    else if (definitions.getConstraints().containsKey(t.getName())) {
        ProfiledType pt = definitions.getConstraints().get(t.getName());
        write("<a href=\"" + (dtRoot + definitions.getSrcFile(pt.getBaseType()) + ".html#" + pt.getBaseType() + "\">" + pt.getBaseType()) + "</a>");
        w = w + pt.getBaseType().length() + 2;
        write("(<a style=\"color:navy\" href=\"" + (dtRoot + definitions.getSrcFile(t.getName()) + ".html#" + t.getName() + "\">" + t.getName()) + "</a>)");
    } else
        write("<a href=\"" + (dtRoot + definitions.getSrcFile(t.getName()) + ".html#" + t.getName() + "\">" + t.getName()) + "</a>");
    if (t.hasParams()) {
        write("(");
        boolean firstp = true;
        List<String> ap = new ArrayList<>();
        for (String p : t.getParams()) {
            if (definitions.hasLogicalModel(p))
                ap.addAll(definitions.getLogicalModel(p).getImplementations());
            else
                ap.add(p);
        }
        Collections.sort(ap);
        for (String p : ap) {
            if (!firstp) {
                write("|");
                w++;
            }
            // again, p.length() could be wrong if this is an extension, but then it won't wrap
            if (w + p.length() > 80) {
                write("\r\n  ");
                for (int j = 0; j < indent; j++) write(" ");
                w = indent + 2;
            }
            w = w + p.length();
            // TODO: Display action and/or profile information
            if (p.equals("Any")) {
                write("<a href=\"" + prefix + "resourcelist.html" + "\">" + p + "</a>");
            } else if (t.getName().equals("Reference") && t.getParams().size() == 1 && !Utilities.noString(t.getProfile()))
                write("<a href=\"" + prefix + t.getProfile() + "\"><span style=\"color: DarkViolet\">@" + t.getProfile().substring(1) + "</span></a>");
            else
                write("<a href=\"" + (dtRoot + definitions.getSrcFile(p) + ".html#" + p) + "\">" + p + "</a>");
            firstp = false;
        }
        write(")");
        w++;
    }
    return w;
}
Also used : ProfiledType(org.hl7.fhir.definitions.model.ProfiledType) ArrayList(java.util.ArrayList)

Example 12 with TypeRef

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

the class TurtleSpecGenerator method generateCoreElem.

private void generateCoreElem(ElementDefn elem, int indent, String path, boolean backbone) throws Exception {
    String left = Utilities.padLeft("", ' ', indent * 2);
    if (isChoice(elem)) {
        write(left + "# ");
        String en = elem.getName();
        writeElementName(elem, path, en);
        write(": ");
        write(elem.describeCardinality());
        write(" <span style=\"color: navy\">");
        write(Utilities.escapeXml(elem.getShortDefn()));
        write("</span>");
        List<TypeRef> tl = getTypes(elem);
        write(". One of these ");
        write(Integer.toString(tl.size()));
        write("\r\n");
        for (TypeRef t : tl) {
            generateElementType(elem, path, left + "  ", t, elem.getName().replace("[x]", Utilities.capitalize(t.getName())));
            write("\r\n");
        }
    } else if (elem.getTypes().size() == 1) {
        TypeRef t = elem.getTypes().get(0);
        String en = elem.getName();
        generateElementType(elem, path, left, t, en);
        if (elem.getMaxCardinality() > 1)
            write(", ... ; # ");
        else
            write("; # ");
        write(elem.describeCardinality());
        write(" <span style=\"color: navy\">");
        write(Utilities.escapeXml(elem.getShortDefn()));
        write("</span>\r\n");
    } else {
        // children elements
        write(left + "fhir:");
        String en = elem.getName();
        writeElementName(elem, path, en);
        write("[ # ");
        write(elem.describeCardinality());
        write(" <span style=\"color: navy\">");
        write(Utilities.escapeXml(elem.getShortDefn()));
        write("</span>\r\n");
        for (ElementDefn child : elem.getElements()) {
            generateCoreElem(child, indent + 1, path + "." + en, backbone);
        }
        if (elem.getMaxCardinality() > 1)
            write(left + "], ...;\r\n");
        else
            write(left + "];\r\n");
    }
}
Also used : TypeRef(org.hl7.fhir.definitions.model.TypeRef) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn)

Example 13 with TypeRef

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

the class XmlSpecGenerator method writeTypeLinks.

private int writeTypeLinks(ElementDefn elem, int indent) throws Exception {
    write(" <span style=\"color: darkgreen\">");
    int i = 0;
    // this is wrong if the type is an attribute, but the wrapping concern shouldn't apply in this case, so this is ok
    int w = indent + 12 + elem.getName().length();
    for (TypeRef t : elem.getTypes()) {
        if (i > 0) {
            write("|");
            w++;
        }
        if (w + t.getName().length() > 80) {
            write("\r\n  ");
            for (int j = 0; j < indent; j++) write(" ");
            w = indent + 2;
        }
        // again, could be wrong if this is an extension, but then it won't wrap
        w = w + t.getName().length();
        if (t.isXhtml() || t.getName().equals("list"))
            write(t.getName());
        else if (t.getName().equals("Extension") && t.getParams().size() == 0 && !Utilities.noString(t.getProfile()))
            write("<a href=\"" + prefix + t.getProfile() + "\"><span style=\"color: DarkViolet\">@" + t.getProfile().substring(1) + "</span></a>");
        else if (definitions.getConstraints().containsKey(t.getName())) {
            ProfiledType pt = definitions.getConstraints().get(t.getName());
            write("<a href=\"" + (dtRoot + definitions.getSrcFile(pt.getBaseType()) + ".html#" + pt.getBaseType() + "\">" + pt.getBaseType()) + "</a>");
            w = w + pt.getBaseType().length() + 2;
            write("(<a style=\"color:navy\" href=\"" + (dtRoot + definitions.getSrcFile(t.getName()) + ".html#" + t.getName() + "\">" + t.getName()) + "</a>)");
        } else
            write("<a href=\"" + (dtRoot + definitions.getSrcFile(t.getName()) + ".html#" + t.getName() + "\">" + t.getName()) + "</a>");
        if (t.hasParams()) {
            write("(");
            boolean firstp = true;
            List<String> ap = new ArrayList<>();
            for (String p : t.getParams()) {
                if (definitions.hasLogicalModel(p))
                    ap.addAll(definitions.getLogicalModel(p).getImplementations());
                else
                    ap.add(p);
            }
            Collections.sort(ap);
            for (String p : ap) {
                if (!firstp) {
                    write("|");
                    w++;
                }
                // again, p.length() could be wrong if this is an extension, but then it won't wrap
                if (w + p.length() > 80) {
                    write("\r\n  ");
                    for (int j = 0; j < indent; j++) write(" ");
                    w = indent + 2;
                }
                w = w + p.length();
                // TODO: Display action and/or profile information
                if (p.equals("Any")) {
                    write("<a href=\"" + prefix + "resourcelist.html" + "\">" + p + "</a>");
                } else if (t.getName().equals("Reference") && t.getParams().size() == 1 && !Utilities.noString(t.getProfile()))
                    write("<a href=\"" + prefix + t.getProfile() + "\"><span style=\"color: DarkViolet\">@" + t.getProfile().substring(1) + "</span></a>");
                else
                    write("<a href=\"" + (dtRoot + definitions.getSrcFile(p) + ".html#" + p) + "\">" + p + "</a>");
                firstp = false;
            }
            write(")");
            w++;
        }
        i++;
    }
    write("</span>");
    return w;
}
Also used : ProfiledType(org.hl7.fhir.definitions.model.ProfiledType) TypeRef(org.hl7.fhir.definitions.model.TypeRef) ArrayList(java.util.ArrayList)

Example 14 with TypeRef

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

the class XSDBaseGenerator method generateElement.

private void generateElement(ElementDefn root, ElementDefn e, String paramType, Map<String, String> rules) throws Exception {
    List<TypeRef> types = e.getTypes();
    if (types.size() > 1 || (types.size() == 1 && types.get(0).isWildcardType())) {
        if (!e.getName().contains("[x]"))
            throw new Exception("Element has multiple types as a choice doesn't have a [x] in the element name '" + e.getName() + "' in resource " + root.getName());
        if ((types.size() == 1 && types.get(0).isWildcardType())) {
            generateAny(root, e, e.getName().replace("[x]", ""), null);
        } else {
            String close = " minOccurs=\"0\">";
            ;
            if (!forCodeGeneration) {
                write("          <xs:choice minOccurs=\"" + checkRule(e.getMinCardinality().toString(), e.getName() + ".min", rules) + "\" maxOccurs=\"1\">\r\n");
                if (e.hasDefinition()) {
                    write("            <xs:annotation>\r\n");
                    write("              <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(checkRule(e.getDefinition(), e.getName() + ".defn", rules)) + "</xs:documentation>\r\n");
                    write("            </xs:annotation>\r\n");
                }
                close = "/>";
            }
            for (TypeRef t : types) {
                String type = encodeType(e, t, true);
                String name = e.getName().substring(0, e.getName().length() - 3) + nameForType(type);
                write("           <xs:element name=\"" + name + "\" type=\"" + type + "\" ");
                if (e.unbounded())
                    write(" maxOccurs=\"" + checkRule("unbounded", e.getName() + ".max", rules) + "\"" + close + "\r\n");
                else
                    write(" maxOccurs=\"" + checkRule("1", e.getName() + ".max", rules) + "\"" + close + "\r\n");
                if (forCodeGeneration) {
                    write("              <xs:annotation>\r\n");
                    if (e.hasDefinition()) {
                        write("                <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(checkRule(e.getDefinition(), e.getName() + ".defn", rules)) + " (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 {
        write("          ");
        if ("extension".equals(e.getName()))
            write("<xs:element name=\"" + e.getName() + "\" type=\"Extension\" ");
        else if ("div".equals(e.getName()) && e.typeCode().equals("xhtml"))
            write("<xs:element ref=\"xhtml:div\" ");
        else if (e.usesCompositeType()) {
            ElementDefn ref = root.getElementByName(definitions, e.typeCode().substring(1), true, false, null);
            String rtn = this.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 (types.size() == 0 && e.getElements().size() > 0) {
            String tn = root.getName() + "." + Utilities.capitalize(e.getName());
            int i = 0;
            while (typenames.contains(tn)) {
                i++;
                tn = root.getName() + "." + Utilities.capitalize(e.getName()) + Integer.toString(i);
            }
            write("<xs:element name=\"" + e.getName() + "\" type=\"" + tn + "\" ");
            structures.put(tn, e);
            this.types.put(e, tn);
            typenames.add(tn);
        } else if (types.size() == 1) {
            if (types.get(0).isUnboundGenericParam() && paramType != null)
                write("<xs:element name=\"" + e.getName() + "\" type=\"" + paramType + "\" ");
            else if (!Utilities.noString(e.getRegex())) {
                String tn = root.getName() + Utilities.capitalize(e.getName()) + "Type";
                regexQueue.put(tn, e.getRegex());
                write("<xs:element name=\"" + e.getName() + "\" type=\"" + tn + "\" ");
            } else
                write("<xs:element name=\"" + e.getName() + "\" type=\"" + encodeType(e, types.get(0), true) + "\" ");
        } else
            throw new Exception("how do we get here? " + e.getName() + " in " + root.getName() + " " + Integer.toString(types.size()));
        write("minOccurs=\"" + checkRule(e.getMinCardinality().toString(), e.getName() + ".min", rules) + "\"");
        if (e.unbounded())
            write(" maxOccurs=\"" + checkRule("unbounded", e.getName() + ".max", rules) + "\"");
        else
            write(" maxOccurs=\"" + checkRule("1", e.getName() + ".max", rules) + "\"");
        if (e.hasDefinition()) {
            write(">\r\n");
            write("            <xs:annotation>\r\n");
            write("              <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(checkRule(e.getDefinition(), e.getName() + ".defn", rules)) + "</xs:documentation>\r\n");
            write("            </xs:annotation>\r\n");
            write("          </xs:element>\r\n");
        } else {
            write("/>\r\n");
        }
    }
}
Also used : TypeRef(org.hl7.fhir.definitions.model.TypeRef) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 15 with TypeRef

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

the class JsonLDDefinitionsGenerator method generate.

// private String genDate;
// private String version;
// private BuildWorkerContext workerContext;
public void generate(Definitions definitions, IniFile ini, String tmpResDir, String dstDir, String srcDir, FHIRVersion version, String genDate, BuildWorkerContext workerContext) throws Exception {
    // this.genDate = genDate;
    // this.version = version;
    // this.workerContext = workerContext;
    JsonObject defn = new JsonObject();
    JsonObject context = new JsonObject();
    defn.add("@context", context);
    defn.addProperty("@id", "http://hl7.org/fhir/");
    context.addProperty("fhir", "http://hl7.org/fhir/");
    context.addProperty("xsd", "http://www.w3.org/2001/XMLSchema#");
    // properties for primitives, helpers, format features
    addProperty(context, "value", "fhir:value", "xsd:string");
    addProperty(context, "decimal", "fhir:value", "xsd:decimal");
    addProperty(context, "integer", "fhir:value", "xsd:integer");
    if (!version.isR4B()) {
        addProperty(context, "integer64", "fhir:value", "xsd:string");
    }
    addProperty(context, "boolean", "fhir:value", "xsd:boolean");
    addProperty(context, "binary", "fhir:value", "xsd:base64Binary");
    addProperty(context, "date", "fhir:value", "xsd:date");
    addProperty(context, "dateTime", "fhir:value", "xsd:dateTime");
    addProperty(context, "gYearMonth", "fhir:value", "xsd:gYearMonth");
    addProperty(context, "gYear", "fhir:value", "xsd:gYear");
    addProperty(context, "link", "fhir:link", "@id");
    addProperty(context, "concept", "fhir:concept", "@id");
    addProperty(context, "index", "fhir:index", "xsd:integer");
    addProperty(context, "role", "fhir:nodeRole", "@id");
    // elements defined in FHIR:
    for (TypeRef tr : definitions.getKnownTypes()) {
        if (!definitions.hasPrimitiveType(tr.getName()) && !tr.getName().equals("SimpleQuantity") && !tr.getName().equals("MoneyQuantity")) {
            TypeDefn root = definitions.getElementDefn(tr.getName());
            new JsonLDGenerator(definitions, workerContext, definitions.getKnownTypes()).generate(context, root, version, genDate);
        }
    }
    List<String> names = new ArrayList<String>();
    names.addAll(definitions.getResources().keySet());
    names.addAll(definitions.getBaseResources().keySet());
    names.add("Parameters");
    Collections.sort(names);
    for (String name : names) {
        ResourceDefn root = definitions.getResourceByName(name);
        new JsonLDGenerator(definitions, workerContext, definitions.getKnownTypes()).generate(context, root.getRoot(), version, genDate);
    }
    save(defn, dstDir + "fhir.jsonld");
}
Also used : TypeDefn(org.hl7.fhir.definitions.model.TypeDefn) TypeRef(org.hl7.fhir.definitions.model.TypeRef) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn)

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