Search in sources :

Example 16 with TypeInfo

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

the class JavaParserXmlGenerator 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 pfx = (ti.getDefn().isInlineType()) && !tn.startsWith(analysis.getClassName()) ? analysis.getClassName() : "";
    if (!analysis.isAbstract() || ti != analysis.getRootType()) {
        parser.append("  protected " + stn + " parse" + pfx + tn + "(XmlPullParser xpp) throws XmlPullParserException, IOException, FHIRFormatError {\r\n");
        parser.append("    " + stn + " res = new " + stn + "();\r\n");
        if (ti == analysis.getRootType() && analysis.getStructure().getKind() == StructureDefinitionKind.RESOURCE) {
            parser.append("    parseResourceAttributes(xpp, res);\r\n");
        } else {
            parser.append("    parseElementAttributes(xpp, res);\r\n");
        }
        for (ElementDefinition ed : ti.getChildren()) {
            if (ed.hasRepresentation(PropertyRepresentation.XMLATTR)) {
                parser.append("    if (xpp.getAttributeValue(null, \"" + ed.getName() + "\") != null)\r\n");
                parser.append("        res.set" + upFirst(getElementName(ed.getName(), true)) + "(xpp.getAttributeValue(null, \"" + ed.getName() + "\"));\r\n");
            }
        }
        parser.append("    next(xpp);\r\n");
        parser.append("    int eventType = nextNoWhitespace(xpp);\r\n");
        parser.append("    while (eventType != XmlPullParser.END_TAG) {\r\n");
        parser.append("    if (!parse" + pfx + tn + "Content(eventType, xpp, res))\r\n");
        parser.append("        unknownContent(xpp);\r\n");
        parser.append("      eventType = nextNoWhitespace(xpp);\r\n");
        parser.append("    }\r\n");
        parser.append("    next(xpp);\r\n");
        parser.append("    parseElementClose(res);\r\n");
        parser.append("    return res;\r\n");
        parser.append("  }\r\n\r\n");
    }
    parser.append("  protected boolean parse" + pfx + tn + "Content(int eventType, XmlPullParser xpp, " + stn + " res) throws XmlPullParserException, IOException, FHIRFormatError {\r\n");
    boolean first = true;
    for (ElementDefinition ed : ti.getChildren()) {
        if (!ed.hasRepresentation(PropertyRepresentation.XMLATTR)) {
            genElement(analysis, ti, ed, matchingInheritedElement(ti.getInheritedChildren(), ed, analysis.getName()), first);
            first = false;
        }
    }
    if (!first)
        parser.append("    } else ");
    else
        parser.append("    ");
    parser.append("if (!parse" + ti.getAncestorName() + "Content(eventType, xpp, res)){ \r\n");
    parser.append("      return false;\r\n");
    parser.append("    }\r\n");
    parser.append("    return true;\r\n");
    parser.append("  }\r\n\r\n");
}
Also used : ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Example 17 with TypeInfo

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

the class JavaParserXmlGenerator method genElement.

