view src/kryshen/tema/functions/Define.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: Define.java,v 1.25 2008/02/17 23:58:01 mikhail Exp $
21 */
23 package kryshen.tema.functions;
25 import java.io.IOException;
26 import java.io.StringReader;
27 import java.io.StringWriter;
28 import java.io.Writer;
29 import kryshen.tema.Context;
30 import kryshen.tema.Function;
31 import kryshen.tema.FunctionDataParser;
32 import kryshen.tema.TemplateException;
33 import kryshen.tema.TemplateParser;
34 import kryshen.tema.io.TemplateReader;
36 /**
37 * Define function implementation.
38 *
39 * @author Mikhail Kryshen
40 */
41 public class Define extends Function {
42 public static final Function DEFINE = new Define();
44 public Define() {
45 }
47 public int invoke(FunctionDataParser fdp, Writer out)
48 throws IOException, TemplateException {
50 StringWriter nameWriter = new StringWriter();
51 int r = fdp.parseNextArg(nameWriter);
53 if (r == 0)
54 return 0;
56 StringWriter dataWriter = new StringWriter();
57 r = fdp.parseData(dataWriter);
59 if (r == 0)
60 return 0;
62 final String name = nameWriter.toString();
63 final String data = dataWriter.toString();
65 Function newFunction = new Function() {
66 public int invoke(final FunctionDataParser fdp1, final Writer out)
67 throws IOException, TemplateException {
69 final TemplateParser functionParser =
70 new TemplateParser(fdp1.getTemplateParser());
72 final Context functionContext =
73 functionParser.getContext();
75 functionContext.set
76 ("next_arg", new Function() {
77 public int invoke(FunctionDataParser unusedFdp,
78 final Writer out)
79 throws IOException, TemplateException {
81 if (fdp1.hasMoreData())
82 return fdp1.parseNextArg(out);
84 return 0;
85 }
86 });
88 functionContext.set
89 ("data", new Function() {
90 public int invoke(FunctionDataParser unusedFdp,
91 final Writer out)
92 throws IOException, TemplateException {
94 return fdp1.parseData(out);
95 }
96 });
98 functionContext.set
99 ("has_more_data", new Function() {
100 public int invoke(FunctionDataParser unusedFdp,
101 final Writer out)
102 throws IOException, TemplateException {
104 if (fdp1.hasMoreData()) {
105 out.write("true");
106 return 1;
107 }
109 out.write("false");
110 return 0;
111 }
112 });
114 TemplateReader dataReader = new TemplateReader
115 (new StringReader(data), fdp1.getTemplateReader());
117 return functionParser.parse(dataReader, out);
118 }
119 };
121 fdp.getContext().set(name, newFunction);
123 out.write(name);
124 return r;
125 }
126 }