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