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;
14  
15  import com.eviware.soapui.model.ModelItem;
16  import com.eviware.soapui.support.StringUtils;
17  import com.eviware.soapui.support.types.StringToObjectMap;
18  
19  public class DefaultPropertyExpansionContext extends StringToObjectMap implements PropertyExpansionContext
20  {
21  	private ModelItem modelItem;
22  
23  	public DefaultPropertyExpansionContext( ModelItem modelItem )
24  	{
25  		this.modelItem = modelItem;
26  	}
27  
28  	public String expand( String content )
29  	{
30  		return PropertyExpander.expandProperties( this, content );
31  	}
32  
33  	public ModelItem getModelItem()
34  	{
35  		return modelItem;
36  	}
37  
38  	public Object getProperty( String name )
39  	{
40  		return super.get( name );
41  	}
42  
43  	public String[] getPropertyNames()
44  	{
45  		return keySet().toArray( new String[size()] );
46  	}
47  
48  	@Override
49  	public Object get( Object key )
50  	{
51  		Object result = super.get( key );
52  
53  		if( result == null )
54  		{
55  			result = expand( ( String )key );
56  			if( key.equals( result ) )
57  			{
58  				result = expand( "${" + key + "}" );
59  				if( StringUtils.isNullOrEmpty( ( String )result ) )
60  					result = null;
61  			}
62  		}
63  
64  		return result;
65  	}
66  
67  	public boolean hasProperty( String name )
68  	{
69  		return containsKey( name );
70  	}
71  
72  	public Object removeProperty( String name )
73  	{
74  		return remove( name );
75  	}
76  
77  	public void setProperty( String name, Object value )
78  	{
79  		put( name, value );
80  	}
81  
82  	public void setProperties( PropertyExpansionContext context )
83  	{
84  		for( String name : context.getPropertyNames() )
85  		{
86  			setProperty( name, context.getProperty( name ) );
87  		}
88  	}
89  
90  	public StringToObjectMap getProperties()
91  	{
92  		return this;
93  	}
94  }