view src/kryshen/tema/functions/Standard.java @ 1:548a93c24e55

Tema 0.1jk - Javakonkurs edition (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 14 Dec 2006 23:22:05 +0300
parents
children 6c41a0b43e58
line source
1 /*
2 * Copyright (C) 2005, 2006 Mikhail A. Kryshen
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * $Id: Standard.java,v 1.5 2006/12/14 14:39:26 mikhail Exp $
19 */
21 package kryshen.tema.functions;
23 import java.io.*;
24 import java.util.*;
25 import java.net.*;
27 import java.sql.SQLException;
28 import java.sql.PreparedStatement;
30 import kryshen.tema.*;
32 /**
33 * Standard TEMA functions.
34 */
35 public class Standard {
36 public static final Function SET =
37 new Function() {
38 public int invoke(FunctionDataParser fdp, Writer out)
39 throws IOException, TemplateException {
41 String arg0 = fdp.getNextArg();
42 String arg1 = fdp.getData();
44 fdp.getTemplateParser().setValue(arg0, arg1);
46 out.write(arg0);
47 return 1;
48 }
49 };
51 /* prepare:qury_name sql_statement */
52 public static final Function PREPARE =
53 new Function() {
54 public int invoke(FunctionDataParser fdp, Writer out)
55 throws IOException, TemplateException {
57 String arg0 = fdp.getNextArg();
58 String data = fdp.getData();
60 PreparedStatement statement;
62 try {
63 statement = Tema.getDbConnection()
64 .prepareStatement(data);
65 } catch (SQLException e) {
66 throw new TemplateException
67 (e.getMessage(), e, fdp.getTemplateReader());
68 }
70 fdp.getTemplateParser().setValue(arg0, statement);
72 out.write(arg0);
73 return 1;
74 }
75 };
77 /* query:sql_query template arg1 arg2 ... argN */
78 public static final Function QUERY =
79 new Function() {
80 public int invoke(FunctionDataParser fdp, Writer out)
81 throws IOException, TemplateException {
83 String arg0 = fdp.getNextArg();
84 String arg1 = fdp.getNextArg();
85 List<String> args = fdp.getArgs();
87 TemplateParser tp = fdp.getTemplateParser();
89 TemplateReader tr =
90 new TemplateReader(new StringReader(arg1),
91 fdp.getTemplateReader());
93 try {
94 return Tema.query
95 (tr, (PreparedStatement)tp.getValue(arg0),
96 args, tp, out);
97 } catch (SQLException e) {
98 throw new TemplateException
99 (e.getMessage(), e, fdp.getTemplateReader());
100 }
101 }
102 };
104 public static final Function REPLACE =
105 new Function() {
106 public int invoke(FunctionDataParser fdp, Writer out)
107 throws IOException, TemplateException {
109 String arg0 = fdp.getNextArg();
110 String arg1 = fdp.getNextArg();
112 ReplaceWriter rw = new ReplaceWriter(out, arg0, arg1);
114 int ret = fdp.parseData(rw);
115 rw.finish();
116 return ret;
117 }
118 };
120 public static final Function LOAD =
122 new Function() {
123 public int invoke(FunctionDataParser fdp, Writer out)
124 throws IOException, TemplateException {
126 final String name = fdp.getNextArg();
127 final String className = fdp.getNextArg();
129 List<URL> urls = new ArrayList<URL>(1);
131 while (fdp.hasMoreData()) {
132 urls.add(new URL(fdp.getNextArg()));
133 }
135 ClassLoader loader = this.getClass().getClassLoader();
137 if (urls.size() > 0) {
138 loader = new URLClassLoader
139 (urls.toArray(new URL[0]), loader);
140 }
142 Class<Function> functionClass;
143 Function function;
145 try {
146 functionClass = loadFunctionClass(loader, className);
147 } catch (ClassNotFoundException e) {
148 throw new TemplateException
149 ("Class not found", e, fdp.getTemplateReader());
150 } catch (ClassCastException e) {
151 throw new TemplateException
152 ("Not a function class", e, fdp.getTemplateReader());
153 }
155 try {
156 function = functionClass.newInstance();
157 } catch (InstantiationException e) {
158 throw new TemplateException
159 ("Could not load class", e, fdp.getTemplateReader());
160 } catch (IllegalAccessException e) {
161 throw new TemplateException
162 ("Could not load class", e, fdp.getTemplateReader());
163 }
165 fdp.getTemplateParser().registerFunction(name, function);
168 out.write(name);
169 return 1;
170 }
171 };
173 public static final Function NULL_OUTPUT =
174 new Function() {
175 public int invoke(FunctionDataParser fdp, Writer out)
176 throws IOException, TemplateException {
178 /* Write nothing. */
179 return fdp.parseData(new Writer() {
180 public void close() {}
181 public void flush() {}
182 public void write(char[] cbuf, int off, int len) {}
183 });
184 }
185 };
187 public static final Function XML_ESCAPE =
188 new Function() {
189 public int invoke(FunctionDataParser fdp, Writer out)
190 throws IOException, TemplateException {
192 final String[] chars = {"&", "<", ">", "`", "\""};
193 final String[] escape = {"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"};
195 ReplaceWriter rw = new ReplaceWriter(out, chars, escape);
197 int ret = fdp.parseData(rw);
198 rw.finish();
199 return ret;
200 }
201 };
203 public static final Function XML_CDATA =
204 new Function() {
205 public int invoke(FunctionDataParser fdp, Writer out)
206 throws IOException, TemplateException {
208 ReplaceWriter rw = new ReplaceWriter
209 (out, "]]>", "]]]><![CDATA[]>", "<![CDATA[", "]]>");
211 int ret = fdp.parseData(rw);
212 rw.finish();
213 return ret;
214 }
215 };
218 @SuppressWarnings("unchecked")
219 private static Class<Function> loadFunctionClass
220 (ClassLoader loader, String className)
221 throws ClassNotFoundException {
223 return (Class<Function>)loader.loadClass(className);
224 }
226 /**
227 * Update file (copy if newer).
228 *
229 * @param src source file.
230 * @param dest destination file.
231 *
232 * @trows IOException on copying error.
233 */
234 private static void copyFile(File src, File dest) throws IOException {
235 System.err.print("Copying " + src + "... ");
237 if (src.lastModified() < dest.lastModified()) {
238 System.err.println(dest + " is up to date.");
239 return;
240 }
242 File parent = dest.getParentFile();
243 if (parent != null) parent.mkdirs();
245 InputStream in = new FileInputStream(src);
246 OutputStream out = new FileOutputStream(dest);
248 byte[] buffer = new byte[1024];
249 int len;
250 while ((len = in.read(buffer)) > 0) {
251 out.write(buffer, 0, len);
252 }
253 in.close();
254 out.close();
256 System.err.println("saved " + dest + ".");
257 }
258 }