View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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     private static QName string2qname(String name)
53     {
54        int ix = name.indexOf( '@' );
55        if(ix >= 0)
56           return new QName( name.substring( ix+1 ), name.substring( 0, ix ));
57        else
58           return new QName( name );
59     }
60     
61     private static String qname2string(QName qname)
62     {
63        String ns = qname.getNamespaceURI();
64        String localPart = qname.getLocalPart();
65        if(ns != null && ns.length() > 0)
66           return localPart + "@" + ns;
67        else
68           return localPart;
69     }
70  
71     public static String qnameValues2String(Map<QName, String[]> valueMap)
72     {
73        StringListConfig config = StringListConfig.Factory.newInstance();
74        for(QName qname : valueMap.keySet())
75        {
76           String[] values = valueMap.get(qname);
77           String nameAndValues = qname2string(qname) + "=" + StringUtils.join(values, ",");
78           config.addEntry(nameAndValues);
79        }
80        return config.toString();
81     }
82     
83     public static Map<QName, String[]> string2QNameValues(String string)
84     {
85        LinkedHashMap<QName,String[]> result = new LinkedHashMap<QName,String[]>();
86        if( string != null && string.trim().length() > 0 )
87        {
88           try
89           {
90              StringList list = StringList.fromXml( string );
91              for( String s : list )
92              {
93                 String[] words = s.split("=");
94                 if(words.length == 2)
95                 {
96                    String name = words[0];
97                    String[] values = words[1].split(",");
98                    if(name.length() > 0 && values.length > 0)
99                    {
100                      QName qname = string2qname(name);
101                      result.put(qname, values);
102                   }
103                }
104             }
105          }
106          catch( Exception e )
107          {
108             SoapUI.logError( e );
109          }
110       }
111       
112       return result;
113    }
114 }