java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 21 Dec 2013 20:38:50 +0100
branchv_0
changeset 26 4ec8e5534eb9
parent 20 e225bdcd260e
child 65 f05be87239ad
permissions -rw-r--r--
configuration basics
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk;
    19 
    20 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
    21 import java.io.BufferedReader;
    22 import java.io.InputStreamReader;
    23 import java.io.PrintStream;
    24 import java.util.EnumSet;
    25 import java.util.logging.Level;
    26 import java.util.logging.Logger;
    27 
    28 /**
    29  * Displays info like help, version etc.
    30  *
    31  * @author Ing. František Kučera (frantovo.cz)
    32  */
    33 public class InfoLister {
    34 
    35 	private static final Logger log = Logger.getLogger(InfoLister.class.getName());
    36 	private PrintStream out;
    37 	private ConfigurationProvider configurationProvider;
    38 
    39 	public InfoLister(PrintStream out, ConfigurationProvider configurationProvider) {
    40 		this.out = out;
    41 		this.configurationProvider = configurationProvider;
    42 	}
    43 
    44 	public void showInfo(CLIOptions options) {
    45 		EnumSet<CLIOptions.INFO_TYPE> infoTypes = options.getShowInfo();
    46 		for (CLIOptions.INFO_TYPE infoType : infoTypes) {
    47 			switch (infoType) {
    48 				/**
    49 				 * TODO: implement show info
    50 				 */
    51 				case FORMATTERS:
    52 					println("TODO: list available formatters");
    53 					break;
    54 				case HELP:
    55 					printResource(Constants.HELP_FILE);
    56 					break;
    57 				case LICENSE:
    58 					printResource(Constants.LICENSE_FILE);
    59 					break;
    60 				case TYPES:
    61 					println("TODO: list supported types");
    62 					break;
    63 				case VERSION:
    64 					printResource(Constants.VERSION_FILE);
    65 					break;
    66 				case DATABASES:
    67 					println("TODO: list databases");
    68 					break;
    69 				case CONNECTION:
    70 					println("TODO: test database connection: " + options.getDatabaseNameToTest());
    71 					break;
    72 				default:
    73 					throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);
    74 			}
    75 		}
    76 	}
    77 
    78 	private void printResource(String fileName) {
    79 		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
    80 			while (true) {
    81 				String line = reader.readLine();
    82 				if (line == null) {
    83 					break;
    84 				} else {
    85 					println(line);
    86 				}
    87 			}
    88 		} catch (Exception e) {
    89 			log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
    90 		}
    91 	}
    92 
    93 	private void println(String line) {
    94 		out.println(line);
    95 	}
    96 }