view src/kryshen/tema/Tema.java @ 0:1d2fe61a3a62

Tema 0.1 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 16 May 2006 18:04:09 +0400
parents
children 548a93c24e55
line source
1 /*
2 * Copyright (C) 2005, 2006 Mikhail A. Kryshen
3 *
4 * $Id: Tema.java,v 1.1.1.1 2006/05/16 14:04:09 mikhail Exp $
5 */
7 package kryshen.tema;
9 import java.sql.Driver;
10 import java.sql.DriverManager;
11 import java.sql.Connection;
12 import java.sql.Statement;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.ResultSetMetaData;
16 import java.sql.SQLException;
18 import java.util.Map;
19 import java.util.HashMap;
20 import java.util.Properties;
21 import java.util.List;
22 import java.util.Collections;
24 import java.io.*;
26 /**
27 * Tema main class.
28 *
29 * @author Mikhail A. Kryshen
30 */
31 public class Tema {
32 private static final String CONFIG_FILE = "tema.properties";
33 private static final String ENV_PREFIX = "kryshen.tema.";
35 private static Properties config = new Properties();
36 static Connection connection = null;
38 private static String inputEncoding;
39 private static String outputEncoding;
41 static Map<File, String> fileCache = null;
43 public static void main(String[] args)
44 throws IOException, TemplateException, SQLException,
45 ClassNotFoundException, InstantiationException,
46 IllegalAccessException {
48 InputStream configIn = new FileInputStream(CONFIG_FILE);
49 config.load(configIn);
50 configIn.close();
52 String logFile = getProperty("log");
53 if (logFile != null && logFile.length() > 0)
54 System.setErr(new PrintStream(new FileOutputStream(logFile)));
56 if (Boolean.parseBoolean(getProperty("cache_read")) == true)
57 fileCache = new HashMap<File, String>();
59 inputEncoding = getProperty("input_encoding", "iso-8859-1");
60 outputEncoding = getProperty("output_encoding", "iso-8859-1");
62 String resourceProperty = getProperty("resource");
64 if (resourceProperty != null) {
65 String driverProperty = getProperty("driver");
66 if (driverProperty != null) {
67 // Register driver.
68 Driver d = (Driver)Class.forName(driverProperty).newInstance();
69 }
71 // Open connection.
72 connection = DriverManager.getConnection(resourceProperty);
73 }
75 // String maxFilesString = getProperty("max_output_files");
76 // int maxFiles = -1;
77 // if (maxFilesString != null)
78 // maxFiles = Integer.parseInt(maxFilesString);
80 String main = getProperty("main_template");
81 String output = getProperty("output");
83 Writer out;
85 if ("stderr".equals(output))
86 out = new OutputStreamWriter(System.err);
87 else
88 out = new OutputStreamWriter(System.out);
90 InputStream is = new FileInputStream(main);
91 Reader in = new BufferedReader(new InputStreamReader(is, inputEncoding));
93 TemplateParser p = new TemplateParser();
94 try {
95 p.parse(in, out);
96 } finally {
97 in.close();
98 out.flush();
99 }
101 System.err.println("Done");
102 }
104 // private static void readEscapes() {
105 // for (int i = 1;; i++) {
106 // String pattern = getProperty("escape_char_" + i);
108 // if (pattern == null) break;
110 // escapes.put(pattern.charAt(0),
111 // getProperty("escape_string_" + i));
112 // }
113 // }
115 /**
116 * Get configuration property.
117 */
118 static String getProperty(String name) {
119 String value = System.getProperty(ENV_PREFIX + name);
120 if (value != null) return value;
121 return config.getProperty(name);
122 }
124 /**
125 * Get configuration property.
126 */
127 static String getProperty(String name, String fallback) {
128 String value = getProperty(name);
129 if (value != null) return value;
130 return fallback;
131 }
133 /**
134 * Process subquery and template.
135 *
136 * @param template template to fill with data.
137 * @param ps query statement to execute.
138 * @param args list of query parameters.
139 * @param superParser invoking object.
140 * @param out Writer to output processed data.
141 *
142 * @return number of processed rows in query result.
143 */
144 static int query(String template, PreparedStatement ps,
145 List<String> args,
146 TemplateParser superParser,
147 Writer out)
148 throws IOException, SQLException, TemplateException {
150 StringReader templateReader = new StringReader(template);
152 int i = 1;
154 for (String arg : args) {
155 ps.setString(i++, arg);
156 }
158 ResultSet r = ps.executeQuery();
159 ResultSetMetaData rm = r.getMetaData();
160 int columnCount = rm.getColumnCount();
162 String[] names = new String[columnCount];
164 for (int j = 0; j < columnCount; j++) {
165 names[j] = rm.getColumnName(j + 1);
166 }
168 TemplateParser p = new TemplateParser(superParser);
170 for (i = 1; r.next(); i++) {
171 p.clearValues();
173 for (int j = 0; j < columnCount; j++) {
174 Object value = r.getObject(j + 1);
175 if (r.wasNull()) value = null;
177 p.setValue(names[j], value);
178 }
180 p.setValue("NUMBER", i);
181 p.parse(templateReader, out);
182 templateReader.reset();
183 }
185 r.close();
186 ps.clearParameters();
187 return i - 1;
188 }
190 static Writer createFileWriter(String filename) throws IOException {
191 OutputStream os = new FileOutputStream(filename);
192 return new OutputStreamWriter(os, Tema.outputEncoding);
193 }
195 static Reader createFileReader(File file) throws IOException {
196 InputStream is = new FileInputStream(file);
197 return new InputStreamReader(is, Tema.inputEncoding);
198 }
200 static Reader createCachedFileReader(File file) throws IOException {
201 if (fileCache != null) return new StringReader(readFile(file));
202 else return createFileReader(file);
203 }
205 /**
206 * Read text file.
207 */
208 static String readFile(File file) throws IOException {
209 String data;
211 if (fileCache != null) {
212 data = fileCache.get(file);
213 if (data != null) return data;
214 }
216 Reader r = new BufferedReader(createFileReader(file));
218 // FIXME: possible overflow (long -> int).
219 StringBuffer sb = new StringBuffer((int)file.length());
221 try {
222 for (int c = r.read(); c >= 0; c = r.read()) {
223 sb.append((char)c);
224 }
225 } finally {
226 r.close();
227 }
229 data = sb.toString();
231 if (fileCache != null) {
232 fileCache.put(file, data);
233 }
235 return data;
236 }
237 }