Search in sources :

Example 26 with Element

use of org.hl7.fhir.r4b.elementmodel.Element in project kindling by HL7.

the class TypeParser method convert.

public List<TypeRefComponent> convert(IWorkerContext context, String path, List<TypeRef> types, boolean resource, ElementDefinition ed) throws Exception {
    List<TypeRefComponent> list = new ArrayList<TypeRefComponent>();
    for (TypeRef t : types) {
        // Expand any Resource(A|B|C) references
        if (t.hasParams() && !("Reference".equals(t.getName()) || "canonical".equals(t.getName()))) {
            throw new Exception("Only resource references can specify parameters.  Path " + path);
        }
        if (t.getParams().size() > 0) {
            if (t.getProfile() != null && t.getParams().size() != 1) {
                throw new Exception("Cannot declare profile on a resource reference declaring multiple resource types.  Path " + path);
            }
            if (t.getProfile() != null) {
                TypeRefComponent childType = getTypeComponent(list, t.getName());
                if (t.getVersioning() != null)
                    childType.setVersioning(t.getVersioning());
                if (t.getName().equals("Reference") || t.getName().equals("canonical"))
                    childType.addTargetProfile(t.getProfile());
                else
                    childType.addProfile(t.getProfile());
            } else
                for (String param : t.getParams()) {
                    TypeRefComponent childType = getTypeComponent(list, t.getName());
                    if (t.getVersioning() != null)
                        childType.setVersioning(t.getVersioning());
                    String p = "Any".equals(param) ? "Resource" : param;
                    if (t.getName().equals("Reference") || t.getName().equals("canonical"))
                        childType.addTargetProfile("http://hl7.org/fhir/StructureDefinition/" + p);
                    else
                        childType.addProfile("http://hl7.org/fhir/StructureDefinition/" + p);
                }
        } else if (t.isWildcardType()) {
            // this list is filled out manually because it may be running before the types referred to have been loaded
            for (String n : TypesUtilities.wildcardTypes(version)) {
                TypeRefComponent tc = new TypeRefComponent().setCode(n);
                if (t.getVersioning() != null)
                    tc.setVersioning(t.getVersioning());
                list.add(tc);
            }
        } else if (Utilities.noString(t.getName()) && t.getProfile() != null) {
            StructureDefinition sd = context.fetchResource(StructureDefinition.class, t.getProfile());
            TypeRefComponent tc = getTypeComponent(list, sd != null ? sd.getType() : t.getName());
            if (t.getVersioning() != null)
                tc.setVersioning(t.getVersioning());
            if (t.getName().equals("Reference"))
                tc.addTargetProfile(t.getProfile());
            else
                tc.addProfile(t.getProfile());
        } else if (t.getName().startsWith("=")) {
            if (resource)
                list.add(new TypeRefComponent().setCode("BackboneElement"));
            else
                list.add(new TypeRefComponent().setCode("Element"));
            ToolingExtensions.addStringExtension(ed, "http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name", t.getName().substring(1));
        } else {
            StructureDefinition sd = context.fetchTypeDefinition(t.getName());
            if (sd == null)
                throw new Exception("Unknown type '" + t.getName() + "'");
            TypeRefComponent tc = getTypeComponent(list, sd.getType());
            if (t.getVersioning() != null)
                tc.setVersioning(t.getVersioning());
            if (t.getName().equals("Reference")) {
                if (t.hasProfile())
                    tc.addTargetProfile(t.getProfile());
            } else if (t.hasProfile())
                tc.addProfile(t.getProfile());
        }
    }
    // no duplicates
    for (TypeRefComponent tr1 : list) {
        for (TypeRefComponent tr2 : list) {
            if (tr1 != tr2) {
                if (tr1.getWorkingCode().equals(tr2.getWorkingCode()))
                    throw new Exception("duplicate code " + tr1.getWorkingCode());
            }
        }
    }
    return list;
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) TypeRef(org.hl7.fhir.definitions.model.TypeRef) ArrayList(java.util.ArrayList)

Example 27 with Element

use of org.hl7.fhir.r4b.elementmodel.Element in project kindling by HL7.

the class OldSpreadsheetParser method parseProfileSheet.

