View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support;
14  
15  import java.util.StringTokenizer;
16  
17  public class StringUtils
18  {
19  	public static String unquote( String str )
20  	{
21  		int length = str == null ? -1 : str.length();
22  		if( str == null || length == 0 )
23  			return str;
24  
25  		if( length > 1 && str.charAt( 0 ) == '\"' && str.charAt( length - 1 ) == '\"' )
26  		{
27  			str = str.substring( 1, length - 1 );
28  		}
29  
30  		return str;
31  	}
32  
33  	public static boolean isNullOrEmpty( String str )
34  	{
35  		return str == null || str.length() == 0 || str.trim().length() == 0;
36  	}
37  
38  	public static int parseInt( String str, int defaultValue )
39  	{
40  		if( isNullOrEmpty( str ) )
41  			return defaultValue;
42  
43  		try
44  		{
45  			return Integer.parseInt( str );
46  		}
47  		catch( NumberFormatException e )
48  		{
49  			return defaultValue;
50  		}
51  	}
52  
53  	public static String normalizeSpace( String str )
54  	{
55  		if( !isNullOrEmpty( str ) )
56  		{
57  			StringTokenizer st = new StringTokenizer( str );
58  			if( st.hasMoreTokens() )
59  			{
60  
61  				StringBuffer sb = new StringBuffer( str.length() );
62  				while( true )
63  				{
64  					sb.append( st.nextToken() );
65  					if( st.hasMoreTokens() )
66  					{
67  						sb.append( ' ' );
68  					}
69  					else
70  					{
71  						break;
72  					}
73  				}
74  				return sb.toString();
75  			}
76  			else
77  			{
78  				return "";
79  			}
80  		}
81  		else
82  		{
83  			return str;
84  		}
85  	}
86  
87  	public static boolean hasContent( String str )
88  	{
89  		return str != null && str.trim().length() > 0;
90  	}
91  }