private void genElement(Analysis analysis, TypeInfo ti, ElementDefinition ed, ElementDefinition inh, boolean first) 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.equals("[x]") ? name.replace("[x]", "") : "";
        parser.append("    " + (!first ? "} else " : "") + "if (eventType == XmlPullParser.START_TAG && nameIsTypeName(xpp, \"" + pfx + "\")) {\r\n");
        parser.append("      res.set" + upFirst(getElementName(en, false)) + "(parseType(\"" + en + "\", xpp));\r\n");
    } else {
        String prsr = null;
        if (ed.hasUserData("java.enum")) {
            // getCodeListType(cd.getBinding());
            EnumInfo ei = (EnumInfo) ed.getUserData("java.enum");
            ValueSet vs = ei.getValueSet();
            boolean enShared = vs.hasUserData("shared");
            String en;
            if (enShared) {
                en = "Enumerations." + ei.getName();
            } else {
                en = analysis.getClassName() + "." + ei.getName();
            }
            // en+".fromCode(parseString(xpp))";
            prsr = "parseEnumeration(xpp, " + en + ".NULL, new " + en.substring(0, en.indexOf(".")) + "." + en.substring(en.indexOf(".") + 1) + "EnumFactory())";
        // parseEnumeration(xpp, Narrative.NarrativeStatus.additional, new Narrative.NarrativeStatusEnumFactory())
        } else {
            String tn = ed.getUserString("java.type");
            if (name.equals("extension")) {
                name = "extension";
                tn = "Extension";
            }
            if (tn.contains("Reference("))
                prsr = "parseReference(xpp)";
            else if (tn.contains("canonical("))
                prsr = "parseCanonical(xpp)";
            else if (tn.contains("("))
                prsr = "parse" + tn + "(xpp)";
            else if (tn.startsWith(analysis.getName()) && !tn.equals(analysis.getClassName())) /* && !definitions.hasType(tn)*/
            {
                prsr = "parse" + upFirst(tn) + "(xpp)";
            } else if (tn.equals("Resource") || tn.equals("DomainResource"))
                prsr = "parse" + upFirst(tn) + "Contained(xpp)";
            else if (isPrimitive(ed)) {
                prsr = "parse" + upFirst(tn.substring(0, tn.length() - 4)) + "(xpp)";
            } else if ((ed.hasContentReference() || ed.isInlineType()) && !tn.startsWith(analysis.getClassName())) {
                prsr = "parse" + analysis.getClassName() + upFirst(tn) + "(xpp)";
            } else {
                prsr = "parse" + upFirst(tn) + "(xpp)";
            }
        }
        if (ed.unbounded()) {
            parser.append("    " + (!first ? "} else " : "") + "if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(\"" + name + "\")) {\r\n");
            parser.append("      res.get" + upFirst(getElementName(name, false)) + "().add(" + prsr + ");\r\n");
        } else if (inh != null && inh.unbounded()) {
            parser.append("    " + (!first ? "} else " : "") + "if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(\"" + name + "\")) {\r\n");
            parser.append("      res.add" + upFirst(getElementName(name, false)) + (!ed.typeSummary().equals("xhtml") && (isPrimitive(ed) || ed.typeSummary().startsWith("canonical(")) ? "Element" : "") + "(" + prsr + ");\r\n");
        } else {
            parser.append("    " + (!first ? "} else " : "") + "if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(\"" + name + "\")) {\r\n");
            parser.append("      res.set" + upFirst(getElementName(name, false)) + (!ed.typeSummary().equals("xhtml") && (isPrimitive(ed) || ed.typeSummary().startsWith("canonical(")) ? "Element" : "") + "(" + prsr + ");\r\n");
        }
    }
}
Also used : EnumInfo(org.hl7.fhir.core.generator.analysis.EnumInfo) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 18 with TypeInfo

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

the class JavaParserXmlGenerator method genInnerAbstract.

private void genInnerAbstract(Analysis analysis, TypeInfo ti) throws IOException, Exception {
    String tn = ti.getName();
    parser.append("  protected boolean parse" + upFirst(tn).replace(".", "") + "Content(int eventType, XmlPullParser xpp, " + tn + " res) throws XmlPullParserException, IOException, FHIRFormatError {\r\n");
    boolean first = true;
    if (!analysis.isInterface()) {
        for (ElementDefinition ed : ti.getChildren()) {
            if (!ed.hasRepresentation(PropertyRepresentation.XMLATTR)) {
                genElement(analysis, ti, ed, null, first);
                first = false;
            }
        }
    }
    if (!first)
        parser.append("    } else ");
    else
        parser.append("    ");
    if (ti.getAncestorName() != null) {
        parser.append("if (!parse" + ti.getAncestorName() + "Content(eventType, xpp, res)){ \r\n");
    } else {
        parser.append(" { \r\n");
    }
    parser.append("        return false;\r\n");
    parser.append("    }\r\n");
    parser.append("    return true;\r\n");
    parser.append("  }\r\n\r\n");
}
Also used : ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Example 19 with TypeInfo

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

