KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > ext > StringUtil


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
package org.ejen.ext;
22
23 import org.ejen.util.XSLUtil;
24 import org.apache.xalan.extensions.ExpressionContext;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.Date JavaDoc;
27
28 /**
29  * String operations utilities (static methods).
30  * <p>
31  * <table class="usage">
32  * <tr><th class="usage">Usage (XSL stylesheet)</th></tr>
33  * <tr><td class="usage"><pre>
34  *
35  * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
36  *
37  * &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
38  * ...
39  * <b>xmlns:stu="org.ejen.ext.StringUtil"</b>
40  * version="1.0"&gt;
41  *
42  * &lt;xsl:output method="text" encoding="iso-8859-1"/&gt;
43  *
44  * &lt;xsl:template match="ejen"&gt;
45  *
46  * Date: &lt;xsl:value-of select="stu:{@link #dateFormat(ExpressionContext,String) dateFormat}('yyyy/MM/dd')"/&gt;
47  * &lt;xsl:if test="stu:{@link #equals(ExpressionContext,String,String) equals}('abc','abc')"&gt;
48  * ...
49  * &lt;/xsl:if&gt;
50  * &lt;xsl:if test="stu:{@link #equalsIgnoreCase(ExpressionContext,String,String) equalsIgnoreCase}('abc','AbC')"&gt;
51  * ...
52  * &lt;/xsl:if&gt;
53  * Name (lower case): &lt;xsl:value-of select="stu:{@link #toLowerCase(ExpressionContext,String) toLowerCase}('aTtiLa')"/&gt;
54  * Name (upper case): &lt;xsl:value-of select="stu:{@link #toUpperCase(ExpressionContext,String) toUpperCase}('aTtiLa')"/&gt;
55  * Name (...): &lt;xsl:value-of select="stu:{@link #toULowerCase(ExpressionContext,String) toULowerCase}('aTtiLa')"/&gt;
56  * &lt;xsl:value-of select="stu:{@link #indent(ExpressionContext,int,int) indent}(2,3)"/&gt;
57  * Error: &lt;xsl:value-of select="stu:{@link #replace(ExpressionContext,String,String,String) replace}('in of memory','in','out')"/&gt;
58  *
59  * &lt;/xsl:template&gt;
60  *
61  * &lt;/xsl:stylesheet&gt;
62  * </pre></td></tr></table>
63  * @author F. Wolff
64  * @version 1.0
65  */

