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