view src/kryshen/tema/Tema.java @ 16:5a38106d7a21

Update version info.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:58:28 +0300
parents e9d13c7ffeb1
children 55fe63bb7858
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 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 EventQueue.invokeLater(new Runnable() {
137 public void run() {
138 // Open the demo console.
139 DemoFrame df = new DemoFrame();
140 df.pack();
141 df.setVisible(true);
142 }
143 });
144 return;
145 }
147 Writer out;
149 if (line.hasOption("output")) {
150 File outfile = new File(line.getOptionValue("output"));
151 out = createFileWriter(outfile);
152 } else {
153 out = new OutputStreamWriter(System.out, outputEncoding) {
154 @Override
155 public void write(int c) throws IOException {
156 super.write(c);
157 if (c == '\r' || c == '\n')
158 flush();
159 }
161 @Override
162 public void write(char cbuf[], int off, int len)
163 throws IOException {
164 super.write(cbuf, off, len);
165 flush();
166 }
168 @Override
169 public void write(String str, int off, int len)
170 throws IOException {
171 super.write(str, off, len);
172 flush();
173 }
174 };
175 }
177 String[] files = line.getArgs();
179 if (files.length == 0) {
180 TemplateReader in = createTemplateReader(System.in);
181 if (!process(in, out)) {
182 System.exit(1);
183 }
184 }
186 for (String file: files) {
187 TemplateReader in = createTemplateReader(new File(file));
188 if (!process(in, out)) {
189 System.exit(1);
190 }
191 }
193 out.close();
194 }
196 private static boolean process(TemplateReader in, Writer out)
197 throws IOException {
199 TemplateParser p = new TemplateParser();
200 TemplateException ex = null;
202 p.getContext().set("input_encoding", inputEncoding);
203 p.getContext().set("output_encoding", outputEncoding);
205 try {
206 p.parse(in, out);
207 } catch (TemplateException e) {
208 ex = e;
209 } finally {
210 in.close();
211 }
213 if (ex != null) {
214 System.err.println(ex.getMessage());
215 if (ex.getCause() != null) {
216 System.err.println("Caused by " + ex.getCause());
217 }
218 return false;
219 }
221 return true;
222 }
224 public static BufferedWriter createFileWriter(File file)
225 throws IOException {
227 OutputStream os = new FileOutputStream(file);
228 return new BufferedWriter
229 (new OutputStreamWriter(os, Tema.outputEncoding));
230 }
232 public static BufferedReader createFileReader(File file)
233 throws IOException {
235 InputStream is = new FileInputStream(file);
236 return new BufferedReader
237 (new InputStreamReader(is, Tema.inputEncoding));
238 }
240 public static TemplateReader createTemplateReader(File file)
241 throws IOException {
243 return new TemplateReader(createFileReader(file), file.getPath());
244 }
246 public static TemplateReader createTemplateReader(InputStream in)
247 throws IOException {
249 return new TemplateReader(
250 new InputStreamReader(in, Tema.inputEncoding));
251 }
252 }