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