001package org.hl7.fhir.validation.cli.utils;
002
003import org.apache.commons.io.FileUtils;
004import org.apache.commons.io.IOUtils;
005import org.hl7.fhir.r5.model.Constants;
006import org.hl7.fhir.utilities.VersionUtilities;
007import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
008import org.hl7.fhir.utilities.npm.ToolsVersion;
009
010import java.io.File;
011import java.io.IOException;
012import java.io.InputStream;
013import java.util.Arrays;
014import java.util.Map;
015
016/**
017 * Class for displaying output to the cli user.
018 * <p>
019 * TODO - Clean this up for localization
020 */
021public class Display {
022
023
024  private static String toMB(long maxMemory) {
025    return Long.toString(maxMemory / (1024 * 1024));
026  }
027
028  public static void printCliArgumentsAndInfo(String[] args) throws IOException {
029    System.out.println("  Paths:  Current = " + System.getProperty("user.dir") + ", Package Cache = " + new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).getFolder());
030    System.out.print("  Params:");
031    for (String s : args) {
032      System.out.print(s.contains(" ") ? " \"" + s + "\"" : " " + s);
033    }
034    System.out.println();
035  }
036
037  final static String CURLY_START = "\\{\\{";
038  final static String CURLY_END = "\\}\\}";
039
040  final static String getMoustacheString(final String string) {
041    return CURLY_START + string + CURLY_END;
042  }
043
044  final static String[][] PLACEHOLDERS = {
045    { getMoustacheString("XML_AND_JSON_FHIR_VERSIONS"), "1.0, 1.4, 3.0, 4.0," + Constants.VERSION_MM },
046    { getMoustacheString("TURTLE_FHIR_VERSIONS"), "3.0, 4.0, " + Constants.VERSION_MM },
047    { getMoustacheString("FHIR_MAJOR_VERSIONS"), "1.0|1.4|3.0|" + VersionUtilities.CURRENT_VERSION},
048    { getMoustacheString("FHIR_MINOR_VERSIONS"), "1.0.2|1.4.0|3.0.2|4.0.1|" + VersionUtilities.CURRENT_FULL_VERSION },
049    { getMoustacheString("FHIR_CURRENT_VERSION"), VersionUtilities.CURRENT_VERSION},
050  };
051
052  final static String replacePlaceholders(final String input, final String[][] placeholders) {
053    String output = input;
054    for (String[] placeholder : placeholders) {
055      output = output.replaceAll(placeholder[0], placeholder[1]);
056    }
057    return output;
058  }
059
060  /**
061   * Loads the help details from resources/help.txt, and displays them on the command line to the user.
062   */
063  public static void displayHelpDetails() {
064    ClassLoader classLoader = Display.class.getClassLoader();
065    InputStream help = classLoader.getResourceAsStream("help.txt");
066    try {
067      String data = IOUtils.toString(help, "UTF-8");
068
069      System.out.println(replacePlaceholders(data, PLACEHOLDERS));
070    } catch (IOException e) {
071      e.printStackTrace();
072    }
073  }
074
075
076
077  /**
078   * Prints out system info to the command line.
079   */
080  public static void displaySystemInfo() {
081    System.out.println("  Java:   " + System.getProperty("java.version")
082      + " from " + System.getProperty("java.home")
083      + " on " + System.getProperty("os.arch")
084      + " (" + System.getProperty("sun.arch.data.model") + "bit). "
085      + toMB(Runtime.getRuntime().maxMemory()) + "MB available");
086  }
087
088  /**
089   * Prints current version of the validator.
090   */
091  public static void displayVersion() {
092    System.out.println("FHIR Validation tool " + VersionUtil.getVersionString());
093  }
094}