View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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.URL;
31  
32  public class PathUtils
33  {
34  	public static String getAbsoluteFolder( String path )
35  	{
36  		File folder = new File( path );
37  		
38  		if( !folder.exists() )
39  			return null;
40  		
41  		if( folder.isDirectory())
42  			return folder.getAbsolutePath();
43  		
44  		File parentFile = folder.getParentFile();
45  		return parentFile == null ? null : parentFile.getAbsolutePath();
46  	}
47  
48  	public static String expandPath( String path, AbstractWsdlModelItem<?> modelItem )
49  	{
50  		return expandPath( path, modelItem, null );
51  	}
52  	
53  	public static String expandPath( String path, AbstractWsdlModelItem<?> modelItem, PropertyExpansionContext context )
54  	{
55  		path = context == null ? 
56  				PropertyExpansionUtils.expandProperties(modelItem, path) : 
57  				PropertyExpansionUtils.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 = PropertyExpansionUtils.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  //		if( isHttpPath( str ))
92  //			return root + '/' + str;
93  //		else
94  //			return root + File.separatorChar + str;		
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:/") && !str.startsWith("https:/" ) &&
115 			 str.indexOf("://" ) != 1 && !str.startsWith("file:" ) &&  str.indexOf(":/" ) != 1;
116 	}
117 
118 	public static String createRelativePath(String path, String root,	ModelItem contextModelItem)
119 	{
120 		if( StringUtils.isNullOrEmpty( root ))
121 			return path;
122 		
123 		root = PropertyExpansionUtils.expandProperties(contextModelItem, root);
124 		
125 		return relativize(path, root);
126 	}
127 
128 	public static String relativizeResourcePath( String path, ModelItem modelItem )
129 	{
130 		if( modelItem == null || StringUtils.isNullOrEmpty(path) || isRelativePath(path) || isHttpPath(path))
131 			return path;
132 		
133 		Project project = ModelSupport.getModelItemProject( modelItem );
134 		if( project == null )
135 			return path;
136 
137 		if( StringUtils.isNullOrEmpty(project.getPath()) && project.getResourceRoot().indexOf("${projectDir}") >= 0 )
138 		{
139 			if( UISupport.confirm("Save project before setting path?", "Project has not been saved" ))
140 			{
141 				try
142 				{
143 					project.save();
144 				}
145 				catch (IOException e)
146 				{
147 					SoapUI.logError( e );
148 					UISupport.showErrorMessage(e);
149 					return path;
150 				}
151 			}
152 		}
153 		
154 		String projectPath = PropertyExpansionUtils.expandProperties(project,project.getResourceRoot());
155 		if( StringUtils.isNullOrEmpty(projectPath))
156 			return path;
157 		
158 		return PathUtils.relativize( path, projectPath );
159 	}
160 
161 	public static String resolveResourcePath( String path, ModelItem modelItem )
162 	{
163 		if( path == null || modelItem == null )
164 			return path;
165 		
166 		path = PathUtils.denormalizePath( path );
167 		path = PropertyExpansionUtils.expandProperties( new DefaultPropertyExpansionContext(modelItem), path );
168 		
169 		String prefix = "";
170 		
171 		if( path.startsWith( "file:" ))
172 		{
173 			prefix = path.substring( 0, 5 );
174 			path = path.substring( 5 );
175 		}
176 		
177 		if( PathUtils.isAbsolutePath( path ))
178 			return prefix + path;
179 		
180 		WsdlProject project = (WsdlProject) ModelSupport.getModelItemProject( modelItem );
181 		if( project == null )
182 			return prefix + path;
183 		
184 		String resourceRoot = getExpandedResourceRoot(modelItem);
185 		
186 		if( StringUtils.hasContent(resourceRoot) && !resourceRoot.endsWith( File.separator ))
187 			resourceRoot += File.separator;
188 		
189 		String result = Tools.joinRelativeUrl( resourceRoot, path );
190 		return prefix + result;
191 	}
192 
193 	public static String relativize( String path, String rootPath )
194 	{
195 		if( StringUtils.isNullOrEmpty(path) || StringUtils.isNullOrEmpty(rootPath))
196 			return path;
197 		
198 		if( path.toLowerCase().startsWith( "http:/" ) || path.toLowerCase().startsWith( "https:/" ))
199 		{
200 			String prefix = "";
201 			
202 			while( rootPath != null )
203 			{
204 				if( path.startsWith( rootPath ))
205 				{
206 					path = path.substring( rootPath.length() );
207 					if( path.startsWith( "/" ))
208 						path = path.substring( 1 );
209 					
210 					break;
211 				}
212 				else
213 				{
214 					int ix = rootPath.lastIndexOf( '/' );
215 					rootPath = ix == -1 ? null : rootPath.substring( 0, ix );
216 					prefix += "../";
217 				}
218 			}
219 			
220 			return prefix + path;
221 		}
222 		else
223 		{
224 			String prefix = "";
225 			
226 			// file url?
227 			if( path.toLowerCase().startsWith( "file:" ))
228 			{
229 				try
230 				{
231 					path = new File( new URL( path ).toURI() ).getAbsolutePath();
232 				} 
233 				catch (Exception e)
234 				{
235 					e.printStackTrace();
236 				}
237 			}
238 			
239 			if( rootPath.startsWith( "file:" ))
240 			{
241 				try
242 				{
243 					rootPath = new File( new URL( rootPath ).toURI() ).getAbsolutePath();
244 				} 
245 				catch (Exception e)
246 				{
247 					e.printStackTrace();
248 				}
249 			}
250 			
251 			while( rootPath != null )
252 			{
253 				if( path.startsWith( rootPath ))
254 				{
255 					path = path.substring( rootPath.length() );
256 					if( path.startsWith( File.separator ))
257 						path = path.substring( 1 );
258 					
259 					break;
260 				}
261 				else
262 				{
263 					File file = new File( rootPath );
264 					rootPath = file.getParent();
265 					prefix += ".." + File.separatorChar;
266 				}
267 			}
268 			
269 			return prefix + path;
270 		}
271 	}
272 
273 	public static boolean isAbsolutePath( String path )
274 	{
275 		return !isRelativePath( path );
276 	}
277 
278 	public static boolean isFilePath( String path )
279 	{
280 		if( StringUtils.isNullOrEmpty(path))
281 			return false;
282 		
283 		return !isHttpPath(path);
284 	}
285 
286 	public static String normalizePath(String path)
287 	{
288 		if( StringUtils.isNullOrEmpty(path))
289 			return path;
290 		
291 		return File.separatorChar == '/' ? path : path.replace(File.separatorChar, '/');
292 	}
293 	
294 	public static String denormalizePath(String path)
295 	{
296 		if( StringUtils.isNullOrEmpty(path))
297 			return path;
298 		
299 		if( isHttpPath(path))
300 			return path;
301 		
302 		return File.separatorChar == '/' ? path.replace( '//', File.separatorChar ) : path.replace('/', File.separatorChar);
303 	}
304 
305 	public static String getExpandedResourceRoot( ModelItem modelItem )
306 	{
307 		return getExpandedResourceRoot(modelItem, null );
308 	}
309 	
310 	public static String getExpandedResourceRoot( ModelItem modelItem, PropertyExpansionContext context)
311 	{
312 		if( !(modelItem instanceof AbstractWsdlModelItem<?>) )
313 			return null;
314 		
315 		WsdlProject project = (WsdlProject) ModelSupport.getModelItemProject(modelItem);
316 		if( project == null )
317 			return null;
318 		
319 		String docroot = project.getResourceRoot();
320 		if( !StringUtils.hasContent(docroot))
321 			return new File( "").getAbsolutePath();
322 
323 		docroot = context == null ? 
324 				PropertyExpansionUtils.expandProperties(modelItem, docroot) : 
325 				PropertyExpansionUtils.expandProperties(context, docroot);
326       
327 		return docroot;
328 	}
329 }