franta-hg@61: /** franta-hg@61: * XML Web generátor – program na generování webových stránek franta-hg@61: * Copyright © 2012 František Kučera (frantovo.cz) franta-hg@61: * franta-hg@61: * This program is free software: you can redistribute it and/or modify franta-hg@61: * it under the terms of the GNU General Public License as published by franta-hg@136: * the Free Software Foundation, version 3 of the License. franta-hg@61: * franta-hg@61: * This program is distributed in the hope that it will be useful, franta-hg@61: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@61: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@61: * GNU General Public License for more details. franta-hg@61: * franta-hg@61: * You should have received a copy of the GNU General Public License franta-hg@61: * along with this program. If not, see . franta-hg@61: */ franta-hg@38: package cz.frantovo.xmlWebGenerator; franta-hg@38: franta-hg@38: import java.io.BufferedReader; franta-hg@38: import java.io.IOException; franta-hg@38: import java.io.InputStream; franta-hg@38: import java.io.InputStreamReader; franta-hg@38: franta-hg@38: /** franta-hg@38: * Pomocné funkce pro práci s příkazy franta-hg@76: * franta-hg@76: * Tyto funkce nejsou určené k přímému volání z XSLT. franta-hg@76: * franta-hg@76: * @author František Kučera (frantovo.cz) franta-hg@38: */ franta-hg@38: public class NástrojeCLI { franta-hg@87: franta-hg@38: private static final String PŘÍKAZ_WHICH = "which"; franta-hg@38: franta-hg@38: /** franta-hg@38: * Pomocí programu which zjistí, jestli je daný příkaz v systému přítomný. franta-hg@38: * @param příkaz jehož přítomnost zjišťujeme franta-hg@38: * @return true pokud příkaz v systému existuje franta-hg@38: */ franta-hg@38: public static boolean isPříkazDostupný(String příkaz) { franta-hg@38: try { franta-hg@38: Runtime r = Runtime.getRuntime(); franta-hg@38: Process p = r.exec(new String[]{PŘÍKAZ_WHICH, příkaz}); franta-hg@38: p.waitFor(); franta-hg@38: return p.exitValue() == 0; franta-hg@38: } catch (Exception e) { franta-hg@38: System.err.printf("Při zjišťování dostupnosti příkazu „%s“ došlo k chybě: %s", příkaz, e.getLocalizedMessage()); franta-hg@38: return false; franta-hg@38: } franta-hg@38: } franta-hg@87: franta-hg@38: /** franta-hg@38: * Čte proud dat dokud to jde a výsledek pak vrátí jako text. franta-hg@38: * @param proud vstupní proud franta-hg@38: * @return obsah proudu jako text franta-hg@38: * @throws IOException franta-hg@38: */ franta-hg@38: public static String načtiProud(InputStream proud) throws IOException { franta-hg@38: StringBuilder výsledek = new StringBuilder(); franta-hg@38: BufferedReader buf = new BufferedReader(new InputStreamReader(proud)); franta-hg@38: while (true) { franta-hg@38: String radek = buf.readLine(); franta-hg@38: if (radek == null) { franta-hg@38: break; franta-hg@38: } else { franta-hg@38: výsledek.append(radek); franta-hg@38: výsledek.append("\n"); franta-hg@38: } franta-hg@38: } franta-hg@38: return výsledek.toString(); franta-hg@38: } franta-hg@38: } franta-hg@87: