view src/kryshen/tema/functions/Strings.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.Writer;
25 import kryshen.tema.Function;
26 import kryshen.tema.FunctionDataParser;
27 import kryshen.tema.TemplateException;
28 import kryshen.tema.io.ReplaceWriter;
30 /**
31 * Functions for string manipulation.
32 */
33 public class Strings {
34 public static final Function TO_UPPER = new Function() {
35 public int invoke(FunctionDataParser fdp, Writer out)
36 throws IOException, TemplateException {
38 String data = fdp.getData();
39 out.write(data.toUpperCase());
40 return fdp.getLastReturnCode();
41 }
42 };
44 public static final Function TO_LOWER = new Function() {
45 public int invoke(FunctionDataParser fdp, Writer out)
46 throws IOException, TemplateException {
48 String data = fdp.getData();
49 out.write(data.toLowerCase());
50 return fdp.getLastReturnCode();
51 }
52 };
54 public static final Function SUBSTRING = new Function() {
55 public int invoke(FunctionDataParser fdp, Writer out)
56 throws IOException, TemplateException {
58 String is = fdp.getNextArg();
59 String js = fdp.getNextArg();
61 String data = fdp.getData();
63 try {
64 if (js.equals("$")) {
65 out.write(data.substring(Integer.parseInt(is)));
66 } else {
67 out.write(data.substring(
68 Integer.parseInt(is),
69 Integer.parseInt(js)));
70 }
71 } catch (NumberFormatException e) {
72 throw new TemplateException(
73 e.toString(), e, fdp.getTemplateReader());
74 } catch (IndexOutOfBoundsException e) {
75 throw new TemplateException(
76 e.toString(), e, fdp.getTemplateReader());
77 }
79 return fdp.getLastReturnCode();
80 }
81 };
83 public static final Function REPLACE = new Function() {
84 public int invoke(FunctionDataParser fdp, Writer out)
85 throws IOException, TemplateException {
87 String arg0 = fdp.getNextArg();
88 String arg1 = fdp.getNextArg();
90 ReplaceWriter rw = new ReplaceWriter(out, arg0, arg1);
92 int ret = fdp.parseData(rw);
93 rw.finish();
94 return ret;
95 }
96 };
98 public static final Function XML_ESCAPE = new Function() {
99 public int invoke(FunctionDataParser fdp, Writer out)
100 throws IOException, TemplateException {
102 final String[] chars = {"&", "<", ">", "`", "\""};
103 final String[] escape = {"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"};
105 ReplaceWriter rw = new ReplaceWriter(out, chars, escape);
107 int ret = fdp.parseData(rw);
108 rw.finish();
109 return ret;
110 }
111 };
113 public static final Function XML_CDATA = new Function() {
114 public int invoke(FunctionDataParser fdp, Writer out)
115 throws IOException, TemplateException {
117 ReplaceWriter rw = new ReplaceWriter
118 (out, "]]>", "]]]><![CDATA[]>", "<![CDATA[", "]]>");
120 int ret = fdp.parseData(rw);
121 rw.finish();
122 return ret;
123 }
124 };
126 public static final Function REGEX_MATCH = new Function() {
127 public int invoke(FunctionDataParser fdp, Writer out)
128 throws IOException, TemplateException {
130 String regex = fdp.getNextArg();
131 String data = fdp.getData(out);
133 if (data.matches(regex)) {
134 return 1;
135 }
137 return 0;
138 }
139 };
141 public static final Function REGEX_REPLACE_FIRST = new Function() {
142 public int invoke(FunctionDataParser fdp, Writer out)
143 throws IOException, TemplateException {
145 String regex = fdp.getNextArg();
146 String replacement = fdp.getNextArg();
147 String data = fdp.getData();
149 if (fdp.getLastReturnCode() == 0)
150 return 0;
152 out.write(data.replaceFirst(regex, replacement));
154 return fdp.getLastReturnCode();
155 }
156 };
158 public static final Function REGEX_REPLACE_ALL = new Function() {
159 public int invoke(FunctionDataParser fdp, Writer out)
160 throws IOException, TemplateException {
162 String regex = fdp.getNextArg();
163 String replacement = fdp.getNextArg();
164 String data = fdp.getData();
166 if (fdp.getLastReturnCode() == 0)
167 return 0;
169 out.write(data.replaceAll(regex, replacement));
171 return fdp.getLastReturnCode();
172 }
173 };
175 public static final Function EQUAL = new Function() {
176 public int invoke(FunctionDataParser fdp, Writer out)
177 throws IOException, TemplateException {
179 String s1 = fdp.getNextArg();
181 while (fdp.hasMoreData()) {
182 String s2 = fdp.getNextArg();
184 if (!s1.equals(s2)) {
185 return 0;
186 }
188 s1 = s2;
189 }
191 return 1;
192 }
193 };
195 public static final Function CHAR = new Function() {
196 public int invoke(FunctionDataParser fdp, Writer out)
197 throws IOException, TemplateException {
199 if (!fdp.hasMoreData())
200 return 0;
202 do {
203 String s = fdp.getNextArg();
204 int code;
206 try {
207 if (s.startsWith("0x")) {
208 code = Integer.parseInt(s.substring(2), 16);
209 } else {
210 code = Integer.parseInt(s);
211 }
212 } catch (NumberFormatException e) {
213 throw new TemplateException(e.getMessage(), e,
214 fdp.getTemplateReader());
215 }
217 out.write(code);
218 } while (fdp.hasMoreData());
220 return 1;
221 }
222 };
223 }