the class JavaResourceGenerator method generateEquals.

private void generateEquals(Analysis analysis, TypeInfo ti, boolean owner) throws Exception {
    List<ElementDefinition> children = ti.getChildren();
    String tn = ti.getName();
    boolean isAbstract = analysis.isAbstract();
    boolean isInterface = analysis.isInterface();
    write("      @Override\r\n");
    write("      public boolean equalsDeep(Base other_) {\r\n");
    write("        if (!super.equalsDeep(other_))\r\n");
    write("          return false;\r\n");
    write("        if (!(other_ instanceof " + tn + "))\r\n");
    write("          return false;\r\n");
    write("        " + tn + " o = (" + tn + ") other_;\r\n");
    write("        return ");
    boolean first = true;
    int col = 18;
    for (ElementDefinition c : children) {
        if (!isInterface) {
            if (first)
                first = false;
            else {
                write(" && ");
                col = col + 4;
            }
            String name = getElementName(c.getName(), true);
            if (name.endsWith("[x]"))
                name = name.substring(0, name.length() - 3);
            write("compareDeep(" + name + ", o." + name + ", true)");
            col = col + 21 + name.length() * 2;
            if (col > 100) {
                col = 10;
                write("\r\n          ");
            }
        }
    }
    if (first)
        write("true");
    write(";\r\n");
    write("      }\r\n\r\n");
    write("      @Override\r\n");
    write("      public boolean equalsShallow(Base other_) {\r\n");
    write("        if (!super.equalsShallow(other_))\r\n");
    write("          return false;\r\n");
    write("        if (!(other_ instanceof " + tn + "))\r\n");
    write("          return false;\r\n");
    write("        " + tn + " o = (" + tn + ") other_;\r\n");
    write("        return ");
    first = true;
    col = 18;
    for (ElementDefinition c : children) {
        if (!isInterface) {
            if (isJavaPrimitive(c) && !"xhtml".equals(c.typeSummary())) {
                if (first)
                    first = false;
                else {
                    write(" && ");
                    col = col + 4;
                }
                String name = getElementName(c.getName(), true);
                if (name.endsWith("[x]"))
                    name = name.substring(0, name.length() - 3);
                write("compareValues(" + name + ", o." + name + ", true)");
                col = col + 21 + name.length() * 2;
                if (col > 100) {
                    col = 10;
                    write("\r\n          ");
                }
            }
        }
    }
    if (first)
        write("true");
    write(";\r\n");
    write("      }\r\n\r\n");
}
Also used : ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Example 20 with TypeInfo

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

the class JavaParserRdfGenerator 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);
    String pfx = (ti.getDefn().isInlineType()) && !tn.startsWith(analysis.getClassName()) ? analysis.getClassName() : "";
    composer.append("  protected void compose" + pfx + tn + "(Complex parent, String parentType, String name, " + stn + " element, int index) {\r\n");
    composer.append("    if (element == null) \r\n");
    composer.append("      return;\r\n");
    composer.append("    Complex t;\r\n");
    composer.append("    if (Utilities.noString(parentType))\r\n");
    composer.append("      t = parent;\r\n");
    composer.append("    else {\r\n");
    composer.append("      t = parent.predicate(\"fhir:\"+parentType+'.'+name);\r\n");
    composer.append("    }\r\n");
    composer.append("    compose" + ti.getAncestorName() + "(t, \"" + ti.getDefn().getName() + "\", name, element, index);\r\n");
    if (tn.equals("Coding"))
        composer.append("    decorateCoding(t, element);\r\n");
    else if (tn.equals("CodeableConcept"))
        composer.append("    decorateCodeableConcept(t, element);\r\n");
    for (ElementDefinition ed : ti.getChildren()) {
        genElement(analysis, ti, ed, matchingInheritedElement(ti.getInheritedChildren(), ed, analysis.getName()));
    }
    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