Search in sources :

Example 1 with FhirPublication

use of org.hl7.fhir.utilities.FhirPublication 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 2 with FhirPublication

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

the class MeasureValidator method loadMeasure.

private Measure loadMeasure(Element resource, String id) throws FHIRException {
    try {
        for (Element contained : resource.getChildren("contained")) {
            if (contained.getIdBase().equals(id)) {
                FhirPublication v = FhirPublication.fromCode(context.getVersion());
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                new JsonParser(context).compose(contained, bs, OutputStyle.NORMAL, id);
                byte[] json = bs.toByteArray();
                switch(v) {
                    case DSTU1:
                        throw new FHIRException(context.formatMessage(I18nConstants.UNSUPPORTED_VERSION_R1));
                    case DSTU2:
                        throw new FHIRException(context.formatMessage(I18nConstants.UNSUPPORTED_VERSION_R2));
                    case DSTU2016May:
                        throw new FHIRException(context.formatMessage(I18nConstants.UNSUPPORTED_VERSION_R2B));
                    case STU3:
                        org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(json);
                        Resource r5 = VersionConvertorFactory_30_50.convertResource(r3);
                        if (r5 instanceof Measure)
                            return (Measure) r5;
                        else
                            return null;
                    case R4:
                        org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(json);
                        r5 = VersionConvertorFactory_40_50.convertResource(r4);
                        if (r5 instanceof Measure)
                            return (Measure) r5;
                        else
                            return null;
                    case R5:
                        r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(json);
                        if (r5 instanceof Measure)
                            return (Measure) r5;
                        else
                            return null;
                }
            }
        }
        return null;
    } catch (IOException e) {
        throw new FHIRException(e);
    }
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element) Resource(org.hl7.fhir.r5.model.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FhirPublication(org.hl7.fhir.utilities.FhirPublication) Measure(org.hl7.fhir.r5.model.Measure) JsonParser(org.hl7.fhir.r5.elementmodel.JsonParser)

Example 3 with FhirPublication

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

the class TestUtilities method getValidationEngine.

public static ValidationEngine getValidationEngine(java.lang.String src, java.lang.String txServer, String txLog, FhirPublication version, boolean canRunWithoutTerminologyServer, java.lang.String vString) throws Exception {
    final ValidationEngine validationEngine = new ValidationEngine.ValidationEngineBuilder().withCanRunWithoutTerminologyServer(canRunWithoutTerminologyServer).withVersion(vString).withUserAgent(TestConstants.USER_AGENT).withTerminologyCachePath(Paths.get(TestConstants.TX_CACHE, vString).toString()).withTxServer(txServer, txLog, version).fromSource(src);
    TerminologyCache.setCacheErrors(true);
    return validationEngine;
}
Also used : ValidationEngine(org.hl7.fhir.validation.ValidationEngine)

Example 4 with FhirPublication

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

the class TestUtilities method getValidationEngine.

public static ValidationEngine getValidationEngine(java.lang.String src, java.lang.String txServer, FhirPublication version, java.lang.String vString) throws Exception {
    final ValidationEngine validationEngine = new ValidationEngine.ValidationEngineBuilder().withVersion(vString).withUserAgent(TestConstants.USER_AGENT).withTerminologyCachePath(Paths.get(TestConstants.TX_CACHE, vString).toString()).withTxServer(txServer, TestConstants.TX_CACHE_LOG, version).fromSource(src);
    TerminologyCache.setCacheErrors(true);
    return validationEngine;
}
Also used : ValidationEngine(org.hl7.fhir.validation.ValidationEngine)

Example 5 with FhirPublication

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

the class ValidationService method initializeValidator.

public String initializeValidator(CliContext cliContext, String definitions, TimeTracker tt, String sessionId) throws Exception {
    tt.milestone();
    if (!sessionCache.sessionExists(sessionId)) {
        if (sessionId != null) {
            System.out.println("No such cached session exists for session id " + sessionId + ", re-instantiating validator.");
        }
        System.out.print("  Load FHIR v" + cliContext.getSv() + " from " + definitions);
        ValidationEngine validator = new ValidationEngine.ValidationEngineBuilder().withVersion(cliContext.getSv()).withTimeTracker(tt).withUserAgent("fhir/validator").fromSource(definitions);
        sessionId = sessionCache.cacheSession(validator);
        FhirPublication ver = FhirPublication.fromCode(cliContext.getSv());
        IgLoader igLoader = new IgLoader(validator.getPcm(), validator.getContext(), validator.getVersion(), validator.isDebug());
        System.out.println(" - " + validator.getContext().countAllCaches() + " resources (" + tt.milestone() + ")");
        igLoader.loadIg(validator.getIgs(), validator.getBinaries(), "hl7.terminology", false);
        System.out.print("  Terminology server " + cliContext.getTxServer());
        String txver = validator.setTerminologyServer(cliContext.getTxServer(), cliContext.getTxLog(), ver);
        System.out.println(" - Version " + txver + " (" + tt.milestone() + ")");
        validator.setDebug(cliContext.isDoDebug());
        validator.getContext().setLogger(new SystemOutLoggingService(cliContext.isDoDebug()));
        for (String src : cliContext.getIgs()) {
            igLoader.loadIg(validator.getIgs(), validator.getBinaries(), src, cliContext.isRecursive());
        }
        System.out.print("  Get set... ");
        validator.setQuestionnaireMode(cliContext.getQuestionnaireMode());
        validator.setLevel(cliContext.getLevel());
        validator.setDoNative(cliContext.isDoNative());
        validator.setHintAboutNonMustSupport(cliContext.isHintAboutNonMustSupport());
        for (String s : cliContext.getExtensions()) {
            if ("*".equals(s)) {
                validator.setAnyExtensionsAllowed(true);
            } else {
                validator.getExtensionDomains().add(s);
            }
        }
        validator.setLanguage(cliContext.getLang());
        validator.setLocale(cliContext.getLocale());
        validator.setSnomedExtension(cliContext.getSnomedCTCode());
        validator.setAssumeValidRestReferences(cliContext.isAssumeValidRestReferences());
        validator.setShowMessagesFromReferences(cliContext.isShowMessagesFromReferences());
        validator.setNoExtensibleBindingMessages(cliContext.isNoExtensibleBindingMessages());
        validator.setNoUnicodeBiDiControlChars(cliContext.isNoUnicodeBiDiControlChars());
        validator.setNoInvariantChecks(cliContext.isNoInvariants());
        validator.setWantInvariantInMessage(cliContext.isWantInvariantsInMessages());
        validator.setSecurityChecks(cliContext.isSecurityChecks());
        validator.setCrumbTrails(cliContext.isCrumbTrails());
        validator.setShowTimes(cliContext.isShowTimes());
        validator.setAllowExampleUrls(cliContext.isAllowExampleUrls());
        StandAloneValidatorFetcher fetcher = new StandAloneValidatorFetcher(validator.getPcm(), validator.getContext(), validator);
        validator.setFetcher(fetcher);
        validator.getContext().setLocator(fetcher);
        validator.getBundleValidationRules().addAll(cliContext.getBundleValidationRules());
        TerminologyCache.setNoCaching(cliContext.isNoInternalCaching());
        // generate any missing snapshots
        validator.prepare();
        System.out.println(" go (" + tt.milestone() + ")");
    } else {
        System.out.println("Cached session exists for session id " + sessionId + ", returning stored validator session id.");
    }
    return sessionId;
}
Also used : IgLoader(org.hl7.fhir.validation.IgLoader) FhirPublication(org.hl7.fhir.utilities.FhirPublication) SystemOutLoggingService(org.hl7.fhir.r5.context.SystemOutLoggingService) ValidationEngine(org.hl7.fhir.validation.ValidationEngine)

Aggregations

FhirPublication (org.hl7.fhir.utilities.FhirPublication)3 ValidationEngine (org.hl7.fhir.validation.ValidationEngine)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 JsonParser (org.hl7.fhir.r5.elementmodel.JsonParser)2 SystemOutLoggingService (org.hl7.fhir.r5.context.SystemOutLoggingService)1 Element (org.hl7.fhir.r5.elementmodel.Element)1 org.hl7.fhir.r5.model (org.hl7.fhir.r5.model)1 Measure (org.hl7.fhir.r5.model.Measure)1 Resource (org.hl7.fhir.r5.model.Resource)1 IgLoader (org.hl7.fhir.validation.IgLoader)1