Search in sources :

Example 1 with VersionURLInfo

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

the class StandAloneValidatorFetcher method resolveURL.

@Override
public boolean resolveURL(IResourceValidator validator, Object appContext, String path, String url, String type) throws IOException, FHIRException {
    if (!Utilities.isAbsoluteUrl(url)) {
        return false;
    }
    if (url.contains("|")) {
        url = url.substring(0, url.lastIndexOf("|"));
    }
    if (type != null && type.equals("uri") && isMappingUri(url)) {
        return true;
    }
    // if we've got to here, it's a reference to a FHIR URL. We're going to try to resolve it on the fly
    String pid = null;
    String ver = null;
    String base = findBaseUrl(url);
    if (base == null) {
        return !url.startsWith("http://hl7.org/fhir") && !type.equals("canonical");
    }
    // the next operations are expensive. we're going to cache them
    if (urlList.containsKey(url)) {
        return urlList.get(url);
    }
    if (base.equals("http://terminology.hl7.org")) {
        pid = "hl7.terminology";
    } else if (url.startsWith("http://hl7.org/fhir")) {
        pid = pcm.getPackageId(base);
    } else {
        if (pidList.containsKey(base)) {
            pid = pidList.get(base);
        } else {
            pid = pcm.findCanonicalInLocalCache(base);
            pidList.put(base, pid);
        }
    }
    ver = url.contains("|") ? url.substring(url.indexOf("|") + 1) : null;
    if (pid == null && Utilities.startsWithInList(url, "http://hl7.org/fhir", "http://terminology.hl7.org")) {
        urlList.put(url, false);
        return false;
    }
    if (url.startsWith("http://hl7.org/fhir")) {
        // first possibility: it's a reference to a version specific URL http://hl7.org/fhir/X.X/...
        VersionURLInfo vu = VersionUtilities.parseVersionUrl(url);
        if (vu != null) {
            NpmPackage pi = pcm.loadPackage(VersionUtilities.packageForVersion(vu.getVersion()), VersionUtilities.getCurrentVersion(vu.getVersion()));
            boolean res = pi.hasCanonical(vu.getUrl());
            urlList.put(url, res);
            return res;
        }
    }
    // ok maybe it's a reference to a package we know
    if (pid != null) {
        if ("sharedhealth.fhir.ca.common".equals(pid)) {
            // special case - optimise this
            return false;
        }
        NpmPackage pi = null;
        if (pidMap.containsKey(pid + "|" + ver)) {
            pi = pidMap.get(pid + "|" + ver);
        } else if (installer.packageExists(pid, ver)) {
            try {
                installer.loadPackage(pid, ver);
                pi = pcm.loadPackage(pid);
                pidMap.put(pid + "|" + ver, pi);
            } catch (Exception e) {
                pidMap.put(pid + "|" + ver, null);
            }
        } else {
            pidMap.put(pid + "|" + ver, null);
        }
        if (pi != null) {
            context.loadFromPackage(pi, null);
            return pi.hasCanonical(url);
        }
    }
    // we don't bother with urls outside fhir space in the standalone validator - we assume they are valid
    return !url.startsWith("http://hl7.org/fhir") && !type.equals("canonical");
}
Also used : NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) VersionURLInfo(org.hl7.fhir.utilities.VersionUtilities.VersionURLInfo) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 2 with VersionURLInfo

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

the class InstanceValidator method start.

