java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 16 Dec 2013 23:17:34 +0100
branchv_0
changeset 20 e225bdcd260e
parent 19 873669135d97
child 26 4ec8e5534eb9
permissions -rw-r--r--
refactor, configuration
     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 java.io.BufferedReader;
    21 import java.io.InputStreamReader;
    22 import java.io.PrintStream;
    23 import java.util.EnumSet;
    24 import java.util.logging.Level;
    25 import java.util.logging.Logger;
    26 
    27 /**
    28  * Displays info like help, version etc.
    29  *
    30  * @author Ing. František Kučera (frantovo.cz)
    31  */
    32 public class InfoLister {
    33 
    34 	private static final Logger log = Logger.getLogger(InfoLister.class.getName());
    35 	private PrintStream out;
    36 	private ConfigurationProvider configurationProvider;
    37 
    38 	public InfoLister(PrintStream out, ConfigurationProvider configurationProvider) {
    39 		this.out = out;
    40 		this.configurationProvider = configurationProvider;
    41 	}
    42 
    43 	public void showInfo(CLIOptions options) {
    44 		EnumSet<CLIOptions.INFO_TYPE> infoTypes = options.getShowInfo();
    45 		for (CLIOptions.INFO_TYPE infoType : infoTypes) {
    46 			switch (infoType) {
    47 				/**
    48 				 * TODO: implement show info
    49 				 */
    50 				case FORMATTERS:
    51 					println("TODO: list available formatters");
    52 					break;
    53 				case HELP:
    54 					printResource(Constants.HELP_FILE);
    55 					break;
    56 				case LICENSE:
    57 					printResource(Constants.LICENSE_FILE);
    58 					break;
    59 				case TYPES:
    60 					println("TODO: list supported types");
    61 					break;
    62 				case VERSION:
    63 					printResource(Constants.VERSION_FILE);
    64 					break;
    65 				case DATABASES:
    66 					println("TODO: list databases");
    67 					break;
    68 				case CONNECTION:
    69 					println("TODO: test database connection: " + options.getDatabaseNameToTest());
    70 					break;
    71 				default:
    72 					throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);
    73 			}
    74 		}
    75 	}
    76 
    77 	private void printResource(String fileName) {
    78 		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
    79 			while (true) {
    80 				String line = reader.readLine();
    81 				if (line == null) {
    82 					break;
    83 				} else {
    84 					println(line);
    85 				}
    86 			}
    87 		} catch (Exception e) {
    88 			log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
    89 		}
    90 	}
    91 
    92 	private void println(String line) {
    93 		out.println(line);
    94 	}
    95 }