Search in sources :

Example 11 with TypeInfo

use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.

the class JavaParserJsonGenerator method genElementComposer.

private void genElementComposer(Analysis analysis, TypeInfo ti, ElementDefinition ed, ElementDefinition inh) throws Exception {
    String name = ed.getName();
    if (name.endsWith("[x]") || name.equals("[type]")) {
        String en = name.endsWith("[x]") && !name.equals("[x]") ? name.replace("[x]", "") : "value";
        String pfx = name.endsWith("[x]") ? name.replace("[x]", "") : "";
        composer.append("      if (element.has" + upFirst(en) + "()) {\r\n");
        composer.append("        composeType(\"" + pfx + "\", element.get" + upFirst(en) + "());\r\n");
        composer.append("      }\r\n");
    } else {
        String tn = ed.getUserString("java.type");
        String comp = null;
        String en = null;
        boolean enShared = false;
        if (ed.hasUserData("java.enum")) {
            // getCodeListType(cd.getBinding());
            EnumInfo ei = (EnumInfo) ed.getUserData("java.enum");
            ValueSet vs = ei.getValueSet();
            enShared = vs.hasUserData("shared");
            if (enShared) {
                en = "Enumerations." + ei.getName();
            } else {
                en = analysis.getClassName() + "." + ei.getName();
            }
        } else {
            if (name.equals("extension")) {
                name = "extension";
                tn = "Extension";
            }
            if (tn.equals("XhtmlNode")) {
                tn = "xhtml";
                comp = "composeXhtml";
            } else if (tn.equals("code")) {
                tn = "Code";
                comp = "composeCode";
            } else if (tn.equals("instant")) {
                tn = "Instant";
            } else if (tn.contains("Reference(")) {
                comp = "composeReference";
                tn = "Reference";
            } else if (tn.contains("canonical(")) {
                comp = "composeCanonical";
                tn = "CanonicalType";
            } else if (tn.contains("(")) {
                comp = "compose" + tn;
            } else if (tn.startsWith(analysis.getName()) && !tn.equals(analysis.getClassName())) {
                comp = "compose" + leaf(tn);
            } else if (isPrimitive(ed)) {
                comp = "compose" + leaf(tn);
                // remove Type"
                comp = comp.substring(0, comp.length() - 4);
            } else {
                comp = "compose" + leaf(tn);
            }
        }
        if (ed.unbounded()) {
            tn = ed.getUserString("java.type");
            if (tn.contains("Reference(")) {
                comp = "composeReference";
                tn = "Reference";
            } else if (tn.contains("canonical(")) {
                comp = "composeCanonical";
                tn = "CanonicalType";
            }
            String stn = tn;
            if (ed.isInlineType() || ed.hasContentReference()) {
                stn = analysis.getClassName() + "." + tn;
            }
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            if (en == null) {
                if (tn.equals("String"))
                    tn = "StringType";
                if (definitions.hasPrimitiveType(tn)) {
                    tn = upFirst(tn) + "Type";
                }
                if (isPrimitive(ed) || ed.typeSummary().startsWith("canonical(")) {
                    composer.append("        if (anyHasValue(element.get" + upFirst(getElementName(name, false)) + "())) {\r\n");
                    composer.append("          openArray(\"" + name + "\");\r\n");
                    composer.append("          for (" + (tn.contains("(") ? stn : upFirst(tn)) + " e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                    composer.append("            " + comp + "Core(null, e, e != element.get" + upFirst(getElementName(name, false)) + "().get(element.get" + upFirst(getElementName(name, false)) + "().size()-1));\r\n");
                    composer.append("          closeArray();\r\n");
                    composer.append("        }\r\n");
                    composer.append("        if (anyHasExtras(element.get" + upFirst(getElementName(name, false)) + "())) {\r\n");
                    composer.append("          openArray(\"_" + name + "\");\r\n");
                    composer.append("          for (" + (stn.contains("(") ? stn : upFirst(stn)) + " e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                    composer.append("            " + comp + "Extras(null, e, true);\r\n");
                    composer.append("          closeArray();\r\n");
                    composer.append("        }\r\n");
                } else if (ed.typeSummary().equals("Resource")) {
                    composer.append("        openArray(\"" + name + "\");\r\n");
                    composer.append("        for (" + (stn.contains("(") ? tn : upFirst(tn)) + " e : element.get" + upFirst(getElementName(name, false)) + "()) {\r\n");
                    composer.append("          open(null);\r\n");
                    composer.append("          " + comp + "(e);\r\n");
                    composer.append("          close();\r\n");
                    composer.append("        }\r\n");
                    composer.append("        closeArray();\r\n");
                } else {
                    composer.append("        openArray(\"" + name + "\");\r\n");
                    composer.append("        for (" + (stn.contains("(") ? stn : upFirst(stn)) + " e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                    composer.append("          " + comp + "(null, e);\r\n");
                    composer.append("        closeArray();\r\n");
                }
            } else {
                composer.append("        openArray(\"" + name + "\");\r\n");
                composer.append("        for (Enumeration<" + prepEnumName(en) + "> e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                composer.append("          composeEnumerationCore(null, e, new " + prepEnumName(en) + "EnumFactory(), true);\r\n");
                composer.append("        closeArray();\r\n");
                composer.append("        if (anyHasExtras(element.get" + upFirst(getElementName(name, false)) + "())) {\r\n");
                composer.append("          openArray(\"_" + name + "\");\r\n");
                composer.append("          for (Enumeration<" + prepEnumName(en) + "> e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                composer.append("            composeEnumerationExtras(null, e, new " + prepEnumName(en) + "EnumFactory(), true);\r\n");
                composer.append("          closeArray();\r\n");
                composer.append("        }\r\n");
            }
            composer.append("      };\r\n");
        } else if (en != null) {
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "Element()) {\r\n");
            if (enShared) {
                composer.append("        composeEnumerationCore(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), new " + prepEnumName(en) + "EnumFactory(), false);\r\n");
                composer.append("        composeEnumerationExtras(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), new " + prepEnumName(en) + "EnumFactory(), false);\r\n");
            } else {
                composer.append("        composeEnumerationCore(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), new " + prepEnumName(en) + "EnumFactory(), false);\r\n");
                composer.append("        composeEnumerationExtras(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), new " + prepEnumName(en) + "EnumFactory(), false);\r\n");
            }
            composer.append("      }\r\n");
        // composer.append("        composeString(\""+name+"\", element.get"+upFirst(getElementName(name, false))+"().toCode());\r\n");
        } else if (ed.typeSummary().equals("Resource")) {
            composer.append("        if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("          open(\"" + name + "\");\r\n");
            composer.append("          " + comp + "(element.get" + upFirst(getElementName(name, false)) + "());\r\n");
            composer.append("          close();\r\n");
            composer.append("        }\r\n");
        } else if (!"xhtml".equals(ed.typeSummary()) && (isPrimitive(ed) || ed.typeSummary().startsWith("canonical("))) {
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "Element()) {\r\n");
            composer.append("        " + comp + "Core(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), false);\r\n");
            composer.append("        " + comp + "Extras(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), false);\r\n");
            composer.append("      }\r\n");
        } else if (tn.equals("xhtml")) {
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("        XhtmlNode node = element.getDiv();\r\n");
            composer.append("        if (node.getNsDecl() == null) {\r\n");
            composer.append("          node.attribute(\"xmlns\", XHTML_NS);\r\n");
            composer.append("        }\r\n");
            composer.append("        " + comp + "(\"" + name + "\", node);\r\n");
            composer.append("      }\r\n");
        } else if (inh != null && inh.unbounded()) {
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("        " + comp + "(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "FirstRep());\r\n");
            composer.append("      }\r\n");
        } else {
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("        " + comp + "(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "());\r\n");
            composer.append("      }\r\n");
        }
    }
}
Also used : EnumInfo(org.hl7.fhir.core.generator.analysis.EnumInfo) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 12 with TypeInfo

use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.

the class JavaParserJsonGenerator method genInnerComposer.

private void genInnerComposer(Analysis analysis, TypeInfo ti) throws IOException, Exception {
    String tn = ti.getName();
    String stn = (ti == analysis.getRootType() ? tn : analysis.getClassName() + "." + tn);
    composer.append("  protected void compose" + tn + "(String name, " + stn + " element) throws IOException {\r\n");
    composer.append("    if (element != null) {\r\n");
    boolean isResource = ti == analysis.getRootType() && analysis.getStructure().getKind() == StructureDefinitionKind.RESOURCE;
    if (isResource) {
        composer.append("      prop(\"resourceType\", name);\r\n");
    } else {
        composer.append("      open(name);\r\n");
    }
    composer.append("      compose" + upFirst(tn).replace(".", "") + "Properties(element);\r\n");
    if (!isResource) {
        composer.append("      close();\r\n");
    }
    composer.append("    }\r\n");
    composer.append("  }\r\n\r\n");
    composer.append("  protected void compose" + tn + "Properties(" + stn + " element) throws IOException {\r\n");
    composer.append("    compose" + ti.getAncestorName() + "Properties(element);\r\n");
    for (ElementDefinition e : ti.getChildren()) {
        genElementComposer(analysis, analysis.getRootType(), e, matchingInheritedElement(ti.getInheritedChildren(), e, analysis.getName()));
    }
    composer.append("  }\r\n\r\n");
}
Also used : ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Example 13 with TypeInfo

use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.

the class JavaParserJsonGenerator method genInner.

private void genInner(Analysis analysis, TypeInfo ti) throws IOException, Exception {
    String tn = ti.getName();
    String stn = (ti == analysis.getRootType() ? tn : analysis.getClassName() + "." + tn);
    String pn = "parse" + tn;
    if (stn.contains(".") && !pn.startsWith("parse" + analysis.getClassName())) {
        pn = "parse" + analysis.getClassName() + tn;
    }
    boolean bUseOwner = false;
    parser.append("  protected " + stn + " " + pn + "(JsonObject json) throws IOException, FHIRFormatError {\r\n");
    parser.append("    " + stn + " res = new " + stn + "();\r\n");
    parser.append("    " + pn + "Properties(json, res);\r\n");
    parser.append("    return res;\r\n");
    parser.append("  }\r\n\r\n");
    parser.append("  protected void " + pn + "Properties(JsonObject json, " + stn + " res) throws IOException, FHIRFormatError {\r\n");
    parser.append("    parse" + ti.getAncestorName() + "Properties(json, res);\r\n");
    for (ElementDefinition e : ti.getChildren()) {
        genElementParser(analysis, ti, e, bUseOwner, matchingInheritedElement(ti.getInheritedChildren(), e, analysis.getName()));
    }
    parser.append("  }\r\n\r\n");
}
Also used : ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Example 14 with TypeInfo

use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.

the class JavaParserXmlGenerator method genElementCompose.

private void genElementCompose(Analysis analysis, TypeInfo ti, ElementDefinition ed, ElementDefinition inh) throws Exception {
    String name = ed.getName();
    if (name.endsWith("[x]") || name.equals("[type]")) {
        String en = name.endsWith("[x]") && !name.equals("[x]") ? name.replace("[x]", "") : "value";
        String pfx = name.endsWith("[x]") ? name.replace("[x]", "") : "";
        composer.append("    if (element.has" + upFirst(getElementName(en, false)) + "()) {\r\n");
        composer.append("      composeType(\"" + pfx + "\", element.get" + upFirst(getElementName(en, false)) + "());\r\n");
        composer.append("    }");
    } else {
        String comp = null;
        String en = null;
        String tn = ed.getUserString("java.type");
        if (ed.hasUserData("java.enum")) {
            EnumInfo ei = (EnumInfo) ed.getUserData("java.enum");
            ValueSet vs = ei.getValueSet();
            boolean enShared = vs.hasUserData("shared");
            if (enShared) {
                en = "Enumerations." + ei.getName();
            } else {
                en = analysis.getClassName() + "." + ei.getName();
            }
        } else {
            if (name.equals("extension")) {
                name = "extension";
                tn = "Extension";
            }
            if (tn.equals("XhtmlNode")) {
                tn = "xhtml";
                comp = "composeXhtml";
            } else if (isPrimitive(ed)) {
                comp = "compose" + tn.substring(0, tn.length() - 4);
            } else if (tn.contains("Reference(")) {
                comp = "composeReference";
                tn = "Reference";
            } else if (tn.contains("canonical(")) {
                comp = "composeCanonical";
                tn = "CanonicalType";
            } else if (tn.contains("(")) {
                comp = "compose" + tn;
            } else if ((ed.hasContentReference() || ed.isInlineType()) && !tn.startsWith(analysis.getClassName())) {
                comp = "compose" + analysis.getClassName() + tn;
            } else {
                comp = "compose" + tn;
            }
        }
        if (ed.unbounded()) {
            if (en != null) {
                composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "()) \r\n");
                composer.append("        for (Enumeration<" + en + "> e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                composer.append("          composeEnumeration(\"" + name + "\", e, new " + en + "EnumFactory());\r\n");
            } else {
                String stn = ed.isInlineType() || ed.hasContentReference() ? analysis.getClassName() + "." + tn : tn;
                // String pfx = ed.isInlineType() || ed.hasContentReference() ? analysis.getClassName() : "";
                composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "()) { \r\n");
                composer.append("      for (" + stn + " e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                if (ed.typeSummary().equals("Resource")) {
                    composer.append("        {\r\n");
                    composer.append("          xml.enter(FHIR_NS, \"" + name + "\");\r\n");
                    composer.append("          " + comp + "(e);\r\n");
                    composer.append("          xml.exit(FHIR_NS, \"" + name + "\");\r\n");
                    composer.append("        }\r\n");
                } else {
                    composer.append("          " + comp + "(\"" + name + "\", e);\r\n");
                }
                composer.append("    }\r\n");
            }
        } else if (en != null) {
            composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "Element())\r\n");
            composer.append("      composeEnumeration(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), new " + en + "EnumFactory());\r\n");
        } else if (!"xhtml".equals(ed.typeSummary()) && (isJavaPrimitive(ed) || ed.typeSummary().startsWith("canonical("))) {
            composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "Element()) {\r\n");
            composer.append("      " + comp + "(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element());\r\n");
            composer.append("    }\r\n");
        } else if (ed.typeSummary().equals("Resource")) {
            composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("      xml.enter(FHIR_NS, \"" + name + "\");\r\n");
            composer.append("      " + comp + "(element.get" + upFirst(getElementName(name, false)) + "());\r\n");
            composer.append("      xml.exit(FHIR_NS, \"" + name + "\");\r\n");
            composer.append("    }\r\n");
        } else if (inh != null && inh.unbounded()) {
            composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("      " + comp + "(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "FirstRep());\r\n");
            composer.append("    }\r\n");
        } else {
            composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("      " + comp + "(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "());\r\n");
            composer.append("    }\r\n");
        }
    }
}
Also used : EnumInfo(org.hl7.fhir.core.generator.analysis.EnumInfo) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 15 with TypeInfo

use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.

the class JavaParserXmlGenerator method genInnerAbstractComposer.

private void genInnerAbstractComposer(Analysis analysis, TypeInfo ti) throws IOException, Exception {
    String tn = ti.getName();
    composer.append("  protected void compose" + tn + "Elements(" + tn + " element) throws IOException {\r\n");
    composer.append("    compose" + ti.getAncestorName() + "Elements(element);\r\n");
    if (!analysis.isInterface()) {
        for (ElementDefinition ed : ti.getChildren()) {
            if (!ed.hasRepresentation(PropertyRepresentation.XMLATTR)) {
                genElementCompose(analysis, ti, ed, null);
            }
        }
    }
    composer.append("  }\r\n\r\n");
}
Also used : ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Aggregations

ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)22 ModelInfo (org.hl7.elm_modelinfo.r1.ModelInfo)9 Test (org.junit.Test)9 ClassInfo (org.hl7.elm_modelinfo.r1.ClassInfo)8 EnumInfo (org.hl7.fhir.core.generator.analysis.EnumInfo)5 TypeRefComponent (org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent)5 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)5 ValueSet (org.hl7.fhir.r5.model.ValueSet)5 QName (javax.xml.namespace.QName)4 TypeInfo (org.hl7.fhir.core.generator.analysis.TypeInfo)4 ArrayList (java.util.ArrayList)3 TypeInfo (org.hl7.elm_modelinfo.r1.TypeInfo)3 File (java.io.File)2 ModelSpecifier (org.hl7.elm_modelinfo.r1.ModelSpecifier)2 ProfileUtilities (org.hl7.fhir.r5.conformance.ProfileUtilities)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ChoiceTypeSpecifier (org.hl7.elm_modelinfo.r1.ChoiceTypeSpecifier)1