1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support;
14
15 import java.io.BufferedInputStream;
16 import java.io.BufferedOutputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.lang.reflect.Method;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28
29 import com.eviware.soapui.support.types.StringToStringMap;
30
31 public class Tools
32 {
33 public static final int COPY_BUFFER_SIZE = 1000;
34
35 public static String[] tokenizeArgs(String args)
36 {
37 if( args == null || args.trim().length() == 0 )
38 return null;
39
40 List<String> l = Arrays.asList( args.split( " " ));
41 List<String> result = new ArrayList<String>();
42
43 for( int c = 0; c < l.size(); c++ )
44 {
45 String s = l.get( c );
46 if( s.startsWith( "\"" ))
47 {
48 c++;
49 s += " " + l.get( c );
50 while( !(s.endsWith( "\"") && !s.endsWith( "//\"" )) && c < l.size() )
51 {
52 c++;
53 }
54
55
56 s = c == l.size() ? s.substring( 1 ) : s.substring( 1, s.length()-1 );
57
58
59 s = s.replace( "//\"", "\"" );
60 }
61
62 result.add( s );
63 }
64
65 return result.toArray( new String[result.size()]);
66 }
67
68 public static String convertToHtml( String str )
69 {
70 StringBuffer result = new StringBuffer( "<html><body>");
71
72 for( int c = 0; c < str.length(); c++ )
73 {
74 char ch = str.charAt( c );
75 if( ch == '\n' )
76 result.append( "<br>" );
77 else
78 result.append( ch );
79 }
80
81 result.append( "</body></html>");
82 return result.toString();
83 }
84
85 public static String getFilename(String filePath)
86 {
87 if (filePath == null || filePath.length() == 0)
88 return filePath;
89
90 int ix = filePath.lastIndexOf( File.separatorChar );
91 if (ix <= 0)
92 return filePath;
93
94 return filePath.substring(ix+1, filePath.length());
95 }
96
97 public static String getDir(String filePath)
98 {
99 if (filePath == null || filePath.length() == 0)
100 return filePath;
101
102 int ix = filePath.lastIndexOf( File.separatorChar );
103 if (ix <= 0)
104 return filePath;
105
106 return ensureDir(filePath.substring(0, ix), "");
107 }
108
109 public static String ensureDir(String dir, String basedir )
110 {
111 if( dir == null || dir.length() == 0 )
112 return "";
113
114 File dirFile = new File( dir );
115 if( !dirFile.isAbsolute() )
116 {
117 if( basedir.length() == 0 )
118 basedir = new File("").getAbsolutePath();
119
120 dirFile = new File( basedir, dir );
121 }
122
123 dirFile.mkdirs();
124 return dirFile.getAbsolutePath();
125 }
126
127 public static String ensureFileDir(String file, String basedir )
128 {
129 if( file == null || file.length() == 0 )
130 return "";
131
132 File dirFile = new File( basedir, file );
133 if( !dirFile.isAbsolute() )
134 {
135 if( basedir.length() == 0 )
136 basedir = new File("").getAbsolutePath();
137
138 dirFile = new File( basedir, file );
139 }
140
141 String absolutePath = dirFile.getAbsolutePath();
142 if( !dirFile.exists() )
143 {
144 int ix = absolutePath.lastIndexOf( File.separatorChar );
145 File fileDir = new File( absolutePath.substring( 0, ix ));
146 fileDir.mkdirs();
147 }
148
149 return absolutePath;
150 }
151
152 public static String ensureDir(String outputDir)
153 {
154 if( outputDir == null )
155 outputDir = "";
156
157 File output = new File(outputDir);
158 output.mkdirs();
159 return outputDir;
160 }
161
162
163 private static final byte[] copyBuffer = new byte[8192];
164 public static final long READ_ALL = 0;
165
166 public static void openURL(String url)
167 {
168 String osName = System.getProperty("os.name");
169
170 try
171 {
172 if (osName.startsWith("Mac OS")) {
173 Class fileMgr = Class.forName("com.apple.eio.FileManager");
174 Method openURL = fileMgr.getDeclaredMethod("openURL",
175 new Class[] {String.class});
176 openURL.invoke(null, new Object[] {url});
177 }
178 else if (osName.startsWith("Windows"))
179 {
180 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
181 }
182 else {
183 String[] browsers = {
184 "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
185 String browser = null;
186 for (int count = 0; count < browsers.length && browser == null; count++)
187 if (Runtime.getRuntime().exec(
188 new String[] {"which", browsers[count]}).waitFor() == 0)
189 browser = browsers[count];
190 if (browser == null)
191 throw new Exception("Could not find web browser");
192 else
193 Runtime.getRuntime().exec(new String[] {browser, url});
194 }
195 }
196 catch (Exception e)
197 {
198 UISupport.showErrorMessage(e);
199 }
200 }
201
202 public static ByteArrayOutputStream readAll(InputStream instream, long maxSize) throws IOException
203 {
204 ByteArrayOutputStream outstream = new ByteArrayOutputStream( 4096 );
205
206 byte[] buffer = new byte[4096];
207 int len;
208 int read = 0;
209 int toRead = 4096;
210
211 if( maxSize > 0 )
212 {
213 if( read + toRead > maxSize )
214 toRead = (int) (maxSize - read);
215 }
216
217 while ((len = instream.read(buffer, 0, toRead )) > 0)
218 {
219 outstream.write(buffer, 0, len);
220 read += toRead;
221
222 if( maxSize > 0 )
223 {
224 if( read + toRead > maxSize )
225 toRead = (int) (maxSize - read);
226 }
227 }
228
229 outstream.close();
230 return outstream;
231 }
232
233 public static int copyFile( File source, File target, boolean overwrite ) throws IOException
234 {
235 int bytes = 0;
236
237 if( overwrite && target.exists() )
238 {
239 target.delete();
240 }
241 else if( !target.exists() )
242 {
243 String path = target.getAbsolutePath();
244 int ix = path.lastIndexOf( File.separatorChar );
245 if( ix != -1 )
246 {
247 path = path.substring( 0, ix );
248 File pathFile = new File( path );
249 if( !pathFile.exists() )
250 pathFile.mkdirs();
251 }
252 }
253
254 BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
255 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target));
256
257 int read = in.read( copyBuffer );
258 while( read != -1 )
259 {
260 if( read > 0 )
261 {
262 out.write( copyBuffer, 0, read );
263 bytes += read;
264 }
265 read = in.read( copyBuffer );
266 }
267
268 in.close();
269 out.close();
270
271 return bytes;
272 }
273
274 /***
275 * Joins a relative url to a base url.. needs improvements..
276 */
277
278 public static String joinRelativeUrl( String baseUrl, String url )
279 {
280 boolean isWindowsUrl = baseUrl.indexOf( '//' ) >= 0;
281 boolean isUsedInUnix = File.separatorChar == '/';
282
283 if( isUsedInUnix && isWindowsUrl )
284 {
285 baseUrl = baseUrl.replace( '//', '/' );
286 url = url.replace( '//', '/' );
287 }
288
289 boolean isFile = baseUrl.startsWith("file:");
290
291 int ix = baseUrl.lastIndexOf( '//' );
292 if( ix == -1 ) ix = baseUrl.lastIndexOf( '/' );
293
294
295 while( url.startsWith( ".//" ) || url.startsWith( "./" ))
296 url = url.substring( 2 );
297
298
299 while( url.startsWith( "../" ) || url.startsWith( "..//" ))
300 {
301 int ix2 = baseUrl.lastIndexOf( '//', ix-1 );
302 if( ix2 == -1 )
303 ix2 = baseUrl.lastIndexOf( '/', ix-1 );
304 if( ix2 == -1 )
305 break;
306
307 baseUrl = baseUrl.substring( 0, ix2+1 );
308 ix = ix2;
309
310 url = url.substring( 3 );
311 }
312
313
314 while( url.indexOf( "/./" ) != -1 || url.indexOf( "//.//") != -1 )
315 {
316 int ix2 = url.indexOf( "/./");
317 if( ix2 == -1 )
318 ix2 = url.indexOf( "//.//" );
319
320 url = url.substring( 0, ix2 ) + url.substring( ix2+2 );
321 }
322
323
324 while( url.indexOf( "/../" ) != -1 || url.indexOf( "//..//") != -1 )
325 {
326 int ix2 = -1;
327
328 int ix3 = url.indexOf( "/../");
329 if( ix3 == -1 )
330 {
331 ix3 = url.indexOf( "//..//" );
332 ix2 = url.lastIndexOf( '//', ix3-1 );
333 }
334 else
335 {
336 ix2 = url.lastIndexOf( '/', ix3-1 );
337 }
338
339 if( ix2 == -1 )
340 break;
341
342 url = url.substring( 0, ix2 ) + url.substring( ix3+3 );
343 }
344
345 String result = baseUrl.substring( 0, ix+1 ) + url;
346 if( isFile )
347 result = result.replace( '/', File.separatorChar );
348
349 return result;
350 }
351
352 public static boolean isEmpty(String str)
353 {
354 return str == null || str.trim().length() == 0;
355 }
356
357 public static long writeAll( OutputStream out, InputStream in ) throws IOException
358 {
359 byte [] buffer = new byte[COPY_BUFFER_SIZE];
360 long total = 0;
361
362 int sz = in.read( buffer );
363 while( sz != -1 )
364 {
365 out.write( buffer, 0, sz );
366 total += sz;
367 sz = in.read( buffer );
368 }
369
370 return total;
371 }
372
373 public static String expandProperties(StringToStringMap values, String content, boolean leaveMissing)
374 {
375 int ix = content.indexOf( "${" );
376 if( ix == -1 )
377 return content;
378
379 StringBuffer buf = new StringBuffer();
380 int lastIx = 0;
381 while( ix != -1 )
382 {
383 buf.append( content.substring( lastIx, ix ));
384
385 int ix2 = content.indexOf( '}', ix+2 );
386 if( ix2 == -1 )
387 break;
388
389 int ix3 = content.lastIndexOf( "${", ix2 );
390 if( ix3 != ix )
391 {
392 buf.append( content.substring( ix, ix3 ));
393 ix = ix3;
394 }
395
396 String propertyName = content.substring( ix+2, ix2 );
397 Object property = values.get( propertyName );
398 if( property != null )
399 {
400 buf.append( property.toString() );
401 }
402 else if( leaveMissing )
403 {
404 buf.append( "${" ).append( propertyName ).append( '}' );
405 }
406
407 lastIx = ix2+1;
408 ix = content.indexOf( "${", lastIx );
409 }
410
411 if( lastIx < content.length() )
412 buf.append( content.substring( lastIx ));
413
414 return buf.toString();
415 }
416
417 /***
418 * Replaces the host part of the specified endpoint with the specified host
419 *
420 * @param endpoint the endpoint to modify
421 * @param host the host to set
422 * @return the modified endpoint
423 */
424
425 public static String replaceHost(String endpoint, String host)
426 {
427 int ix1 = endpoint.indexOf( "://" );
428 if( ix1 < 0 )
429 return endpoint;
430
431 int ix2 = endpoint.indexOf( ":", ix1+3 );
432 if( ix2 == -1 || host.indexOf( ":") > 0 )
433 {
434 ix2 = endpoint.indexOf( "/", ix1+3 );
435 if( ix2 == ix1+3 )
436 ix2 = -1;
437 }
438
439 return endpoint.substring( 0, ix1 )+ "://" + host + (ix2 == -1 ? "" : endpoint.substring(ix2));
440 }
441 }