KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > exchange > util > StringTools


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.exchange.util;
19
20 import java.util.StringTokenizer JavaDoc;
21
22 /**
23  * Utility class that groups string manipulation functions.
24  *
25  * @author Stefano Fornari
26  * @version $Id: StringTools.java,v 1.1 2005/06/22 06:51:27 fabius Exp $
27  */

28 public class StringTools {
29
30     /**
31      * Splits a comma separated values string into an array of strings.
32      *
33      * @param s the comma separated values list - NOT NULL
34      *
35      * @return the elements in the list as an array
36      */

37     public static String JavaDoc[] split(String JavaDoc s) {
38         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, ", ");
39
40         String JavaDoc[] values = new String JavaDoc[st.countTokens()];
41
42         for (int i=0; i<values.length; ++i) {
43             values[i] = st.nextToken();
44         }
45
46         return values;
47     }
48
49     /**
50      * Joins the given Strin[] in a comma separated String
51      *
52      * @param array the String[] to join - NOT NULL
53      *
54      * @return a comma separated list as a single string
55      */

56     public static String JavaDoc join(String JavaDoc[] array) {
57         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
58         for (int i=0; (i<array.length); ++i) {
59             if (i == 0) {
60                 sb.append(array[i]);
61             } else {
62                 sb.append(',').append(array[i]);
63             }
64         }
65
66         return sb.toString();
67     }
68
69     /**
70      * Returns true if the given string is null or zero-length, false otherwise.
71      *
72      * @param s the string to check
73      *
74      * @return true if the given string is null or zero-length, false otherwise.
75      */

76     public static boolean isEmpty(String JavaDoc s) {
77         return (s == null) || (s.length() == 0);
78     }
79
80     /**
81      * Replaces special characters from the given string with an underscore ('_').
82      *
83      * @param s the string to replace.
84      *
85      * @return the replaced string.
86      */

87     public static String JavaDoc replaceSpecial(String JavaDoc s) {
88         String JavaDoc ret = new String JavaDoc(s);
89
90         char[] chars = s.toCharArray();
91         for (int i=0; i < chars.length; ++i) {
92             if ((chars[i] < '0' || chars[i] > '9') &&
93                 (chars[i] < 'a' || chars[i] > 'z') &&
94                 (chars[i] < 'A' || chars[i] > 'Z')) {
95                 chars[i] = '_';
96             }
97         }
98
99         return new String JavaDoc(chars);
100     }
101
102
103     /**
104      * <p>Escapes the characters in a <code>String</code> using XML entities.</p>
105      *
106      *
107      * <p>Supports only the four basic XML entities (gt, lt, quot, amp).
108      * Does not support DTDs or external entities.</p>
109      *
110      * @param str the <code>String</code> to escape, may be null
111      * @return a new escaped <code>String</code>, <code>null</code> if null string input
112      * @see #unescapeXml(java.lang.String)
113      **/

114     public static String JavaDoc escapeXml(String JavaDoc str) {
115         if (str == null) {
116             return null;
117         }
118
119         if (str != null) {
120             return Entities.XML.escape(str);
121         } else {
122             return null;
123         }
124     }
125
126     /**
127      * <p>Unescapes a string containing XML entity escapes to a string
128      * containing the actual Unicode characters corresponding to the
129      * escapes.</p>
130      *
131      * <p>Supports only the four basic XML entities (gt, lt, quot, amp).
132      * Does not support DTDs or external entities.</p>
133      *
134      * @param str the <code>String</code> to unescape, may be null
135      * @return a new unescaped <code>String</code>, <code>null</code> if null string input
136      * @see #escapeXml(String)
137      **/

138     public static String JavaDoc unescapeXml(String JavaDoc str) {
139         if (str == null) {
140             return null;
141         }
142         return Entities.XML.unescape(str);
143     }
144
145
146 }
Popular Tags