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 getFilename(String filePath)
69 {
70 if (filePath == null || filePath.length() == 0)
71 return filePath;
72
73 int ix = filePath.lastIndexOf( File.separatorChar );
74 if (ix <= 0)
75 return filePath;
76
77 return filePath.substring(ix+1, filePath.length());
78 }
79
80 public static String getDir(String filePath)
81 {
82 if (filePath == null || filePath.length() == 0)
83 return filePath;
84
85 int ix = filePath.lastIndexOf( File.separatorChar );
86 if (ix <= 0)
87 return filePath;
88
89 return ensureDir(filePath.substring(0, ix), "");
90 }
91
92 public static String ensureDir(String dir, String basedir )
93 {
94 if( dir == null || dir.length() == 0 )
95 return "";
96
97 File dirFile = new File( dir );
98 if( !dirFile.isAbsolute() )
99 {
100 if( basedir.length() == 0 )
101 basedir = new File("").getAbsolutePath();
102
103 dirFile = new File( basedir, dir );
104 }
105
106 dirFile.mkdirs();
107 return dirFile.getAbsolutePath();
108 }
109
110 public static String ensureFileDir(String file, String basedir )
111 {
112 if( file == null || file.length() == 0 )
113 return "";
114
115 File dirFile = new File( basedir, file );
116 if( !dirFile.isAbsolute() )
117 {
118 if( basedir.length() == 0 )
119 basedir = new File("").getAbsolutePath();
120
121 dirFile = new File( basedir, file );
122 }
123
124 String absolutePath = dirFile.getAbsolutePath();
125 if( !dirFile.exists() )
126 {
127 int ix = absolutePath.lastIndexOf( File.separatorChar );
128 File fileDir = new File( absolutePath.substring( 0, ix ));
129 fileDir.mkdirs();
130 }
131
132 return absolutePath;
133 }
134
135 public static String ensureDir(String outputDir)
136 {
137 if( outputDir == null )
138 outputDir = "";
139
140 File output = new File(outputDir);
141 output.mkdirs();
142 return outputDir;
143 }
144
145 private static final String errMsg = "Error attempting to launch web browser";
146
147
148 private static final byte[] copyBuffer = new byte[8192];
149 public static final long READ_ALL = 0;
150
151 public static void openURL(String url)
152 {
153 String osName = System.getProperty("os.name");
154
155 try
156 {
157 if (osName.startsWith("Mac OS")) {
158 Class fileMgr = Class.forName("com.apple.eio.FileManager");
159 Method openURL = fileMgr.getDeclaredMethod("openURL",
160 new Class[] {String.class});
161 openURL.invoke(null, new Object[] {url});
162 }
163 else if (osName.startsWith("Windows"))
164 {
165 if( url.startsWith( "file:"))
166 {
167 Runtime.getRuntime().exec("cmd.exe /C explorer " + url );
168 }
169 else
170 {
171 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
172 }
173 }
174 else {
175 String[] browsers = {
176 "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
177 String browser = null;
178 for (int count = 0; count < browsers.length && browser == null; count++)
179 if (Runtime.getRuntime().exec(
180 new String[] {"which", browsers[count]}).waitFor() == 0)
181 browser = browsers[count];
182 if (browser == null)
183 throw new Exception("Could not find web browser");
184 else
185 Runtime.getRuntime().exec(new String[] {browser, url});
186 }
187 }
188 catch (Exception e)
189 {
190 UISupport.showErrorMessage(e);
191 }
192 }
193
194 public static ByteArrayOutputStream readAll(InputStream instream, long maxSize) throws IOException
195 {
196 ByteArrayOutputStream outstream = new ByteArrayOutputStream( 4096 );
197
198 byte[] buffer = new byte[4096];
199 int len;
200 int read = 0;
201 int toRead = 4096;
202
203 if( maxSize > 0 )
204 {
205 if( read + toRead > maxSize )
206 toRead = (int) (maxSize - read);
207 }
208
209 while ((len = instream.read(buffer, 0, toRead )) > 0)
210 {
211 outstream.write(buffer, 0, len);
212 read += toRead;
213
214 if( maxSize > 0 )
215 {
216 if( read + toRead > maxSize )
217 toRead = (int) (maxSize - read);
218 }
219 }
220
221 outstream.close();
222 return outstream;
223 }
224
225 public static int copyFile( File source, File target, boolean overwrite ) throws IOException
226 {
227 int bytes = 0;
228
229 if( overwrite && target.exists() )
230 {
231 target.delete();
232 }
233 else if( !target.exists() )
234 {
235 String path = target.getAbsolutePath();
236 int ix = path.lastIndexOf( File.separatorChar );
237 if( ix != -1 )
238 {
239 path = path.substring( 0, ix );
240 File pathFile = new File( path );
241 if( !pathFile.exists() )
242 pathFile.mkdirs();
243 }
244 }
245
246 BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
247 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target));
248
249 int read = in.read( copyBuffer );
250 while( read != -1 )
251 {
252 if( read > 0 )
253 {
254 out.write( copyBuffer, 0, read );
255 bytes += read;
256 }
257 read = in.read( copyBuffer );
258 }
259
260 in.close();
261 out.close();
262
263 return bytes;
264 }
265
266 /***
267 * Joins a relative url to a base url.. needs improvements..
268 */
269
270 public static String joinRelativeUrl( String baseUrl, String url )
271 {
272 boolean isFile = baseUrl.startsWith("file:");
273
274 int ix = baseUrl.lastIndexOf( '//' );
275 if( ix == -1 ) ix = baseUrl.lastIndexOf( '/' );
276
277
278 while( url.startsWith( ".//" ) || url.startsWith( "./" ))
279 url = url.substring( 2 );
280
281
282 while( url.startsWith( "../" ) || url.startsWith( "..//" ))
283 {
284 int ix2 = baseUrl.lastIndexOf( '//', ix-1 );
285 if( ix2 == -1 )
286 ix2 = baseUrl.lastIndexOf( '/', ix-1 );
287 if( ix2 == -1 )
288 break;
289
290 baseUrl = baseUrl.substring( 0, ix2+1 );
291 ix = ix2;
292
293 url = url.substring( 3 );
294 }
295
296
297 while( url.indexOf( "/./" ) != -1 || url.indexOf( "//.//") != -1 )
298 {
299 int ix2 = url.indexOf( "/./");
300 if( ix2 == -1 )
301 ix2 = url.indexOf( "//.//" );
302
303 url = url.substring( 0, ix2 ) + url.substring( ix2+2 );
304 }
305
306
307 while( url.indexOf( "/../" ) != -1 || url.indexOf( "//..//") != -1 )
308 {
309 int ix2 = -1;
310
311 int ix3 = url.indexOf( "/../");
312 if( ix3 == -1 )
313 {
314 ix3 = url.indexOf( "//..//" );
315 ix2 = url.lastIndexOf( '//', ix3-1 );
316 }
317 else
318 {
319 ix2 = url.lastIndexOf( '/', ix3-1 );
320 }
321
322 if( ix2 == -1 )
323 break;
324
325 url = url.substring( 0, ix2 ) + url.substring( ix3+3 );
326 }
327
328 String result = baseUrl.substring( 0, ix+1 ) + url;
329 if( isFile )
330 result = result.replace( '/', File.separatorChar );
331
332 return result;
333 }
334
335 public static boolean isEmpty(String str)
336 {
337 return str == null || str.trim().length() == 0;
338 }
339
340 public static long writeAll( OutputStream out, InputStream in ) throws IOException
341 {
342 byte [] buffer = new byte[COPY_BUFFER_SIZE];
343 long total = 0;
344
345 int sz = in.read( buffer );
346 while( sz != -1 )
347 {
348 out.write( buffer, 0, sz );
349 total += sz;
350 sz = in.read( buffer );
351 }
352
353 return total;
354 }
355
356 public static String expandProperties(StringToStringMap values, String content, boolean leaveMissing)
357 {
358 int ix = content.indexOf( "${" );
359 if( ix == -1 )
360 return content;
361
362 StringBuffer buf = new StringBuffer();
363 int lastIx = 0;
364 while( ix != -1 )
365 {
366 buf.append( content.substring( lastIx, ix ));
367
368 int ix2 = content.indexOf( '}', ix+2 );
369 if( ix2 == -1 )
370 break;
371
372 int ix3 = content.lastIndexOf( "${", ix2 );
373 if( ix3 != ix )
374 {
375 buf.append( content.substring( ix, ix3 ));
376 ix = ix3;
377 }
378
379 String propertyName = content.substring( ix+2, ix2 );
380 Object property = values.get( propertyName );
381 if( property != null )
382 {
383 buf.append( property.toString() );
384 }
385 else if( leaveMissing )
386 {
387 buf.append( "${" ).append( propertyName ).append( '}' );
388 }
389
390 lastIx = ix2+1;
391 ix = content.indexOf( "${", lastIx );
392 }
393
394 if( lastIx < content.length() )
395 buf.append( content.substring( lastIx ));
396
397 return buf.toString();
398 }
399 }