šablona/funkce/src/cz/frantovo/xmlWebGenerator/NástrojeCLI.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 14 Jan 2012 19:10:26 +0100
changeset 87 25dec6931f18
parent 76 c7746d95283d
permissions -rw-r--r--
Lepší odsazení, tabulátory.

/**
 * XML Web generátor – program na generování webových stránek
 * Copyright © 2012 František Kučera (frantovo.cz)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cz.frantovo.xmlWebGenerator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Pomocné funkce pro práci s příkazy
 * 
 * Tyto funkce nejsou určené k přímému volání z XSLT.
 * 
 * @author František Kučera (frantovo.cz)
 */
public class NástrojeCLI {

	private static final String PŘÍKAZ_WHICH = "which";

	/**
	 * Pomocí programu which zjistí, jestli je daný příkaz v systému přítomný.
	 * @param příkaz jehož přítomnost zjišťujeme
	 * @return true pokud příkaz v systému existuje
	 */
	public static boolean isPříkazDostupný(String příkaz) {
		try {
			Runtime r = Runtime.getRuntime();
			Process p = r.exec(new String[]{PŘÍKAZ_WHICH, příkaz});
			p.waitFor();
			return p.exitValue() == 0;
		} catch (Exception e) {
			System.err.printf("Při zjišťování dostupnosti příkazu „%s“ došlo k chybě: %s", příkaz, e.getLocalizedMessage());
			return false;
		}
	}

	/**
	 * Čte proud dat dokud to jde a výsledek pak vrátí jako text.
	 * @param proud vstupní proud
	 * @return obsah proudu jako text
	 * @throws IOException 
	 */
	public static String načtiProud(InputStream proud) throws IOException {
		StringBuilder výsledek = new StringBuilder();
		BufferedReader buf = new BufferedReader(new InputStreamReader(proud));
		while (true) {
			String radek = buf.readLine();
			if (radek == null) {
				break;
			} else {
				výsledek.append(radek);
				výsledek.append("\n");
			}
		}
		return výsledek.toString();
	}
}