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.io.IOException;
16  import java.io.LineNumberReader;
17  import java.io.StringReader;
18  import java.io.Writer;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.StringTokenizer;
22  
23  import com.eviware.soapui.support.types.StringList;
24  
25  public class StringUtils
26  {
27     public static final String NEWLINE = System.getProperty("line.separator");
28  
29     public static String unquote( String str )
30  	{
31  		int length = str == null ? -1 : str.length();
32  		if( str == null || length == 0 )
33  			return str;
34  
35  		if( length > 1 && str.charAt( 0 ) == '\"' && str.charAt( length - 1 ) == '\"' )
36  		{
37  			str = str.substring( 1, length - 1 );
38  		}
39  
40  		return str;
41  	}
42  
43  	public static boolean isNullOrEmpty( String str )
44  	{
45  		return str == null || str.length() == 0 || str.trim().length() == 0;
46  	}
47  
48  	public static int parseInt( String str, int defaultValue )
49  	{
50  		if( isNullOrEmpty( str ) )
51  			return defaultValue;
52  
53  		try
54  		{
55  			return Integer.parseInt( str );
56  		}
57  		catch( NumberFormatException e )
58  		{
59  			return defaultValue;
60  		}
61  	}
62  
63     public static List<String> splitLines(String string)
64     {
65        try
66        {
67           ArrayList<String> list = new ArrayList<String>();
68           
69           LineNumberReader reader = new LineNumberReader(new StringReader(string));
70           String s;
71           while( (s = reader.readLine()) != null )
72           {
73              list.add(s);
74           }
75           return list;
76        }
77        catch(IOException e)
78        {
79           // I don't think this can really happen with a StringReader.
80           throw new RuntimeException(e);
81        }
82     }
83  
84  	public static String normalizeSpace( String str )
85  	{
86  		if( !isNullOrEmpty( str ) )
87  		{
88  			StringTokenizer st = new StringTokenizer( str );
89  			if( st.hasMoreTokens() )
90  			{
91  
92  				StringBuffer sb = new StringBuffer( str.length() );
93  				while( true )
94  				{
95  					sb.append( st.nextToken() );
96  					if( st.hasMoreTokens() )
97  					{
98  						sb.append( ' ' );
99  					}
100 					else
101 					{
102 						break;
103 					}
104 				}
105 				return sb.toString();
106 			}
107 			else
108 			{
109 				return "";
110 			}
111 		}
112 		else
113 		{
114 			return str;
115 		}
116 	}
117 
118 	public static boolean hasContent( String str )
119 	{
120 		return str != null && str.trim().length() > 0;
121 	}
122 	
123    public static String stripStartAndEnd(String s, String start, String end)
124    {
125       if(s.startsWith(start) && s.endsWith(end))
126          return s.substring(start.length(), s.length() - end.length());
127       else
128          return s;
129    }
130 
131 	public static Writer createSeparatedRow( Writer writer, StringList values, char separator, char quote ) throws IOException
132 	{
133 		for( int c = 0; c < values.size(); c++ )
134 		{
135 			String value = values.get( c );
136 			
137 			if( c > 0 )
138 				writer.append( separator );
139 			
140 			if( quote > 0 )
141 			{
142 				writer.append( quote );
143 				
144 				if( value != null )
145 				{
146 					for( int i = 0; i < value.length(); i++ )
147 					{
148 						char ch = value.charAt( i );
149 						
150 						if( ch == quote )
151 							writer.append( '//' );
152 						else if( ch == '//' )
153 							writer.append( '//' );
154 						
155 						writer.append( ch );
156 					}
157 				}
158 				
159 				writer.append( quote );
160 			}
161 			else if( value != null )
162 			{
163 				writer.append( value );
164 			}
165 		}
166 		
167 		return writer;
168 	}
169 
170 	public static StringList readSeparatedRow( String row, char separator, char quote )
171 	{
172 		StringList result = new StringList();
173 		
174 		while( row != null && row.length() > 0 )
175 		{
176 			if( row.startsWith( String.valueOf( quote ) ))
177 			{
178 				StringBuffer buf = new StringBuffer();
179 				char last = row.charAt( 0 );
180 				int ix = 1;
181 				while( ix < row.length() )
182 				{
183 					char ch = row.charAt( ix );
184 					if( ch == quote && last != '//' )
185 					{
186 						result.add( buf.toString() );
187 						row = row.length() > ix+1 ? row.substring( ix+1 ) : null;
188 						if( row != null && row.length() > 1  && row.charAt( 0 ) == separator )
189 							row = row.substring( 1 );
190 						break;
191 					}
192 					else if( ch != '//' || last == '//')
193 					{
194 						buf.append( ch );
195 					}
196 					
197 					last = ch;
198 					ix++;
199 				}
200 			}
201 			else
202 			{
203 				int ix = row.indexOf( separator );
204 				if( ix == -1 )
205 				{
206 					result.add( row );
207 					row = null;
208 				}
209 				else
210 				{
211 					result.add( row.substring( 0, ix ));
212 					row = row.substring( ix+1 );
213 				}
214 			}
215 		}
216 		
217 		return result;
218 	}
219 
220 	public static String createFileName( String str, char whitespaceChar )
221 	{
222 		StringBuffer result = new StringBuffer();
223 		
224 		for( int c = 0; c < str.length(); c++ )
225 		{
226 			char ch = str.charAt( c );
227 			
228 			if( Character.isWhitespace( ch ))
229 				result.append( whitespaceChar );
230 			else if( Character.isLetterOrDigit( ch ))
231 				result.append( ch );
232 		}
233 		
234 		return result.toString();
235 	}
236 
237 	public static String[] merge( String[] incomingNames, String string )
238 	{
239 		StringList result = new StringList( incomingNames );
240 		result.add( string );
241 		return result.toStringArray();
242 	}
243 }