use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.
the class PackageHacker method edit.
private void edit(String name) throws FileNotFoundException, IOException {
File f = new File(name);
if (!f.exists())
throw new Error("Unable to find " + f.getAbsolutePath());
NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f));
System.out.println("Altering Package " + f.getAbsolutePath());
System.out.println(nice(pck.getNpm()));
change(pck.getNpm(), pck.getFolders().get("package").getContent());
System.out.println("Revised Package");
System.out.println("=======================");
System.out.println(nice(pck.getNpm()));
System.out.println("=======================");
System.out.print("save? y/n: ");
int r = System.in.read();
if (r == 'y') {
f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak")));
pck.save(new FileOutputStream(f));
}
}
use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.
the class IgLoader method loadIg.
/**
* @param igs
* @param binaries
* @param src Source of the IG
*
* @param recursive
* @throws IOException
* @throws FHIRException
*
* @see IgLoader#loadIgSource(String, boolean, boolean) loadIgSource for detailed description of the src parameter
*/
public void loadIg(List<ImplementationGuide> igs, Map<String, byte[]> binaries, String src, boolean recursive) throws IOException, FHIRException {
final String explicitFhirVersion;
final String srcPackage;
if (src.startsWith("[") && src.indexOf(']', 1) > 1) {
explicitFhirVersion = src.substring(1, src.indexOf(']', 1));
srcPackage = src.substring(src.indexOf(']', 1) + 1);
if (!VersionUtilities.isSupportedVersion(explicitFhirVersion)) {
throw new FHIRException("Unsupported FHIR Version: " + explicitFhirVersion + " valid versions are " + VersionUtilities.listSupportedVersions());
}
} else {
explicitFhirVersion = null;
srcPackage = src;
}
NpmPackage npm = srcPackage.matches(FilesystemPackageCacheManager.PACKAGE_VERSION_REGEX_OPT) && !new File(srcPackage).exists() ? getPackageCacheManager().loadPackage(srcPackage, null) : null;
if (npm != null) {
for (String s : npm.dependencies()) {
if (!getContext().getLoadedPackages().contains(s)) {
if (!VersionUtilities.isCorePackage(s)) {
loadIg(igs, binaries, s, false);
}
}
}
System.out.print(" Load " + srcPackage);
if (!srcPackage.contains("#")) {
System.out.print("#" + npm.version());
}
int count = getContext().loadFromPackage(npm, ValidatorUtils.loaderForVersion(npm.fhirVersion()));
System.out.println(" - " + count + " resources (" + getContext().clock().milestone() + ")");
} else {
System.out.print(" Load " + srcPackage);
String canonical = null;
int count = 0;
Map<String, byte[]> source = loadIgSource(srcPackage, recursive, true);
String version = Constants.VERSION;
if (getVersion() != null) {
version = getVersion();
}
if (source.containsKey("version.info")) {
version = readInfoVersion(source.get("version.info"));
}
if (explicitFhirVersion != null) {
version = explicitFhirVersion;
}
for (Map.Entry<String, byte[]> t : source.entrySet()) {
String fn = t.getKey();
if (!exemptFile(fn)) {
Resource r = loadFileWithErrorChecking(version, t, fn);
if (r != null) {
count++;
getContext().cacheResource(r);
if (r instanceof ImplementationGuide) {
canonical = ((ImplementationGuide) r).getUrl();
igs.add((ImplementationGuide) r);
if (canonical.contains("/ImplementationGuide/")) {
Resource r2 = r.copy();
((ImplementationGuide) r2).setUrl(canonical.substring(0, canonical.indexOf("/ImplementationGuide/")));
getContext().cacheResource(r2);
}
}
}
}
}
if (canonical != null) {
ValidatorUtils.grabNatives(binaries, source, canonical);
}
System.out.println(" - " + count + " resources (" + getContext().clock().milestone() + ")");
}
}
use of org.hl7.fhir.utilities.npm.NpmPackage 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.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.
the class SimpleWorkerContext method loadFromPackageAndDependenciesInt.
public int loadFromPackageAndDependenciesInt(NpmPackage pi, IContextResourceLoader loader, BasePackageCacheManager pcm, String path) throws IOException, FHIRException {
int t = 0;
for (String e : pi.dependencies()) {
if (!loadedPackages.contains(e) && !VersionUtilities.isCorePackage(e)) {
NpmPackage npm = pcm.loadPackage(e);
if (!VersionUtilities.versionsMatch(version, npm.fhirVersion())) {
System.out.println(formatMessage(I18nConstants.PACKAGE_VERSION_MISMATCH, e, version, npm.fhirVersion(), path));
}
t = t + loadFromPackageAndDependenciesInt(npm, loader.getNewLoader(npm), pcm, path + " -> " + npm.name() + "#" + npm.version());
}
}
t = t + loadFromPackageInt(pi, loader, loader.getTypes());
return t;
}
Aggregations