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 StringToStringMap()
31  	{
32  		super();
33  	}
34  
35  	public StringToStringMap(int initialCapacity, float loadFactor)
36  	{
37  		super(initialCapacity, loadFactor);
38  	}
39  
40  	public StringToStringMap(int initialCapacity)
41  	{
42  		super(initialCapacity);
43  	}
44  
45  	public StringToStringMap(Map<? extends String, ? extends String> m)
46  	{
47  		super(m);
48  	}
49  	
50  	public String get( String key, String defaultValue )
51  	{
52  		String value = get( key );
53  		return value == null ? defaultValue : value;
54  	}
55  
56  	public String toXml()
57  	{
58  		StringToStringMapConfig xmlConfig = StringToStringMapConfig.Factory.newInstance();
59  		
60  		for( String key : keySet() )
61  		{
62  			Entry entry = xmlConfig.addNewEntry();
63  			entry.setKey( key );
64  			entry.setValue( get( key ));
65  		}
66  		
67  		return xmlConfig.toString();
68  	}
69  
70  	public static StringToStringMap fromXml(String value)
71  	{
72  		StringToStringMap result = new StringToStringMap();
73  		if( value == null || value.trim().length() == 0 )
74  			return result;
75  		
76  		try
77  		{
78  			StringToStringMapConfig nsMapping = StringToStringMapConfig.Factory.parse(value);
79  			
80  			for (Entry entry : nsMapping.getEntryList())
81  			{
82  				result.put(entry.getKey(), entry.getValue());
83  			}
84  		}
85  		catch (Exception e)
86  		{
87  			SoapUI.logError( e );
88  		}		
89  		
90  		return result;
91  	}
92  
93  	public final boolean getBoolean(String key)
94  	{
95  		return Boolean.parseBoolean( get( key ));
96  	}
97  
98  	public boolean hasValue(String key)
99  	{
100 		return containsKey( key ) && get( key ).length() > 0;
101 	}
102 
103 	public void putIfMissing(String key, String value)
104 	{
105 		if( !containsKey( key )) 
106 			put( key, value );
107 	}
108 
109 	public void put(String key, boolean value)
110 	{
111 		put( key, Boolean.toString( value ));
112 	}
113 
114 	public static StringToStringMap fromHttpHeader( String value )
115 	{
116 		StringToStringMap result = new StringToStringMap();
117 		
118 		int ix = value.indexOf( ';' );
119 		while( ix > 0 )
120 		{
121 			extractNVPair( value.substring( 0, ix ), result );
122 			value = value.substring( ix+1 );
123 			ix = value.indexOf( ';' );
124 		}
125 		
126 		if( value.length() > 2 )
127 		{
128 			extractNVPair( value, result );
129 		}
130 		
131 		return result;
132 	}
133 
134 	private static void extractNVPair( String value, StringToStringMap result )
135 	{
136 		int ix;
137 		ix = value.indexOf( '=' );
138 		if( ix != -1 )
139 		{
140 			String str = value.substring( ix+1 ).trim();
141 			if( str.startsWith( "\"" ) &&  str.endsWith( "\"" ))
142 				str = str.substring( 1, str.length()-1 );
143 				
144 			result.put( value.substring( 0, ix ).trim(), str );
145 		}
146 	}
147 }