šablona/funkce/src/cz/frantovo/xmlWebGenerator/makra/Skriptování.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 05 Jul 2012 20:13:56 +0200
changeset 108 8d9cab64c335
parent 107 379a2a893fd1
child 113 18bf0044f5ab
permissions -rw-r--r--
#20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
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;
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    21
import static cz.frantovo.xmlWebGenerator.Xmlns.*;
95
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
    22
import java.io.ByteArrayInputStream;
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
import java.io.File;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
import java.io.PrintStream;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
import java.net.URI;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
import java.util.Collections;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
import java.util.HashMap;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
import java.util.Map;
95
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
    29
import javax.xml.parsers.DocumentBuilder;
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
    30
import javax.xml.parsers.DocumentBuilderFactory;
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    31
import javax.xml.transform.Source;
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    32
import javax.xml.transform.dom.DOMSource;
95
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
    33
import org.w3c.dom.Document;
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    34
import org.w3c.dom.Node;
94
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
/**
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
 * 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
    38
 *
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
 * @author František Kučera (frantovo.cz)
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
public class Skriptování {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
108
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    43
	private enum FORMÁT {
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    44
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    45
		xml,
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    46
		xhtml,
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    47
		text
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    48
	}
94
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
	 * klíč = jazyk – např. bash
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	 * hodnota = interpret – např. /bin/bash
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	 */
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	private static final Map<String, String> interpreti;
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	static {
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		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
    57
		podporovanýJazyk.put("bash", "/bin/bash");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		podporovanýJazyk.put("perl", "/usr/bin/perl");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		podporovanýJazyk.put("php", "/usr/bin/php");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		podporovanýJazyk.put("python", "/usr/bin/python");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		interpreti = Collections.unmodifiableMap(podporovanýJazyk);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	}
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	/**
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	 * 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
    66
	 * 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
    67
	 *
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    68
	 * @param skriptText skript k vykonání
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    69
	 * @param skriptSoubor cesta k souboru se skriptem/programem
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	 * @param jazyk programovací jazyk
95
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
    71
	 * @param výstupníFormát text (výchozí) | xml (v tom případě kontrolujeme validitu)
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	 * @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
    73
	 * @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
    74
	 * @param perexStránky perex stránky → proměnná prostředí
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    75
	 * @return výstup příkazu buď jako textový řetězec nebo jako XML (DOMSource)
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	 */
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    77
	public static Source interpretuj(String skriptText, String skriptSoubor, String jazyk, String výstupníFormát, String uriStránky, String nadpisStránky, String perexStránky) throws Exception {
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    78
		String výstupSkriptu = získejVýstupSkriptu(skriptText, skriptSoubor, jazyk, uriStránky, nadpisStránky, perexStránky);
108
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    79
		return vyrobXml(výstupSkriptu, zjistiFormát(výstupníFormát));
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    80
	}
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    81
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    82
	private static String získejVýstupSkriptu(String skriptText, String skriptSoubor, String jazyk, String uriStránky, String nadpisStránky, String perexStránky) throws Exception {
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
    83
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
		try {
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    85
			if (isNeprázdný(skriptSoubor)) {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    86
				System.err.println("\tInterpretuji skript ze souboru: " + skriptSoubor);
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
			} else {
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    88
				System.err.println("\tInterpretuji skript v jazyce:   " + jazyk);
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    89
			}
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    90
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    91
			File souborStránky = new File(new URI(uriStránky));
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    92
			File f;
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    93
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    94
			if (isNeprázdný(skriptText)) {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    95
				/** Skript je zadán uvnitř elementu přímo ve stránce */
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    96
				String interpret = interpreti.get(jazyk);
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    97
				if (interpret == null) {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    98
					throw new Exception("Neznámý skriptovací jazyk: " + jazyk);
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
    99
				}
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   100
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   101
				f = File.createTempFile("xml-web-generátor-", ".skript");
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
				f.deleteOnExit();
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   103
				f.setExecutable(true);
94
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
				PrintStream ps = new PrintStream(f);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
				ps.print("#!");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
				ps.println(interpret);
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
				ps.println();
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   109
				ps.print(skriptText);
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
				ps.close();
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   111
			} else if (isNeprázdný(skriptSoubor)) {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   112
				/** Skript/program je uložen v externím souboru */
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   113
				if (skriptSoubor.startsWith(File.separator)) {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   114
					/** absolutní cesta */
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   115
					f = new File(skriptSoubor);
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   116
				} else {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   117
					/** relativní cesta */
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   118
					f = new File(souborStránky.getParent(), File.separatorChar + skriptSoubor);
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   119
				}
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   120
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   121
				if (!f.canExecute()) {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   122
					throw new Exception("Soubor se skriptem není spustitelný → nastavte: chmod +x " + f);
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   123
				}
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   124
			} else {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   125
				throw new Exception("Musí být vyplněn text skriptu, nebo cesta k souboru.");
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   126
			}
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   129
			String[] prostředí = new String[]{
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   130
				"LANG=" + System.getenv("LANG"),
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   131
				"USER=" + System.getenv("USER"),
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   132
				"XWG_SKRIPTOVANI_JAVA=" + "šablona" + File.separator + "funkce" + File.separator + "src" + File.separator + Skriptování.class.getName().replaceAll("\\.", File.separator) + ".java",
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   133
				"XWG_STRANKA_URI=" + uriStránky, // env:URI aktuálně zpracovávané stránky
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   134
				"XWG_STRANKA_SOUBOR=" + souborStránky.getAbsolutePath(), // env:absolutní cesta k souboru
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   135
				"XWG_STRANKA_NADPIS=" + nadpisStránky, // env:nadpis stránky
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   136
				"XWG_STRANKA_PEREX=" + perexStránky // env:perex stránky
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   137
			};
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   139
			Runtime r = Runtime.getRuntime();
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   140
			Process p = r.exec(new String[]{f.getAbsolutePath()}, prostředí);
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   141
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   142
			String výsledek = načtiProud(p.getInputStream());
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   143
			String chyby = načtiProud(p.getErrorStream());
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   144
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   145
			p.waitFor();
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   146
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   147
			if (p.exitValue() == 0) {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   148
				if (chyby.length() > 0) {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   149
					System.err.println("--- Chybový výstup skriptu -----");
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   150
					System.err.println(chyby);
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   151
					System.err.println("--------------------------------");
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   152
					System.err.println("Nicméně skript skončil úspěšně, takže pokračujeme dál.");
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   153
				}
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   154
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   155
				return výsledek.trim();
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   156
			} else {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   157
				System.err.println("--- Standardní výstup skriptu: -----");
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   158
				System.err.println(výsledek);
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   159
				System.err.println("--- Cyhbový výstup skriptu: ---------");
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   160
				System.err.println(chyby);
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   161
				System.err.println("--------------------------------------");
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   162
				throw new Exception("Návratová hodnota: " + p.exitValue());
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   163
			}
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   164
		} catch (Exception e) {
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   165
			System.err.println("Došlo k chybě při vykonávání skriptu.");
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
			System.err.println("--------");
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   167
			System.err.println(skriptText);
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
			System.err.println("--------");
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
			e.printStackTrace(System.err);
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   170
			throw e;
95
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
   171
		}
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
   172
	}
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
   173
