KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > StringUtils


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util;
19
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 /**
25  * A set of helper methods related to string manipulation.
26  *
27  */

28 public final class StringUtils {
29
30     /**
31      * constructor to stop anyone instantiating the class
32      */

33     private StringUtils() {
34     }
35
36     /** the line separator for this OS */
37     public static final String JavaDoc LINE_SEP = System.getProperty("line.separator");
38
39     /**
40      * Splits up a string into a list of lines. It is equivalent
41      * to <tt>split(data, '\n')</tt>.
42      * @param data the string to split up into lines.
43      * @return the list of lines available in the string.
44      */

45     public static Vector JavaDoc lineSplit(String JavaDoc data) {
46         return split(data, '\n');
47     }
48
49     /**
50      * Splits up a string where elements are separated by a specific
51      * character and return all elements.
52      * @param data the string to split up.
53      * @param ch the separator character.
54      * @return the list of elements.
55      */

56     public static Vector JavaDoc split(String JavaDoc data, int ch) {
57         Vector JavaDoc elems = new Vector JavaDoc();
58         int pos = -1;
59         int i = 0;
60         while ((pos = data.indexOf(ch, i)) != -1) {
61             String JavaDoc elem = data.substring(i, pos);
62             elems.addElement(elem);
63             i = pos + 1;
64         }
65         elems.addElement(data.substring(i));
66         return elems;
67     }
68
69     /**
70      * Replace occurrences into a string.
71      * @param data the string to replace occurrences into
72      * @param from the occurrence to replace.
73      * @param to the occurrence to be used as a replacement.
74      * @return the new string with replaced occurrences.
75      */

76     public static String JavaDoc replace(String JavaDoc data, String JavaDoc from, String JavaDoc to) {
77         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(data.length());
78         int pos = -1;
79         int i = 0;
80         while ((pos = data.indexOf(from, i)) != -1) {
81             buf.append(data.substring(i, pos)).append(to);
82             i = pos + from.length();
83         }
84         buf.append(data.substring(i));
85         return buf.toString();
86     }
87
88     /**
89      * Convenient method to retrieve the full stacktrace from a given exception.
90      * @param t the exception to get the stacktrace from.
91      * @return the stacktrace from the given exception.
92      */

93     public static String JavaDoc getStackTrace(Throwable JavaDoc t) {
94         StringWriter JavaDoc sw = new StringWriter JavaDoc();
95         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw, true);
96         t.printStackTrace(pw);
97         pw.flush();
98         pw.close();
99         return sw.toString();
100     }
101
102     /**
103      * Checks that a string buffer ends up with a given string. It may sound
104      * trivial with the existing
105      * JDK API but the various implementation among JDKs can make those
106      * methods extremely resource intensive
107      * and perform poorly due to massive memory allocation and copying. See
108      * @param buffer the buffer to perform the check on
109      * @param suffix the suffix
110      * @return <code>true</code> if the character sequence represented by the
111      * argument is a suffix of the character sequence represented by
112      * the StringBuffer object; <code>false</code> otherwise. Note that the
113      * result will be <code>true</code> if the argument is the
114      * empty string.
115      */

116     public static boolean endsWith(StringBuffer JavaDoc buffer, String JavaDoc suffix) {
117         if (suffix.length() > buffer.length()) {
118             return false;
119         }
120         // this loop is done on purpose to avoid memory allocation performance
121
// problems on various JDKs
122
// StringBuffer.lastIndexOf() was introduced in jdk 1.4 and
123
// implementation is ok though does allocation/copying
124
// StringBuffer.toString().endsWith() does massive memory
125
// allocation/copying on JDK 1.5
126
// See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169
127
int endIndex = suffix.length() - 1;
128         int bufferIndex = buffer.length() - 1;
129         while (endIndex >= 0) {
130             if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) {
131                 return false;
132             }
133             bufferIndex--;
134             endIndex--;
135         }
136         return true;
137     }
138
139     /**
140      * xml does not do "c" like interpretation of strings.
141      * i.e. \n\r\t etc.
142      * this method processes \n, \r, \t, \f, \\
143      * also subs \s -> " \n\r\t\f"
144      * a trailing '\' will be ignored
145      *
146      * @param input raw string with possible embedded '\'s
147      * @return converted string
148      * @since Ant 1.7
149      */

150     public static String JavaDoc resolveBackSlash(String JavaDoc input) {
151         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
152         boolean backSlashSeen = false;
153         for (int i = 0; i < input.length(); ++i) {
154             char c = input.charAt(i);
155             if (!backSlashSeen) {
156                 if (c == '\\') {
157                     backSlashSeen = true;
158                 } else {
159                     b.append(c);
160                 }
161             } else {
162                 switch (c) {
163                     case '\\':
164                         b.append((char) '\\');
165                         break;
166                     case 'n':
167                         b.append((char) '\n');
168                         break;
169                     case 'r':
170                         b.append((char) '\r');
171                         break;
172                     case 't':
173                         b.append((char) '\t');
174                         break;
175                     case 'f':
176                         b.append((char) '\f');
177                         break;
178                     case 's':
179                         b.append(" \t\n\r\f");
180                         break;
181                     default:
182                         b.append(c);
183                 }
184                 backSlashSeen = false;
185             }
186         }
187         return b.toString();
188     }
189     
190     /**
191      * Takes a human readable size representation eg 10K
192      * a long value. Doesn't support 1.1K or other rational values.
193      * @param humanSize
194      * @return a long value representation
195      * @throws Exception
196      * @since Ant 1.7
197      */

198     public static long parseHumanSizes(String JavaDoc humanSize) throws Exception JavaDoc {
199         final long KILOBYTE = 1024;
200         final long MEGABYTE = KILOBYTE * 1024;
201         final long GIGABYTE = MEGABYTE * 1024;
202         final long TERABYTE = GIGABYTE * 1024;
203         final long PETABYTE = TERABYTE * 1024;
204         //last character isn't a digit
205
if(!Character.isDigit(humanSize.charAt(humanSize.length()-1))) {
206             char c = humanSize.charAt(humanSize.length()-1);
207             long value = Long.valueOf(humanSize.substring(0, humanSize.length()-1)).longValue();
208             switch (c) {
209                 case 'K':
210                     return value * KILOBYTE;
211                 case 'M':
212                     return value * MEGABYTE;
213                 case 'G':
214                     return value * GIGABYTE;
215                 case 'T':
216                     return value * TERABYTE;
217                 case 'P':
218                     return value * PETABYTE;
219                 default:
220                     return value;
221             }
222         } else {
223             return Long.parseLong(humanSize);
224         }
225     }
226 }
227
Popular Tags