šablona/funkce/src/cz/frantovo/xmlWebGenerator/NástrojeCLI.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 22:06:44 +0200
changeset 136 d5feb9d8ebc3
parent 87 25dec6931f18
permissions -rw-r--r--
fix license version: GNU GPLv3
franta-hg@61
     1
/**
franta-hg@61
     2
 * XML Web generátor – program na generování webových stránek
franta-hg@61
     3
 * Copyright © 2012 František Kučera (frantovo.cz)
franta-hg@61
     4
 * 
franta-hg@61
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@61
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@136
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@61
     8
 * 
franta-hg@61
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@61
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@61
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
franta-hg@61
    12
 * GNU General Public License for more details.
franta-hg@61
    13
 * 
franta-hg@61
    14
 * You should have received a copy of the GNU General Public License
franta-hg@61
    15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
franta-hg@61
    16
 */
franta-hg@38
    17
package cz.frantovo.xmlWebGenerator;
franta-hg@38
    18
franta-hg@38
    19
import java.io.BufferedReader;
franta-hg@38
    20
import java.io.IOException;
franta-hg@38
    21
import java.io.InputStream;
franta-hg@38
    22
import java.io.InputStreamReader;
franta-hg@38
    23
franta-hg@38
    24
/**
franta-hg@38
    25
 * Pomocné funkce pro práci s příkazy
franta-hg@76
    26
 * 
franta-hg@76
    27
 * Tyto funkce nejsou určené k přímému volání z XSLT.
franta-hg@76
    28
 * 
franta-hg@76
    29
 * @author František Kučera (frantovo.cz)
franta-hg@38
    30
 */
franta-hg@38
    31
public class NástrojeCLI {
franta-hg@87
    32
franta-hg@38
    33
	private static final String PŘÍKAZ_WHICH = "which";
franta-hg@38
    34
franta-hg@38
    35
	/**
franta-hg@38
    36
	 * Pomocí programu which zjistí, jestli je daný příkaz v systému přítomný.
franta-hg@38
    37
	 * @param příkaz jehož přítomnost zjišťujeme
franta-hg@38
    38
	 * @return true pokud příkaz v systému existuje
franta-hg@38
    39
	 */
franta-hg@38
    40
	public static boolean isPříkazDostupný(String příkaz) {
franta-hg@38
    41
		try {
franta-hg@38
    42
			Runtime r = Runtime.getRuntime();
franta-hg@38
    43
			Process p = r.exec(new String[]{PŘÍKAZ_WHICH, příkaz});
franta-hg@38
    44
			p.waitFor();
franta-hg@38
    45
			return p.exitValue() == 0;
franta-hg@38
    46
		} catch (Exception e) {
franta-hg@38
    47
			System.err.printf("Při zjišťování dostupnosti příkazu „%s“ došlo k chybě: %s", příkaz, e.getLocalizedMessage());
franta-hg@38
    48
			return false;
franta-hg@38
    49
		}
franta-hg@38
    50
	}
franta-hg@87
    51
franta-hg@38
    52
	/**
franta-hg@38
    53
	 * Čte proud dat dokud to jde a výsledek pak vrátí jako text.
franta-hg@38
    54
	 * @param proud vstupní proud
franta-hg@38
    55
	 * @return obsah proudu jako text
franta-hg@38
    56
	 * @throws IOException 
franta-hg@38
    57
	 */
franta-hg@38
    58
	public static String načtiProud(InputStream proud) throws IOException {
franta-hg@38
    59
		StringBuilder výsledek = new StringBuilder();
franta-hg@38
    60
		BufferedReader buf = new BufferedReader(new InputStreamReader(proud));
franta-hg@38
    61
		while (true) {
franta-hg@38
    62
			String radek = buf.readLine();
franta-hg@38
    63
			if (radek == null) {
franta-hg@38
    64
				break;
franta-hg@38
    65
			} else {
franta-hg@38
    66
				výsledek.append(radek);
franta-hg@38
    67
				výsledek.append("\n");
franta-hg@38
    68
			}
franta-hg@38
    69
		}
franta-hg@38
    70
		return výsledek.toString();
franta-hg@38
    71
	}
franta-hg@38
    72
}
franta-hg@87
    73