View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.support.types;
14  
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import com.eviware.soapui.SoapUI;
19  import com.eviware.soapui.config.StringToStringMapConfig;
20  import com.eviware.soapui.config.StringToStringMapConfig.Entry;
21  
22  /***
23   * HashMap<String,String>
24   * 
25   * @author Ole.Matzura
26   */
27  
28  public class StringToStringMap extends HashMap<String,String>
29  {
30  	private boolean equalsOnThis;
31  
32  	public StringToStringMap()
33  	{
34  		super();
35  	}
36  
37  	public StringToStringMap(int initialCapacity, float loadFactor)
38  	{
39  		super(initialCapacity, loadFactor);
40  	}
41  
42  	public StringToStringMap(int initialCapacity)
43  	{
44  		super(initialCapacity);
45  	}
46  
47  	public StringToStringMap(Map<? extends String, ? extends String> m)
48  	{
49  		super(m);
50  	}
51  	
52  	public String get( String key, String defaultValue )
53  	{
54  		String value = get( key );
55  		return value == null ? defaultValue : value;
56  	}
57  
58  	public String toXml()
59  	{
60  		StringToStringMapConfig xmlConfig = StringToStringMapConfig.Factory.newInstance();
61  		
62  		for( String key : keySet() )
63  		{
64  			Entry entry = xmlConfig.addNewEntry();
65  			entry.setKey( key );
66  			entry.setValue( get( key ));
67  		}
68  		
69  		return xmlConfig.toString();
70  	}
71  
72  	public static StringToStringMap fromXml(String value)
73  	{
74  		StringToStringMap result = new StringToStringMap();
75  		if( value == null || value.trim().length() == 0 )
76  			return result;
77  		
78  		try
79  		{
80  			StringToStringMapConfig nsMapping = StringToStringMapConfig.Factory.parse(value);
81  			
82  			for (Entry entry : nsMapping.getEntryList())
83  			{
84  				result.put(entry.getKey(), entry.getValue());
85  			}
86  		}
87  		catch (Exception e)
88  		{
89  			SoapUI.logError( e );
90  		}		
91  		
92  		return result;
93  	}
94  
95  	public final boolean getBoolean(String key)
96  	{
97  		return Boolean.parseBoolean( get( key ));
98  	}
99  
100 	public boolean hasValue(String key)
101 	{
102 		return containsKey( key ) && get( key ).length() > 0;
103 	}
104 
105 	public void putIfMissing(String key, String value)
106 	{
107 		if( !containsKey( key )) 
108 			put( key, value );
109 	}
110 
111 	public void put(String key, boolean value)
112 	{
113 		put( key, Boolean.toString( value ));
114 	}
115 
116 	public static StringToStringMap fromHttpHeader( String value )
117 	{
118 		StringToStringMap result = new StringToStringMap();
119 		
120 		int ix = value.indexOf( ';' );
121 		while( ix > 0 )
122 		{
123 			extractNVPair( value.substring( 0, ix ), result );
124 			value = value.substring( ix+1 );
125 			ix = value.indexOf( ';' );
126 		}
127 		
128 		if( value.length() > 2 )
129 		{
130 			extractNVPair( value, result );
131 		}
132 		
133 		return result;
134 	}
135 
136 	private static void extractNVPair( String value, StringToStringMap result )
137 	{
138 		int ix;
139 		ix = value.indexOf( '=' );
140 		if( ix != -1 )
141 		{
142 			String str = value.substring( ix+1 ).trim();
143 			if( str.startsWith( "\"" ) &&  str.endsWith( "\"" ))
144 				str = str.substring( 1, str.length()-1 );
145 				
146 			result.put( value.substring( 0, ix ).trim(), str );
147 		}
148 	}
149 	
150 	public void setEqualsOnThis( boolean equalsOnThis )
151 	{
152 		this.equalsOnThis = equalsOnThis;
153 	}
154 
155 	@Override
156 	public boolean equals( Object o )
157 	{
158 		return equalsOnThis ? this == o : super.equals( o );
159 	}
160 }