Search in sources :

Example 56 with Rule

use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireValidator method checkIntegerOption.

private void checkIntegerOption(List<ValidationMessage> errors, Element answer, NodeStack stack, QuestionnaireWithContext qSrc, QuestionnaireItemComponent qItem, boolean openChoice) {
    Element v = answer.getNamedChild("valueInteger");
    NodeStack ns = stack.push(v, -1, null, null);
    if (qItem.getAnswerOption().size() > 0) {
        List<IntegerType> list = new ArrayList<IntegerType>();
        for (QuestionnaireItemAnswerOptionComponent components : qItem.getAnswerOption()) {
            try {
                list.add(components.getValueIntegerType());
            } catch (FHIRException e) {
            // If it's the wrong type, just keep going
            }
        }
        if (list.isEmpty() && !openChoice) {
            rule(errors, IssueType.STRUCTURE, v.line(), v.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_NOOPTIONSINTEGER);
        } else {
            boolean found = false;
            for (IntegerType item : list) {
                if (item.getValue() == Integer.parseInt(v.primitiveValue())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                rule(errors, IssueType.STRUCTURE, v.line(), v.col(), stack.getLiteralPath(), found, I18nConstants.QUESTIONNAIRE_QR_ITEM_NOINTEGER, v.primitiveValue());
            }
        }
    } else
        hint(errors, IssueType.STRUCTURE, v.line(), v.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_INTNOOPTIONS);
}
Also used : IntegerType(org.hl7.fhir.r5.model.IntegerType) QuestionnaireItemAnswerOptionComponent(org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemAnswerOptionComponent) Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) NodeStack(org.hl7.fhir.validation.instance.utils.NodeStack) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 57 with Rule

use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireValidator method validateQuestionnaireResponseItemType.

private String validateQuestionnaireResponseItemType(List<ValidationMessage> errors, Element element, NodeStack stack, String... types) {
    List<Element> values = new ArrayList<Element>();
    element.getNamedChildrenWithWildcard("value[x]", values);
    for (int i = 0; i < types.length; i++) {
        if (types[i].equals("text")) {
            types[i] = "string";
        }
    }
    if (values.size() > 0) {
        NodeStack ns = stack.push(values.get(0), -1, null, null);
        CommaSeparatedStringBuilder l = new CommaSeparatedStringBuilder();
        for (String s : types) {
            l.append(s);
            if (values.get(0).getName().equals("value" + Utilities.capitalize(s)))
                return (s);
        }
        if (types.length == 1)
            rule(errors, IssueType.STRUCTURE, values.get(0).line(), values.get(0).col(), ns.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_WRONGTYPE, types[0]);
        else
            rule(errors, IssueType.STRUCTURE, values.get(0).line(), values.get(0).col(), ns.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_WRONGTYPE2, l.toString());
    }
    return null;
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) NodeStack(org.hl7.fhir.validation.instance.utils.NodeStack)

Example 58 with Rule

use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule in project org.hl7.fhir.core by hapifhir.

the class StructureDefinitionValidator method validateProfileTypeOrTarget.

private void validateProfileTypeOrTarget(List<ValidationMessage> errors, Element profile, String code, NodeStack stack, String path) {
    String p = profile.primitiveValue();
    StructureDefinition sd = context.fetchResource(StructureDefinition.class, p);
    if (code.equals("Reference")) {
        if (warning(errors, IssueType.EXCEPTION, stack.getLiteralPath(), sd != null, I18nConstants.SD_ED_TYPE_PROFILE_UNKNOWN, p)) {
            StructureDefinition t = determineBaseType(sd);
            if (t == null) {
                rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), false, I18nConstants.SD_ED_TYPE_PROFILE_NOTYPE, p);
            } else {
                rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), sd.getKind() == StructureDefinitionKind.RESOURCE, I18nConstants.SD_ED_TYPE_PROFILE_WRONG, p, t, code, path);
            }
        }
    } else {
        if (sd == null) {
            sd = getXverExt(errors, stack.getLiteralPath(), profile, p);
        }
        if (warning(errors, IssueType.EXCEPTION, stack.getLiteralPath(), sd != null, I18nConstants.SD_ED_TYPE_PROFILE_UNKNOWN, p)) {
            StructureDefinition t = determineBaseType(sd);
            if (t == null) {
                rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), false, I18nConstants.SD_ED_TYPE_PROFILE_NOTYPE, p);
            } else {
                rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), isInstanceOf(t, code), I18nConstants.SD_ED_TYPE_PROFILE_WRONG, p, t, code, path);
                if (t.getType().equals("Extension")) {
                    boolean isModifierDefinition = checkIsModifierExtension(sd);
                    boolean isModifierContext = path.endsWith(".modifierExtension");
                    if (isModifierDefinition) {
                        rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), isModifierContext, I18nConstants.SD_ED_TYPE_PROFILE_NOT_MODIFIER, p, t, code, path);
                    } else {
                        rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), !isModifierContext, I18nConstants.SD_ED_TYPE_PROFILE_IS_MODIFIER, p, t, code, path);
                    }
                }
            }
        }
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition)

Example 59 with Rule

use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule in project org.hl7.fhir.core by hapifhir.

the class StructureDefinitionValidator method validateStructureDefinition.

