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  
13  package com.eviware.soapui.model.support;
14  
15  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
16  import com.eviware.soapui.impl.wsdl.WsdlProject;
17  import com.eviware.soapui.model.ModelItem;
18  import com.eviware.soapui.model.iface.Interface;
19  import com.eviware.soapui.model.project.Project;
20  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
21  import com.eviware.soapui.support.UISupport;
22  import com.eviware.soapui.support.types.StringList;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.UUID;
27  
28  /***
29   * Utility methods for soapui model-related interfaces
30   *
31   * @author Ole.Matzura
32   */
33  
34  public class ModelSupport
35  {
36     public static <T extends ModelItem> String[] getNames( List<T> list )
37     {
38        String[] names = new String[list.size()];
39        for( int c = 0; c < names.length; c++ )
40        {
41           names[c] = list.get( c ).getName();
42        }
43  
44        return names;
45     }
46  
47     public static <T extends ModelItem> List<T> getChildren( ModelItem modelItem, Class<T> childType )
48     {
49        List<T> result = new ArrayList<T>();
50        for( ModelItem child : modelItem.getChildren() )
51        {
52           if( child.getClass().equals( childType ) )
53           {
54              result.add( (T) child );
55           }
56        }
57  
58        return result;
59     }
60  
61     public static <T extends ModelItem> String[] getNames( List<T> list, ModelItemFilter<T> filter )
62     {
63        String[] names = new String[list.size()];
64        for( int c = 0; c < names.length; c++ )
65        {
66           if( filter == null || filter.accept( list.get( c ) ) )
67              names[c] = list.get( c ).getName();
68        }
69  
70        return names;
71     }
72  
73     public static String[] getNames( String[] firstItems, List<? extends ModelItem> list )
74     {
75        String[] names = new String[list.size() + firstItems.length];
76        for( int c = 0; c < firstItems.length; c++ )
77        {
78           names[c] = firstItems[c];
79        }
80  
81        for( int c = 0; c < list.size(); c++ )
82        {
83           names[c + firstItems.length] = list.get( c ).getName();
84        }
85  
86        return names;
87     }
88  
89     public static String[] getNames( List<? extends ModelItem> list, String[] lastItems )
90     {
91        String[] names = new String[list.size() + lastItems.length];
92        for( int c = 0; c < lastItems.length; c++ )
93        {
94           names[c + list.size()] = lastItems[c];
95        }
96  
97        for( int c = 0; c < list.size(); c++ )
98        {
99           names[c] = list.get( c ).getName();
100       }
101 
102       return names;
103    }
104 
105    public static String generateModelItemID()
106    {
107       return UUID.randomUUID().toString();
108    }
109 
110    @SuppressWarnings( "unchecked" )
111    public static <T extends ModelItem> T findModelItemById( String id, ModelItem root )
112    {
113       if( root == null || id == null )
114          return null;
115 
116       for( ModelItem child : root.getChildren() )
117       {
118          if( child.getId().equals( id ) )
119             return (T) child;
120 
121          ModelItem result = findModelItemById( id, child );
122          if( result != null )
123             return (T) result;
124       }
125 
126       return null;
127    }
128 
129    public static String promptForUniqueName( String typeName, ModelItem parent, String def )
130    {
131       String name = UISupport.prompt( "Specify name for new " + typeName, "New " + typeName, def );
132       StringList names = new StringList();
133       for( ModelItem item : parent.getChildren() )
134          names.add( item.getName() );
135 
136       while( name != null && names.contains( name ) )
137       {
138          name = UISupport.prompt( "Specify unique name for new " + typeName, "New " + typeName, def );
139       }
140 
141       return name;
142    }
143 
144    public static Project getModelItemProject( ModelItem modelItem )
145    {
146       if( modelItem == null )
147          return null;
148 
149       while( !( modelItem instanceof Project ) && modelItem != null )
150       {
151          modelItem = modelItem.getParent();
152       }
153 
154       return (Project) modelItem;
155    }
156 
157    public static String getResourceRoot( AbstractWsdlModelItem<?> testStep )
158    {
159       WsdlProject project = (WsdlProject) getModelItemProject( testStep );
160       if( project == null )
161          return null;
162 
163       return PropertyExpansionUtils.expandProperties( project, project.getResourceRoot() );
164    }
165 
166    public interface ModelItemFilter<T extends ModelItem>
167    {
168       public boolean accept( T modelItem );
169    }
170 
171    public static class InterfaceTypeFilter implements ModelItemFilter<Interface>
172    {
173       private String type;
174 
175       public InterfaceTypeFilter( String type )
176       {
177          this.type = type;
178       }
179 
180       public boolean accept( Interface modelItem )
181       {
182          return modelItem.getInterfaceType().equals( type );
183       }
184 
185    }
186 }