KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > system > StringUtils


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

16 package net.sf.jftp.system;
17
18 import java.io.*;
19
20
21 public class StringUtils
22 {
23     /**
24     * Makes a (path) string shorter to get it displayed correctly
25     */

26     public static String JavaDoc cutPath(String JavaDoc s)
27     {
28         int maxlabel = 64;
29
30         if(s.length() > maxlabel)
31         {
32             while(s.indexOf("/") >= 0)
33             {
34
35                 s = StringUtils.cutAfter(s, '/');
36
37                 if(s.length() < 16)
38                 {
39                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
40                     sb.insert(0, ".../");
41
42                     return sb.toString();
43                 }
44             }
45         }
46
47         return s;
48     }
49
50     /**
51     * Removes the a string at the beginning of a string
52     */

53     public static String JavaDoc removeStart(String JavaDoc str, String JavaDoc what)
54     {
55         if(str.startsWith(what))
56         {
57             int x = what.length();
58
59             return str.substring(x);
60         }
61         else
62         {
63             return str;
64         }
65     }
66
67     /**
68     * Returns the rest of a string after a given character
69     */

70     public static String JavaDoc cutAfter(String JavaDoc str, char c)
71     {
72         for(int i = 0; i < str.length(); i++)
73         {
74             if(str.charAt(i) == c)
75             {
76                 // System.out.println(str.substring(i+1));
77
return str.substring(i + 1);
78             }
79         }
80
81         return str;
82     }
83
84     /**
85     * Used to search for return codes in a string array.
86     * Returns the first one found
87     */

88     public static String JavaDoc contains(String JavaDoc[] tmp, String JavaDoc[] str)
89     {
90         for(int i = 0; i < tmp.length; i++)
91         {
92             for(int j = 0; j < str.length; j++)
93             {
94                 if(tmp[i].startsWith(str[j]))
95                 {
96                     return tmp[i];
97                 }
98             }
99         }
100
101         return "";
102     }
103
104     /**
105     * Returns true if the given string contains the given character
106     */

107     public static boolean strstr(String JavaDoc tmp, char str)
108     {
109         for(int i = 0; i < tmp.length(); i++)
110         {
111             if(tmp.charAt(i) == str)
112             {
113                 return true;
114             }
115         }
116
117         return false;
118     }
119
120     /**
121     * Returns a string representing a given character
122     */

123     public static String JavaDoc string(char c)
124     {
125         char[] buf = new char[1];
126         buf[0] = c;
127
128         return new String JavaDoc(buf);
129     }
130
131     /**
132     * Get a filename out of a full path string
133     */

134     public static String JavaDoc getFile(String JavaDoc file)
135     {
136         int x = file.lastIndexOf("/");
137
138         // unix
139
if(x >= 0)
140         {
141             file = file.substring(x + 1);
142         }
143
144         // windows
145
x = file.lastIndexOf("\\");
146
147         if(x >= 0)
148         {
149             file = file.substring(x + 1);
150         }
151
152         // may work, but can test the other method better
153
//int x = file.lastIndexOf(File.separatorChar);
154
//if(x >= 0) file = file.substring(x+1);
155
//System.out.println(file);
156
return file;
157     }
158
159     /**
160     * Returns a string representing a relative directory path.
161     * Examples: "/tmp/dir/" -> "dir/" and "/tmp/dir" -> "dir"
162     */

163     public static String JavaDoc getDir(String JavaDoc tmp)
164     {
165         int x;
166
167         while(true)
168         {
169             x = tmp.indexOf("/");
170
171             if((x == (tmp.length() - 1)) || (x < 0))
172             {
173                 break;
174             }
175             else
176             {
177                 tmp = tmp.substring(x + 1);
178             }
179         }
180
181         while(true)
182         {
183             x = tmp.indexOf("\\");
184
185             if((x == (tmp.length() - 1)) || (x < 0))
186             {
187                 break;
188             }
189             else
190             {
191                 tmp = tmp.substring(x + 1);
192             }
193         }
194
195         return tmp;
196     }
197
198     /*
199     * Returns true if the string represents a relative filename, false otherwise
200     */

201     public static boolean isRelative(String JavaDoc file)
202     {
203         // unix
204
if(file.startsWith("/"))
205         {
206             return false;
207         }
208
209         // windows
210
if((file.length() > 2) && (file.charAt(1) == ':'))
211         {
212             return false;
213         }
214
215         //System.out.println("true: " + file);
216
// default
217
return true;
218     }
219
220     /**
221     * Main method containing a few testcases for getFile() / isRelative()
222     */

223     public static void main(String JavaDoc[] argv)
224     {
225         String JavaDoc a1 = "E:\\programme\\test.html";
226         String JavaDoc a2 = "programme\\test.html";
227         String JavaDoc a3 = "test.html";
228         String JavaDoc a4 = "/programme/test.html";
229         String JavaDoc a5 = "programme/test.html";
230
231         System.out.println("getfile: " + getFile(a1) + " - false, " +
232                            isRelative(a1));
233         System.out.println("getfile: " + getFile(a2) + " - true, " +
234                            isRelative(a2));
235         System.out.println("getfile: " + getFile(a3) + " - true, " +
236                            isRelative(a3));
237         System.out.println("getfile: " + getFile(a4) + " - false, " +
238                            isRelative(a4));
239         System.out.println("getfile: " + getFile(a5) + " - true, " +
240                            isRelative(a5));
241     }
242
243     public static String JavaDoc cut(String JavaDoc tmp, String JavaDoc where)
244     {
245         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
246
247         for(int i = 0; i < tmp.length(); i++)
248         {
249             if(!string(tmp.charAt(i)).equals(where))
250             {
251                 ret.append(string(tmp.charAt(i)));
252             }
253
254             if(string(tmp.charAt(i)).equals(where))
255             {
256                 return ret.toString();
257             }
258         }
259
260         return ret.toString();
261     }
262 }
263
Popular Tags