private ConstraintStructure parseProfileSheet(Definitions definitions, Profile ap, String n, List<String> namedSheets, boolean published, String usage, List<ValidationMessage> issues, WorkGroup wg, String fmm) throws Exception {
    Sheet sheet;
    ResourceDefn resource = new ResourceDefn();
    sheet = loadSheet(n + "-Inv");
    Map<String, Invariant> invariants = null;
    if (sheet != null) {
        invariants = readInvariants(sheet, n, n + "-Inv");
    } else {
        invariants = new HashMap<String, Invariant>();
    }
    sheet = loadSheet(n);
    if (sheet == null)
        throw new Exception("The StructureDefinition referred to a tab by the name of '" + n + "', but no tab by the name could be found");
    for (int row = 0; row < sheet.rows.size(); row++) {
        ElementDefn e = processLine(resource, sheet, row, invariants, true, ap, row == 0);
        if (e != null)
            for (TypeRef t : e.getTypes()) {
                if (t.getProfile() != null && !t.getName().equals("Extension") && t.getProfile().startsWith("#")) {
                    if (!namedSheets.contains(t.getProfile().substring(1)))
                        namedSheets.add(t.getProfile().substring(1));
                }
            }
    }
    sheet = loadSheet(n + "-Extensions");
    if (sheet != null) {
        int row = 0;
        while (row < sheet.rows.size()) {
            if (sheet.getColumn(row, "Code").startsWith("!"))
                row++;
            else
                row = processExtension(resource.getRoot().getElementByName(definitions, "extensions", true, false), sheet, row, definitions, ap.metadata("extension.uri"), ap, issues, invariants, wg);
        }
    }
    sheet = loadSheet(n + "-Search");
    if (sheet != null) {
        readSearchParams(resource, sheet, true);
    }
    if (invariants != null) {
        for (Invariant inv : invariants.values()) {
            if (Utilities.noString(inv.getContext()))
                throw new Exception("Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " has no context");
            else {
                ElementDefn ed = findContext(resource.getRoot(), inv.getContext(), "Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " Context");
                // TODO: Need to resolve context based on element name, not just path
                if (ed.getName().endsWith("[x]") && !inv.getContext().endsWith("[x]"))
                    inv.setFixedName(inv.getContext().substring(inv.getContext().lastIndexOf(".") + 1));
                ed.getInvariants().put(inv.getId(), inv);
                if (Utilities.noString(inv.getXpath())) {
                    throw new Exception("Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " (" + inv.getEnglish() + ") has no XPath statement");
                } else if (inv.getXpath().contains("\""))
                    throw new Exception("Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " (" + inv.getEnglish() + ") contains a \" character");
            // if (Utilities.noString(inv.getExpression()))
            // throw new Exception("Type "+resource.getRoot().getName()+" Invariant "+inv.getId()+" ("+inv.getEnglish()+") has no Expression statement (in FHIRPath format)");
            }
        }
    }
    resource.getRoot().setProfileName(n);
    if (n.toLowerCase().equals(ap.getId()))
        throw new Exception("Duplicate Profile Name: Package id " + ap.getId() + " and profile id " + n.toLowerCase() + " are the same");
    if (profileIds.containsKey(n.toLowerCase()))
        throw new Exception("Duplicate Profile Name: " + n.toLowerCase() + " in " + ap.getId() + ", already registered in " + profileIds.get(n.toLowerCase()).getOwner());
    ConstraintStructure p = new ConstraintStructure(n.toLowerCase(), resource.getRoot().getProfileName(), resource, ig != null ? ig : definitions.getUsageIG(usage, "Parsing " + name), wg, fmm, Utilities.existsInList(ap.metadata("Experimental"), "y", "Y", "true", "TRUE", "1"));
    p.setOwner(ap.getId());
    profileIds.put(n.toLowerCase(), p);
    return p;
}
Also used : Invariant(org.hl7.fhir.definitions.model.Invariant) TypeRef(org.hl7.fhir.definitions.model.TypeRef) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) ConstraintStructure(org.hl7.fhir.definitions.model.ConstraintStructure) Sheet(org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 28 with Element

use of org.hl7.fhir.r4b.elementmodel.Element in project kindling by HL7.

the class OldSpreadsheetParser method resolveElementReferences.

private void resolveElementReferences(ResourceDefn parent, ElementDefn root) throws Exception {
    for (TypeRef ref : root.getTypes()) {
        if (ref.isElementReference()) {
            ElementDefn referredElement = parent.getRoot().getElementByName(definitions, ref.getName().substring(1), true, false, null);
            if (referredElement == null)
                throw new Exception("Element reference " + ref.getName() + " cannot be found in type " + parent.getName());
            if (referredElement.getDeclaredTypeName() == null)
                throw new Exception("Element reference " + ref.getName() + " in " + parent.getName() + " refers to an anonymous group of elements. Please specify names with the '=<name>' construct in the typename column.");
            ref.setResolvedTypeName(referredElement.getDeclaredTypeName());
        }
    }
    for (ElementDefn element : root.getElements()) {
        resolveElementReferences(parent, element);
    }
}
Also used : TypeRef(org.hl7.fhir.definitions.model.TypeRef) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 29 with Element

use of org.hl7.fhir.r4b.elementmodel.Element in project kindling by HL7.

the class OldSpreadsheetParser method scanNestedTypes.

private void scanNestedTypes(ResourceDefn parent, ElementDefn root, String parentName) throws Exception {
    for (ElementDefn element : root.getElements()) {
        if (element.hasNestedElements()) {
            String nestedTypeName;
            ElementDefn newCompositeType = new ElementDefn();
            // generated name for this nested type
            if (element.typeCode().startsWith("=")) {
                if (isProfile)
                    throw new Exception("Cannot use '=' types in profiles on " + parentName);
                element.setStatedType(element.typeCode().substring(1));
                nestedTypeName = element.typeCode().substring(1);
            } else {
                nestedTypeName = parentName + Utilities.capitalize(element.getName());
            }
            newCompositeType.setAnonymousTypedGroup(true);
            // Add Component to the actually generated name to avoid
            // confusing between the element name and the element's type
            newCompositeType.setName(nestedTypeName + "Component");
            newCompositeType.setDefinition("A nested type in " + parent.getName() + ": " + element.getDefinition());
            newCompositeType.getElements().addAll(element.getElements());
            if (parent.getRoot().getNestedTypes().containsKey(nestedTypeName))
                throw new Exception("Nested type " + nestedTypeName + " already exist in resource " + parent.getName());
            parent.getRoot().getNestedTypes().put(nestedTypeName, newCompositeType);
            // Clear out the name of the local type, so old code
            // will not see a change.
            element.getTypes().clear();
            element.setDeclaredTypeName(newCompositeType.getName());
            scanNestedTypes(parent, element, nestedTypeName);
        }
    }
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 30 with Element

use of org.hl7.fhir.r4b.elementmodel.Element in project kindling by HL7.

the class OldSpreadsheetParser method findContext.

private ElementDefn findContext(ElementDefn root, String pathname, String source) throws Exception {
    String[] path = pathname.split("\\.");
    if (!path[0].equals(root.getName()))
        throw new Exception("Element Path '" + pathname + "' is not legal found at " + source);
    ElementDefn res = root;
    for (int i = 1; i < path.length; i++) {
        String en = path[i];
        if (en.length() == 0)
            throw new Exception("Improper path " + pathname + " found at " + source);
        if (en.charAt(en.length() - 1) == '*')
            throw new Exception("no-list wrapper found at " + source);
        ElementDefn t = res.getElementByName(en, true, definitions, "find context", false);
        if (t == null) {
            throw new Exception("Reference to undefined Element " + pathname + " found at " + source);
        }
        res = t;
    }
    return res;
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)659 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)488 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)486 ArrayList (java.util.ArrayList)240 FHIRException (org.hl7.fhir.exceptions.FHIRException)162 Element (org.hl7.fhir.r5.elementmodel.Element)98 IOException (java.io.IOException)97 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)91 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)84 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)84 Element (org.w3c.dom.Element)74 JsonElement (com.google.gson.JsonElement)62 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)61 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)60 HashSet (java.util.HashSet)53 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)49 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)48 Cell (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell)47 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)46 IndexedElement (org.hl7.fhir.validation.instance.utils.IndexedElement)43