001package org.hl7.fhir.convertors.misc;
002
003import java.io.File;
004import java.io.IOException;
005import java.io.InputStream;
006
007import org.hl7.fhir.utilities.FileUtilities;
008import org.hl7.fhir.utilities.Utilities;
009import org.hl7.fhir.utilities.filesystem.DirectoryVisitor;
010import org.hl7.fhir.utilities.filesystem.DirectoryVisitor.IDirectoryVisitorImplementation;
011import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
012import org.hl7.fhir.utilities.npm.NpmPackage;
013
014@SuppressWarnings("checkstyle:systemout")
015public class SpecMapUnpacker {
016
017  public static void main(String[] args) throws IOException {
018     new SpecMapUnpacker().unpack(args[0]);
019  }
020
021  public class SpecMapScanner implements IDirectoryVisitorImplementation {
022
023    @Override
024    public boolean enterDirectory(File directory) throws IOException {
025      return true;
026    }
027
028    @Override
029    public boolean visitFile(File file) throws IOException {
030      System.out.println("Look at "+file.getAbsolutePath());
031      try {
032        NpmPackage npm = NpmPackage.fromPackage(ManagedFileAccess.inStream(file));
033        if (npm.hasFile("other", "spec.internals")) {
034          byte[] cnt = FileUtilities.streamToBytes(npm.load("other", "spec.internals"));
035          FileUtilities.bytesToFile(cnt, Utilities.path(FileUtilities.getDirectoryForFile(file.getAbsolutePath()), "page-map.json"));
036          System.out.println("  ...extracted");
037          return true;
038        } else {
039          System.out.println("  ...not found");
040          return false;
041        }
042        
043      } catch (Exception e) {
044        System.out.println("  ...error: "+e.getMessage());
045        return false;
046      }
047    }
048    
049  }
050  
051  private void unpack(String path) throws IOException {
052    System.out.println("Scanning "+path);
053    int count = DirectoryVisitor.visitDirectory(new SpecMapScanner(), path, "tgz");
054    System.out.println("Done. "+count+" files extracted");
055  }
056
057}