view src/kryshen/tema/Tema.java @ 23:55fe63bb7858

Fix error reporting.
author Mikhail Kryshen <mikhail@kryshen.net>
date Wed, 22 Apr 2009 03:30:59 +0400
parents 5a38106d7a21
children
line source
1 /*
2 * Copyright 2006-2009 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 */
21 package kryshen.tema;
23 import java.awt.EventQueue;
24 import java.io.BufferedReader;
25 import java.io.BufferedWriter;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.io.OutputStream;
33 import java.io.OutputStreamWriter;
34 import java.io.PrintStream;
35 import java.io.Writer;
36 import java.lang.reflect.InvocationTargetException;
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 2009 Mikhail Kryshen <mikhail@kryshen.net>";
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, InvocationTargetException {
78 Options options = new Options();
80 options.addOption(null, "demo", false, "demo mode");
81 options.addOption("v", "version", false,
82 "print the version information and exit");
83 options.addOption("h", "help", false,
84 "print this help message");
85 options.addOption("o", "output", true,
86 "output file");
87 options.addOption(null, "log", true,
88 "log all error messages to file");
89 options.addOption(null, "input-encoding", true,
90 "input encoding");
91 options.addOption(null, "output-encoding", true,
92 "output encoding");
94 CommandLineParser parser = new GnuParser();
95 CommandLine line;
97 try {
98 line = parser.parse(options, args );
99 } catch (ParseException ex) {
100 System.err.println(ex.getMessage());
101 System.exit(1);
102 return; // to avoid "not initialized" compile-time errors.
103 }
105 if (line.hasOption("version")) {
106 System.err.println(TITLE + " " + VERSION);
107 System.err.println(COPYRIGHT);
108 System.err.println(LICENSE);
109 return;
110 }
112 if (line.hasOption("help")) {
113 HelpFormatter formatter = new HelpFormatter();
114 formatter.printHelp("tema [options] [files]", options);
115 return;
116 }
118 if (line.hasOption("input-encoding")) {
119 inputEncoding = line.getOptionValue("input-encoding");
120 }
122 if (line.hasOption("output-encoding")) {
123 outputEncoding = line.getOptionValue("output-encoding");
124 }
126 if (line.hasOption("log")) {
127 System.setErr(new PrintStream
128 (new FileOutputStream(line.getOptionValue("log"))));
129 }
131 if (line.hasOption("demo")) {
132 EventQueue.invokeLater(new Runnable() {
133 public void run() {
134 // Open the demo console.
135 DemoFrame df = new DemoFrame();
136 df.pack();
137 df.setVisible(true);
138 }
139 });
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 }