View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.model.propertyexpansion.resolvers.providers;
14  
15  import java.io.File;
16  import java.net.MalformedURLException;
17  import java.net.URL;
18  
19  import com.eviware.soapui.model.project.Project;
20  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
21  import com.eviware.soapui.model.propertyexpansion.resolvers.DynamicPropertyResolver.ValueProvider;
22  import com.eviware.soapui.model.support.ModelSupport;
23  
24  public class ProjectDirProvider implements ValueProvider
25  {
26  	public String getValue( PropertyExpansionContext context )
27  	{
28  		Project project = ModelSupport.getModelItemProject( context.getModelItem() );
29  		if( project != null )
30  		{
31  			return getProjectFolder( project );
32  		}
33  
34  		return null;
35  	}
36  
37  	public static String getProjectFolder( Project project )
38  	{
39  		if( project.getPath() != null )
40  		{
41  			File file = new File( project.getPath() );
42  			if( file.exists() )
43  			{
44  				return new File( file.getAbsolutePath() ).getParent();
45  			}
46  			else
47  			{
48  				try
49  				{
50  					URL url = new URL( project.getPath() );
51  					String str = url.getProtocol() + "://" + url.getHost()
52  							+ ( ( url.getPort() != -1 ? ":" + url.getPort() : "" ) ) + url.getPath();
53  					int ix = str.lastIndexOf( '/' );
54  					if( ix != -1 )
55  						return str.substring( 0, ix );
56  				}
57  				catch( MalformedURLException e )
58  				{
59  				}
60  			}
61  		}
62  
63  		return null;
64  	}
65  }