KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > StringUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.util.misc;
24
25 import java.util.Set JavaDoc;
26
27 import com.sun.appserv.management.util.stringifier.SmartStringifier;
28
29
30 /**
31     Escapes/unescapes strings
32  */

33 public final class StringUtil
34 {
35     private StringUtil() {}
36     
37     public final static char QUOTE_CHAR = '\"';
38     public final static String JavaDoc QUOTE = "" + QUOTE_CHAR;
39     
40     /**
41         Line separator as returned by System.getProperty()
42      */

43     public final static String JavaDoc LS = System.getProperty( "line.separator", "\n" );
44     
45         public static String JavaDoc
46     quote( Object JavaDoc o )
47     {
48         return( quote( o, QUOTE_CHAR ) );
49     }
50     
51         public static String JavaDoc
52     quote( Object JavaDoc o, char leftHandChar )
53     {
54         final String JavaDoc s = o == null ? "null" : SmartStringifier.toString( o );
55         
56         char leftChar = leftHandChar;
57         char rightChar = leftHandChar;
58         
59         if ( leftHandChar == '(' )
60         {
61             rightChar = ')';
62         }
63         else if ( leftHandChar == '{' )
64         {
65             rightChar = '}';
66         }
67         else if ( leftHandChar == '[' )
68         {
69             rightChar = ']';
70         }
71         else if ( leftHandChar == '<' )
72         {
73             rightChar = '>';
74         }
75         else
76         {
77             // same char on both left and right
78
}
79         
80         final String JavaDoc out = leftChar + s + rightChar;
81
82         return( out );
83     }
84     
85     
86         public static String JavaDoc
87     toHexString( byte theByte )
88     {
89         String JavaDoc result = Integer.toHexString( ((int)theByte) & 0x000000FF );
90         if ( result.length() == 1 )
91         {
92             result = "0" + result;
93         }
94         return( result );
95     }
96     
97         public static String JavaDoc
98     toHexString( byte[] bytes )
99     {
100         return( toHexString( bytes, null ) );
101     }
102     
103         public static String JavaDoc
104     toHexString( byte[] bytes, String JavaDoc delim )
105     {
106         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
107         
108         if ( bytes.length == 0 )
109         {
110             // nothing
111
}
112         else if ( delim == null || delim.length() == 0 )
113         {
114             for( int i = 0; i < bytes.length; ++i )
115             {
116                 buf.append( toHexString( bytes[ i ] ) );
117             }
118         }
119         else
120         {
121             for( int i = 0; i < bytes.length; ++i )
122             {
123                 buf.append( toHexString( bytes[ i ] ) + delim );
124             }
125         
126             // remove trailing delim
127
buf.setLength( buf.length() - 1);
128         }
129         
130         return( buf.toString() );
131     }
132     
133         public static String JavaDoc
134     stripSuffix(
135         final String JavaDoc s,
136         final String JavaDoc suffix )
137     {
138         String JavaDoc result = s;
139         
140         if ( s.endsWith( suffix ) )
141         {
142             result = s.substring( 0, s.length() - suffix.length() );
143         }
144         
145         return( result );
146     }
147     
148     
149         public static String JavaDoc
150     replaceSuffix(
151         final String JavaDoc s,
152         final String JavaDoc fromSuffix,
153         final String JavaDoc toSuffix )
154     {
155         if ( ! s.endsWith( fromSuffix ) )
156         {
157             throw new IllegalArgumentException JavaDoc( fromSuffix );
158         }
159
160         return( stripSuffix( s, fromSuffix ) + toSuffix );
161     }
162     
163         public static String JavaDoc
164     stripPrefix(
165         final String JavaDoc s,
166         final String JavaDoc prefix )
167     {
168         String JavaDoc result = s;
169         
170         if ( s.startsWith( prefix ) )
171         {
172             result = s.substring( prefix.length(), s.length() );
173         }
174         
175         return( result );
176     }
177     
178     
179         public static String JavaDoc
180     stripPrefixAndSuffix(
181         final String JavaDoc s,
182         final String JavaDoc prefix,
183         final String JavaDoc suffix )
184     {
185         return stripPrefix( stripSuffix( s, suffix ), prefix );
186     }
187     
188
189         public static String JavaDoc
190     upperCaseFirstLetter( final String JavaDoc s)
191     {
192         String JavaDoc result = s;
193         
194         if ( s.length() >= 1 )
195         {
196             result = s.substring( 0, 1 ).toUpperCase() + s.substring( 1, s.length() );
197         }
198
199         return( result );
200     }
201     
202     
203         private static String JavaDoc
204     toString( final Object JavaDoc o)
205     {
206         String JavaDoc result = null;
207         
208         if ( o == null )
209         {
210             result = "null";
211         }
212         else if ( o instanceof byte[] )
213         {
214             final byte[] b = byte[].class.cast( o );
215             result = "byte[] of length " + b.length;
216         }
217         else if ( o instanceof String JavaDoc[] )
218         {
219             result = toString( ", ", (Object JavaDoc[])o);
220         }
221         else
222         {
223             result = o.toString();
224         }
225         
226         final int MAX_LENGTH = 256;
227         if ( result.length() > MAX_LENGTH )
228         {
229             result = result.substring( 0, MAX_LENGTH - 1 );
230         }
231         
232         return result;
233     }
234     
235         public static String JavaDoc
236     toString( final String JavaDoc[] args )
237     {
238         return toString( ", ", args );
239     }
240     
241         public static String JavaDoc
242     toString( final String JavaDoc delim, final String JavaDoc... args )
243     {
244         return toString( delim, (Object JavaDoc[])args );
245     }
246     
247     /**
248         Turn an array (or varargs) set of Objects into a String
249         using the specified delimiter.
250      */

251         public static String JavaDoc
252     toString( final String JavaDoc delim, final Object JavaDoc... args )
253     {
254         String JavaDoc result = null;
255         
256         if ( args == null )
257         {
258             result = "" + null;
259         }
260         else if ( args.length == 0 )
261         {
262             result = "";
263         }
264         else if ( args.length == 1 )
265         {
266             result = toString( args[ 0 ] );
267         }
268         else
269         {
270             final StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
271             
272             for( int i = 0; i < args.length - 1; ++i )
273             {
274                 builder.append( toString( args[ i ] ) );
275                 builder.append( delim );
276             }
277             builder.append( toString( args[ args.length - 1 ] ) );
278             
279             result = builder.toString();
280         }
281         
282         
283         return result;
284      }
285      
286     /**
287         @return the prefix found, or null if not found
288      */

289         public static String JavaDoc
290     getPrefix(
291         final Set JavaDoc<String JavaDoc> prefixes,
292         final String JavaDoc s )
293     {
294         String JavaDoc result = null;
295         for( final String JavaDoc prefix : prefixes )
296         {
297             if ( s.startsWith( prefix ) )
298             {
299                 result = prefix;
300                 break;
301             }
302         }
303         return result;
304     }
305     
306     /**
307         @return the String after stripping the prefix
308         @throws IllegalArgumentException if no prefix found
309      */

310         public static String JavaDoc
311     findAndStripPrefix(
312         final Set JavaDoc<String JavaDoc> prefixes,
313         final String JavaDoc s )
314     {
315         final String JavaDoc prefix = getPrefix( prefixes, s );
316         if ( prefix == null )
317         {
318             throw new IllegalArgumentException JavaDoc( s );
319         }
320         
321         return stripPrefix( s, prefix );
322     }
323     
324     private static String JavaDoc NEWLINE_STR = null;
325     
326         public static String JavaDoc
327     NEWLINE()
328     {
329         if ( NEWLINE_STR == null )
330         {
331             NEWLINE_STR = System.getProperty( "line.separator" );
332         }
333         return NEWLINE_STR;
334     }
335    
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
Popular Tags