šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Skriptování.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 23 Jun 2012 18:00:41 +0200
changeset 94 4b3ba32f613c
child 95 eea9c4713045
permissions -rw-r--r--
Skriptování: první verze
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * XML Web generátor – program na generování webových stránek
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2012 František Kučera (frantovo.cz)
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package cz.frantovo.xmlWebGenerator.makra;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
import static cz.frantovo.xmlWebGenerator.NástrojeCLI.načtiProud;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
import java.io.File;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
import java.io.PrintStream;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
import java.net.URI;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
import java.util.Collections;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
import java.util.HashMap;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
import java.util.Map;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
/**
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
 * Provedeme skript a do stránky vložíme jeho výstup.
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 *
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
 * @author František Kučera (frantovo.cz)
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 */
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
public class Skriptování {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	/**
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	 * klíč = jazyk – např. bash
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	 * hodnota = interpret – např. /bin/bash
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	 */
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	private static final Map<String, String> interpreti;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	static {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		Map<String, String> podporovanýJazyk = new HashMap<String, String>();
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		podporovanýJazyk.put("bash", "/bin/bash");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		podporovanýJazyk.put("perl", "/usr/bin/perl");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		podporovanýJazyk.put("php", "/usr/bin/php");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		podporovanýJazyk.put("python", "/usr/bin/python");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		interpreti = Collections.unmodifiableMap(podporovanýJazyk);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	}
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	/**
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	 * TODO: podporovat i složitější scénáře (např. kompilaci),
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	 * než jen vložení do souboru a přidání správného záhlaví.
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	 *
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	 * @param skript program k vykonání
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	 * @param jazyk programovací jazyk
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	 * @param uriStránky URI aktuálně generované stránky → proměnná prostředí
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	 * @param nadpisStránky nadpis stránky → proměnná prostředí
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	 * @param perexStránky perex stránky → proměnná prostředí
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	 * @return výstup příkazu
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	 */
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	public static String interpretuj(String skript, String jazyk, String uriStránky, String nadpisStránky, String perexStránky) {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		try {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			System.err.println("\tInterpretuji skript v jazyce: " + jazyk);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			String interpret = interpreti.get(jazyk);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
			if (interpret == null) {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
				System.err.println("Neznámý skriptovací jazyk: " + jazyk);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
				return null;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
			} else {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
				File f = File.createTempFile("xml-web-generátor-", ".skript");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
				f.deleteOnExit();
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
				PrintStream ps = new PrintStream(f);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
				ps.print("#!");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
				ps.println(interpret);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
				ps.println();
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				ps.print(skript);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
				ps.close();
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
				f.setExecutable(true);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
				String[] prostředí = new String[]{
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
					"LANG=" + System.getenv("LANG"),
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
					"USER=" + System.getenv("USER"),
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
					"XWG_STRANKA_URI=" + uriStránky,
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
					"XWG_STRANKA_SOUBOR=" + (new File(new URI(uriStránky)).getAbsolutePath()),
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
					"XWG_STRANKA_NADPIS=" + nadpisStránky,
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
					"XWG_STRANKA_PEREX=" + perexStránky
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
				};
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
				Runtime r = Runtime.getRuntime();
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
				Process p = r.exec(new String[]{f.getAbsolutePath()}, prostředí);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
				String výsledek = načtiProud(p.getInputStream());
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
				String chyby = načtiProud(p.getErrorStream());
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
				p.waitFor();
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
				/**
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
				 * TODO: podporovat zvláštní návratový kód, kterým skript řekne,
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
				 * že výstupem je XML a má se vložit jako fragment do dokumentu,
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
				 * ne jako prostý text.
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
				 */
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
				if (p.exitValue() == 0) {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
					if (chyby.length() > 0) {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
						System.err.println("--- Cyhbový výstup skriptu -----");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
						System.err.println(chyby);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
						System.err.println("--------------------------------");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
					}
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
					return výsledek.trim();
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
				} else {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
					System.err.println("--- Standardní výstup skriptu: -----");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
					System.err.println(výsledek);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
					System.err.println("--- Cyhbový výstup skriptu: ---------");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
					System.err.println(chyby);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
					System.err.println("--------------------------------------");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
					throw new RuntimeException("Návratová hodnota: " + p.exitValue());
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
				}
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
			}
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
		} catch (Exception e) {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
			System.err.println("Došlo k chybě při vykonávání skriptu v jazyce: " + jazyk);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
			System.err.println("--------");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
			System.err.println(skript);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
			System.err.println("--------");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
			e.printStackTrace(System.err);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
			return null;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
		}
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
	}
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
}