From f890462721fa7de2ea5f783643ffd3bce92ec43a Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Sat, 1 Jun 2024 22:50:51 +1000 Subject: [PATCH] Add utility to unpack the spec.internals file --- .../fhir/convertors/misc/SpecMapUnpacker.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/SpecMapUnpacker.java diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/SpecMapUnpacker.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/SpecMapUnpacker.java new file mode 100644 index 000000000..b5640a4e4 --- /dev/null +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/SpecMapUnpacker.java @@ -0,0 +1,56 @@ +package org.hl7.fhir.convertors.misc; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +import org.hl7.fhir.utilities.TextFile; +import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.filesystem.DirectoryVisitor; +import org.hl7.fhir.utilities.filesystem.DirectoryVisitor.IDirectoryVisitorImplementation; +import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.npm.NpmPackage; + +public class SpecMapUnpacker { + + public static void main(String[] args) throws IOException { + new SpecMapUnpacker().unpack(args[0]); + } + + public class SpecMapScanner implements IDirectoryVisitorImplementation { + + @Override + public boolean enterDirectory(File directory) throws IOException { + return true; + } + + @Override + public boolean visitFile(File file) throws IOException { + System.out.println("Look at "+file.getAbsolutePath()); + try { + NpmPackage npm = NpmPackage.fromPackage(ManagedFileAccess.inStream(file)); + if (npm.hasFile("other", "spec.internals")) { + byte[] cnt = TextFile.streamToBytes(npm.load("other", "spec.internals")); + TextFile.bytesToFile(cnt, Utilities.path(Utilities.getDirectoryForFile(file.getAbsolutePath()), "page-map.json")); + System.out.println(" ...extracted"); + return true; + } else { + System.out.println(" ...not found"); + return false; + } + + } catch (Exception e) { + System.out.println(" ...error: "+e.getMessage()); + return false; + } + } + + } + + private void unpack(String path) throws IOException { + System.out.println("Scanning "+path); + int count = DirectoryVisitor.visitDirectory(new SpecMapScanner(), path, "tgz"); + System.out.println("Done. "+count+" files extracted"); + } + +}