Search in sources :

Example 11 with DefinitionException

use of org.hl7.fhir.exceptions.DefinitionException in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method getChildMap.

public static List<ElementDefinition> getChildMap(StructureDefinition profile, ElementDefinition element) throws DefinitionException {
    if (element.getContentReference() != null) {
        for (ElementDefinition e : profile.getSnapshot().getElement()) {
            if (element.getContentReference().equals("#" + e.getId()))
                return getChildMap(profile, e);
        }
        throw new DefinitionException("Unable to resolve name reference " + element.getContentReference() + " at path " + element.getPath());
    } else {
        List<ElementDefinition> res = new ArrayList<ElementDefinition>();
        List<ElementDefinition> elements = profile.getSnapshot().getElement();
        String path = element.getPath();
        for (int index = elements.indexOf(element) + 1; index < elements.size(); index++) {
            ElementDefinition e = elements.get(index);
            if (e.getPath().startsWith(path + ".")) {
                // We only want direct children, not all descendants
                if (!e.getPath().substring(path.length() + 1).contains("."))
                    res.add(e);
            } else
                break;
        }
        return res;
    }
}
Also used : ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 12 with DefinitionException

use of org.hl7.fhir.exceptions.DefinitionException in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method generateSchematrons.

// generate schematrons for the rules in a structure definition
public void generateSchematrons(OutputStream dest, StructureDefinition structure) throws IOException, DefinitionException {
    if (structure.getDerivation() != TypeDerivationRule.CONSTRAINT)
        throw new DefinitionException("not the right kind of structure to generate schematrons for");
    if (!structure.hasSnapshot())
        throw new DefinitionException("needs a snapshot");
    StructureDefinition base = context.fetchResource(StructureDefinition.class, structure.getBaseDefinition());
    SchematronWriter sch = new SchematronWriter(dest, SchematronType.PROFILE, base.getName());
    ElementDefinition ed = structure.getSnapshot().getElement().get(0);
    generateForChildren(sch, "f:" + ed.getPath(), ed, structure, base);
    sch.dump();
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) SchematronWriter(org.hl7.fhir.utilities.xml.SchematronWriter) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition)

Example 13 with DefinitionException

use of org.hl7.fhir.exceptions.DefinitionException in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContext method seeProfile.

public void seeProfile(String url, StructureDefinition p) throws FHIRException {
    if (Utilities.noString(url))
        url = p.getUrl();
    if (!p.hasSnapshot() && p.getKind() != StructureDefinitionKind.LOGICAL) {
        if (!p.hasBaseDefinition())
            throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + ") has no base and no snapshot");
        StructureDefinition sd = fetchResource(StructureDefinition.class, p.getBaseDefinition());
        if (sd == null)
            throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + ") base " + p.getBaseDefinition() + " could not be resolved");
        List<ValidationMessage> msgs = new ArrayList<ValidationMessage>();
        List<String> errors = new ArrayList<String>();
        ProfileUtilities pu = new ProfileUtilities(this, msgs, this);
        pu.sortDifferential(sd, p, url, errors);
        for (String err : errors) msgs.add(new ValidationMessage(Source.ProfileValidator, IssueType.EXCEPTION, p.getUserString("path"), "Error sorting Differential: " + err, ValidationMessage.IssueSeverity.ERROR));
        pu.generateSnapshot(sd, p, p.getUrl(), p.getName());
        for (ValidationMessage msg : msgs) {
            if (msg.getLevel() == ValidationMessage.IssueSeverity.ERROR || msg.getLevel() == ValidationMessage.IssueSeverity.FATAL)
                throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + "). Error generating snapshot: " + msg.getMessage());
        }
        if (!p.hasSnapshot())
            throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + "). Error generating snapshot");
        pu = null;
    }
    if (structures.containsKey(p.getUrl()) && !allowLoadingDuplicates)
        throw new DefinitionException("Duplicate structures " + p.getUrl());
    structures.put(p.getId(), p);
    structures.put(p.getUrl(), p);
    if (!p.getUrl().equals(url))
        structures.put(url, p);
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.dstu3.conformance.ProfileUtilities) ArrayList(java.util.ArrayList) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 14 with DefinitionException

use of org.hl7.fhir.exceptions.DefinitionException in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContext method seeValueSet.

public void seeValueSet(String url, ValueSet vs) throws DefinitionException {
    if (Utilities.noString(url))
        url = vs.getUrl();
    if (valueSets.containsKey(vs.getUrl()) && !allowLoadingDuplicates)
        throw new DefinitionException("Duplicate Profile " + vs.getUrl());
    valueSets.put(vs.getId(), vs);
    valueSets.put(vs.getUrl(), vs);
    if (!vs.getUrl().equals(url))
        valueSets.put(url, vs);
}
Also used : DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 15 with DefinitionException

use of org.hl7.fhir.exceptions.DefinitionException in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method compareChildren.

private boolean compareChildren(ElementDefinition ed, ProfileComparison outcome, String path, DefinitionNavigator left, DefinitionNavigator right) throws DefinitionException, IOException, FHIRFormatError {
    List<DefinitionNavigator> lc = left.children();
    List<DefinitionNavigator> rc = right.children();
    // walk into it
    if (lc.isEmpty() && !rc.isEmpty() && right.current().getType().size() == 1 && left.hasTypeChildren(right.current().getType().get(0)))
        lc = left.childrenFromType(right.current().getType().get(0));
    if (rc.isEmpty() && !lc.isEmpty() && left.current().getType().size() == 1 && right.hasTypeChildren(left.current().getType().get(0)))
        rc = right.childrenFromType(left.current().getType().get(0));
    if (lc.size() != rc.size()) {
        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Different number of children at " + path + " (" + Integer.toString(lc.size()) + "/" + Integer.toString(rc.size()) + ")", ValidationMessage.IssueSeverity.ERROR));
        status(ed, ProfileUtilities.STATUS_ERROR);
        return false;
    } else {
        for (int i = 0; i < lc.size(); i++) {
            DefinitionNavigator l = lc.get(i);
            DefinitionNavigator r = rc.get(i);
            String cpath = comparePaths(l.path(), r.path(), path, l.nameTail(), r.nameTail());
            if (cpath != null) {
                if (!compareElements(outcome, cpath, l, r))
                    return false;
            } else {
                outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Different path at " + path + "[" + Integer.toString(i) + "] (" + l.path() + "/" + r.path() + ")", ValidationMessage.IssueSeverity.ERROR));
                status(ed, ProfileUtilities.STATUS_ERROR);
                return false;
            }
        }
    }
    return true;
}
Also used : DefinitionNavigator(org.hl7.fhir.dstu3.utils.DefinitionNavigator) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

Aggregations

DefinitionException (org.hl7.fhir.exceptions.DefinitionException)186 ArrayList (java.util.ArrayList)99 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)91 FHIRException (org.hl7.fhir.exceptions.FHIRException)50 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)38 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)32 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)29 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)29 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)28 IOException (java.io.IOException)27 HashMap (java.util.HashMap)24 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)23 ElementDefinition (org.hl7.fhir.r4b.model.ElementDefinition)20 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)17 StructureDefinition (org.hl7.fhir.r4.model.StructureDefinition)17 ElementDefinition (org.hl7.fhir.r4.model.ElementDefinition)16 XhtmlParser (org.hl7.fhir.utilities.xhtml.XhtmlParser)16 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)14 StructureDefinition (org.hl7.fhir.dstu2016may.model.StructureDefinition)13 StructureDefinition (org.hl7.fhir.dstu2.model.StructureDefinition)12