view src/kryshen/tema/functions/Define.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.StringReader;
25 import java.io.StringWriter;
26 import java.io.Writer;
27 import kryshen.tema.Context;
28 import kryshen.tema.Function;
29 import kryshen.tema.FunctionDataParser;
30 import kryshen.tema.TemplateException;
31 import kryshen.tema.TemplateParser;
32 import kryshen.tema.io.TemplateReader;
34 /**
35 * Define function implementation.
36 *
37 * @author Mikhail Kryshen
38 */
39 public class Define extends Function {
40 public static final Function DEFINE = new Define();
42 public Define() {
43 }
45 public int invoke(FunctionDataParser fdp, Writer out)
46 throws IOException, TemplateException {
48 StringWriter nameWriter = new StringWriter();
49 int r = fdp.parseNextArg(nameWriter);
51 if (r == 0)
52 return 0;
54 StringWriter dataWriter = new StringWriter();
55 r = fdp.parseData(dataWriter);
57 if (r == 0)
58 return 0;
60 final String name = nameWriter.toString();
61 final String data = dataWriter.toString();
63 Function newFunction = new Function() {
64 public int invoke(final FunctionDataParser fdp1, final Writer out)
65 throws IOException, TemplateException {
67 final TemplateParser functionParser =
68 new TemplateParser(fdp1.getTemplateParser());
70 final Context functionContext =
71 functionParser.getContext();
73 functionContext.set
74 ("next_arg", new Function() {
75 public int invoke(FunctionDataParser unusedFdp,
76 final Writer out)
77 throws IOException, TemplateException {
79 if (fdp1.hasMoreData())
80 return fdp1.parseNextArg(out);
82 return 0;
83 }
84 });
86 functionContext.set
87 ("data", new Function() {
88 public int invoke(FunctionDataParser unusedFdp,
89 final Writer out)
90 throws IOException, TemplateException {
92 return fdp1.parseData(out);
93 }
94 });
96 functionContext.set
97 ("has_more_data", new Function() {
98 public int invoke(FunctionDataParser unusedFdp,
99 final Writer out)
100 throws IOException, TemplateException {
102 if (fdp1.hasMoreData()) {
103 out.write("true");
104 return 1;
105 }
107 out.write("false");
108 return 0;
109 }
110 });
112 TemplateReader dataReader = new TemplateReader
113 (new StringReader(data), fdp1.getTemplateReader());
115 return functionParser.parse(dataReader, out);
116 }
117 };
119 fdp.getContext().set(name, newFunction);
121 out.write(name);
122 return r;
123 }
124 }