View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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 	public static String createFileName( String str, char whitespaceChar )
233 	{
234 		StringBuffer result = new StringBuffer();
235 
236 		for( int c = 0; c < str.length(); c++ )
237 		{
238 			char ch = str.charAt( c );
239 
240 			if( Character.isWhitespace( ch ) && whitespaceChar != 0 )
241 				result.append( whitespaceChar );
242 			else if( Character.isLetterOrDigit( ch ) )
243 				result.append( ch );
244 		}
245 
246 		return result.toString();
247 	}
248 	
249 	public static String createXmlName( String str )
250 	{
251 		StringBuffer result = new StringBuffer();
252 		boolean skipped = false;
253 		boolean numbersOnly = true;
254 		
255 		for( int c = 0; c < str.length(); c++ )
256 		{
257 			char ch = str.charAt( c );
258 
259 			if( Character.isLetter( ch ))
260 			{
261 				if( skipped )
262 					result.append(  Character.toUpperCase( ch ) );
263 				else
264 					result.append( ch );
265 				numbersOnly = false;
266 				skipped = false;
267 			}
268 			else if( Character.isDigit( ch ) )
269 			{
270 				result.append( ch );
271 				skipped = false;
272 			}
273 			else
274 			{
275 				skipped = true;
276 			}
277 		}
278 
279 		str = result.toString();
280 		if( numbersOnly && str.length() > 0 )
281 			str = "_" + str;
282 		
283 		return str;
284 	}
285 
286 	public static String[] merge( String[] incomingNames, String string )
287 	{
288 		StringList result = new StringList( incomingNames );
289 		result.add( string );
290 		return result.toStringArray();
291 	}
292 
293 	public static String quote( String str )
294 	{
295 		if( str == null )
296 			return str;
297 
298 		if( str.length() < 2 || !str.startsWith( "\"" ) || !str.endsWith( "\"" ) )
299 			str = "\"" + str + "\"";
300 
301 		return str;
302 	}
303 
304 	public static String join( String[] array, String separator )
305 	{
306 		StringBuffer buf = new StringBuffer();
307 		for( int i = 0; i < array.length; i++ )
308 		{
309 			if( i > 0 )
310 				buf.append( separator );
311 			buf.append( array[i] );
312 		}
313 		return buf.toString();
314 	}
315 
316 	public static String toHtml( String string )
317 	{
318 		if( StringUtils.isNullOrEmpty( string ) )
319 			return "<html><body></body></html>";
320 
321 		BufferedReader st = new BufferedReader( new StringReader( string ) );
322 		StringBuffer buf = new StringBuffer( "<html><body>" );
323 
324 		try
325 		{
326 			String str = st.readLine();
327 
328 			while( str != null )
329 			{
330 				if( str.equalsIgnoreCase( "<br/>" ) )
331 				{
332 					str = "<br>";
333 				}
334 
335 				buf.append( str );
336 
337 				if( !str.equalsIgnoreCase( "<br>" ) )
338 				{
339 					buf.append( "<br>" );
340 				}
341 
342 				str = st.readLine();
343 			}
344 		}
345 		catch( IOException e )
346 		{
347 			e.printStackTrace();
348 		}
349 
350 		buf.append( "</body></html>" );
351 		string = buf.toString();
352 		return string;
353 	}
354 
355 	public static String replace( String data, String from, String to )
356 	{
357 		StringBuffer buf = new StringBuffer( data.length() );
358 		int pos = -1;
359 		int i = 0;
360 		while( ( pos = data.indexOf( from, i ) ) != -1 )
361 		{
362 			buf.append( data.substring( i, pos ) ).append( to );
363 			i = pos + from.length();
364 		}
365 		buf.append( data.substring( i ) );
366 		return buf.toString();
367 	}
368 
369 	public static String fixLineSeparator( String xml ) throws UnsupportedEncodingException
370 	{
371 		if( "\r\n".equals( System.getProperty( "line.separator" ) ) )
372 		{
373 			xml = xml.replaceAll( "\r[^\n]", System.getProperty( "line.separator" ) );
374 		}
375 		else
376 		{
377 			xml = xml.replaceAll( "\r\n", System.getProperty( "line.separator" ) );
378 		}
379 
380 		return xml;
381 	}
382 
383 	public static String capitalize( String string )
384 	{
385 		if( isNullOrEmpty( string ) )
386 			return string;
387 		return string.toUpperCase().substring( 0, 1 ) + string.toLowerCase().substring( 1 );
388 	}
389 
390 	public static String[] toStringArray( Object[] selectedOptions )
391 	{
392 		String[] result = new String[selectedOptions.length];
393 		for( int c = 0; c < selectedOptions.length; c++ )
394 			result[c] = String.valueOf( selectedOptions[c] );
395 		return result;
396 	}
397 
398 	public static List<String> toStringList( Object[] selectedOptions )
399 	{
400 		StringList result = new StringList();
401 		
402 		for( Object o : selectedOptions )
403 		{
404 			result.add( o.toString() );
405 		}
406 		
407 		return result;
408 	}
409 }