public void validateStructureDefinition(List<ValidationMessage> errors, Element src, NodeStack stack) {
    StructureDefinition sd = null;
    try {
        sd = loadAsSD(src);
        List<ElementDefinition> snapshot = sd.getSnapshot().getElement();
        sd.setSnapshot(null);
        StructureDefinition base = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition());
        if (warning(errors, IssueType.NOTFOUND, stack.getLiteralPath(), base != null, I18nConstants.UNABLE_TO_FIND_BASE__FOR_, sd.getBaseDefinition(), "StructureDefinition, so can't check the differential")) {
            if (rule(errors, IssueType.NOTFOUND, stack.getLiteralPath(), sd.hasDerivation(), I18nConstants.SD_MUST_HAVE_DERIVATION, sd.getUrl())) {
                if (sd.getDerivation() == TypeDerivationRule.CONSTRAINT) {
                    List<ValidationMessage> msgs = new ArrayList<>();
                    ProfileUtilities pu = new ProfileUtilities(context, msgs, null);
                    pu.setXver(xverManager);
                    pu.generateSnapshot(base, sd, sd.getUrl(), "http://hl7.org/fhir/R4/", sd.getName());
                    if (msgs.size() > 0) {
                        for (ValidationMessage msg : msgs) {
                            // we need to set the location for the context
                            String loc = msg.getLocation();
                            if (loc.contains("#")) {
                                msg.setLocation(stack.getLiteralPath() + ".differential.element.where(path = '" + loc.substring(loc.indexOf("#") + 1) + "')");
                            } else {
                                msg.setLocation(stack.getLiteralPath());
                            }
                            errors.add(msg);
                        }
                    }
                    if (!snapshot.isEmpty() && wantCheckSnapshotUnchanged) {
                        int was = snapshot.size();
                        int is = sd.getSnapshot().getElement().size();
                        rule(errors, IssueType.NOTFOUND, stack.getLiteralPath(), was == is, I18nConstants.SNAPSHOT_EXISTING_PROBLEM, was, is);
                    }
                }
            }
            if ("constraint".equals(src.getChildValue("derivation"))) {
                rule(errors, IssueType.NOTFOUND, stack.getLiteralPath(), base.getKindElement().primitiveValue().equals(src.getChildValue("kind")), I18nConstants.SD_DERIVATION_KIND_MISMATCH, base.getKindElement().primitiveValue(), src.getChildValue("kind"));
            }
        }
    } catch (FHIRException | IOException e) {
        rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), false, I18nConstants.ERROR_GENERATING_SNAPSHOT, e.getMessage());
    }
    List<Element> differentials = src.getChildrenByName("differential");
    List<Element> snapshots = src.getChildrenByName("snapshot");
    for (Element differential : differentials) {
        validateElementList(errors, differential, stack.push(differential, -1, null, null), false, snapshots.size() > 0, sd);
    }
    for (Element snapshot : snapshots) {
        validateElementList(errors, snapshot, stack.push(snapshot, -1, null, null), true, true, sd);
    }
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Example 60 with Rule

use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule in project org.hl7.fhir.core by hapifhir.

the class StructureDefinitionValidator method validateTypeProfile.

private void validateTypeProfile(List<ValidationMessage> errors, Element profile, String code, NodeStack stack, String path) {
    String p = profile.primitiveValue();
    StructureDefinition sd = context.fetchResource(StructureDefinition.class, p);
    if (sd == null) {
        sd = getXverExt(errors, stack.getLiteralPath(), profile, p);
    }
    if (warning(errors, IssueType.EXCEPTION, stack.getLiteralPath(), sd != null, I18nConstants.SD_ED_TYPE_PROFILE_UNKNOWN, p)) {
        StructureDefinition t = determineBaseType(sd);
        if (t == null) {
            rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), false, I18nConstants.SD_ED_TYPE_PROFILE_NOTYPE, p);
        } else if (!isInstanceOf(t, code)) {
            rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), false, I18nConstants.SD_ED_TYPE_PROFILE_WRONG, p, t, code, path);
        } else {
            if (t.getType().equals("Extension")) {
                boolean isModifierDefinition = checkIsModifierExtension(sd);
                boolean isModifierContext = path.endsWith(".modifierExtension");
                if (isModifierDefinition) {
                    rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), isModifierContext, I18nConstants.SD_ED_TYPE_PROFILE_NOT_MODIFIER, p, t, code, path);
                } else {
                    rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), !isModifierContext, I18nConstants.SD_ED_TYPE_PROFILE_IS_MODIFIER, p, t, code, path);
                }
            }
        }
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)76 ArrayList (java.util.ArrayList)46 Element (org.hl7.fhir.r5.elementmodel.Element)38 IOException (java.io.IOException)28 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)26 NodeStack (org.hl7.fhir.validation.instance.utils.NodeStack)23 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)20 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)20 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)19 IndexedElement (org.hl7.fhir.validation.instance.utils.IndexedElement)17 NotImplementedException (org.apache.commons.lang3.NotImplementedException)16 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)14 FHIRLexerException (org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)14 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)13 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)13 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)13 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)13 ValueSet (org.hl7.fhir.r5.model.ValueSet)13 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)13 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)12