use of org.hl7.fhir.validation.cli.utils.VersionSourceInformation in project org.hl7.fhir.core by hapifhir.
the class IgLoader method scanForVersions.
public void scanForVersions(List<String> sources, VersionSourceInformation versions) throws FHIRException, IOException {
List<String> refs = new ArrayList<String>();
ValidatorUtils.parseSources(sources, refs, context);
for (String ref : refs) {
Content cnt = loadContent(ref, "validate", false);
String s = TextFile.bytesToString(cnt.focus);
if (s.contains("http://hl7.org/fhir/3.0")) {
versions.see("3.0", "Profile in " + ref);
}
if (s.contains("http://hl7.org/fhir/1.0")) {
versions.see("1.0", "Profile in " + ref);
}
if (s.contains("http://hl7.org/fhir/4.0")) {
versions.see("4.0", "Profile in " + ref);
}
if (s.contains("http://hl7.org/fhir/1.4")) {
versions.see("1.4", "Profile in " + ref);
}
try {
if (s.startsWith("{")) {
JsonObject json = JsonTrackingParser.parse(s, null);
if (json.has("fhirVersion")) {
versions.see(VersionUtilities.getMajMin(JSONUtil.str(json, "fhirVersion")), "fhirVersion in " + ref);
}
} else {
Document doc = ValidatorUtils.parseXml(cnt.focus);
String v = XMLUtil.getNamedChildValue(doc.getDocumentElement(), "fhirVersion");
if (v != null) {
versions.see(VersionUtilities.getMajMin(v), "fhirVersion in " + ref);
}
}
} catch (Exception e) {
// nothing
}
}
}
use of org.hl7.fhir.validation.cli.utils.VersionSourceInformation in project org.hl7.fhir.core by hapifhir.
the class ValidationService method determineVersion.
public String determineVersion(CliContext cliContext, String sessionId) throws Exception {
if (cliContext.getMode() != EngineMode.VALIDATION) {
return "current";
}
System.out.println("Scanning for versions (no -version parameter):");
VersionSourceInformation versions = scanForVersions(cliContext);
for (String s : versions.getReport()) {
if (!s.equals("(nothing found)")) {
System.out.println(" " + s);
}
}
if (versions.isEmpty()) {
System.out.println(" No Version Info found: Using Default version '" + VersionUtilities.CURRENT_DEFAULT_VERSION + "'");
return VersionUtilities.CURRENT_DEFAULT_FULL_VERSION;
}
if (versions.size() == 1) {
System.out.println("-> use version " + versions.version());
return versions.version();
}
throw new Exception("-> Multiple versions found. Specify a particular version using the -version parameter");
}
use of org.hl7.fhir.validation.cli.utils.VersionSourceInformation in project org.hl7.fhir.core by hapifhir.
the class ValidationService method scanForVersions.
public VersionSourceInformation scanForVersions(CliContext cliContext) throws Exception {
VersionSourceInformation versions = new VersionSourceInformation();
IgLoader igLoader = new IgLoader(new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION), new SimpleWorkerContext.SimpleWorkerContextBuilder().fromNothing(), null);
for (String src : cliContext.getIgs()) {
igLoader.scanForIgVersion(src, cliContext.isRecursive(), versions);
}
igLoader.scanForVersions(cliContext.getSources(), versions);
return versions;
}
use of org.hl7.fhir.validation.cli.utils.VersionSourceInformation in project org.hl7.fhir.core by hapifhir.
the class IgLoader method loadIgSourceForVersion.
private Map<String, byte[]> loadIgSourceForVersion(String src, boolean recursive, boolean explore, VersionSourceInformation versions) throws FHIRException, IOException {
if (Common.isNetworkPath(src)) {
String v = null;
if (src.contains("|")) {
v = src.substring(src.indexOf("|") + 1);
src = src.substring(0, src.indexOf("|"));
}
String pid = getPackageCacheManager().getPackageId(src);
if (!Utilities.noString(pid)) {
versions.see(fetchVersionByPackage(pid + (v == null ? "" : "#" + v)), "Package " + src);
return null;
} else {
return fetchVersionFromUrl(src + (v == null ? "" : "|" + v), explore, versions);
}
}
File f = new File(Utilities.path(src));
if (f.exists()) {
if (f.isDirectory() && new File(Utilities.path(src, "package.tgz")).exists()) {
versions.see(loadPackageForVersion(new FileInputStream(Utilities.path(src, "package.tgz"))), "Package " + src);
return null;
}
if (f.isDirectory() && new File(Utilities.path(src, "igpack.zip")).exists())
return readZip(new FileInputStream(Utilities.path(src, "igpack.zip")));
if (f.isDirectory() && new File(Utilities.path(src, "validator.pack")).exists())
return readZip(new FileInputStream(Utilities.path(src, "validator.pack")));
if (f.isDirectory())
return scanDirectory(f, recursive);
if (src.endsWith(".tgz")) {
versions.see(loadPackageForVersion(new FileInputStream(src)), "Package " + src);
return null;
}
if (src.endsWith(".pack"))
return readZip(new FileInputStream(src));
if (src.endsWith("igpack.zip"))
return readZip(new FileInputStream(src));
Manager.FhirFormat fmt = ResourceChecker.checkIsResource(getContext(), isDebug(), TextFile.fileToBytes(f), src, true);
if (fmt != null) {
Map<String, byte[]> res = new HashMap<String, byte[]>();
res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), TextFile.fileToBytesNCS(src));
return res;
}
} else if ((src.matches(FilesystemPackageCacheManager.PACKAGE_REGEX) || src.matches(FilesystemPackageCacheManager.PACKAGE_VERSION_REGEX)) && !src.endsWith(".zip") && !src.endsWith(".tgz")) {
versions.see(fetchVersionByPackage(src), "Package " + src);
return null;
}
throw new FHIRException("Unable to find/resolve/read -ig " + src);
}
use of org.hl7.fhir.validation.cli.utils.VersionSourceInformation in project org.hl7.fhir.core by hapifhir.
the class IgLoader method fetchVersionFromUrl.
private Map<String, byte[]> fetchVersionFromUrl(String src, boolean explore, VersionSourceInformation versions) throws FHIRException, IOException {
if (src.endsWith(".tgz")) {
versions.see(loadPackageForVersion(fetchFromUrlSpecific(src, false)), "From Package " + src);
return null;
}
if (src.endsWith(".pack"))
return readZip(fetchFromUrlSpecific(src, false));
if (src.endsWith("igpack.zip"))
return readZip(fetchFromUrlSpecific(src, false));
InputStream stream = null;
if (explore) {
stream = fetchFromUrlSpecific(Utilities.pathURL(src, "package.tgz"), true);
if (stream != null) {
versions.see(loadPackageForVersion(stream), "From Package at " + src);
return null;
}
// todo: these options are deprecated - remove once all IGs have been rebuilt post R4 technical correction
stream = fetchFromUrlSpecific(Utilities.pathURL(src, "igpack.zip"), true);
if (stream != null)
return readZip(stream);
stream = fetchFromUrlSpecific(Utilities.pathURL(src, "validator.pack"), true);
if (stream != null)
return readZip(stream);
stream = fetchFromUrlSpecific(Utilities.pathURL(src, "validator.pack"), true);
// // -----
}
// ok, having tried all that... now we'll just try to access it directly
byte[] cnt;
if (stream == null)
cnt = fetchFromUrlSpecific(src, "application/json", true, null);
else
cnt = TextFile.streamToBytes(stream);
Manager.FhirFormat fmt = ResourceChecker.checkIsResource(getContext(), isDebug(), cnt, src, true);
if (fmt != null) {
Map<String, byte[]> res = new HashMap<String, byte[]>();
res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), cnt);
return res;
}
String fn = Utilities.path("[tmp]", "fetch-resource-error-content.bin");
TextFile.bytesToFile(cnt, fn);
System.out.println("Error Fetching " + src);
System.out.println("Some content was found, saved to " + fn);
System.out.println("1st 100 bytes = " + presentForDebugging(cnt));
throw new FHIRException("Unable to find/resolve/read " + (explore ? "-ig " : "") + src);
}
Aggregations