1
2
3
4
5
6
7
8
9
10
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.Writer;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.StringTokenizer;
23
24 import com.eviware.soapui.support.types.StringList;
25
26 public class StringUtils
27 {
28 public static final String NEWLINE = System.getProperty("line.separator");
29
30 public static String unquote( String str )
31 {
32 int length = str == null ? -1 : str.length();
33 if( str == null || length == 0 )
34 return str;
35
36 if( length > 1 && str.charAt( 0 ) == '\"' && str.charAt( length - 1 ) == '\"' )
37 {
38 str = str.substring( 1, length - 1 );
39 }
40
41 return str;
42 }
43
44 public static boolean isNullOrEmpty( String str )
45 {
46 return str == null || str.length() == 0 || str.trim().length() == 0;
47 }
48
49 public static int parseInt( String str, int defaultValue )
50 {
51 if( isNullOrEmpty( str ) )
52 return defaultValue;
53
54 try
55 {
56 return Integer.parseInt( str );
57 }
58 catch( NumberFormatException e )
59 {
60 return defaultValue;
61 }
62 }
63
64 public static List<String> splitLines(String string)
65 {
66 try
67 {
68 ArrayList<String> list = new ArrayList<String>();
69
70 LineNumberReader reader = new LineNumberReader(new StringReader(string));
71 String s;
72 while( (s = reader.readLine()) != null )
73 {
74 list.add(s);
75 }
76 return list;
77 }
78 catch(IOException e)
79 {
80
81 throw new RuntimeException(e);
82 }
83 }
84
85 public static String normalizeSpace( String str )
86 {
87 if( !isNullOrEmpty( str ) )
88 {
89 StringTokenizer st = new StringTokenizer( str );
90 if( st.hasMoreTokens() )
91 {
92
93 StringBuffer sb = new StringBuffer( str.length() );
94 while( true )
95 {
96 sb.append( st.nextToken() );
97 if( st.hasMoreTokens() )
98 {
99 sb.append( ' ' );
100 }
101 else
102 {
103 break;
104 }
105 }
106 return sb.toString();
107 }
108 else
109 {
110 return "";
111 }
112 }
113 else
114 {
115 return str;
116 }
117 }
118
119 public static boolean hasContent( String str )
120 {
121 return str != null && str.trim().length() > 0;
122 }
123
124 public static String stripStartAndEnd(String s, String start, String end)
125 {
126 if(s.startsWith(start) && s.endsWith(end))
127 return s.substring(start.length(), s.length() - end.length());
128 else
129 return s;
130 }
131
132 public static Writer createSeparatedRow( Writer writer, StringList values, char separator, char quote ) throws IOException
133 {
134 for( int c = 0; c < values.size(); c++ )
135 {
136 String value = values.get( c );
137
138 if( c > 0 )
139 writer.append( separator );
140
141 if( quote > 0 )
142 {
143 writer.append( quote );
144
145 if( value != null )
146 {
147 for( int i = 0; i < value.length(); i++ )
148 {
149 char ch = value.charAt( i );
150
151 if( ch == quote )
152 writer.append( '//' );
153 else if( ch == '//' )
154 writer.append( '//' );
155
156 writer.append( ch );
157 }
158 }
159
160 writer.append( quote );
161 }
162 else if( value != null )
163 {
164 writer.append( value );
165 }
166 }
167
168 return writer;
169 }
170
171 public static StringList readSeparatedRow( String row, char separator, char quote )
172 {
173 StringList result = new StringList();
174
175 while( row != null && row.length() > 0 )
176 {
177 if( row.startsWith( String.valueOf( quote ) ))
178 {
179 StringBuffer buf = new StringBuffer();
180 char last = row.charAt( 0 );
181 int ix = 1;
182 while( ix < row.length() )
183 {
184 char ch = row.charAt( ix );
185 if( ch == quote && last != '//' )
186 {
187 result.add( buf.toString() );
188 row = row.length() > ix+1 ? row.substring( ix+1 ) : null;
189 if( row != null && row.length() > 1 && row.charAt( 0 ) == separator )
190 {
191 row = row.substring( 1 );
192 ix = -1;
193 }
194 break;
195 }
196 else if( ch != '//' || last == '//')
197 {
198 buf.append( ch );
199 }
200
201 last = ch;
202 ix++;
203 }
204
205 if( row != null && ix == row.length() )
206 {
207 result.add( row );
208 row = null;
209 }
210 }
211 else
212 {
213 int ix = row.indexOf( separator );
214 if( ix == -1 )
215 {
216 result.add( row );
217 row = null;
218 }
219 else
220 {
221 result.add( row.substring( 0, ix ));
222 row = row.substring( ix+1 );
223 }
224 }
225 }
226
227 return result;
228 }
229
230 public static String createFileName( String str, char whitespaceChar )
231 {
232 StringBuffer result = new StringBuffer();
233
234 for( int c = 0; c < str.length(); c++ )
235 {
236 char ch = str.charAt( c );
237
238 if( Character.isWhitespace( ch ) && whitespaceChar != 0 )
239 result.append( whitespaceChar );
240 else if( Character.isLetterOrDigit( ch ))
241 result.append( ch );
242 }
243
244 return result.toString();
245 }
246
247 public static String[] merge( String[] incomingNames, String string )
248 {
249 StringList result = new StringList( incomingNames );
250 result.add( string );
251 return result.toStringArray();
252 }
253
254 public static String quote( String str )
255 {
256 if( str == null )
257 return str;
258
259 if( str.length() < 2 || !str.startsWith( "\"") || !str.endsWith( "\"" ))
260 str = "\"" + str + "\"";
261
262 return str;
263 }
264
265 public static String join(String[] array, String separator)
266 {
267 StringBuffer buf = new StringBuffer();
268 for(int i = 0; i < array.length; i++)
269 {
270 if(i > 0)
271 buf.append(separator);
272 buf.append(array[i]);
273 }
274 return buf.toString();
275 }
276
277 public static String toHtml( String string )
278 {
279 if( StringUtils.isNullOrEmpty(string))
280 return "<html><body></body></html>";
281
282 BufferedReader st = new BufferedReader( new StringReader( string ));
283 StringBuffer buf = new StringBuffer( "<html><body>" );
284
285 try
286 {
287 String str = st.readLine();
288
289 while( str != null )
290 {
291 if( str.equalsIgnoreCase( "<br/>" ))
292 {
293 str = "<br>";
294 }
295
296 buf.append( str );
297
298 if( !str.equalsIgnoreCase( "<br>" ))
299 {
300 buf.append( "<br>" );
301 }
302
303 str = st.readLine();
304 }
305 }
306 catch( IOException e )
307 {
308 e.printStackTrace();
309 }
310
311 buf.append( "</body></html>" );
312 string = buf.toString();
313 return string;
314 }
315 }