view src/kryshen/tema/Functions.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: Functions.java,v 1.1.1.1 2006/05/16 14:04:09 mikhail Exp $
5 */
7 package kryshen.tema;
9 import java.io.*;
10 import java.util.*;
12 import java.sql.SQLException;
13 import java.sql.PreparedStatement;
15 import kryshen.tema.functions.*;
17 public class Functions {
18 public static final Function GET =
19 new Function("get") {
20 public int invoke(FunctionDataParser fdp, Writer out)
21 throws IOException, TemplateException {
23 String name = fdp.getData();
24 return fdp.getTemplateParser().parseVariable(name, out);
25 }
26 };
28 public static final Function SET =
29 new Function("set") {
30 public int invoke(FunctionDataParser fdp, Writer out)
31 throws IOException, TemplateException {
33 String arg0 = fdp.getNextArg();
34 String arg1 = fdp.getData();
36 fdp.getTemplateParser().setValue(arg0, arg1);
38 out.write(arg0);
39 return 1;
40 }
41 };
43 /* prepare:qury_name sql_statement */
44 public static final Function PREPARE =
45 new Function("prepare") {
46 public int invoke(FunctionDataParser fdp, Writer out)
47 throws IOException, TemplateException {
49 String arg0 = fdp.getNextArg();
50 String data = fdp.getData();
52 PreparedStatement statement;
54 try {
55 statement = Tema.connection.prepareStatement(data);
56 } catch (SQLException e) {
57 throw new TemplateException(e.getMessage(), e);
58 }
60 fdp.getTemplateParser().setValue(arg0, statement);
62 out.write(arg0);
63 return 1;
64 }
65 };
67 public static final Function OPTIONAL =
68 new Function("optional") {
69 public int invoke(FunctionDataParser fdp, Writer out)
70 throws IOException, TemplateException {
72 StringWriter sw = new StringWriter();
73 int s = fdp.parseData(sw);
74 sw.close();
75 String data = sw.toString();
77 if (s > 0) {
78 out.write(data);
79 return 1;
80 } else {
81 return 0;
82 }
83 }
84 };
86 public static final Function IMAGE =
87 new Function("image") {
88 public int invoke(FunctionDataParser fdp, Writer out)
89 throws IOException, TemplateException {
91 String arg0 = fdp.getNextArg();
92 String arg1 = fdp.getNextArg();
93 String arg2 = fdp.getNextArg();
94 int arg3 = fdp.hasMoreData() ?
95 Integer.parseInt(fdp.getNextArg()) : -1;
96 int arg4 = fdp.hasMoreData() ?
97 Integer.parseInt(fdp.getNextArg()) : -1;
99 File source = new File
100 (Tema.getProperty("resource_base"), arg0);
102 try {
103 ImageConverter.convert
104 (source, /* source file */
105 new File(arg1), /* dest file */
106 arg2, /* format */
107 arg3, /* max width */
108 arg4); /* max height */
109 } catch (Exception e) {
110 System.err.println(e);
111 return 0;
112 }
114 out.write(arg1);
115 return 1;
116 }
117 };
119 public static final Function COPY =
120 new Function("copy") {
121 public int invoke(FunctionDataParser fdp, Writer out)
122 throws IOException, TemplateException {
124 String arg0 = fdp.getNextArg();
125 String arg1 = fdp.getNextArg();
127 File source = new File
128 (Tema.getProperty("resource_base"), arg0);
129 File dest = new File(arg1);
131 try {
132 copyFile(source, dest);
133 } catch (IOException e) {
134 System.err.println(e);
135 return 0;
136 }
137 out.write(arg1);
138 return 1;
139 }
140 };
142 /* query:sql_query template arg1 arg2 ... argN */
143 public static final Function QUERY =
144 new Function("query") {
145 public int invoke(FunctionDataParser fdp, Writer out)
146 throws IOException, TemplateException {
148 String arg0 = fdp.getNextArg();
149 String arg1 = fdp.getNextArg();
150 List<String> args = fdp.getArgs();
152 TemplateParser tp = fdp.getTemplateParser();
154 try {
155 return Tema.query
156 (arg1, (PreparedStatement)tp.getValue(arg0),
157 args, tp, out);
158 } catch (SQLException e) {
159 throw new TemplateException(e.getMessage(), e);
160 }
161 }
162 };
164 public static final Function WRITE =
165 new Function("write") {
166 public int invoke(FunctionDataParser fdp, Writer out)
167 throws IOException, TemplateException {
169 String arg0 = fdp.getNextArg();
171 System.err.println("Writing " + arg0 + "...");
173 Writer fw;
175 try {
176 fw = Tema.createFileWriter(arg0);
177 } catch (IOException e) {
178 System.err.println(e);
179 return 0;
180 }
182 try {
183 fdp.parseData(fw);
184 } finally {
185 fw.close();
186 }
188 out.write(arg0);
190 System.err.println("Saved " + arg0 + ".");
191 return 1;
192 }
193 };
195 public static final Function READ =
196 new Function("read") {
197 public int invoke(FunctionDataParser fdp, Writer out)
198 throws IOException, TemplateException {
200 String filename = fdp.getData();
201 String file;
203 try {
204 file = Tema.readFile(new File(filename));
205 } catch (IOException e) {
206 System.err.println(e);
207 return 0;
208 }
210 out.write(file);
211 return 1;
212 }
213 };
215 public static final Function INCLUDE =
216 new Function("include") {
217 public int invoke(FunctionDataParser fdp, Writer out)
218 throws IOException, TemplateException {
220 String filename = fdp.getData();
222 Reader fin = new BufferedReader
223 (Tema.createCachedFileReader(new File(filename)));
225 int r = fdp.getTemplateParser().parse(fin, out);
227 fin.close();
228 return r;
229 }
230 };
232 public static final Function NULL_OUTPUT =
233 new Function("!") {
234 public int invoke(FunctionDataParser fdp, Writer out)
235 throws IOException, TemplateException {
237 /* Write nothing. */
238 return fdp.parseData(new Writer() {
239 public void close() {}
240 public void flush() {}
241 public void write(char[] cbuf, int off, int len) {}
242 });
243 }
244 };
246 public static final Function REPLACE =
247 new Function("replace") {
248 public int invoke(FunctionDataParser fdp, Writer out)
249 throws IOException, TemplateException {
251 String arg0 = fdp.getNextArg();
252 String arg1 = fdp.getNextArg();
254 ReplaceWriter rw = new ReplaceWriter(out, arg0, arg1);
256 int ret = fdp.parseData(rw);
257 rw.finish();
258 return ret;
259 }
260 };
262 public static final Function XML_ESCAPE =
263 new Function("xml_escape") {
264 public int invoke(FunctionDataParser fdp, Writer out)
265 throws IOException, TemplateException {
267 final String[] chars = {"&", "<", ">", "`", "\""};
268 final String[] escape = {"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"};
270 ReplaceWriter rw = new ReplaceWriter(out, chars, escape);
272 int ret = fdp.parseData(rw);
273 rw.finish();
274 return ret;
275 }
276 };
278 public static final Function XML_CDATA =
279 new Function("xml_cdata") {
280 public int invoke(FunctionDataParser fdp, Writer out)
281 throws IOException, TemplateException {
283 ReplaceWriter rw = new ReplaceWriter
284 (out, "]]>", "]]]><![CDATA[]>", "<![CDATA[", "]]>");
286 int ret = fdp.parseData(rw);
287 rw.finish();
288 return ret;
289 }
290 };
292 public static final Function DEFINE = new Define();
294 private static void copyFile(File src, File dest) throws IOException {
295 System.err.print("Copying " + src + "... ");
297 if (src.lastModified() < dest.lastModified()) {
298 System.err.println(dest + " is up to date.");
299 return;
300 }
302 File parent = dest.getParentFile();
303 if (parent != null) parent.mkdirs();
305 InputStream in = new FileInputStream(src);
306 OutputStream out = new FileOutputStream(dest);
308 byte[] buffer = new byte[1024];
309 int len;
310 while ((len = in.read(buffer)) > 0) {
311 out.write(buffer, 0, len);
312 }
313 in.close();
314 out.close();
316 System.err.println("saved " + dest + ".");
317 }
319 static void registerAllFunctions(TemplateParser p) {
320 p.registerFunction(GET);
321 p.registerFunction(SET);
322 p.registerFunction(OPTIONAL);
323 p.registerFunction(IMAGE);
324 p.registerFunction(COPY);
325 p.registerFunction(QUERY);
326 p.registerFunction(PREPARE);
327 p.registerFunction(WRITE);
328 p.registerFunction(READ);
329 p.registerFunction(INCLUDE);
330 p.registerFunction(NULL_OUTPUT);
331 p.registerFunction(REPLACE);
332 p.registerFunction(XML_ESCAPE);
333 p.registerFunction(XML_CDATA);
334 p.registerFunction(DEFINE);
335 }
336 }