view src/kryshen/tema/functions/Strings.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: Strings.java,v 1.13 2008/02/16 17:38:03 mikhail Exp $
21 */
23 package kryshen.tema.functions;
25 import java.io.IOException;
26 import java.io.Writer;
27 import kryshen.tema.Function;
28 import kryshen.tema.FunctionDataParser;
29 import kryshen.tema.TemplateException;
30 import kryshen.tema.io.ReplaceWriter;
32 /**
33 * Functions for string manipulation.
34 */
35 public class Strings {
36 public static final Function TO_UPPER = new Function() {
37 public int invoke(FunctionDataParser fdp, Writer out)
38 throws IOException, TemplateException {
40 String data = fdp.getData();
41 out.write(data.toUpperCase());
42 return fdp.getLastReturnCode();
43 }
44 };
46 public static final Function TO_LOWER = new Function() {
47 public int invoke(FunctionDataParser fdp, Writer out)
48 throws IOException, TemplateException {
50 String data = fdp.getData();
51 out.write(data.toLowerCase());
52 return fdp.getLastReturnCode();
53 }
54 };
56 public static final Function SUBSTRING = new Function() {
57 public int invoke(FunctionDataParser fdp, Writer out)
58 throws IOException, TemplateException {
60 String is = fdp.getNextArg();
61 String js = fdp.getNextArg();
63 String data = fdp.getData();
65 try {
66 if (js.equals("$")) {
67 out.write(data.substring(Integer.parseInt(is)));
68 } else {
69 out.write(data.substring(
70 Integer.parseInt(is),
71 Integer.parseInt(js)));
72 }
73 } catch (NumberFormatException e) {
74 throw new TemplateException(
75 e.toString(), e, fdp.getTemplateReader());
76 } catch (IndexOutOfBoundsException e) {
77 throw new TemplateException(
78 e.toString(), e, fdp.getTemplateReader());
79 }
81 return fdp.getLastReturnCode();
82 }
83 };
85 public static final Function REPLACE = new Function() {
86 public int invoke(FunctionDataParser fdp, Writer out)
87 throws IOException, TemplateException {
89 String arg0 = fdp.getNextArg();
90 String arg1 = fdp.getNextArg();
92 ReplaceWriter rw = new ReplaceWriter(out, arg0, arg1);
94 int ret = fdp.parseData(rw);
95 rw.finish();
96 return ret;
97 }
98 };
100 public static final Function XML_ESCAPE = new Function() {
101 public int invoke(FunctionDataParser fdp, Writer out)
102 throws IOException, TemplateException {
104 final String[] chars = {"&", "<", ">", "`", "\""};
105 final String[] escape = {"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"};
107 ReplaceWriter rw = new ReplaceWriter(out, chars, escape);
109 int ret = fdp.parseData(rw);
110 rw.finish();
111 return ret;
112 }
113 };
115 public static final Function XML_CDATA = new Function() {
116 public int invoke(FunctionDataParser fdp, Writer out)
117 throws IOException, TemplateException {
119 ReplaceWriter rw = new ReplaceWriter
120 (out, "]]>", "]]]><![CDATA[]>", "<![CDATA[", "]]>");
122 int ret = fdp.parseData(rw);
123 rw.finish();
124 return ret;
125 }
126 };
128 public static final Function REGEX_MATCH = new Function() {
129 public int invoke(FunctionDataParser fdp, Writer out)
130 throws IOException, TemplateException {
132 String regex = fdp.getNextArg();
133 String data = fdp.getData(out);
135 if (data.matches(regex)) {
136 return 1;
137 }
139 return 0;
140 }
141 };
143 public static final Function REGEX_REPLACE_FIRST = new Function() {
144 public int invoke(FunctionDataParser fdp, Writer out)
145 throws IOException, TemplateException {
147 String regex = fdp.getNextArg();
148 String replacement = fdp.getNextArg();
149 String data = fdp.getData();
151 if (fdp.getLastReturnCode() == 0)
152 return 0;
154 out.write(data.replaceFirst(regex, replacement));
156 return fdp.getLastReturnCode();
157 }
158 };
160 public static final Function REGEX_REPLACE_ALL = new Function() {
161 public int invoke(FunctionDataParser fdp, Writer out)
162 throws IOException, TemplateException {
164 String regex = fdp.getNextArg();
165 String replacement = fdp.getNextArg();
166 String data = fdp.getData();
168 if (fdp.getLastReturnCode() == 0)
169 return 0;
171 out.write(data.replaceAll(regex, replacement));
173 return fdp.getLastReturnCode();
174 }
175 };
177 public static final Function EQUAL = new Function() {
178 public int invoke(FunctionDataParser fdp, Writer out)
179 throws IOException, TemplateException {
181 String s1 = fdp.getNextArg();
183 while (fdp.hasMoreData()) {
184 String s2 = fdp.getNextArg();
186 if (!s1.equals(s2)) {
187 return 0;
188 }
190 s1 = s2;
191 }
193 return 1;
194 }
195 };
197 public static final Function CHAR = new Function() {
198 public int invoke(FunctionDataParser fdp, Writer out)
199 throws IOException, TemplateException {
201 if (!fdp.hasMoreData())
202 return 0;
204 do {
205 String s = fdp.getNextArg();
206 int code;
208 try {
209 if (s.startsWith("0x")) {
210 code = Integer.parseInt(s.substring(2), 16);
211 } else {
212 code = Integer.parseInt(s);
213 }
214 } catch (NumberFormatException e) {
215 throw new TemplateException(e.getMessage(), e,
216 fdp.getTemplateReader());
217 }
219 out.write(code);
220 } while (fdp.hasMoreData());
222 return 1;
223 }
224 };
225 }