Search in sources :

Example 16 with IndexOf

use of org.hl7.elm.r1.IndexOf in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method getChildList.

public List<ElementDefinition> getChildList(StructureDefinition profile, String path, String id, boolean diff, boolean refs) {
    List<ElementDefinition> res = new ArrayList<ElementDefinition>();
    boolean capturing = id == null;
    if (id == null && !path.contains("."))
        capturing = true;
    List<ElementDefinition> list = diff ? profile.getDifferential().getElement() : profile.getSnapshot().getElement();
    for (ElementDefinition e : list) {
        if (e == null)
            throw new Error(context.formatMessage(I18nConstants.ELEMENT__NULL_, profile.getUrl()));
        if (e.getId() == null)
            throw new Error(context.formatMessage(I18nConstants.ELEMENT_ID__NULL__ON_, e.toString(), profile.getUrl()));
        if (!capturing && id != null && e.getId().equals(id)) {
            capturing = true;
        }
        // If our element is a slice, stop capturing children as soon as we see the next slice
        if (capturing && e.hasId() && id != null && !e.getId().equals(id) && e.getPath().equals(path))
            break;
        if (capturing) {
            String p = e.getPath();
            if (refs && !Utilities.noString(e.getContentReference()) && path.startsWith(p)) {
                if (path.length() > p.length()) {
                    return getChildList(profile, e.getContentReference() + "." + path.substring(p.length() + 1), null, diff);
                } else if (e.getContentReference().startsWith("#")) {
                    return getChildList(profile, e.getContentReference().substring(1), null, diff);
                } else if (e.getContentReference().contains("#")) {
                    String url = e.getContentReference().substring(0, e.getContentReference().indexOf("#"));
                    StructureDefinition sd = context.fetchResource(StructureDefinition.class, url);
                    if (sd == null) {
                        throw new DefinitionException("Unable to find Structure " + url);
                    }
                    return getChildList(sd, e.getContentReference().substring(e.getContentReference().indexOf("#") + 1), null, diff);
                } else {
                    return getChildList(profile, e.getContentReference(), null, diff);
                }
            } else if (p.startsWith(path + ".") && !p.equals(path)) {
                String tail = p.substring(path.length() + 1);
                if (!tail.contains(".")) {
                    res.add(e);
                }
            }
        }
    }
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) ArrayList(java.util.ArrayList) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) ElementDefinition(org.hl7.fhir.r4b.model.ElementDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 17 with IndexOf

use of org.hl7.elm.r1.IndexOf in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method closeChildren.

private void closeChildren(StructureDefinition base, ElementDefinition edb, StructureDefinition derived, ElementDefinition edm) {
    String path = edb.getPath() + ".";
    int baseStart = base.getSnapshot().getElement().indexOf(edb);
    int baseEnd = findEnd(base.getSnapshot().getElement(), edb, baseStart + 1);
    int diffStart = derived.getDifferential().getElement().indexOf(edm);
    int diffEnd = findEnd(derived.getDifferential().getElement(), edm, diffStart + 1);
    for (int cBase = baseStart; cBase < baseEnd; cBase++) {
        ElementDefinition edBase = base.getSnapshot().getElement().get(cBase);
        if (isImmediateChild(edBase, edb)) {
            ElementDefinition edMatch = getMatchInDerived(edBase, derived.getDifferential().getElement(), diffStart, diffEnd);
            if (edMatch == null) {
                ElementDefinition edd = derived.getDifferential().addElement();
                edd.setPath(edBase.getPath());
                edd.setMax("0");
            } else {
                closeChildren(base, edBase, derived, edMatch);
            }
        }
    }
}
Also used : ElementDefinition(org.hl7.fhir.r4b.model.ElementDefinition)

Example 18 with IndexOf

use of org.hl7.elm.r1.IndexOf in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method extensionIsComplex.

private boolean extensionIsComplex(String value) {
    if (value.contains("#")) {
        StructureDefinition ext = context.fetchResource(StructureDefinition.class, value.substring(0, value.indexOf("#")));
        if (ext == null)
            return false;
        String tail = value.substring(value.indexOf("#") + 1);
        ElementDefinition ed = null;
        for (ElementDefinition ted : ext.getSnapshot().getElement()) {
            if (tail.equals(ted.getSliceName())) {
                ed = ted;
                break;
            }
        }
        if (ed == null)
            return false;
        int i = ext.getSnapshot().getElement().indexOf(ed);
        int j = i + 1;
        while (j < ext.getSnapshot().getElement().size() && !ext.getSnapshot().getElement().get(j).getPath().equals(ed.getPath())) j++;
        return j - i > 5;
    } else {
        StructureDefinition ext = context.fetchResource(StructureDefinition.class, value);
        return ext != null && ext.getSnapshot().getElement().size() > 5;
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Example 19 with IndexOf

use of org.hl7.elm.r1.IndexOf in project org.hl7.fhir.core by hapifhir.

the class R3TEchnicalCorrectionProcessor method execute.

private void execute(String src, String packageRoot) throws FileNotFoundException, IOException {
    System.out.println("Loading resources from " + src);
    List<Resource> resources = new ArrayList<>();
    Map<String, Resource> definitions = new HashMap<>();
    for (File f : new File(src).listFiles()) {
        if (f.getName().endsWith(".xml") && !(f.getName().endsWith("warnings.xml") || f.getName().endsWith(".diff.xml"))) {
            try {
                Resource r = new XmlParser().parse(new FileInputStream(f));
                if (f.getName().contains("canonical")) {
                    resources.add(r);
                }
                if (Utilities.existsInList(f.getName(), "conceptmaps.xml", "dataelements.xml", "extension-definitions.xml", "profiles-others.xml", "profiles-resources.xml", "profiles-types.xml", "search-parameters.xml", "v2-tables.xml", "v3-codesystems.xml", "valuesets.xml")) {
                    definitions.put(f.getName(), r);
                }
                r.setUserData("path", f.getName().substring(0, f.getName().indexOf(".")));
            // FileUtils.copyFile(f, new File(f.getAbsolutePath()+"1"));
            // FileUtils.copyFile(f, new File(f.getAbsolutePath()+"2"));
            } catch (Exception e) {
                System.out.println("Unable to load " + f.getName() + ": " + e.getMessage());
            }
        }
        if (f.getName().endsWith(".json") && !(f.getName().endsWith("schema.json") || f.getName().endsWith(".diff.json"))) {
            try {
            // new JsonParser().parse(new FileInputStream(f));
            // FileUtils.copyFile(f, new File(f.getAbsolutePath()+"1"));
            // FileUtils.copyFile(f, new File(f.getAbsolutePath()+"2"));
            } catch (Exception e) {
                System.out.println("Unable to load " + f.getName() + ": " + e.getMessage());
            }
        }
    }
    System.out.println(Integer.toString(resources.size()) + " resources");
    System.out.println(Integer.toString(definitions.size()) + " resources");
    produceExamplesXml(resources, src);
    produceDefinitionsXml(definitions, src);
    produceExamplesJson(resources, src);
    produceDefinitionsJson(definitions, src);
    for (Resource r : definitions.values()) {
        if (r instanceof Bundle) {
            Bundle bnd = (Bundle) r;
            for (BundleEntryComponent be : bnd.getEntry()) {
                resources.add(be.getResource());
            }
        }
    }
    extractToPackageMaster(resources, packageRoot);
    System.out.println("Done");
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) HashMap(java.util.HashMap) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.model.Resource) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) ArrayList(java.util.ArrayList) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(java.text.ParseException)

Example 20 with IndexOf

use of org.hl7.elm.r1.IndexOf in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method getChildList.

public List<ElementDefinition> getChildList(StructureDefinition profile, String path, String id, boolean diff, boolean refs) {
    List<ElementDefinition> res = new ArrayList<ElementDefinition>();
    boolean capturing = id == null;
    if (id == null && !path.contains("."))
        capturing = true;
    List<ElementDefinition> list = diff ? profile.getDifferential().getElement() : profile.getSnapshot().getElement();
    for (ElementDefinition e : list) {
        if (e == null)
            throw new Error(context.formatMessage(I18nConstants.ELEMENT__NULL_, profile.getUrl()));
        if (e.getId() == null)
            throw new Error(context.formatMessage(I18nConstants.ELEMENT_ID__NULL__ON_, e.toString(), profile.getUrl()));
        if (!capturing && id != null && e.getId().equals(id)) {
            capturing = true;
        }
        // If our element is a slice, stop capturing children as soon as we see the next slice
        if (capturing && e.hasId() && id != null && !e.getId().equals(id) && e.getPath().equals(path))
            break;
        if (capturing) {
            String p = e.getPath();
            if (refs && !Utilities.noString(e.getContentReference()) && path.startsWith(p)) {
                if (path.length() > p.length()) {
                    return getChildList(profile, e.getContentReference() + "." + path.substring(p.length() + 1), null, diff);
                } else if (e.getContentReference().startsWith("#")) {
                    return getChildList(profile, e.getContentReference().substring(1), null, diff);
                } else if (e.getContentReference().contains("#")) {
                    String url = e.getContentReference().substring(0, e.getContentReference().indexOf("#"));
                    StructureDefinition sd = context.fetchResource(StructureDefinition.class, url);
                    if (sd == null) {
                        throw new DefinitionException("Unable to find Structure " + url);
                    }
                    return getChildList(sd, e.getContentReference().substring(e.getContentReference().indexOf("#") + 1), null, diff);
                } else {
                    return getChildList(profile, e.getContentReference(), null, diff);
                }
            } else if (p.startsWith(path + ".") && !p.equals(path)) {
                String tail = p.substring(path.length() + 1);
                if (!tail.contains(".")) {
                    res.add(e);
                }
            }
        }
    }
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ArrayList(java.util.ArrayList) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Aggregations

DefinitionException (org.hl7.fhir.exceptions.DefinitionException)12 ArrayList (java.util.ArrayList)11 FHIRException (org.hl7.fhir.exceptions.FHIRException)11 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)9 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)8 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)7 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)6 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)6 List (java.util.List)5 ElementDefinition (org.hl7.fhir.r4.model.ElementDefinition)5 ElementDefinition (org.hl7.fhir.r4b.model.ElementDefinition)5 File (java.io.File)4 NotImplementedException (org.apache.commons.lang3.NotImplementedException)4 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)4 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)4 URISyntaxException (java.net.URISyntaxException)3 ParseException (java.text.ParseException)3 IndexOf (io.atlasmap.v2.IndexOf)2