KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > StringTool


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2003 Marc Eppelmann
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.util;
23
24 import java.io.File JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 /**
28  * A extended Java Implementation of Pythons string.replace()
29  *
30  * @author marc.eppelmann@gmx.de
31  */

32 public class StringTool
33 {
34
35     // ~ Constructors
36
// *********************************************************************************
37

38     /**
39      * Default Constructor
40      */

41     public StringTool()
42     {
43         super();
44     }
45
46     // ~ Methods
47
// **************************************************************************************
48

49     /**
50      * Standalone callable Test method
51      *
52      * @param args Commandline Args
53      */

54     public static void main(String JavaDoc[] args)
55     {
56         System.out.println("Test: string.replace(abc$defg,$de ,null ):"
57                 + StringTool.replace("abc$defg", "$de", null, true));
58     }
59
60     /**
61      * Replaces <b>from</b> with <b>to</b> in given String: <b>value</b>
62      *
63      * @param value original String
64      * @param from Search Pattern
65      * @param to Replace with this
66      *
67      * @return the replaced String
68      */

69     public static String JavaDoc replace(String JavaDoc value, String JavaDoc from, String JavaDoc to)
70     {
71         return replace(value, from, to, true);
72     }
73
74     /**
75      * Replaces <b>from</b> with <b>to</b> in given String: <b>value</b>
76      *
77      * @param value original String
78      * @param from Search Pattern
79      * @param to Replace with this
80      * @param aCaseSensitiveFlag set to true be case sensitive.
81      *
82      * @return the replaced String
83      */

84     public static String JavaDoc replace(String JavaDoc value, String JavaDoc from, String JavaDoc to, boolean aCaseSensitiveFlag)
85     {
86         if ((value == null) || (value.length() == 0) || (from == null) || (from.length() == 0)) { return value; }
87
88         if (to == null)
89         {
90             to = "";
91         }
92
93         if (!aCaseSensitiveFlag)
94         {
95             from = from.toLowerCase();
96         }
97
98         String JavaDoc result = value;
99         int lastIndex = 0;
100         int index = value.indexOf(from);
101
102         if (index != -1)
103         {
104             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
105
106             while (index != -1)
107             {
108                 buffer.append(value.substring(lastIndex, index)).append(to);
109                 lastIndex = index + from.length();
110                 index = value.indexOf(from, lastIndex);
111             }
112
113             buffer.append(value.substring(lastIndex));
114             result = buffer.toString();
115         }
116
117         return result;
118     }
119
120     /**
121      * Normalizes a Windows or Unix Path.
122      *
123      * Reason: Javas File accepts / or \ for Pathes. Batches or ShellScripts does it not!
124      *
125      * TODO: implement support for MAC < MacOSX
126      *
127      * @param destination
128      * @param fileSeparator a target-system fileseparator
129      *
130      * @return the normalized path
131      */

132     public static String JavaDoc normalizePath(String JavaDoc destination, String JavaDoc fileSeparator)
133     {
134         String JavaDoc FILESEP = (fileSeparator == null) ? File.separator : fileSeparator;
135
136         destination = StringTool.replace(destination, "\\", "/");
137
138         // all occs of "//" by "/"
139
destination = StringTool.replace(destination, "//", "/");
140
141         destination = StringTool.replace(destination, ":", ";");
142         destination = StringTool.replace(destination, ";", ":");
143
144         destination = StringTool.replace(destination, "/", FILESEP);
145
146         if ("\\".equals(FILESEP))
147         {
148             destination = StringTool.replace(destination, ":", ";");
149
150             // results in "C;\" instead of "C:\"
151
// so correct it:
152
destination = StringTool.replace(destination, ";\\", ":\\");
153         }
154
155         // Convert the file separator characters
156
return (destination);
157     }
158
159     /**
160      * Normalizes a mixed Windows/Unix Path. Does Only work for Windows or Unix Pathes Reason:
161      * Java.File accepts / or \ for Pathes. Batches or ShellScripts does it not!
162      *
163      * @param destination accepted mixed form by java.File like "C:/a/mixed\path\accepted/by\Java"
164      *
165      * @return the normalized Path
166      */

167     public static String JavaDoc normalizePath(String JavaDoc destination)
168     {
169         return (normalizePath(destination, null));
170     }
171
172     /**
173      * Converts an String Array to a space separated String w/o any check
174      *
175      * @param args The StringArray
176      * @return the space separated result.
177      */

178     public static String JavaDoc stringArrayToSpaceSeparatedString(String JavaDoc[] args)
179     {
180         String JavaDoc result = "";
181         for (int idx = 0; idx < args.length; idx++)
182         {
183             result += args[idx] + " ";
184         }
185         return result;
186     }
187
188     public static String JavaDoc getPlatformEncoding()
189     {
190         // TODO Auto-generated method stub
191
return System.getProperty("file.encoding");
192     }
193
194     public static String JavaDoc UTF16()
195     {
196         return "UTF-16";
197     }
198
199     /**
200      * Transforms a (Array)List of Strings into a line.separator="\n" separated Stringlist.
201      *
202      * @param aStringList
203      *
204      * @return a printable list
205      */

206     public static String JavaDoc stringArrayListToString(ArrayList JavaDoc aStringList)
207     {
208         return stringArrayListToString(aStringList, null);
209     }
210
211     /**
212      * Transforms a (Array)List of Strings into an aLineSeparator separated Stringlist.
213      *
214      * @param aStringList
215      *
216      * @return a printable list
217      */

218     public static String JavaDoc stringArrayListToString(ArrayList JavaDoc aStringList, String JavaDoc aLineSeparator)
219     {
220         String JavaDoc LineSeparator = aLineSeparator;
221         if (LineSeparator == null) LineSeparator = System.getProperty("line.separator", "\n");
222
223         StringBuffer JavaDoc temp = new StringBuffer JavaDoc();
224
225         for (int idx = 0; idx < aStringList.size(); idx++)
226         {
227             temp.append(aStringList.get(idx)).append(LineSeparator);
228         }
229
230         return temp.toString();
231     }
232
233     /**
234      * True if a given string starts with the another given String
235      *
236      * @param str The String to search in
237      * @param prefix The string to search for
238      *
239      * @return True if str starts with prefix
240      */

241     public static boolean startsWith(String JavaDoc str, String JavaDoc prefix)
242     {
243         return (str != null) && str.startsWith(prefix);
244     }
245
246     /**
247      * The same as startsWith but ignores the case.
248      *
249      * @param str The String to search in
250      * @param prefix The string to search for
251      *
252      * @return rue if str starts with prefix
253      */

254     public static boolean startsWithIgnoreCase(String JavaDoc str, String JavaDoc prefix)
255     {
256         return (str != null) && (prefix != null)
257                 && str.toUpperCase().startsWith(prefix.toUpperCase());
258     }
259
260 }
261
Popular Tags