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,
141 			final boolean notify)
142 	{
143 		String source = expand();
144 		if (StringUtils.hasContent(source))
145 		{
146 			try
147 			{
148 				new URL(source);
149 			}
150 			catch (Exception e)
151 			{
152 				File file = new File(source);
153 				if (!file.exists())
154 				{
155 					if (context.hasThisModelItem(modelItem, errorDescription, source))
156 						return;
157 					context.addPathToResolve(modelItem, errorDescription, source, new ResolveContext.FileResolver(
158 							"Select File", extension, fileType, file.getParent())
159 					{
160 
161 						@Override
162 						public boolean apply(File newFile)
163 						{
164 							set(newFile.getAbsolutePath(), notify);
165 							return true;
166 						}
167 					});
168 				}
169 				else
170 				{
171 					if (context.hasThisModelItem(modelItem, errorDescription, source))
172 						context.getPath(modelItem, errorDescription, source).setSolved(true);
173 				}
174 			}
175 		}
176 	}
177 
178 	public void resolveFolder(ResolveContext context, String errorDescription, final boolean notify)
179 	{
180 		String source = expand();
181 		if (StringUtils.hasContent(source))
182 		{
183 			try
184 			{
185 				new URL(source);
186 			}
187 			catch (Exception e)
188 			{
189 				File file = new File(source);
190 				if (!file.exists() || !file.isDirectory())
191 				{
192 					if (context.hasThisModelItem(modelItem, errorDescription, source))
193 						return;
194 					context.addPathToResolve(modelItem, errorDescription, source, new ResolveContext.DirectoryResolver(
195 							"Select Directory", source)
196 					{
197 						@Override
198 						public boolean apply(File newFile)
199 						{
200 							set(newFile.getAbsolutePath(), notify);
201 							return true;
202 						}
203 					});
204 				}
205 				else
206 				{
207 					if (context.hasThisModelItem(modelItem, errorDescription, source))
208 						context.getPath(modelItem, errorDescription, source).setSolved(true);
209 				}
210 			}
211 		}
212 	}
213 }