1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.support;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.net.MalformedURLException;
18 import java.net.URL;
19
20 import com.eviware.soapui.SoapUI;
21 import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
22 import com.eviware.soapui.impl.wsdl.WsdlProject;
23 import com.eviware.soapui.model.ModelItem;
24 import com.eviware.soapui.model.project.Project;
25 import com.eviware.soapui.model.propertyexpansion.DefaultPropertyExpansionContext;
26 import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
27 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
28 import com.eviware.soapui.model.support.ModelSupport;
29 import com.eviware.soapui.support.StringUtils;
30 import com.eviware.soapui.support.Tools;
31 import com.eviware.soapui.support.UISupport;
32
33 public class PathUtils
34 {
35 public static String getAbsoluteFolder( String path )
36 {
37 File folder = new File( path );
38
39 if( !folder.exists() )
40 return null;
41
42 if( folder.isDirectory() )
43 return folder.getAbsolutePath();
44
45 File parentFile = folder.getParentFile();
46 return parentFile == null ? null : parentFile.getAbsolutePath();
47 }
48
49 public static String expandPath( String path, AbstractWsdlModelItem<?> modelItem )
50 {
51 return expandPath( path, modelItem, null );
52 }
53
54 public static String expandPath( String path, AbstractWsdlModelItem<?> modelItem, PropertyExpansionContext context )
55 {
56
57 path = stripQuotes( path );
58 if( isHttpPath( path ) )
59 path = path.replaceAll( " ", "%20" );
60
61 path = context == null ? PropertyExpander.expandProperties( modelItem, path ) : PropertyExpander
62 .expandProperties( context, path );
63
64 if( !isRelativePath( path ) )
65 return path;
66
67 String root = getExpandedResourceRoot( modelItem, context );
68 if( StringUtils.isNullOrEmpty( root ) || StringUtils.isNullOrEmpty( root ) )
69 return path;
70
71 if( isHttpPath( root ) )
72 root += "/";
73 else
74 root += File.separatorChar;
75
76 return Tools.joinRelativeUrl( root, path );
77 }
78
79 private static String stripQuotes( String path )
80 {
81 if( path != null )
82 {
83 if( path.startsWith( "\"" ) & path.endsWith( "\"" ) )
84 {
85 path = path.substring( 1, path.length() - 1 );
86 }
87 }
88 return path;
89 }
90
91 public static String adjustRelativePath( String str, String root, ModelItem contextModelItem )
92 {
93 if( StringUtils.isNullOrEmpty( root ) || StringUtils.isNullOrEmpty( str ) )
94 return str;
95
96 if( !isRelativePath( str ) )
97 return str;
98
99 root = PropertyExpander.expandProperties( contextModelItem, root );
100
101 if( isHttpPath( root ) )
102 root += "/";
103 else
104 root += File.separatorChar;
105
106 return Tools.joinRelativeUrl( root, str );
107
108
109
110
111
112 }
113
114 public static boolean isHttpPath( String str )
115 {
116 if( StringUtils.isNullOrEmpty( str ) )
117 return false;
118
119 str = str.toLowerCase();
120
121 return str.startsWith( "http:/" ) || str.startsWith( "https:/" );
122 }
123
124 public static boolean isRelativePath( String str )
125 {
126 if( StringUtils.isNullOrEmpty( str ) )
127 return false;
128
129 str = str.toLowerCase();
130
131 return !str.startsWith( "/" ) && !str.startsWith( "//" ) && !str.startsWith( "http:/" )
132 && !str.startsWith( "https:/" ) && str.indexOf( "://" ) != 1 && !str.startsWith( "file:" )
133 && str.indexOf( ":/" ) != 1;
134 }
135
136 public static String createRelativePath( String path, String root, ModelItem contextModelItem )
137 {
138 if( StringUtils.isNullOrEmpty( root ) )
139 return path;
140
141 root = PropertyExpander.expandProperties( contextModelItem, root );
142
143 return relativize( path, root );
144 }
145
146 public static String relativizeResourcePath( String path, ModelItem modelItem )
147 {
148 if( modelItem == null || StringUtils.isNullOrEmpty( path ) || isRelativePath( path ) || isHttpPath( path ) )
149 return path;
150
151 Project project = ModelSupport.getModelItemProject( modelItem );
152 if( project == null )
153 return path;
154
155 if( StringUtils.isNullOrEmpty( project.getPath() ) && project.getResourceRoot().indexOf( "${projectDir}" ) >= 0 )
156 {
157 if( UISupport.confirm( "Save project before setting path?", "Project has not been saved" ) )
158 {
159 try
160 {
161 project.save();
162 }
163 catch( IOException e )
164 {
165 SoapUI.logError( e );
166 UISupport.showErrorMessage( e );
167 return path;
168 }
169 }
170 }
171
172 String projectPath = PropertyExpander.expandProperties( project, project.getResourceRoot() );
173 if( StringUtils.isNullOrEmpty( projectPath ) )
174 return path;
175
176 return PathUtils.relativize( path, projectPath );
177 }
178
179 public static String resolveResourcePath( String path, ModelItem modelItem )
180 {
181 if( path == null || modelItem == null )
182 return path;
183
184 path = PathUtils.denormalizePath( path );
185 path = PropertyExpander.expandProperties( new DefaultPropertyExpansionContext( modelItem ), path );
186
187 String prefix = "";
188
189 if( path.startsWith( "file:" ) )
190 {
191 prefix = path.substring( 0, 5 );
192 path = path.substring( 5 );
193 }
194
195 if( PathUtils.isAbsolutePath( path ) )
196 return prefix + path;
197
198 WsdlProject project = ( WsdlProject )ModelSupport.getModelItemProject( modelItem );
199 if( project == null )
200 return prefix + path;
201
202 String resourceRoot = getExpandedResourceRoot( modelItem );
203
204 if( StringUtils.hasContent( resourceRoot ) && !resourceRoot.endsWith( File.separator ) )
205 resourceRoot += File.separator;
206
207 String result = Tools.joinRelativeUrl( resourceRoot, path );
208 return prefix + result;
209 }
210
211 public static String relativize( String path, String rootPath )
212 {
213 if( StringUtils.isNullOrEmpty( path ) || StringUtils.isNullOrEmpty( rootPath ) )
214 return path;
215
216 if( path.toLowerCase().startsWith( "http:/" ) || path.toLowerCase().startsWith( "https:/" ) )
217 {
218 String prefix = "";
219
220 while( rootPath != null )
221 {
222 if( path.startsWith( rootPath ) )
223 {
224 path = path.substring( rootPath.length() );
225 if( path.startsWith( "/" ) )
226 path = path.substring( 1 );
227
228 break;
229 }
230 else
231 {
232 int ix = rootPath.lastIndexOf( '/' );
233 rootPath = ix == -1 ? null : rootPath.substring( 0, ix );
234 prefix += "../";
235 }
236 }
237
238 return prefix + path;
239 }
240 else
241 {
242 String prefix = "";
243
244
245 if( path.toLowerCase().startsWith( "file:" ) )
246 {
247 try
248 {
249 path = new File( new URL( path ).toURI() ).getAbsolutePath();
250 }
251 catch( Exception e )
252 {
253 e.printStackTrace();
254 }
255 }
256
257 if( rootPath.startsWith( "file:" ) )
258 {
259 try
260 {
261 rootPath = new File( new URL( rootPath ).toURI() ).getAbsolutePath();
262 }
263 catch( Exception e )
264 {
265 e.printStackTrace();
266 }
267 }
268
269
270 if( rootPath.toUpperCase().charAt( 0 ) != path.toUpperCase().charAt( 0 ) && ((rootPath.indexOf( "://" ) == 1 || rootPath.indexOf( ":/" ) == 1 )
271 && ( path.indexOf( "://" ) == 1 || path.indexOf( ":/") == 1 )))
272 {
273 return path;
274 }
275
276 while( rootPath != null )
277 {
278 if( path.startsWith( rootPath ) )
279 {
280 path = path.substring( rootPath.length() );
281 if( path.startsWith( File.separator ) )
282 path = path.substring( 1 );
283
284 break;
285 }
286 else
287 {
288 File file = new File( rootPath );
289 rootPath = file.getParent();
290 prefix += ".." + File.separatorChar;
291 }
292 }
293
294 return prefix + path;
295 }
296 }
297
298 public static boolean isAbsolutePath( String path )
299 {
300 return !isRelativePath( path );
301 }
302
303 public static boolean isFilePath( String path )
304 {
305 if( StringUtils.isNullOrEmpty( path ) )
306 return false;
307
308 return !isHttpPath( path );
309 }
310
311 public static String normalizePath( String path )
312 {
313 if( StringUtils.isNullOrEmpty( path ) )
314 return path;
315
316 return File.separatorChar == '/' ? path : path.replace( File.separatorChar, '/' );
317 }
318
319 public static String denormalizePath( String path )
320 {
321 if( StringUtils.isNullOrEmpty( path ) )
322 return path;
323
324 if( isHttpPath( path ) )
325 return path;
326
327 return File.separatorChar == '/' ? path.replace( '//', File.separatorChar ) : path.replace( '/',
328 File.separatorChar );
329 }
330
331 public static String getExpandedResourceRoot( ModelItem modelItem )
332 {
333 return getExpandedResourceRoot( modelItem, null );
334 }
335
336 public static String getExpandedResourceRoot( ModelItem modelItem, PropertyExpansionContext context )
337 {
338 if( !( modelItem instanceof AbstractWsdlModelItem<?> ) )
339 return null;
340
341 WsdlProject project = ( WsdlProject )ModelSupport.getModelItemProject( modelItem );
342 if( project == null )
343 return null;
344
345 String docroot = project.getResourceRoot();
346 if( !StringUtils.hasContent( docroot ) )
347 return new File( "" ).getAbsolutePath();
348
349 docroot = context == null ? PropertyExpander.expandProperties( modelItem, docroot ) : PropertyExpander
350 .expandProperties( context, docroot );
351
352 return docroot;
353 }
354
355 public static String ensureFilePathIsUrl( String url )
356 {
357 if( isFilePath( url ) && !url.startsWith( "file:" ) )
358 {
359 try
360 {
361 return new File( url ).toURI().toURL().toString();
362 }
363 catch( MalformedURLException e )
364 {
365 e.printStackTrace();
366 }
367 }
368
369 return url;
370 }
371 }