root/šablona/funkce/src/cz/frantovo/xmlWebGenerator/NástrojeCLI.java @ 61:9503eb8377f1

Revision 61:9503eb8377f1, 2.2 KB (checked in by František Kučera <franta-hg@…>, 12 years ago)

Licence: informace o licenci v každém souboru.

Line 
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 */
18package cz.frantovo.xmlWebGenerator;
19
20import java.io.BufferedReader;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.InputStreamReader;
24
25/**
26 * Pomocné funkce pro práci s příkazy
27 * @author fiki
28 */
29public class NástrojeCLI {
30       
31        private static final String PŘÍKAZ_WHICH = "which";
32
33        /**
34         * Pomocí programu which zjistí, jestli je daný příkaz v systému přítomný.
35         * @param příkaz jehož přítomnost zjišťujeme
36         * @return true pokud příkaz v systému existuje
37         */
38        public static boolean isPříkazDostupný(String příkaz) {
39                try {
40                        Runtime r = Runtime.getRuntime();
41                        Process p = r.exec(new String[]{PŘÍKAZ_WHICH, příkaz});
42                        p.waitFor();
43                        return p.exitValue() == 0;
44                } catch (Exception e) {
45                        System.err.printf("Při zjišťování dostupnosti příkazu „%s“ došlo k chybě: %s", příkaz, e.getLocalizedMessage());
46                        return false;
47                }
48        }
49       
50        /**
51         * Čte proud dat dokud to jde a výsledek pak vrátí jako text.
52         * @param proud vstupní proud
53         * @return obsah proudu jako text
54         * @throws IOException
55         */
56        public static String načtiProud(InputStream proud) throws IOException {
57                StringBuilder výsledek = new StringBuilder();
58                BufferedReader buf = new BufferedReader(new InputStreamReader(proud));
59                while (true) {
60                        String radek = buf.readLine();
61                        if (radek == null) {
62                                break;
63                        } else {
64                                výsledek.append(radek);
65                                výsledek.append("\n");
66                        }
67                }
68                return výsledek.toString();
69        }
70}
Note: See TracBrowser for help on using the browser.