1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.support;
14
15 import com.eviware.soapui.SoapUI;
16 import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
17 import com.eviware.soapui.impl.wsdl.WsdlProject;
18 import com.eviware.soapui.model.ModelItem;
19 import com.eviware.soapui.model.project.Project;
20 import com.eviware.soapui.model.propertyexpansion.DefaultPropertyExpansionContext;
21 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
22 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
23 import com.eviware.soapui.model.support.ModelSupport;
24 import com.eviware.soapui.support.StringUtils;
25 import com.eviware.soapui.support.Tools;
26 import com.eviware.soapui.support.UISupport;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
31 import java.net.URL;
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 ?
57 PropertyExpansionUtils.expandProperties(modelItem, path) :
58 PropertyExpansionUtils.expandProperties(context, path);
59
60 if( !isRelativePath(path))
61 return path;
62
63 String root = getExpandedResourceRoot(modelItem, context);
64 if( StringUtils.isNullOrEmpty( root ) || StringUtils.isNullOrEmpty( root ))
65 return path;
66
67 if( isHttpPath(root))
68 root += "/";
69 else
70 root += File.separatorChar;
71
72 return Tools.joinRelativeUrl(root, path);
73 }
74
75 public static String adjustRelativePath(String str, String root, ModelItem contextModelItem )
76 {
77 if( StringUtils.isNullOrEmpty( root ) || StringUtils.isNullOrEmpty( str ))
78 return str;
79
80 if( !isRelativePath( str ))
81 return str;
82
83 root = PropertyExpansionUtils.expandProperties(contextModelItem, root);
84
85 if( isHttpPath(root))
86 root += "/";
87 else
88 root += File.separatorChar;
89
90 return Tools.joinRelativeUrl(root, str);
91
92
93
94
95
96 }
97
98 public static boolean isHttpPath(String str)
99 {
100 if( StringUtils.isNullOrEmpty( str ))
101 return false;
102
103 str = str.toLowerCase();
104
105 return str.startsWith("http:/") || str.startsWith("https:/" );
106 }
107
108 public static boolean isRelativePath(String str)
109 {
110 if( StringUtils.isNullOrEmpty( str ))
111 return false;
112
113 str = str.toLowerCase();
114
115 return !str.startsWith("/") && !str.startsWith("//") && !str.startsWith("http:/") && !str.startsWith("https:/" ) &&
116 str.indexOf("://" ) != 1 && !str.startsWith("file:" ) && 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 = PropertyExpansionUtils.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 = PropertyExpansionUtils.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 = PropertyExpansionUtils.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 while( rootPath != null )
253 {
254 if( path.startsWith( rootPath ))
255 {
256 path = path.substring( rootPath.length() );
257 if( path.startsWith( File.separator ))
258 path = path.substring( 1 );
259
260 break;
261 }
262 else
263 {
264 File file = new File( rootPath );
265 rootPath = file.getParent();
266 prefix += ".." + File.separatorChar;
267 }
268 }
269
270 return prefix + path;
271 }
272 }
273
274 public static boolean isAbsolutePath( String path )
275 {
276 return !isRelativePath( path );
277 }
278
279 public static boolean isFilePath( String path )
280 {
281 if( StringUtils.isNullOrEmpty(path))
282 return false;
283
284 return !isHttpPath(path);
285 }
286
287 public static String normalizePath(String path)
288 {
289 if( StringUtils.isNullOrEmpty(path))
290 return path;
291
292 return File.separatorChar == '/' ? path : path.replace(File.separatorChar, '/');
293 }
294
295 public static String denormalizePath(String path)
296 {
297 if( StringUtils.isNullOrEmpty(path))
298 return path;
299
300 if( isHttpPath(path))
301 return path;
302
303 return File.separatorChar == '/' ? path.replace( '//', File.separatorChar ) : path.replace('/', File.separatorChar);
304 }
305
306 public static String getExpandedResourceRoot( ModelItem modelItem )
307 {
308 return getExpandedResourceRoot(modelItem, null );
309 }
310
311 public static String getExpandedResourceRoot( ModelItem modelItem, PropertyExpansionContext context)
312 {
313 if( !(modelItem instanceof AbstractWsdlModelItem<?>) )
314 return null;
315
316 WsdlProject project = (WsdlProject) ModelSupport.getModelItemProject(modelItem);
317 if( project == null )
318 return null;
319
320 String docroot = project.getResourceRoot();
321 if( !StringUtils.hasContent(docroot))
322 return new File( "").getAbsolutePath();
323
324 docroot = context == null ?
325 PropertyExpansionUtils.expandProperties(modelItem, docroot) :
326 PropertyExpansionUtils.expandProperties(context, docroot);
327
328 return docroot;
329 }
330
331 public static String ensureFilePathIsUrl( String url )
332 {
333 if( isFilePath( url ) && !url.startsWith( "file:" ))
334 {
335 try
336 {
337 return new File( url ).toURI().toURL().toString();
338 }
339 catch( MalformedURLException e )
340 {
341 e.printStackTrace();
342 }
343 }
344
345 return url;
346 }
347 }