use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class IgLoader method loadIgSource.
/**
* @param src can be one of the following:
* <br> - a canonical url for an ig - this will be converted to a package id and loaded into the cache
* <br> - a package id for an ig - this will be loaded into the cache
* <br> - a direct reference to a package ("package.tgz") - this will be extracted by the cache manager, but not put in the cache
* <br> - a folder containing resources - these will be loaded directly
* @param recursive if true and src resolves to a folder, recursively find and load IgSources from that directory
* @param explore should be true if we're trying to load an -ig parameter, and false if we're loading source
*
* @return
* @throws FHIRException
* @throws IOException
*/
public Map<String, byte[]> loadIgSource(String src, boolean recursive, boolean explore) 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 = explore ? getPackageCacheManager().getPackageId(src) : null;
if (!Utilities.noString(pid))
return fetchByPackage(pid + (v == null ? "" : "#" + v));
else
return fetchFromUrl(src + (v == null ? "" : "|" + v), explore);
}
File f = new File(Utilities.path(src));
if (f.exists()) {
if (f.isDirectory() && new File(Utilities.path(src, "package.tgz")).exists())
return loadPackage(new FileInputStream(Utilities.path(src, "package.tgz")), Utilities.path(src, "package.tgz"));
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"))
return loadPackage(new FileInputStream(src), src);
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")) {
return fetchByPackage(src);
}
throw new FHIRException("Unable to find/resolve/read " + (explore ? "-ig " : "") + src);
}
use of org.hl7.fhir.utilities.graphql.Package 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.utilities.graphql.Package 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);
}
use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class SimpleWorkerContext method loadFromPackageInt.
public int loadFromPackageInt(NpmPackage pi, IContextResourceLoader loader, String... types) throws IOException, FHIRException {
int t = 0;
if (progress) {
System.out.println("Load Package " + pi.name() + "#" + pi.version());
}
if (loadedPackages.contains(pi.id() + "#" + pi.version())) {
return 0;
}
loadedPackages.add(pi.id() + "#" + pi.version());
if ((types == null || types.length == 0) && loader != null) {
types = loader.getTypes();
}
if (VersionUtilities.isR2Ver(pi.fhirVersion()) || !pi.canLazyLoad()) {
// can't lazy load R2 because of valueset/codesystem implementation
if (types.length == 0) {
types = new String[] { "StructureDefinition", "ValueSet", "SearchParameter", "OperationDefinition", "Questionnaire", "ConceptMap", "StructureMap", "NamingSystem" };
}
for (String s : pi.listResources(types)) {
try {
loadDefinitionItem(s, pi.load("package", s), loader, null, new PackageVersion(pi.id(), pi.version(), pi.dateAsDate()));
t++;
} catch (Exception e) {
throw new FHIRException(formatMessage(I18nConstants.ERROR_READING__FROM_PACKAGE__, s, pi.name(), pi.version(), e.getMessage()), e);
}
}
} else {
if (types.length == 0) {
types = new String[] { "StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter", "OperationDefinition", "Questionnaire", "ConceptMap", "StructureMap", "NamingSystem", "Measures" };
}
for (PackageResourceInformation pri : pi.listIndexedResources(types)) {
if (!pri.getFilename().contains("ig-r4")) {
try {
registerResourceFromPackage(new PackageResourceLoader(pri, loader), new PackageVersion(pi.id(), pi.version(), pi.dateAsDate()));
t++;
} catch (FHIRException e) {
throw new FHIRException(formatMessage(I18nConstants.ERROR_READING__FROM_PACKAGE__, pri.getFilename(), pi.name(), pi.version(), e.getMessage()), e);
}
}
}
}
for (String s : pi.list("other")) {
binaries.put(s, TextFile.streamToBytes(pi.load("other", s)));
}
if (version == null) {
version = pi.version();
}
return t;
}
use of org.hl7.fhir.utilities.graphql.Package in project CRD by HL7-DaVinci.
the class CqlRule method build.
private void build(HashMap<String, byte[]> cqlFiles, HashMap<String, byte[]> jsonElmFiles, HashMap<String, byte[]> xmlElmFiles) {
// build a list of all of the CQL Libraries
List<CqlRule.CqlLibrary> cqlLibraries = new ArrayList<>();
for (String fileName : cqlFiles.keySet()) {
logger.debug("CqlRule: file: " + fileName);
CqlRule.CqlLibrary cqlLibrary = new CqlRule.CqlLibrary();
cqlLibrary.cql = cqlFiles.get(fileName);
// only add those that are the right fhir version
String fhirVersionFromCql = getFhirVersionFromCqlFile(cqlLibrary.cql);
// last character of fhirVersion ("R4" => "4") and first character of FHIR version from file ("3.0.0" => "3")
if (fhirVersion.substring(fhirVersion.length() - 1).equalsIgnoreCase(fhirVersionFromCql.substring(0, 1))) {
String fileNameWithoutExtension = fileName.substring(0, fileName.length() - 4);
String xmlElmName = fileNameWithoutExtension + ".xml";
if (xmlElmFiles.containsKey(xmlElmName)) {
cqlLibrary.xlmElm = true;
cqlLibrary.elm = xmlElmFiles.get(xmlElmName);
}
String jsonElmName = fileNameWithoutExtension + ".json";
if (jsonElmFiles.containsKey(jsonElmName)) {
cqlLibrary.xlmElm = false;
cqlLibrary.elm = jsonElmFiles.get(jsonElmName);
}
cqlLibraries.add(cqlLibrary);
}
}
precompiled = false;
for (CqlLibrary cqlLibrary : cqlLibraries) {
if (precompiled) {
if (cqlLibrary.elm == null) {
throw new RuntimeException("Package indicated CQL was precompiled, but elm xml missing.");
}
// TODO: need to set rulePackage.elmLibraries and mainCqlLibraryId
} else {
InputStream cqlStream = new ByteArrayInputStream(cqlLibrary.cql);
VersionedIdentifier id = getIdFromCqlFile(cqlLibrary.cql);
String fhirVersionFromFile = getFhirVersionFromCqlFile(cqlLibrary.cql);
logger.info("CqlRule::Constructor() add id: " + id.getId() + ", fhir version: " + fhirVersionFromFile);
if (rawCqlLibraries.containsKey(fhirVersionFromFile)) {
// logger.info("CqlRule::Constructor() add rawCqlLibraries add: " + id.getId());
rawCqlLibraries.get(fhirVersionFromFile).put(id, cqlStream);
} else {
HashMap<VersionedIdentifier, InputStream> map = new HashMap<>();
map.put(id, cqlStream);
// logger.info("CqlRule::Constructor() add rawCqlLibraries new: " + id.getId());
rawCqlLibraries.put(fhirVersionFromFile, map);
}
if (id.getId().equals(mainCqlLibraryName)) {
// logger.info("CqlRule::Constructor() add mainCqlLibraryId: " + id.getId());
mainCqlLibraryId.put(fhirVersionFromFile, id);
}
}
}
}
Aggregations