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