001package org.hl7.fhir.r5.utils.validation;
002
003import lombok.Getter;
004import org.hl7.fhir.exceptions.FHIRException;
005import org.hl7.fhir.r5.elementmodel.Element;
006import org.hl7.fhir.r5.model.CanonicalResource;
007import org.hl7.fhir.r5.model.CanonicalType;
008import org.hl7.fhir.r5.model.PackageInformation;
009import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage;
010
011import java.io.IOException;
012import java.net.URISyntaxException;
013import java.util.HashSet;
014import java.util.List;
015import java.util.Locale;
016import java.util.Set;
017
018@MarkedToMoveToAdjunctPackage
019public interface IValidatorResourceFetcher {
020
021  Element fetch(IResourceValidator validator, Object appContext, String url) throws FHIRException, IOException;
022
023  boolean resolveURL(IResourceValidator validator, Object appContext, String path, String url, String type, boolean canonical, List<CanonicalType> targets) throws IOException, FHIRException;
024
025  byte[] fetchRaw(IResourceValidator validator, String url) throws IOException; // for attachment checking
026
027  IValidatorResourceFetcher setLocale(Locale locale);
028
029  /**
030   * this is used when the validator encounters a reference to a structure definition, value set or code system at some random URL reference
031   * while validating.
032   * <p>
033   * Added in v5.2.2. return null to leave functionality as it was before then.
034   *
035   * @return an R5 version of the resource
036   * @throws URISyntaxException
037   */
038  CanonicalResource fetchCanonicalResource(IResourceValidator validator, Object appContext, String url) throws URISyntaxException;
039
040  /**
041   * Whether to try calling fetchCanonicalResource for this reference (not whether it will succeed - just throw an exception from fetchCanonicalResource if it doesn't resolve. This is a policy thing.
042   * <p>
043   * Added in v5.2.2. return false to leave functionality as it was before then.
044   *
045   * @param url
046   * @return
047   */
048  boolean fetchesCanonicalResource(IResourceValidator validator, String url);
049
050  Set<ResourceVersionInformation> fetchCanonicalResourceVersions(IResourceValidator validator, Object appContext, String url);
051
052  public static class ResourceVersionInformation {
053    @Getter private String version;
054    @Getter private String sourcePackage;
055
056    public ResourceVersionInformation(String version, PackageInformation sourcePackage) {
057      this.version = version;
058      this.sourcePackage = sourcePackage == null ? null : sourcePackage.getVID();
059    }
060
061    public static Set<String> toStrings(Set<IValidatorResourceFetcher.ResourceVersionInformation> possibleVersions) {
062      Set<String> result = new HashSet<>();
063      for (IValidatorResourceFetcher.ResourceVersionInformation v : possibleVersions) {
064        if (v.getSourcePackage() == null) {
065          result.add(v.getVersion());
066        } else {
067          result.add(v.getVersion()+" (from "+v.getSourcePackage()+")");
068        }
069      }
070      return result;
071    }
072  }
073}