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     /***
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  		StringToStringMap result = new StringToStringMap();
89  		if( value == null || value.trim().length() == 0 )
90  			return result;
91  		
92  		try
93  		{
94  			StringToStringMapConfig nsMapping = StringToStringMapConfig.Factory.parse(value);
95  			
96  			for (Entry entry : nsMapping.getEntryList())
97  			{
98  				result.put(entry.getKey(), entry.getValue());
99  			}
100 		}
101 		catch (Exception e)
102 		{
103 			SoapUI.logError( e );
104 		}		
105 		
106 		return result;
107 	}
108 
109 	public final boolean getBoolean(String key)
110 	{
111 		return Boolean.parseBoolean( get( key ));
112 	}
113 
114 	public boolean hasValue(String key)
115 	{
116 		return containsKey( key ) && get( key ).length() > 0;
117 	}
118 
119 	public void putIfMissing(String key, String value)
120 	{
121 		if( !containsKey( key )) 
122 			put( key, value );
123 	}
124 
125 	public void put(String key, boolean value)
126 	{
127 		put( key, Boolean.toString( value ));
128 	}
129 
130 	public static StringToStringMap fromHttpHeader( String value )
131 	{
132 		StringToStringMap result = new StringToStringMap();
133 		
134 		int ix = value.indexOf( ';' );
135 		while( ix > 0 )
136 		{
137 			extractNVPair( value.substring( 0, ix ), result );
138 			value = value.substring( ix+1 );
139 			ix = value.indexOf( ';' );
140 		}
141 		
142 		if( value.length() > 2 )
143 		{
144 			extractNVPair( value, result );
145 		}
146 		
147 		return result;
148 	}
149 
150 	private static void extractNVPair( String value, StringToStringMap result )
151 	{
152 		int ix;
153 		ix = value.indexOf( '=' );
154 		if( ix != -1 )
155 		{
156 			String str = value.substring( ix+1 ).trim();
157 			if( str.startsWith( "\"" ) &&  str.endsWith( "\"" ))
158 				str = str.substring( 1, str.length()-1 );
159 				
160 			result.put( value.substring( 0, ix ).trim(), str );
161 		}
162 	}
163 	
164 	public void setEqualsOnThis( boolean equalsOnThis )
165 	{
166 		this.equalsOnThis = equalsOnThis;
167 	}
168 
169 	@Override
170 	public boolean equals( Object o )
171 	{
172 		return equalsOnThis ? this == o : super.equals( o );
173 	}
174 
175 	public int getInt( String key, int def )
176 	{
177 		try
178 		{
179 			return Integer.parseInt( get( key ) );
180 		}
181 		catch( Exception e )
182 		{
183 			return def;
184 		}		
185 	}
186 }