View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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  	/***
59  	 * Get the inverse of this map.
60  	 */
61  	public StringToStringMap inverse()
62  	{
63  		StringToStringMap inverse = new StringToStringMap();
64  		for( String key : keySet() )
65  		{
66  			String value = get( key );
67  			inverse.put( value, key );
68  		}
69  		return inverse;
70  	}
71  
72  	public String toXml()
73  	{
74  		StringToStringMapConfig xmlConfig = StringToStringMapConfig.Factory.newInstance();
75  
76  		for( String key : keySet() )
77  		{
78  			Entry entry = xmlConfig.addNewEntry();
79  			entry.setKey( key );
80  			entry.setValue( get( key ) );
81  		}
82  
83  		return xmlConfig.toString();
84  	}
85  
86  	public static StringToStringMap fromXml( String value )
87  	{
88  		if( value == null || value.trim().length() == 0 )
89  			return new StringToStringMap();
90  
91  		try
92  		{
93  			StringToStringMapConfig nsMapping = StringToStringMapConfig.Factory.parse( value );
94  
95  			return fromXml( nsMapping );
96  		}
97  		catch( Exception e )
98  		{
99  			SoapUI.logError( e );
100 		}
101 
102 		return new StringToStringMap();
103 	}
104 
105 	public static StringToStringMap fromXml( StringToStringMapConfig nsMapping )
106 	{
107 		StringToStringMap result = new StringToStringMap();
108 		for( Entry entry : nsMapping.getEntryList() )
109 		{
110 			result.put( entry.getKey(), entry.getValue() );
111 		}
112 		return result;
113 	}
114 
115 	public final boolean getBoolean( String key )
116 	{
117 		return Boolean.parseBoolean( get( key ) );
118 	}
119 
120 	public boolean hasValue( String key )
121 	{
122 		return containsKey( key ) && get( key ).length() > 0;
123 	}
124 
125 	public void putIfMissing( String key, String value )
126 	{
127 		if( !containsKey( key ) )
128 			put( key, value );
129 	}
130 
131 	public void put( String key, boolean value )
132 	{
133 		put( key, Boolean.toString( value ) );
134 	}
135 
136 	public static StringToStringMap fromHttpHeader( String value )
137 	{
138 		StringToStringMap result = new StringToStringMap();
139 
140 		int ix = value.indexOf( ';' );
141 		while( ix > 0 )
142 		{
143 			extractNVPair( value.substring( 0, ix ), result );
144 			value = value.substring( ix + 1 );
145 			ix = value.indexOf( ';' );
146 		}
147 
148 		if( value.length() > 2 )
149 		{
150 			extractNVPair( value, result );
151 		}
152 
153 		return result;
154 	}
155 
156 	private static void extractNVPair( String value, StringToStringMap result )
157 	{
158 		int ix;
159 		ix = value.indexOf( '=' );
160 		if( ix != -1 )
161 		{
162 			String str = value.substring( ix + 1 ).trim();
163 			if( str.startsWith( "\"" ) && str.endsWith( "\"" ) )
164 				str = str.substring( 1, str.length() - 1 );
165 
166 			result.put( value.substring( 0, ix ).trim(), str );
167 		}
168 	}
169 
170 	public void setEqualsOnThis( boolean equalsOnThis )
171 	{
172 		this.equalsOnThis = equalsOnThis;
173 	}
174 
175 	@Override
176 	public boolean equals( Object o )
177 	{
178 		return equalsOnThis ? this == o : super.equals( o );
179 	}
180 
181 	public int getInt( String key, int def )
182 	{
183 		try
184 		{
185 			return Integer.parseInt( get( key ) );
186 		}
187 		catch( Exception e )
188 		{
189 			return def;
190 		}
191 	}
192 
193 	public String[] getKeys()
194 	{
195 		return keySet().toArray( new String[size()] );
196 	}
197 }