view src/kryshen/tema/functions/Standard.java @ 2:6c41a0b43e58

Tema 0.3 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 19 Feb 2008 20:32:17 +0300
parents 548a93c24e55
children e9d13c7ffeb1
line source
1 /*
2 * Copyright 2006-2008 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 *
20 * $Id: Standard.java,v 1.34 2008/02/19 17:32:17 mikhail Exp $
21 */
23 package kryshen.tema.functions;
25 import java.io.IOException;
26 import java.io.Writer;
27 import java.net.URL;
28 import java.net.URLClassLoader;
29 import java.util.ArrayList;
30 import java.util.List;
31 import kryshen.tema.Context;
32 import kryshen.tema.Function;
33 import kryshen.tema.FunctionDataParser;
34 import kryshen.tema.Tema;
35 import kryshen.tema.TemplateException;
36 import kryshen.tema.io.NullWriter;
38 /**
39 * Standard TEMA functions.
40 */
41 public class Standard {
43 /**
44 * Output Tema version.
45 */
46 public static final Function TEMA = new Function() {
47 public int invoke(FunctionDataParser fdp, Writer out)
48 throws IOException, TemplateException {
49 out.write(Tema.TITLE + " " + Tema.VERSION
50 /*+ " by " + Tema.AUTHOR*/);
51 return 1;
52 }
53 };
55 /**
56 * Define static variable.
57 */
58 public static final Function SET = new Function() {
59 public int invoke(FunctionDataParser fdp, Writer out)
60 throws IOException, TemplateException {
62 String name = fdp.getNextArg();
64 if (fdp.getLastReturnCode() == 0)
65 return 0;
67 String data = fdp.getData(out);
68 fdp.getContext().set(name, data);
70 return fdp.getLastReturnCode();
71 }
72 };
74 /**
75 * Recursively remove definition.
76 */
77 public static final Function UNSET = new Function() {
78 public int invoke(FunctionDataParser fdp, Writer out)
79 throws IOException, TemplateException {
81 int r = 0;
83 while (fdp.hasMoreData()) {
84 String name = fdp.getNextArg();
86 if (fdp.getLastReturnCode() == 0)
87 continue;
89 if (fdp.getContext().unset(name))
90 r++;
91 }
93 return r;
94 }
95 };
97 /**
98 * Export variable to the global (outermost) context.
99 */
100 public static final Function EXPORT = new Function() {
101 public int invoke(FunctionDataParser fdp,
102 final Writer out)
103 throws IOException, TemplateException {
105 String name = fdp.getNextArg();
107 if (fdp.getLastReturnCode() == 0)
108 return 0;
110 // Set and export.
111 if (fdp.hasMoreData()) {
112 Object value = fdp.getData(out);
114 fdp.getContext().export(name, value);
115 return fdp.getLastReturnCode();
116 }
118 // Only export.
119 if (fdp.getContext().export(name)) {
120 out.write(name);
121 return 1;
122 }
124 return 0;
125 }
126 };
128 /**
129 * Copy macro definitions.
130 */
131 public static final Function ASSIGN = new Function() {
132 public int invoke(FunctionDataParser fdp, Writer out)
133 throws IOException, TemplateException {
135 String name1 = fdp.getNextArg();
137 if (fdp.getLastReturnCode() == 0)
138 return 0;
140 String name2 = fdp.getData();
142 if (fdp.getLastReturnCode() == 0)
143 return 0;
145 Context context = fdp.getContext();
146 Object value = context.get(name2);
148 if (value == null)
149 return 0;
151 context.set(name1, value);
153 return 1;
154 }
155 };
157 /**
158 * Invoke function specified by name.
159 */
160 public static final Function INVOKE = new Function() {
161 public int invoke(FunctionDataParser fdp, Writer out)
162 throws IOException, TemplateException {
164 String name = fdp.getNextArg();
166 if (fdp.getLastReturnCode() == 0)
167 return 0;
169 return fdp.getTemplateParser().invoke(name, fdp, out);
170 }
171 };
173 /**
174 * Create definiton as a new Java object.
175 */
176 public static final Function LOAD = new Function() {
177 public int invoke(FunctionDataParser fdp, Writer out)
178 throws IOException, TemplateException {
180 final String name = fdp.getNextArg();
181 final String className = fdp.getNextArg();
183 List<URL> urls = new ArrayList<URL>(1);
185 while (fdp.hasMoreData()) {
186 urls.add(new URL(fdp.getNextArg()));
187 }
189 ClassLoader loader = this.getClass().getClassLoader();
191 if (urls.size() > 0) {
192 loader = new URLClassLoader
193 (urls.toArray(new URL[0]), loader);
194 }
196 Class<?> loadedClass;
197 Object instance;
199 try {
200 loadedClass = loader.loadClass(className);
201 } catch (ClassNotFoundException e) {
202 throw new TemplateException
203 ("Class not found", e, fdp.getTemplateReader());
204 }
206 try {
207 instance = loadedClass.newInstance();
208 } catch (InstantiationException e) {
209 throw new TemplateException
210 ("Could not load class", e, fdp.getTemplateReader());
211 } catch (IllegalAccessException e) {
212 throw new TemplateException
213 ("Could not load class", e, fdp.getTemplateReader());
214 }
216 fdp.getContext().set(name, instance);
218 out.write(name);
219 return 1;
220 }
221 };
223 public static final Function ECHO = new Function() {
224 public int invoke(FunctionDataParser fdp, Writer out)
225 throws IOException, TemplateException {
227 if (!fdp.hasMoreData())
228 return 0;
230 return fdp.parseData(out);
231 }
232 };
234 /**
235 * Process arguments, but output nothing.
236 */
237 public static final Function SILENT = new Function() {
238 public int invoke(FunctionDataParser fdp, Writer out)
239 throws IOException, TemplateException {
241 if (!fdp.hasMoreData())
242 return 0;
244 return fdp.parseData(NullWriter.INSTANCE);
245 }
246 };
248 /**
249 * Skip arguments.
250 */
251 public static final Function SKIP = new Function() {
252 public int invoke(FunctionDataParser fdp, Writer out)
253 throws IOException, TemplateException {
255 // Ignore arguments.
256 return 0;
257 }
258 };
259 }