view src/kryshen/tema/Tema.java @ 2:6c41a0b43e58

Tema 0.3 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 19 Feb 2008 20:32:17 +0300
parents 548a93c24e55
children ac5f1bc82b13
line source
1 /*
2 * Copyright 2006-2008 Mikhail Kryshen
3 *
4 * This file is part of Tema.
5 *
6 * Tema is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * Tema is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the
17 * GNU Lesser General Public License along with Tema.
18 * If not, see <http://www.gnu.org/licenses/>.
19 *
20 * $Id: Tema.java,v 1.42 2008/02/19 17:14:34 mikhail Exp $
21 */
23 package kryshen.tema;
25 import java.io.BufferedReader;
26 import java.io.BufferedWriter;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.OutputStream;
34 import java.io.OutputStreamWriter;
35 import java.io.PrintStream;
36 import java.io.Writer;
37 import java.nio.charset.Charset;
38 import java.sql.SQLException;
39 import kryshen.tema.demo.DemoFrame;
40 import kryshen.tema.io.TemplateReader;
41 import org.apache.commons.cli.CommandLine;
42 import org.apache.commons.cli.CommandLineParser;
43 import org.apache.commons.cli.GnuParser;
44 import org.apache.commons.cli.HelpFormatter;
45 import org.apache.commons.cli.Options;
46 import org.apache.commons.cli.ParseException;
48 /**
49 * Tema main class.
50 *
51 * @author Mikhail Kryshen
52 */
53 public class Tema {
54 public static final String TITLE = "Tema";
55 public static final String VERSION = "0.3";
56 public static final String AUTHOR = "Mikhail Kryshen";
57 public static final String COPYRIGHT =
58 "Copyright 2008 Mikhail Kryshen <mikhail@kryshen.pp.ru>";
59 public static final String LICENSE =
60 "This is free software. " +
61 "You may redistribute copies of it under the terms of" +
62 System.getProperty("line.separator") +
63 "the GNU Lesser General Public License version 3 or later" +
64 System.getProperty("line.separator") +
65 "<http://gnu.org/licenses/lgpl.html>.";
67 private static final String DEFAULT_CHARSET =
68 Charset.defaultCharset().name();
70 private static String inputEncoding = DEFAULT_CHARSET;
71 private static String outputEncoding = DEFAULT_CHARSET;
73 public static void main(String[] args)
74 throws IOException, SQLException,
75 ClassNotFoundException, InstantiationException,
76 IllegalAccessException {
78 boolean demo = false;
79 boolean version = false;
80 boolean help = false;
82 Options options = new Options();
84 options.addOption(null, "demo", false, "demo mode");
85 options.addOption("v", "version", false,
86 "print the version information and exit");
87 options.addOption("h", "help", false,
88 "print this help message");
89 options.addOption("o", "output", true,
90 "output file");
91 options.addOption(null, "log", true,
92 "log all error messages to file");
93 options.addOption(null, "input-encoding", true,
94 "input encoding");
95 options.addOption(null, "output-encoding", true,
96 "output encoding");
98 CommandLineParser parser = new GnuParser();
99 CommandLine line;
101 try {
102 line = parser.parse(options, args );
103 } catch (ParseException ex) {
104 System.err.println(ex.getMessage());
105 System.exit(1);
106 return; // to avoid "not initialized" compile-time errors.
107 }
109 if (line.hasOption("version")) {
110 System.err.println(TITLE + " " + VERSION);
111 System.err.println(COPYRIGHT);
112 System.err.println(LICENSE);
113 return;
114 }
116 if (line.hasOption("help")) {
117 HelpFormatter formatter = new HelpFormatter();
118 formatter.printHelp("tema [options] [files]", options);
119 return;
120 }
122 if (line.hasOption("input-encoding")) {
123 inputEncoding = line.getOptionValue("input-encoding");
124 }
126 if (line.hasOption("output-encoding")) {
127 outputEncoding = line.getOptionValue("output-encoding");
128 }
130 if (line.hasOption("log")) {
131 System.setErr(new PrintStream
132 (new FileOutputStream(line.getOptionValue("log"))));
133 }
135 if (line.hasOption("demo")) {
136 // Open the demo console.
137 DemoFrame df = new DemoFrame();
138 df.pack();
139 df.setVisible(true);
140 return;
141 }
143 Writer out;
145 if (line.hasOption("output")) {
146 File outfile = new File(line.getOptionValue("output"));
147 out = createFileWriter(outfile);
148 } else {
149 out = new OutputStreamWriter(System.out, outputEncoding) {
150 @Override
151 public void write(int c) throws IOException {
152 super.write(c);
153 if (c == '\r' || c == '\n')
154 flush();
155 }
157 @Override
158 public void write(char cbuf[], int off, int len)
159 throws IOException {
160 super.write(cbuf, off, len);
161 flush();
162 }
164 @Override
165 public void write(String str, int off, int len)
166 throws IOException {
167 super.write(str, off, len);
168 flush();
169 }
170 };
171 }
173 String[] files = line.getArgs();
175 if (files.length == 0) {
176 TemplateReader in = createTemplateReader(System.in);
177 if (!process(in, out)) {
178 System.exit(1);
179 }
180 }
182 for (String file: files) {
183 TemplateReader in = createTemplateReader(new File(file));
184 if (!process(in, out)) {
185 System.exit(1);
186 }
187 }
189 out.close();
190 }
192 private static boolean process(TemplateReader in, Writer out)
193 throws IOException {
195 TemplateParser p = new TemplateParser();
196 TemplateException ex = null;
198 p.getContext().set("input_encoding", inputEncoding);
199 p.getContext().set("output_encoding", outputEncoding);
201 try {
202 p.parse(in, out);
203 } catch (TemplateException e) {
204 ex = e;
205 } finally {
206 in.close();
207 }
209 if (ex != null) {
210 System.err.println(ex.getMessage());
211 if (ex.getCause() != null) {
212 System.err.println("Caused by " + ex.getCause());
213 }
214 return false;
215 }
217 return true;
218 }
220 public static BufferedWriter createFileWriter(File file)
221 throws IOException {
223 OutputStream os = new FileOutputStream(file);
224 return new BufferedWriter
225 (new OutputStreamWriter(os, Tema.outputEncoding));
226 }
228 public static BufferedReader createFileReader(File file)
229 throws IOException {
231 InputStream is = new FileInputStream(file);
232 return new BufferedReader
233 (new InputStreamReader(is, Tema.inputEncoding));
234 }
236 public static TemplateReader createTemplateReader(File file)
237 throws IOException {
239 return new TemplateReader(createFileReader(file), file.getPath());
240 }
242 public static TemplateReader createTemplateReader(InputStream in)
243 throws IOException {
245 return new TemplateReader(
246 new InputStreamReader(in, Tema.inputEncoding));
247 }
248 }