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