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

Update header comment.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:51:47 +0300
parents 6c41a0b43e58
children 7b11f5174e29
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.Function;
28 import kryshen.tema.FunctionDataParser;
29 import kryshen.tema.TemplateException;
30 import kryshen.tema.TemplateParser;
31 import kryshen.tema.io.NullWriter;
32 import kryshen.tema.io.TemplateReader;
34 /**
35 * Logical and conditional functions.
36 *
37 * @author Mikhail Kryshen
38 */
39 public class Control {
41 /**
42 * Copy input to output and return zero value.
43 */
44 public static final Function FALSE =
45 new Function() {
46 public int invoke(FunctionDataParser fdp, Writer out)
47 throws IOException, TemplateException {
49 if (fdp.hasMoreData())
50 fdp.parseData(out);
52 return 0;
53 }
54 };
56 /**
57 * Copy input to output and return non-zero value.
58 */
59 public static final Function TRUE =
60 new Function() {
61 public int invoke(FunctionDataParser fdp, Writer out)
62 throws IOException, TemplateException {
64 if (fdp.hasMoreData())
65 fdp.parseData(out);
67 return 1;
68 }
69 };
71 /**
72 * Outputs it's data if it has non-zero value.
73 */
74 public static final Function OPTIONAL = new Function() {
75 public int invoke(FunctionDataParser fdp, Writer out)
76 throws IOException, TemplateException {
78 StringWriter sw = new StringWriter();
79 int s = fdp.parseData(sw);
80 sw.close();
81 String data = sw.toString();
83 if (s != 0) {
84 out.write(data);
85 return 1;
86 } else {
87 return 0;
88 }
89 }
90 };
92 /**
93 * Logical negation (applied to the return value).
94 */
95 public static final Function NOT = new Function() {
96 public int invoke(FunctionDataParser fdp, Writer out)
97 throws IOException, TemplateException {
99 return (fdp.parseData(out) == 0) ? 1 : 0;
100 }
101 };
103 public static final Function IF = new Function() {
104 public int invoke(FunctionDataParser fdp, Writer out)
105 throws IOException, TemplateException {
107 int value = fdp.parseNextArg(NullWriter.INSTANCE);
109 if (value != 0) {
110 return fdp.parseData(out);
111 }
113 return 0;
114 }
115 };
117 public static final Function IF_ELSE = new Function() {
118 public int invoke(FunctionDataParser fdp, Writer out)
119 throws IOException, TemplateException {
121 int value = fdp.parseNextArg(NullWriter.INSTANCE);
123 if (value != 0) {
124 return fdp.parseNextArg(out);
125 } else {
126 fdp.skipNextArg();
127 return fdp.parseData(out);
128 }
129 }
130 };
132 /**
133 * Outputs it's evaluated data while the evaluation result is non-zero.
134 */
135 public static final Function WHILE = new Function() {
136 public int invoke(FunctionDataParser fdp, final Writer out)
137 throws IOException, TemplateException {
139 String data = fdp.getData();
140 StringReader dataReader = new StringReader(data);
142 TemplateParser parser = fdp.getTemplateParser();
144 int r = 0; // summarized return value
145 int e = 0; // evaluation value
147 while (true) {
148 StringWriter sw = new StringWriter();
150 e = parser.parse(new TemplateReader(dataReader), sw);
152 if (e == 0)
153 break;
155 r += Math.abs(e);
156 out.write(sw.toString());
157 dataReader.reset();
158 }
160 dataReader.close();
161 return r;
162 }
163 };
164 }