Search in sources :

Example 61 with ValidationMessage

use of org.hl7.fhir.utilities.validation.ValidationMessage in project org.hl7.fhir.core by hapifhir.

the class BaseValidator method loadFoundResource.

protected Resource loadFoundResource(List<ValidationMessage> errors, String path, Element resource, Class<? extends Resource> class1) throws FHIRException {
    try {
        FhirPublication v = FhirPublication.fromCode(context.getVersion());
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        new JsonParser(context).compose(resource, bs, OutputStyle.NORMAL, resource.getIdBase());
        byte[] json = bs.toByteArray();
        Resource r5 = null;
        switch(v) {
            case DSTU1:
                rule(errors, IssueType.INVALID, resource.line(), resource.col(), path, false, I18nConstants.UNSUPPORTED_VERSION_R1, resource.getIdBase());
                // this can't happen
                return null;
            case DSTU2:
                org.hl7.fhir.dstu2.model.Resource r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(json);
                r5 = VersionConvertorFactory_10_50.convertResource(r2);
                break;
            case DSTU2016May:
                org.hl7.fhir.dstu2016may.model.Resource r2a = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(json);
                r5 = VersionConvertorFactory_14_50.convertResource(r2a);
                break;
            case STU3:
                org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(json);
                r5 = VersionConvertorFactory_30_50.convertResource(r3);
                break;
            case R4:
                org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(json);
                r5 = VersionConvertorFactory_40_50.convertResource(r4);
                break;
            case R5:
                r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(json);
                break;
            default:
                // this can't happen
                return null;
        }
        if (class1.isInstance(r5))
            return (Resource) r5;
        else {
            rule(errors, IssueType.INVALID, resource.line(), resource.col(), path, false, I18nConstants.REFERENCE_REF_WRONGTARGET_LOAD, resource.getIdBase(), class1.toString(), r5.fhirType());
            return null;
        }
    } catch (IOException e) {
        throw new FHIRException(e);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FhirPublication(org.hl7.fhir.utilities.FhirPublication) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) JsonParser(org.hl7.fhir.r5.elementmodel.JsonParser)

Example 62 with ValidationMessage

use of org.hl7.fhir.utilities.validation.ValidationMessage in project org.hl7.fhir.core by hapifhir.

the class ValidationService method validateSources.

public ValidationResponse validateSources(ValidationRequest request) throws Exception {
    if (request.getCliContext().getSv() == null) {
        String sv = determineVersion(request.getCliContext(), request.sessionId);
        request.getCliContext().setSv(sv);
    }
    String definitions = VersionUtilities.packageForVersion(request.getCliContext().getSv()) + "#" + VersionUtilities.getCurrentVersion(request.getCliContext().getSv());
    String sessionId = initializeValidator(request.getCliContext(), definitions, new TimeTracker(), request.sessionId);
    ValidationEngine validator = sessionCache.fetchSessionValidatorEngine(sessionId);
    if (request.getCliContext().getProfiles().size() > 0) {
        System.out.println("  .. validate " + request.listSourceFiles() + " against " + request.getCliContext().getProfiles().toString());
    } else {
        System.out.println("  .. validate " + request.listSourceFiles());
    }
    ValidationResponse response = new ValidationResponse().setSessionId(sessionId);
    for (FileInfo fp : request.getFilesToValidate()) {
        List<ValidationMessage> messages = new ArrayList<>();
        validator.validate(fp.getFileContent().getBytes(), Manager.FhirFormat.getFhirFormat(fp.getFileType()), request.getCliContext().getProfiles(), messages);
        ValidationOutcome outcome = new ValidationOutcome().setFileInfo(fp);
        messages.forEach(outcome::addMessage);
        response.addOutcome(outcome);
    }
    System.out.println("  Max Memory: " + Runtime.getRuntime().maxMemory());
    return response;
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) TimeTracker(org.hl7.fhir.utilities.TimeTracker) ArrayList(java.util.ArrayList) ValidationEngine(org.hl7.fhir.validation.ValidationEngine)

Example 63 with ValidationMessage

use of org.hl7.fhir.utilities.validation.ValidationMessage in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method validate.

// testing entry point
public OperationOutcome validate(FhirFormat format, InputStream stream, List<String> profiles) throws FHIRException, IOException, EOperationOutcome {
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    InstanceValidator validator = getValidator(format);
    validator.validate(null, messages, stream, format, asSdList(profiles));
    return ValidatorUtils.messagesToOutcome(messages, context, fhirPathEngine);
}
Also used : InstanceValidator(org.hl7.fhir.validation.instance.InstanceValidator) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

Example 64 with ValidationMessage

use of org.hl7.fhir.utilities.validation.ValidationMessage in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method validate.

public OperationOutcome validate(byte[] source, FhirFormat cntType, List<String> profiles, List<ValidationMessage> messages) throws FHIRException, IOException, EOperationOutcome {
    InstanceValidator validator = getValidator(cntType);
    validator.validate(null, messages, new ByteArrayInputStream(source), cntType, asSdList(profiles));
    return ValidatorUtils.messagesToOutcome(messages, context, fhirPathEngine);
}
Also used : InstanceValidator(org.hl7.fhir.validation.instance.InstanceValidator)

Example 65 with ValidationMessage

use of org.hl7.fhir.utilities.validation.ValidationMessage in project org.hl7.fhir.core by hapifhir.

the class DefaultRenderer method render.

@Override
public void render(OperationOutcome oo) {
    int error = 0;
    int warn = 0;
    int info = 0;
    String file = ToolingExtensions.readStringExtension(oo, ToolingExtensions.EXT_OO_FILE);
    for (OperationOutcome.OperationOutcomeIssueComponent issue : oo.getIssue()) {
        if (issue.getSeverity() == OperationOutcome.IssueSeverity.FATAL || issue.getSeverity() == OperationOutcome.IssueSeverity.ERROR)
            error++;
        else if (issue.getSeverity() == OperationOutcome.IssueSeverity.WARNING)
            warn++;
        else
            info++;
    }
    if (moreThanOne) {
        dst.print("-- ");
        dst.print(file);
        dst.print(" --");
        dst.println(Utilities.padLeft("", '-', Integer.max(38, file.length() + 6)));
    }
    dst.println((error == 0 ? "Success" : "*FAILURE*") + ": " + Integer.toString(error) + " errors, " + Integer.toString(warn) + " warnings, " + Integer.toString(info) + " notes");
    for (OperationOutcome.OperationOutcomeIssueComponent issue : oo.getIssue()) {
        dst.println(getIssueSummary(issue));
        ValidationMessage vm = (ValidationMessage) issue.getUserData("source.msg");
        if (vm != null && vm.sliceText != null && (crumbTrails || vm.isCriticalSignpost())) {
            for (String s : vm.sliceText) {
                dst.println("    slice info: " + s);
            }
        }
    }
    if (moreThanOne) {
        dst.print("---");
        dst.print(Utilities.padLeft("", '-', file.length()));
        dst.print("---");
        dst.println(Utilities.padLeft("", '-', Integer.max(38, file.length() + 6)));
        dst.println();
    }
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) OperationOutcome(org.hl7.fhir.r5.model.OperationOutcome)

Aggregations

ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)170 ArrayList (java.util.ArrayList)114 FHIRException (org.hl7.fhir.exceptions.FHIRException)92 Element (org.hl7.fhir.r5.elementmodel.Element)60 IOException (java.io.IOException)46 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)44 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)38 NodeStack (org.hl7.fhir.validation.instance.utils.NodeStack)30 IndexedElement (org.hl7.fhir.validation.instance.utils.IndexedElement)28 NotImplementedException (org.apache.commons.lang3.NotImplementedException)21 ProfileUtilities (org.hl7.fhir.r5.conformance.ProfileUtilities)20 ValueSet (org.hl7.fhir.r5.model.ValueSet)20 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)19 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)19 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)18 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)18 FileNotFoundException (java.io.FileNotFoundException)17 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)16 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)16 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)16