root/šablona/funkce/src/cz/frantovo/xmlWebGenerator/NástrojeCLI.java @ 136:d5feb9d8ebc3

Revision 136:d5feb9d8ebc3, 2.3 KB (checked in by František Kučera <franta-hg@…>, 5 years ago)

fix license version: GNU GPLv3

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