view src/kryshen/tema/Tema.java @ 13:ac5f1bc82b13

Fix the demo console initialization.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 16:00:55 +0300
parents 6c41a0b43e58
children e9d13c7ffeb1
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.awt.EventQueue;
26 import java.io.BufferedReader;
27 import java.io.BufferedWriter;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.io.OutputStream;
35 import java.io.OutputStreamWriter;
36 import java.io.PrintStream;
37 import java.io.Writer;
38 import java.lang.reflect.InvocationTargetException;
39 import java.nio.charset.Charset;
40 import java.sql.SQLException;
41 import kryshen.tema.demo.DemoFrame;
42 import kryshen.tema.io.TemplateReader;
43 import org.apache.commons.cli.CommandLine;
44 import org.apache.commons.cli.CommandLineParser;
45 import org.apache.commons.cli.GnuParser;
46 import org.apache.commons.cli.HelpFormatter;
47 import org.apache.commons.cli.Options;
48 import org.apache.commons.cli.ParseException;
50 /**
51 * Tema main class.
52 *
53 * @author Mikhail Kryshen
54 */
55 public class Tema {
56 public static final String TITLE = "Tema";
57 public static final String VERSION = "0.3";
58 public static final String AUTHOR = "Mikhail Kryshen";
59 public static final String COPYRIGHT =
60 "Copyright 2008 Mikhail Kryshen <mikhail@kryshen.pp.ru>";
61 public static final String LICENSE =
62 "This is free software. " +
63 "You may redistribute copies of it under the terms of" +
64 System.getProperty("line.separator") +
65 "the GNU Lesser General Public License version 3 or later" +
66 System.getProperty("line.separator") +
67 "<http://gnu.org/licenses/lgpl.html>.";
69 private static final String DEFAULT_CHARSET =
70 Charset.defaultCharset().name();
72 private static String inputEncoding = DEFAULT_CHARSET;
73 private static String outputEncoding = DEFAULT_CHARSET;
75 public static void main(String[] args)
76 throws IOException, SQLException,
77 ClassNotFoundException, InstantiationException,
78 IllegalAccessException, InvocationTargetException {
80 boolean demo = false;
81 boolean version = false;
82 boolean help = false;
84 Options options = new Options();
86 options.addOption(null, "demo", false, "demo mode");
87 options.addOption("v", "version", false,
88 "print the version information and exit");
89 options.addOption("h", "help", false,
90 "print this help message");
91 options.addOption("o", "output", true,
92 "output file");
93 options.addOption(null, "log", true,
94 "log all error messages to file");
95 options.addOption(null, "input-encoding", true,
96 "input encoding");
97 options.addOption(null, "output-encoding", true,
98 "output encoding");
100 CommandLineParser parser = new GnuParser();
101 CommandLine line;
103 try {
104 line = parser.parse(options, args );
105 } catch (ParseException ex) {
106 System.err.println(ex.getMessage());
107 System.exit(1);
108 return; // to avoid "not initialized" compile-time errors.
109 }
111 if (line.hasOption("version")) {
112 System.err.println(TITLE + " " + VERSION);
113 System.err.println(COPYRIGHT);
114 System.err.println(LICENSE);
115 return;
116 }
118 if (line.hasOption("help")) {
119 HelpFormatter formatter = new HelpFormatter();
120 formatter.printHelp("tema [options] [files]", options);
121 return;
122 }
124 if (line.hasOption("input-encoding")) {
125 inputEncoding = line.getOptionValue("input-encoding");
126 }
128 if (line.hasOption("output-encoding")) {
129 outputEncoding = line.getOptionValue("output-encoding");
130 }
132 if (line.hasOption("log")) {
133 System.setErr(new PrintStream
134 (new FileOutputStream(line.getOptionValue("log"))));
135 }
137 if (line.hasOption("demo")) {
138 EventQueue.invokeLater(new Runnable() {
139 public void run() {
140 // Open the demo console.
141 DemoFrame df = new DemoFrame();
142 df.pack();
143 df.setVisible(true);
144 }
145 });
146 return;
147 }
149 Writer out;
151 if (line.hasOption("output")) {
152 File outfile = new File(line.getOptionValue("output"));
153 out = createFileWriter(outfile);
154 } else {
155 out = new OutputStreamWriter(System.out, outputEncoding) {
156 @Override
157 public void write(int c) throws IOException {
158 super.write(c);
159 if (c == '\r' || c == '\n')
160 flush();
161 }
163 @Override
164 public void write(char cbuf[], int off, int len)
165 throws IOException {
166 super.write(cbuf, off, len);
167 flush();
168 }
170 @Override
171 public void write(String str, int off, int len)
172 throws IOException {
173 super.write(str, off, len);
174 flush();
175 }
176 };
177 }
179 String[] files = line.getArgs();
181 if (files.length == 0) {
182 TemplateReader in = createTemplateReader(System.in);
183 if (!process(in, out)) {
184 System.exit(1);
185 }
186 }
188 for (String file: files) {
189 TemplateReader in = createTemplateReader(new File(file));
190 if (!process(in, out)) {
191 System.exit(1);
192 }
193 }
195 out.close();
196 }
198 private static boolean process(TemplateReader in, Writer out)
199 throws IOException {
201 TemplateParser p = new TemplateParser();
202 TemplateException ex = null;
204 p.getContext().set("input_encoding", inputEncoding);
205 p.getContext().set("output_encoding", outputEncoding);
207 try {
208 p.parse(in, out);
209 } catch (TemplateException e) {
210 ex = e;
211 } finally {
212 in.close();
213 }
215 if (ex != null) {
216 System.err.println(ex.getMessage());
217 if (ex.getCause() != null) {
218 System.err.println("Caused by " + ex.getCause());
219 }
220 return false;
221 }
223 return true;
224 }
226 public static BufferedWriter createFileWriter(File file)
227 throws IOException {
229 OutputStream os = new FileOutputStream(file);
230 return new BufferedWriter
231 (new OutputStreamWriter(os, Tema.outputEncoding));
232 }
234 public static BufferedReader createFileReader(File file)
235 throws IOException {
237 InputStream is = new FileInputStream(file);
238 return new BufferedReader
239 (new InputStreamReader(is, Tema.inputEncoding));
240 }
242 public static TemplateReader createTemplateReader(File file)
243 throws IOException {
245 return new TemplateReader(createFileReader(file), file.getPath());
246 }
248 public static TemplateReader createTemplateReader(InputStream in)
249 throws IOException {
251 return new TemplateReader(
252 new InputStreamReader(in, Tema.inputEncoding));
253 }
254 }