103
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   174
	private static boolean isNeprázdný(String s) {
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   175
		return !(s == null || s.trim().isEmpty());
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   176
	}
aa91d1c6d4c1 #20 Skriptování: možnost spouštět skripty ze souborů (atribut: src).
František Kučera <franta-hg@frantovo.cz>
parents: 101
diff changeset
   177
108
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   178
	private static FORMÁT zjistiFormát(String výstupníFormát) {
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   179
		try {
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   180
			return FORMÁT.valueOf(výstupníFormát);
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   181
		} catch (NullPointerException e) {
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   182
			return FORMÁT.text;
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   183
		} catch (IllegalArgumentException e) {
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   184
			return FORMÁT.text;
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   185
		}
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   186
	}
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   187
95
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
   188
	/**
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   189
	 * @param zadání výstup vygenerovaný skriptem
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   190
	 * @param xmlFormát formát zadání: true = xml fragment | false = prostý text
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   191
	 * @return xml fragment nebo prostý text zabalený do html/body
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   192
	 * @throws Exception
95
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
   193
	 */
108
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   194
	private static Source vyrobXml(String zadání, FORMÁT formát) throws Exception {
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   195
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   196
		DocumentBuilder db = dbf.newDocumentBuilder();
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   197
		Document d;
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   198
108
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   199
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   200
		if (formát == FORMÁT.text) {
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   201
			d = db.newDocument();
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   202
			Node html = d.createElementNS(XHTML, "html");
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   203
			Node body = d.createElementNS(XHTML, "body");
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   204
			Node text = d.createTextNode(zadání);
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   205
			body.appendChild(text);
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   206
			html.appendChild(body);
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   207
			d.appendChild(html);
108
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   208
		} else {
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   209
			if (formát == FORMÁT.xhtml) {
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   210
				zadání = "<html xmlns='" + XHTML + "' xmlns:m='" + MAKRO + "'><body>" + zadání + "</body></html>";
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   211
			}
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   212
			try {
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   213
				d = db.parse(new ByteArrayInputStream(zadání.getBytes()));
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   214
			} catch (Exception e) {
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   215
				System.err.println("Chyba: Skript vrátil neplatné XML.");
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   216
				throw e;
8d9cab64c335 #20 Skriptování: výstupní formáty: text (prostý), xhtml (fragment), xml (celý dokument, může mít XML deklaraci).
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   217
			}
95
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
   218
		}
107
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   219
379a2a893fd1 #20 Skriptování: Makra ve skriptech a Skripty v makrech + výpis verzí z Mercurialu.
František Kučera <franta-hg@frantovo.cz>
parents: 103
diff changeset
   220
		return new DOMSource(d);
95
eea9c4713045 #20 Skriptování: podpora výstupu ve formátu XML (musí být validní)
František Kučera <franta-hg@frantovo.cz>
parents: 94
diff changeset
   221
	}
94
4b3ba32f613c Skriptování: první verze
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   222
}