View Javadoc

1   package org.andromda.cartridges.webservice;
2   
3   import java.util.Collection;
4   import java.util.LinkedHashSet;
5   
6   import org.andromda.cartridges.webservice.metafacades.WSDLEnumerationType;
7   import org.andromda.cartridges.webservice.metafacades.WSDLType;
8   import org.andromda.metafacades.uml.ClassifierFacade;
9   import org.andromda.metafacades.uml.Service;
10  import org.andromda.metafacades.uml.TypeMappings;
11  import org.apache.commons.collections.Closure;
12  import org.apache.commons.collections.CollectionUtils;
13  import org.apache.commons.lang.StringUtils;
14  
15  /**
16   * Contains utilities used within the WebService cartridge.
17   *
18   * @author Chad Brandon
19   */
20  public class WebServiceUtils
21  {
22      /**
23       * Retrieves all roles from the given <code>services</code> collection.
24       *
25       * @param services the collection services.
26       * @return all roles from the collection.
27       */
28      public Collection getAllRoles(Collection services)
29      {
30          final Collection allRoles = new LinkedHashSet();
31          CollectionUtils.forAllDo(services, new Closure()
32          {
33              public void execute(Object object)
34              {
35                  if (object != null && Service.class.isAssignableFrom(object.getClass()))
36                  {
37                      allRoles.addAll(((Service)object).getAllRoles());
38                  }
39              }
40          });
41          return allRoles;
42      }
43  
44      /**
45       * Reverses the <code>packageName</code>.
46       *
47       * @param packageName the package name to reverse.
48       * @return the reversed package name.
49       */
50      public static String reversePackage(String packageName)
51      {
52          return StringUtils.reverseDelimited(packageName, WebServiceGlobals.NAMESPACE_DELIMITER);
53      }
54  
55      /**
56       * <p> Creates and returns the schema type for the given <code>type</code>.
57       * It finds the mapped schema type from the passed in
58       * <code>schemaTypeMappings</code>.
59       * </p>
60       * 
61       * @param type the ClassifierFacade instance
62       * @param schemaTypeMappings contains the mappings from model datatypes to
63       *        schema datatypes.
64       * @param namespacePrefix the prefix given to the schema type if it's a
65       *        custom type (non XSD type).
66       * @param qName the qualifed name
67       * @param wrappedArrayTypePrefix a prefix to give to wrapped array types.
68       * @param withPrefix a flag indicating whether or not the type should have
69       *        the prefix defined
70       * @param preserveArray true/false, if true then if the schema type is an
71       *        array we'll preserve the fact that its an array and return an
72       *        array schema type name. If false we will return back the non array
73       *        type even if its an array.
74       * @return the schema type name.
75       */
76      public static java.lang.String getSchemaType(
77          ClassifierFacade type,
78          TypeMappings schemaTypeMappings,
79          String namespacePrefix,
80          String qName,
81          String wrappedArrayTypePrefix,
82          boolean withPrefix,
83          boolean preserveArray)
84      {
85          StringBuffer schemaType = new StringBuffer();
86          String modelName = type.getFullyQualifiedName(true);
87          if (schemaTypeMappings != null)
88          {
89              namespacePrefix = namespacePrefix + ':';
90              String mappedValue = schemaTypeMappings.getTo(modelName);
91              if (!mappedValue.equals(modelName))
92              {
93                  schemaType.append(mappedValue);
94              }
95              else
96              {
97                  if (withPrefix)
98                  {
99                      schemaType.append(namespacePrefix);
100                 }
101                 if (type.isArrayType())
102                 {
103                     ClassifierFacade nonArray = type.getNonArray();
104                     if (nonArray != null)
105                     {
106                         if (nonArray instanceof WSDLType)
107                         {
108                             schemaType.append(((WSDLType)nonArray).getQName());
109                         }
110                         else if (nonArray instanceof WSDLEnumerationType)
111                         {
112                             schemaType.append(((WSDLEnumerationType)nonArray).getQName());
113                         }
114                     }
115                 }
116                 else
117                 {
118                     schemaType.append(qName);
119                 }
120             }
121             // remove any array '[]' suffix
122             schemaType = new StringBuffer(schemaType.toString().replaceAll("\\[\\]", ""));
123             if (preserveArray && type.isArrayType())
124             {
125                 int insertIndex = namespacePrefix.length();
126                 if (!schemaType.toString().startsWith(namespacePrefix))
127                 {
128                     if (withPrefix)
129                     {
130                         // add the prefix for any normal XSD types
131                         // that may not have been set above
132                         schemaType.insert(0, namespacePrefix);                     
133                     }
134                     else
135                     {
136                         // since we aren't adding the prefix, set
137                         // the correct insert index
138                         insertIndex = 0;
139                     }
140                 }
141                 schemaType.insert(insertIndex, wrappedArrayTypePrefix);
142             }
143             if (withPrefix && !schemaType.toString().startsWith(namespacePrefix))
144             {
145                 schemaType.insert(0, WebServiceGlobals.XSD_NAMESPACE_PREFIX);
146             }
147         }
148         return schemaType.toString();
149     }
150 }