View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.BufferedReader;
16  import java.io.IOException;
17  import java.io.LineNumberReader;
18  import java.io.StringReader;
19  import java.io.UnsupportedEncodingException;
20  import java.io.Writer;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.StringTokenizer;
24  
25  import com.eviware.soapui.support.types.StringList;
26  
27  public class StringUtils
28  {
29  	public static final String NEWLINE = System.getProperty( "line.separator" );
30  
31  	public static String unquote( String str )
32  	{
33  		int length = str == null ? -1 : str.length();
34  		if( str == null || length == 0 )
35  			return str;
36  
37  		if( length > 1 && str.charAt( 0 ) == '\"' && str.charAt( length - 1 ) == '\"' )
38  		{
39  			str = str.substring( 1, length - 1 );
40  		}
41  
42  		return str;
43  	}
44  
45  	public static boolean isNullOrEmpty( String str )
46  	{
47  		return str == null || str.length() == 0 || str.trim().length() == 0;
48  	}
49  
50  	public static int parseInt( String str, int defaultValue )
51  	{
52  		if( isNullOrEmpty( str ) )
53  			return defaultValue;
54  
55  		try
56  		{
57  			return Integer.parseInt( str );
58  		}
59  		catch( NumberFormatException e )
60  		{
61  			return defaultValue;
62  		}
63  	}
64  
65  	public static List<String> splitLines( String string )
66  	{
67  		try
68  		{
69  			ArrayList<String> list = new ArrayList<String>();
70  
71  			LineNumberReader reader = new LineNumberReader( new StringReader( string ) );
72  			String s;
73  			while( ( s = reader.readLine() ) != null )
74  			{
75  				list.add( s );
76  			}
77  			return list;
78  		}
79  		catch( IOException e )
80  		{
81  			// I don't think this can really happen with a StringReader.
82  			throw new RuntimeException( e );
83  		}
84  	}
85  
86  	public static String normalizeSpace( String str )
87  	{
88  		if( !isNullOrEmpty( str ) )
89  		{
90  			StringTokenizer st = new StringTokenizer( str );
91  			if( st.hasMoreTokens() )
92  			{
93  
94  				StringBuffer sb = new StringBuffer( str.length() );
95  				while( true )
96  				{
97  					sb.append( st.nextToken() );
98  					if( st.hasMoreTokens() )
99  					{
100 						sb.append( ' ' );
101 					}
102 					else
103 					{
104 						break;
105 					}
106 				}
107 				return sb.toString();
108 			}
109 			else
110 			{
111 				return "";
112 			}
113 		}
114 		else
115 		{
116 			return str;
117 		}
118 	}
119 
120 	public static boolean hasContent( String str )
121 	{
122 		return str != null && str.trim().length() > 0;
123 	}
124 
125 	public static String stripStartAndEnd( String s, String start, String end )
126 	{
127 		if( s.startsWith( start ) && s.endsWith( end ) )
128 			return s.substring( start.length(), s.length() - end.length() );
129 		else
130 			return s;
131 	}
132 
133 	public static Writer createSeparatedRow( Writer writer, StringList values, char separator, char quote )
134 			throws IOException
135 	{
136 		for( int c = 0; c < values.size(); c++ )
137 		{
138 			String value = values.get( c );
139 
140 			if( c > 0 )
141 				writer.append( separator );
142 
143 			if( quote > 0 )
144 			{
145 				writer.append( quote );
146 
147 				if( value != null )
148 				{
149 					for( int i = 0; i < value.length(); i++ )
150 					{
151 						char ch = value.charAt( i );
152 
153 						if( ch == quote )
154 							writer.append( '//' );
155 						else if( ch == '//' )
156 							writer.append( '//' );
157 
158 						writer.append( ch );
159 					}
160 				}
161 
162 				writer.append( quote );
163 			}
164 			else if( value != null )
165 			{
166 				writer.append( value );
167 			}
168 		}
169 
170 		return writer;
171 	}
172 
173 	public static StringList readSeparatedRow( String row, char separator, char quote )
174 	{
175 		StringList result = new StringList();
176 
177 		while( row != null && row.length() > 0 )
178 		{
179 			if( row.startsWith( String.valueOf( quote ) ) )
180 			{
181 				StringBuffer buf = new StringBuffer();
182 				char last = row.charAt( 0 );
183 				int ix = 1;
184 				while( ix < row.length() )
185 				{
186 					char ch = row.charAt( ix );
187 					if( ch == quote && last != '//' )
188 					{
189 						result.add( buf.toString() );
190 						row = row.length() > ix + 1 ? row.substring( ix + 1 ) : null;
191 						if( row != null && row.length() > 1 && row.charAt( 0 ) == separator )
192 						{
193 							row = row.substring( 1 );
194 							ix = -1;
195 						}
196 						break;
197 					}
198 					else if( ch != '//' || last == '//' )
199 					{
200 						buf.append( ch );
201 					}
202 
203 					last = ch;
204 					ix++ ;
205 				}
206 
207 				if( row != null && ix == row.length() )
208 				{
209 					result.add( row );
210 					row = null;
211 				}
212 			}
213 			else
214 			{
215 				int ix = row.indexOf( separator );
216 				if( ix == -1 )
217 				{
218 					result.add( row );
219 					row = null;
220 				}
221 				else
222 				{
223 					result.add( row.substring( 0, ix ) );
224 					row = row.substring( ix + 1 );
225 				}
226 			}
227 		}
228 
229 		return result;
230 	}
231 
232 	/***
233 	 * replaces only white spaces from file name
234 	 */
235 	public static String createFileName( String str, char whitespaceChar )
236 	{
237 		StringBuffer result = new StringBuffer();
238 
239 		for( int c = 0; c < str.length(); c++ )
240 		{
241 			char ch = str.charAt( c );
242 
243 			if( Character.isWhitespace( ch ) && whitespaceChar != 0 )
244 				result.append( whitespaceChar );
245 			else if( Character.isLetterOrDigit( ch ) )
246 				result.append( ch );
247 		}
248 
249 		return result.toString();
250 	}
251 	
252 	/***
253 	 * replaces all non letter and non digit characte from file name
254 	 * @param str
255 	 * @param replace
256 	 * @return
257 	 */
258 	public static String createFileName2( String str, char replace )
259 	{
260 		StringBuffer result = new StringBuffer();
261 
262 		for( int c = 0; c < str.length(); c++ )
263 		{
264 			char ch = str.charAt( c );
265 
266 			if( Character.isLetterOrDigit( ch ) )
267 				result.append( ch );
268 			else 
269 				result.append( replace );
270 		}
271 
272 		return result.toString();
273 	}
274 	
275 	public static String createXmlName( String str )
276 	{
277 		StringBuffer result = new StringBuffer();
278 		boolean skipped = false;
279 		boolean numbersOnly = true;
280 		
281 		for( int c = 0; c < str.length(); c++ )
282 		{
283 			char ch = str.charAt( c );
284 
285 			if( Character.isLetter( ch ))
286 			{
287 				if( skipped )
288 					result.append(  Character.toUpperCase( ch ) );
289 				else
290 					result.append( ch );
291 				numbersOnly = false;
292 				skipped = false;
293 			}
294 			else if( Character.isDigit( ch ) )
295 			{
296 				result.append( ch );
297 				skipped = false;
298 			}
299 			else
300 			{
301 				skipped = true;
302 			}
303 		}
304 
305 		str = result.toString();
306 		if( numbersOnly && str.length() > 0 )
307 			str = "_" + str;
308 		
309 		return str;
310 	}
311 
312 	public static String[] merge( String[] incomingNames, String string )
313 	{
314 		StringList result = new StringList( incomingNames );
315 		result.add( string );
316 		return result.toStringArray();
317 	}
318 
319 	public static String quote( String str )
320 	{
321 		if( str == null )
322 			return str;
323 
324 		if( str.length() < 2 || !str.startsWith( "\"" ) || !str.endsWith( "\"" ) )
325 			str = "\"" + str + "\"";
326 
327 		return str;
328 	}
329 
330 	public static String join( String[] array, String separator )
331 	{
332 		StringBuffer buf = new StringBuffer();
333 		for( int i = 0; i < array.length; i++ )
334 		{
335 			if( i > 0 )
336 				buf.append( separator );
337 			buf.append( array[i] );
338 		}
339 		return buf.toString();
340 	}
341 
342 	public static String toHtml( String string )
343 	{
344 		if( StringUtils.isNullOrEmpty( string ) )
345 			return "<html><body></body></html>";
346 
347 		BufferedReader st = new BufferedReader( new StringReader( string ) );
348 		StringBuffer buf = new StringBuffer( "<html><body>" );
349 
350 		try
351 		{
352 			String str = st.readLine();
353 
354 			while( str != null )
355 			{
356 				if( str.equalsIgnoreCase( "<br/>" ) )
357 				{
358 					str = "<br>";
359 				}
360 
361 				buf.append( str );
362 
363 				if( !str.equalsIgnoreCase( "<br>" ) )
364 				{
365 					buf.append( "<br>" );
366 				}
367 
368 				str = st.readLine();
369 			}
370 		}
371 		catch( IOException e )
372 		{
373 			e.printStackTrace();
374 		}
375 
376 		buf.append( "</body></html>" );
377 		string = buf.toString();
378 		return string;
379 	}
380 
381 	public static String replace( String data, String from, String to )
382 	{
383 		StringBuffer buf = new StringBuffer( data.length() );
384 		int pos = -1;
385 		int i = 0;
386 		while( ( pos = data.indexOf( from, i ) ) != -1 )
387 		{
388 			buf.append( data.substring( i, pos ) ).append( to );
389 			i = pos + from.length();
390 		}
391 		buf.append( data.substring( i ) );
392 		return buf.toString();
393 	}
394 
395 	public static String fixLineSeparator( String xml ) throws UnsupportedEncodingException
396 	{
397 		if( "\r\n".equals( System.getProperty( "line.separator" ) ) )
398 		{
399 			xml = xml.replaceAll( "\r[^\n]", System.getProperty( "line.separator" ) );
400 		}
401 		else
402 		{
403 			xml = xml.replaceAll( "\r\n", System.getProperty( "line.separator" ) );
404 		}
405 
406 		return xml;
407 	}
408 
409 	public static String capitalize( String string )
410 	{
411 		if( isNullOrEmpty( string ) )
412 			return string;
413 		return string.toUpperCase().substring( 0, 1 ) + string.toLowerCase().substring( 1 );
414 	}
415 
416 	public static String[] toStringArray( Object[] selectedOptions )
417 	{
418 		String[] result = new String[selectedOptions.length];
419 		for( int c = 0; c < selectedOptions.length; c++ )
420 			result[c] = String.valueOf( selectedOptions[c] );
421 		return result;
422 	}
423 
424 	public static List<String> toStringList( Object[] selectedOptions )
425 	{
426 		StringList result = new StringList();
427 		
428 		for( Object o : selectedOptions )
429 		{
430 			result.add( o.toString() );
431 		}
432 		
433 		return result;
434 	}
435 }