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