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.teststeps;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
17  import com.eviware.soapui.impl.wsdl.support.PathUtils;
18  import com.eviware.soapui.model.testsuite.TestRunContext;
19  import com.eviware.soapui.support.StringUtils;
20  import com.eviware.soapui.support.resolver.ResolveContext;
21  
22  import java.io.File;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  
26  public abstract class AbstractPathPropertySupport 
27  {
28  	private final String propertyName;
29  	private final AbstractWsdlModelItem<?> modelItem;
30  
31  	public AbstractPathPropertySupport(AbstractWsdlModelItem<?> modelItem, String propertyName)
32  	{
33  		this.modelItem = modelItem;
34  		this.propertyName = propertyName;
35  	}
36  
37  	public String set(String value, boolean notify)
38  	{
39  		String old = get();
40  		value = PathUtils.relativizeResourcePath( value, modelItem );
41  		try
42  		{
43  			setPropertyValue(PathUtils.normalizePath( value ));
44  			if( notify )
45  				notifyUpdate(value, old);
46  		} 
47  		catch (Exception e)
48  		{
49  			e.printStackTrace();
50  		}
51  		
52  		return old;
53  	}
54  	
55  	public String get()
56  	{
57  		try
58  		{
59  			return getPropertyValue();
60  		}
61  		catch (Exception e)
62  		{
63  			e.printStackTrace();
64  			return null;
65  		}
66  	}
67  	
68  	public String getPropertyName()
69  	{
70  		return propertyName;
71  	}
72  
73  	public AbstractWsdlModelItem<?> getModelItem()
74  	{
75  		return modelItem;
76  	}
77  
78  	public abstract void setPropertyValue(String value) throws Exception;
79  
80  	protected void notifyUpdate(String value, String old)
81  	{
82  		modelItem.notifyPropertyChanged( modelItem.getClass().getName() + "@" + propertyName, old, value );
83  	}
84  
85  	public String expand(TestRunContext context)
86  	{
87  		try
88  		{
89  			return PathUtils.expandPath( getPropertyValue(), modelItem, context );
90  		} 
91  		catch (Exception e)
92  		{
93  			e.printStackTrace();
94  			return null;
95  		}
96  	}
97  	
98  	public String expand()
99  	{
100 		try
101 		{
102 			return PathUtils.resolveResourcePath( getPropertyValue(), modelItem );
103 		} 
104 		catch (Exception e)
105 		{
106 			e.printStackTrace();
107 			return null;
108 		}
109 	}
110 	
111 	public String expandUrl()
112 	{
113 		String result = expand();
114 		try
115 		{
116 			if( PathUtils.isFilePath(result) && !result.startsWith( "file:" ))
117          {
118             result = new File( result ).toURI().toURL().toString();
119          }
120          else
121          {
122             result = new URL( result ).toString();
123          }
124       }
125 		catch (MalformedURLException e)
126 		{
127 			SoapUI.logError(e);
128 		}
129 		
130 		return result;
131 	}
132 
133 	public abstract String getPropertyValue() throws Exception;
134 
135 	public void resolveFile( ResolveContext context, String errorDescription)
136 	{
137 		resolveFile(context, errorDescription, null, null, true);
138 	}
139 	
140 	public void resolveFile(ResolveContext context, String errorDescription, String extension, String fileType, final boolean notify  )
141 	{
142 		String source = expand();
143 		if( StringUtils.hasContent(source) )
144 		{
145 			try
146 			{
147 				new URL( source );
148 			}
149 			catch( Exception e )
150 			{
151 				File file = new File( source );
152 				if( !file.exists())
153 				{
154 					context.addPathToResolve( modelItem, errorDescription, source, new ResolveContext.FileResolver( "Select File", 
155 							extension, fileType, file.getParent()) {
156 						
157 						@Override
158 						public boolean apply(File newFile)
159 						{
160 							set(newFile.getAbsolutePath(), notify);
161 							return true;
162 						}
163 					} );
164 				}
165 			}
166 		}
167 	}
168 	
169 	public void resolveFolder(ResolveContext context, String errorDescription, final boolean notify  )
170 	{
171 		String source = expand();
172 		if( StringUtils.hasContent(source) )
173 		{
174 			try
175 			{
176 				new URL( source );
177 			}
178 			catch( Exception e )
179 			{
180 				File file = new File( source );
181 				if( !file.exists() || !file.isDirectory())
182 				{
183 					context.addPathToResolve( modelItem, errorDescription, source, new ResolveContext.DirectoryResolver( "Select Directory", source )
184 					{
185 						@Override
186 						public boolean apply(File newFile)
187 						{
188 							set(newFile.getAbsolutePath(), notify);
189 							return true;
190 						}
191 					} );
192 				}
193 			}
194 		}
195 	}
196 }