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  package com.eviware.soapui.impl.wsdl.support.xsd;
13  
14  import java.util.ArrayList;
15  import java.util.Collection;
16  import java.util.LinkedHashMap;
17  import java.util.List;
18  import java.util.Map;
19  
20  import javax.xml.namespace.QName;
21  
22  import com.eviware.soapui.SoapUI;
23  import com.eviware.soapui.config.StringListConfig;
24  import com.eviware.soapui.support.StringUtils;
25  import com.eviware.soapui.support.types.StringList;
26  
27  public class SettingUtils
28  {
29  	public static Collection<? extends QName> string2QNames( String string )
30  	{
31  		List<QName> result = new ArrayList<QName>();
32  		if( string != null && string.trim().length() > 0 )
33  		{
34  			try
35  			{
36  				StringList names = StringList.fromXml( string );
37  				for( String name : names )
38  				{
39  					QName qname = string2qname( name );
40  					result.add( qname );
41  				}
42  			}
43  			catch( Exception e )
44  			{
45  				SoapUI.logError( e );
46  			}
47  		}
48  
49  		return result;
50  	}
51  
52  	public static String qnames2String(Collection<? extends QName> qnames)
53     {
54  	   StringList names = new StringList();
55  	   for(QName qname : qnames)
56  	   {
57  	      String string = qname2string(qname);
58  	      names.add(string);
59  	   }
60  	   return names.toXml();
61     }
62  	
63  	private static QName string2qname( String name )
64  	{
65  		int ix = name.indexOf( '@' );
66  		if( ix >= 0 )
67  			return new QName( name.substring( ix + 1 ), name.substring( 0, ix ) );
68  		else
69  			return new QName( name );
70  	}
71  
72  	private static String qname2string( QName qname )
73  	{
74  		String ns = qname.getNamespaceURI();
75  		String localPart = qname.getLocalPart();
76  		if( ns != null && ns.length() > 0 )
77  			return localPart + "@" + ns;
78  		else
79  			return localPart;
80  	}
81  
82  	public static String qnameValues2String( Map<QName, String[]> valueMap )
83  	{
84  		StringListConfig config = StringListConfig.Factory.newInstance();
85  		for( QName qname : valueMap.keySet() )
86  		{
87  			String[] values = valueMap.get( qname );
88  			String nameAndValues = qname2string( qname ) + "=" + StringUtils.join( values, "," );
89  			config.addEntry( nameAndValues );
90  		}
91  		return config.toString();
92  	}
93  
94  	public static Map<QName, String[]> string2QNameValues( String string )
95  	{
96  		LinkedHashMap<QName, String[]> result = new LinkedHashMap<QName, String[]>();
97  		if( string != null && string.trim().length() > 0 )
98  		{
99  			try
100 			{
101 				StringList list = StringList.fromXml( string );
102 				for( String s : list )
103 				{
104 					String[] words = s.split( "=" );
105 					if( words.length == 2 )
106 					{
107 						String name = words[0];
108 						String[] values = words[1].split( "," );
109 						if( name.length() > 0 && values.length > 0 )
110 						{
111 							QName qname = string2qname( name );
112 							result.put( qname, values );
113 						}
114 					}
115 				}
116 			}
117 			catch( Exception e )
118 			{
119 				SoapUI.logError( e );
120 			}
121 		}
122 
123 		return result;
124 	}
125 }