66 public class StringUtil {
67     public static final String JavaDoc LINE_SEPARATOR = System.getProperty("line.separator",
68             "\n");
69
70     /**
71      * Protected constructor (prevents instanciation).
72      */

73     protected StringUtil() {}
74
75     /**
76      * Formats and returns current date.
77      * <p>
78      * <table class="usage"><tr><td class="usage"><pre>
79      *
80      * &lt;xsl:value-of select="stu:dateFormat('yyyy/MM/dd')"/&gt;
81      * </pre></td></tr></table>
82      * <p>
83      * See {@link java.text.SimpleDateFormat}.
84      * <p>
85      * <dd><dl><dt><b>XSLT parameters:</b>
86      * <dd><b>[Mandatory/AVT]</b> time pattern to be used to format the date.
87      * </dl></dd>
88      * <p>
89      * @param context automatically passed by the xalan extension mechanism.
90      * @param format time pattern.
91      * @return the formated current date.
92      */

93     public static String JavaDoc dateFormat(ExpressionContext context, String JavaDoc format) {
94         return new SimpleDateFormat JavaDoc(XSLUtil.evaluate(context, format)).format(new Date JavaDoc());
95     }
96
97     /**
98      * Compares the String s1 to the String s2.
99      * <p>
100      * <table class="usage"><tr><td class="usage"><pre>
101      *
102      * &lt;xsl:if test="stu:equals('abc','abc')"&gt;
103      * ...
104      * &lt;/xsl:if&gt;
105      * </pre></td></tr></table>
106      * <p>
107      * <dd><dl><dt><b>XSLT parameters:</b>
108      * <dd><b>[Mandatory/AVT]</b> the first String.
109      * <dd><b>[Mandatory/AVT]</b> the second string.
110      * </dl></dd>
111      * <p>
112      * @param context automatically passed by the xalan extension mechanism.
113      * @param s1 the first String.
114      * @param s2 the second String.
115      * @return true if s1 equals s2, false otherwise.
116      */

117     public static boolean equals(ExpressionContext context, String JavaDoc s1, String JavaDoc s2) {
118         return XSLUtil.evaluate(context, s1).equals(XSLUtil.evaluate(context, s2));
119     }
120
121     /**
122      * Compares the String s1 to the String s2, ignoring case considerations.
123      * <p>
124      * <table class="usage"><tr><td class="usage"><pre>
125      *
126      * &lt;xsl:if test="stu:equals('abc','ABc')"&gt;
127      * ...
128      * &lt;/xsl:if&gt;
129      * </pre></td></tr></table>
130      * <p>
131      * <dd><dl><dt><b>XSLT parameters:</b>
132      * <dd><b>[Mandatory/AVT]</b> the first String.
133      * <dd><b>[Mandatory/AVT]</b> the second string.
134      * </dl></dd>
135      * <p>
136      * @param context automatically passed by the xalan extension mechanism.
137      * @param s1 <b>[AVT]</b> - the first String.
138      * @param s2 <b>[AVT]</b> - the second String.
139      * @return true if s1 equals s2, ignoring case, false otherwise.
140      */

141     public static boolean equalsIgnoreCase(ExpressionContext context, String JavaDoc s1, String JavaDoc s2) {
142         return XSLUtil.evaluate(context, s1).equalsIgnoreCase(XSLUtil.evaluate(context,
143                 s2));
144     }
145
146     /**
147      * Converts all of the characters in the String s to lower case using the rules of
148      * the default locale.
149      * <p>
150      * <table class="usage"><tr><td class="usage"><pre>
151      *
152      * &lt;xsl:value-of select="stu:toLowerCase('aBc')"/&gt;
153      * </pre></td></tr></table>
154      * <p>
155      * <dd><dl><dt><b>XSLT parameters:</b>
156      * <dd><b>[Mandatory/AVT]</b> the String to be converted to lower case.
157      * </dl></dd>
158      * <p>
159      * @param context automatically passed by the xalan extension mechanism.
160      * @param s the String to be converted.
161      * @return the String s converted to lower case.
162      */

163     public static String JavaDoc toLowerCase(ExpressionContext context, String JavaDoc s) {
164         return XSLUtil.evaluate(context, s).toLowerCase();
165     }
166
167     /**
168      * Converts all of the characters in the String s to upper case using the rules of
169      * the default locale.
170      * <p>
171      * <table class="usage"><tr><td class="usage"><pre>
172      *
173      * &lt;xsl:value-of select="stu:toUpperCase('aBc')"/&gt;
174      * </pre></td></tr></table>
175      * <p>
176      * <dd><dl><dt><b>XSLT parameters:</b>
177      * <dd><b>[Mandatory/AVT]</b> the String to be converted to upper case.
178      * </dl></dd>
179      * <p>
180      * @param context automatically passed by the xalan extension mechanism.
181      * @param s the String to be converted.
182      * @return the String s converted to upper case.
183      */

184     public static String JavaDoc toUpperCase(ExpressionContext context, String JavaDoc s) {
185         return XSLUtil.evaluate(context, s).toUpperCase();
186     }
187
188     /**
189      * Converts the first character in the String s to uppercase and the remaining
190      * characters to lower case using the rules of the default locale.
191      * <p>
192      * <table class="usage"><tr><td class="usage"><pre>
193      *
194      * &lt;xsl:value-of select="stu:toULowerCase('aBc')"/&gt;
195      * </pre></td></tr></table>
196      * <p>
197      * <dd><dl><dt><b>XSLT parameters:</b>
198      * <dd><b>[Mandatory/AVT]</b> the String to be converted.
199      * </dl></dd>
200      * <p>
201      * @param context automatically passed by the xalan extension mechanism.
202      * @param s the String to be converted.
203      * @return the String s converted.
204      */

205     public static String JavaDoc toULowerCase(ExpressionContext context, String JavaDoc s) {
206         s = XSLUtil.evaluate(context, s);
207         if (s.length() == 0) {
208             return s;
209         }
210         if (s.length() == 1) {
211             return s.toUpperCase();
212         } else {
213             return s.substring(0, 1).toUpperCase()
214                     + s.substring(1).toLowerCase();
215         }
216     }
217
218     /**
219      * Returns a String starting with 'newLines' new lines followed by 'tabs' tabs.
220      * <p>
221      * <table class="usage"><tr><td class="usage"><pre>
222      *
223      * &lt;xsl:value-of select="stu:indent(1,2)"/&gt;
224      * </pre></td></tr></table>
225      * <p>
226      * This call will return <code>"\n\t\t"</code>.
227      * <p>
228      * <dd><dl><dt><b>XSLT parameters:</b>
229      * <dd><b>[Mandatory]</b> number of new lines.
230      * <dd><b>[Mandatory]</b> number of tabs.
231      * </dl></dd>
232      * <p>
233      * @param context automatically passed by the xalan extension mechanism.
234      * @param newLines number of new lines.
235      * @param tabs number of tabs.
236      * @return the indent String.
237      */

238     public static String JavaDoc indent(ExpressionContext context, int newLines, int tabs) {
239         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
240
241         for (int i = 0; i < newLines; i++) {
242             sb.append('\n');
243         }
244         for (int i = 0; i < tabs; i++) {
245             sb.append('\t');
246         }
247         return sb.toString();
248     }
249
250     /**
251      * Returns a new string resulting from replacing all occurrences of s2 in s1 with s3.
252      * <p>
253      * <table class="usage"><tr><td class="usage"><pre>
254      *
255      * &lt;xsl:value-of select="stu:replace('ab-ab-ab','ab','cde')"/&gt;
256      * </pre></td></tr></table>
257      * <p>
258      * This call will return <code>"cde-cde-cde"</code>.
259      * <p>
260      * <dd><dl><dt><b>XSLT parameters:</b>
261      * <dd><b>[Mandatory]</b> the first String.
262      * <dd><b>[Mandatory]</b> the second string (will be unescaped).
263      * <dd><b>[Mandatory]</b> the third string (will be unescaped).
264      * </dl></dd>
265      * <p>
266      * "unescaped" means: common escape sequences ("\t", "\n", "\r", "\f")
267      * converted to their respective values.
268      * <p>
269      * @param context automatically passed by the xalan extension mechanism.
270      * @param s1 the first String.
271      * @param s2 the second String (will be unescaped).
272      * @param s3 the third String (will be unescaped).
273      * @return true if s1 equals s2, ignoring case, false otherwise.
274      */

275     public static String JavaDoc replace(ExpressionContext context, String JavaDoc s1, String JavaDoc s2, String JavaDoc s3) {
276         s2 = unescape(s2);
277         s3 = unescape(s3);
278         int iS2inS1 = s1.indexOf(s2);
279
280         if (iS2inS1 == -1) {
281             return s1;
282         }
283         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s1.substring(0, iS2inS1)).append(s3);
284         int oldIS2inS1 = iS2inS1;
285         int s1Length = s1.length();
286         int s2Length = s2.length();
287
288         while (true) {
289             iS2inS1 = s1.indexOf(s2, iS2inS1 + s2Length);
290             if (iS2inS1 == -1) {
291                 if (oldIS2inS1 + s2Length < s1Length) {
292                     sb.append(s1.substring(oldIS2inS1 + s2Length));
293                 }
294                 break;
295             }
296             sb.append(s1.substring(oldIS2inS1 + s2Length, iS2inS1)).append(s3);
297             oldIS2inS1 = iS2inS1;
298         }
299         return sb.toString();
300     }
301
302     /**
303      * Returns a String with common escape sequences ("\t", "\n", "\r", "\f")
304      * converted to their respective values. The rule is: "\\" becomes "\";
305      * "\*" (with * in "tnrf") becomes the character '\*'; "\*" (with * not in
306      * "tnrf") becomes "*"; if a "\" is the last character of the String, it
307      * is removed (except if the String ends with "\\").
308      * <p>
309      * @param s String to unescape.
310      * @return the String s unescaped.
311      */

312     protected static String JavaDoc unescape(String JavaDoc s) {
313         int ibs = s.indexOf('\\');
314
315         if (ibs == -1) {
316             return s;
317         }
318         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
319         
320         int lastChar = s.length() - 1;
321         int from = 0;
322
323         while (true) {
324             if (from < ibs) {
325                 sb.append(s.substring(from, ibs));
326             }
327             if (ibs >= lastChar) {
328                 break;
329             }
330             char c = s.charAt(ibs + 1);
331
332             switch (c) {
333             case 't':
334                 sb.append('\t');
335                 break;
336
337             case 'n':
338                 sb.append('\n');
339                 break;
340
341             case 'r':
342                 sb.append('\r');
343                 break;
344
345             case 'f':
346                 sb.append('\f');
347                 break;
348
349             default:
350                 sb.append(c);
351                 break; // case '\\' and others.
352
}
353             from = ibs + 2;
354             ibs = s.indexOf('\\', from);
355             if (ibs == -1) {
356                 if (from < s.length()) {
357                     sb.append(s.substring(from, s.length()));
358                 }
359                 break;
360             }
361         }
362         return sb.toString();
363     }
364 }
365
Popular Tags