// checkSpecials = we're only going to run these tests if we are actually validating this content (as opposed to we looked it up)
private void start(ValidatorHostContext hostContext, List<ValidationMessage> errors, Element resource, Element element, StructureDefinition defn, NodeStack stack) throws FHIRException {
    checkLang(resource, stack);
    if (crumbTrails) {
        element.addMessage(signpost(errors, IssueType.INFORMATIONAL, element.line(), element.col(), stack.getLiteralPath(), I18nConstants.VALIDATION_VAL_PROFILE_SIGNPOST, defn.getUrl()));
    }
    if (BUNDLE.equals(element.fhirType())) {
        resolveBundleReferences(element, new ArrayList<Element>());
    }
    startInner(hostContext, errors, resource, element, defn, stack, hostContext.isCheckSpecials());
    Element meta = element.getNamedChild(META);
    if (meta != null) {
        List<Element> profiles = new ArrayList<Element>();
        meta.getNamedChildren("profile", profiles);
        int i = 0;
        for (Element profile : profiles) {
            StructureDefinition sd = context.fetchResource(StructureDefinition.class, profile.primitiveValue());
            if (!defn.getUrl().equals(profile.primitiveValue())) {
                // is this a version specific reference?
                VersionURLInfo vu = VersionUtilities.parseVersionUrl(profile.primitiveValue());
                if (vu != null) {
                    if (!VersionUtilities.versionsCompatible(vu.getVersion(), context.getVersion())) {
                        hint(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath() + ".meta.profile[" + i + "]", false, I18nConstants.VALIDATION_VAL_PROFILE_OTHER_VERSION, vu.getVersion());
                    } else if (vu.getUrl().equals(defn.getUrl())) {
                        hint(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath() + ".meta.profile[" + i + "]", false, I18nConstants.VALIDATION_VAL_PROFILE_THIS_VERSION_OK);
                    } else {
                        StructureDefinition sdt = context.fetchResource(StructureDefinition.class, vu.getUrl());
                        rule(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath() + ".meta.profile[" + i + "]", false, I18nConstants.VALIDATION_VAL_PROFILE_THIS_VERSION_OTHER, sdt == null ? "null" : sdt.getType());
                    }
                } else {
                    if (sd == null) {
                        // we'll try fetching it directly from it's source, but this is likely to fail later even if the resolution succeeds
                        if (fetcher == null) {
                            warning(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath() + ".meta.profile[" + i + "]", false, I18nConstants.VALIDATION_VAL_PROFILE_UNKNOWN, profile.primitiveValue());
                        } else if (!fetcher.fetchesCanonicalResource(this, profile.primitiveValue())) {
                            warning(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath() + ".meta.profile[" + i + "]", false, I18nConstants.VALIDATION_VAL_PROFILE_UNKNOWN_NOT_POLICY, profile.primitiveValue());
                        } else {
                            try {
                                sd = (StructureDefinition) fetcher.fetchCanonicalResource(this, profile.primitiveValue());
                            } catch (Exception e) {
                                if (STACK_TRACE)
                                    e.printStackTrace();
                                warning(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath() + ".meta.profile[" + i + "]", false, I18nConstants.VALIDATION_VAL_PROFILE_UNKNOWN_ERROR, profile.primitiveValue(), e.getMessage());
                            }
                            if (sd != null) {
                                context.cacheResource(sd);
                            }
                        }
                    }
                    if (sd != null) {
                        if (crumbTrails) {
                            element.addMessage(signpost(errors, IssueType.INFORMATIONAL, element.line(), element.col(), stack.getLiteralPath(), I18nConstants.VALIDATION_VAL_PROFILE_SIGNPOST_META, sd.getUrl()));
                        }
                        stack.resetIds();
                        startInner(hostContext, errors, resource, element, sd, stack, false);
                    }
                }
            }
            i++;
        }
    }
    String rt = element.fhirType();
    for (ImplementationGuide ig : igs) {
        for (ImplementationGuideGlobalComponent gl : ig.getGlobal()) {
            if (rt.equals(gl.getType())) {
                StructureDefinition sd = context.fetchResource(StructureDefinition.class, gl.getProfile());
                if (warning(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath(), sd != null, I18nConstants.VALIDATION_VAL_GLOBAL_PROFILE_UNKNOWN, gl.getProfile())) {
                    if (crumbTrails) {
                        element.addMessage(signpost(errors, IssueType.INFORMATIONAL, element.line(), element.col(), stack.getLiteralPath(), I18nConstants.VALIDATION_VAL_PROFILE_SIGNPOST_GLOBAL, sd.getUrl(), ig.getUrl()));
                    }
                    stack.resetIds();
                    startInner(hostContext, errors, resource, element, sd, stack, false);
                }
            }
        }
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ImplementationGuideGlobalComponent(org.hl7.fhir.r5.model.ImplementationGuide.ImplementationGuideGlobalComponent) ImplementationGuide(org.hl7.fhir.r5.model.ImplementationGuide) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) IndexedElement(org.hl7.fhir.validation.instance.utils.IndexedElement) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) VersionURLInfo(org.hl7.fhir.utilities.VersionUtilities.VersionURLInfo) ContactPoint(org.hl7.fhir.r5.model.ContactPoint) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)

Aggregations

IOException (java.io.IOException)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 VersionURLInfo (org.hl7.fhir.utilities.VersionUtilities.VersionURLInfo)2 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 NotImplementedException (org.apache.commons.lang3.NotImplementedException)1 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)1 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)1 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)1 Element (org.hl7.fhir.r5.elementmodel.Element)1 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)1 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)1 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)1 ImplementationGuide (org.hl7.fhir.r5.model.ImplementationGuide)1 ImplementationGuideGlobalComponent (org.hl7.fhir.r5.model.ImplementationGuide.ImplementationGuideGlobalComponent)1 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)1 FHIRLexerException (org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)1 NpmPackage (org.hl7.fhir.utilities.npm.NpmPackage)1 IndexedElement (org.hl7.fhir.validation.instance.utils.IndexedElement)1