view src/kryshen/tema/functions/Standard.java @ 15:e9d13c7ffeb1

Update header comment.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:51:47 +0300
parents 6c41a0b43e58
children
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.functions;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.net.URL;
26 import java.net.URLClassLoader;
27 import java.util.ArrayList;
28 import java.util.List;
29 import kryshen.tema.Context;
30 import kryshen.tema.Function;
31 import kryshen.tema.FunctionDataParser;
32 import kryshen.tema.Tema;
33 import kryshen.tema.TemplateException;
34 import kryshen.tema.io.NullWriter;
36 /**
37 * Standard TEMA functions.
38 */
39 public class Standard {
41 /**
42 * Output Tema version.
43 */
44 public static final Function TEMA = new Function() {
45 public int invoke(FunctionDataParser fdp, Writer out)
46 throws IOException, TemplateException {
47 out.write(Tema.TITLE + " " + Tema.VERSION
48 /*+ " by " + Tema.AUTHOR*/);
49 return 1;
50 }
51 };
53 /**
54 * Define static variable.
55 */
56 public static final Function SET = new Function() {
57 public int invoke(FunctionDataParser fdp, Writer out)
58 throws IOException, TemplateException {
60 String name = fdp.getNextArg();
62 if (fdp.getLastReturnCode() == 0)
63 return 0;
65 String data = fdp.getData(out);
66 fdp.getContext().set(name, data);
68 return fdp.getLastReturnCode();
69 }
70 };
72 /**
73 * Recursively remove definition.
74 */
75 public static final Function UNSET = new Function() {
76 public int invoke(FunctionDataParser fdp, Writer out)
77 throws IOException, TemplateException {
79 int r = 0;
81 while (fdp.hasMoreData()) {
82 String name = fdp.getNextArg();
84 if (fdp.getLastReturnCode() == 0)
85 continue;
87 if (fdp.getContext().unset(name))
88 r++;
89 }
91 return r;
92 }
93 };
95 /**
96 * Export variable to the global (outermost) context.
97 */
98 public static final Function EXPORT = new Function() {
99 public int invoke(FunctionDataParser fdp,
100 final Writer out)
101 throws IOException, TemplateException {
103 String name = fdp.getNextArg();
105 if (fdp.getLastReturnCode() == 0)
106 return 0;
108 // Set and export.
109 if (fdp.hasMoreData()) {
110 Object value = fdp.getData(out);
112 fdp.getContext().export(name, value);
113 return fdp.getLastReturnCode();
114 }
116 // Only export.
117 if (fdp.getContext().export(name)) {
118 out.write(name);
119 return 1;
120 }
122 return 0;
123 }
124 };
126 /**
127 * Copy macro definitions.
128 */
129 public static final Function ASSIGN = new Function() {
130 public int invoke(FunctionDataParser fdp, Writer out)
131 throws IOException, TemplateException {
133 String name1 = fdp.getNextArg();
135 if (fdp.getLastReturnCode() == 0)
136 return 0;
138 String name2 = fdp.getData();
140 if (fdp.getLastReturnCode() == 0)
141 return 0;
143 Context context = fdp.getContext();
144 Object value = context.get(name2);
146 if (value == null)
147 return 0;
149 context.set(name1, value);
151 return 1;
152 }
153 };
155 /**
156 * Invoke function specified by name.
157 */
158 public static final Function INVOKE = new Function() {
159 public int invoke(FunctionDataParser fdp, Writer out)
160 throws IOException, TemplateException {
162 String name = fdp.getNextArg();
164 if (fdp.getLastReturnCode() == 0)
165 return 0;
167 return fdp.getTemplateParser().invoke(name, fdp, out);
168 }
169 };
171 /**
172 * Create definiton as a new Java object.
173 */
174 public static final Function LOAD = new Function() {
175 public int invoke(FunctionDataParser fdp, Writer out)
176 throws IOException, TemplateException {
178 final String name = fdp.getNextArg();
179 final String className = fdp.getNextArg();
181 List<URL> urls = new ArrayList<URL>(1);
183 while (fdp.hasMoreData()) {
184 urls.add(new URL(fdp.getNextArg()));
185 }
187 ClassLoader loader = this.getClass().getClassLoader();
189 if (urls.size() > 0) {
190 loader = new URLClassLoader
191 (urls.toArray(new URL[0]), loader);
192 }
194 Class<?> loadedClass;
195 Object instance;
197 try {
198 loadedClass = loader.loadClass(className);
199 } catch (ClassNotFoundException e) {
200 throw new TemplateException
201 ("Class not found", e, fdp.getTemplateReader());
202 }
204 try {
205 instance = loadedClass.newInstance();
206 } catch (InstantiationException e) {
207 throw new TemplateException
208 ("Could not load class", e, fdp.getTemplateReader());
209 } catch (IllegalAccessException e) {
210 throw new TemplateException
211 ("Could not load class", e, fdp.getTemplateReader());
212 }
214 fdp.getContext().set(name, instance);
216 out.write(name);
217 return 1;
218 }
219 };
221 public static final Function ECHO = new Function() {
222 public int invoke(FunctionDataParser fdp, Writer out)
223 throws IOException, TemplateException {
225 if (!fdp.hasMoreData())
226 return 0;
228 return fdp.parseData(out);
229 }
230 };
232 /**
233 * Process arguments, but output nothing.
234 */
235 public static final Function SILENT = new Function() {
236 public int invoke(FunctionDataParser fdp, Writer out)
237 throws IOException, TemplateException {
239 if (!fdp.hasMoreData())
240 return 0;
242 return fdp.parseData(NullWriter.INSTANCE);
243 }
244 };
246 /**
247 * Skip arguments.
248 */
249 public static final Function SKIP = new Function() {
250 public int invoke(FunctionDataParser fdp, Writer out)
251 throws IOException, TemplateException {
253 // Ignore arguments.
254 return 0;
255 }
256 };
257 }