view src/kryshen/tema/functions/Control.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
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: Control.java,v 1.18 2008/02/19 16:21:00 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.Function;
30 import kryshen.tema.FunctionDataParser;
31 import kryshen.tema.TemplateException;
32 import kryshen.tema.TemplateParser;
33 import kryshen.tema.io.NullWriter;
34 import kryshen.tema.io.TemplateReader;
36 /**
37 * Logical and conditional functions.
38 *
39 * @author Mikhail Kryshen
40 */
41 public class Control {
43 /**
44 * Copy input to output and return zero value.
45 */
46 public static final Function FALSE =
47 new Function() {
48 public int invoke(FunctionDataParser fdp, Writer out)
49 throws IOException, TemplateException {
51 if (fdp.hasMoreData())
52 fdp.parseData(out);
54 return 0;
55 }
56 };
58 /**
59 * Copy input to output and return non-zero value.
60 */
61 public static final Function TRUE =
62 new Function() {
63 public int invoke(FunctionDataParser fdp, Writer out)
64 throws IOException, TemplateException {
66 if (fdp.hasMoreData())
67 fdp.parseData(out);
69 return 1;
70 }
71 };
73 /**
74 * Outputs it's data if it has non-zero value.
75 */
76 public static final Function OPTIONAL = new Function() {
77 public int invoke(FunctionDataParser fdp, Writer out)
78 throws IOException, TemplateException {
80 StringWriter sw = new StringWriter();
81 int s = fdp.parseData(sw);
82 sw.close();
83 String data = sw.toString();
85 if (s != 0) {
86 out.write(data);
87 return 1;
88 } else {
89 return 0;
90 }
91 }
92 };
94 /**
95 * Logical negation (applied to the return value).
96 */
97 public static final Function NOT = new Function() {
98 public int invoke(FunctionDataParser fdp, Writer out)
99 throws IOException, TemplateException {
101 return (fdp.parseData(out) == 0) ? 1 : 0;
102 }
103 };
105 public static final Function IF = new Function() {
106 public int invoke(FunctionDataParser fdp, Writer out)
107 throws IOException, TemplateException {
109 int value = fdp.parseNextArg(NullWriter.INSTANCE);
111 if (value != 0) {
112 return fdp.parseData(out);
113 }
115 return 0;
116 }
117 };
119 public static final Function IF_ELSE = new Function() {
120 public int invoke(FunctionDataParser fdp, Writer out)
121 throws IOException, TemplateException {
123 int value = fdp.parseNextArg(NullWriter.INSTANCE);
125 if (value != 0) {
126 return fdp.parseNextArg(out);
127 } else {
128 fdp.skipNextArg();
129 return fdp.parseData(out);
130 }
131 }
132 };
134 /**
135 * Outputs it's evaluated data while the evaluation result is non-zero.
136 */
137 public static final Function WHILE = new Function() {
138 public int invoke(FunctionDataParser fdp, final Writer out)
139 throws IOException, TemplateException {
141 String data = fdp.getData();
142 StringReader dataReader = new StringReader(data);
144 TemplateParser parser = fdp.getTemplateParser();
146 int r = 0; // summarized return value
147 int e = 0; // evaluation value
149 while (true) {
150 StringWriter sw = new StringWriter();
152 e = parser.parse(new TemplateReader(dataReader), sw);
154 if (e == 0)
155 break;
157 r += Math.abs(e);
158 out.write(sw.toString());
159 dataReader.reset();
160 }
162 dataReader.close();
163 return r;
164 }
165 };
166 }