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