001: package org.andromda.cartridges.webservice;
002:
003: import java.util.Collection;
004: import java.util.LinkedHashSet;
005:
006: import org.andromda.cartridges.webservice.metafacades.WSDLEnumerationType;
007: import org.andromda.cartridges.webservice.metafacades.WSDLType;
008: import org.andromda.metafacades.uml.ClassifierFacade;
009: import org.andromda.metafacades.uml.Service;
010: import org.andromda.metafacades.uml.TypeMappings;
011: import org.apache.commons.collections.Closure;
012: import org.apache.commons.collections.CollectionUtils;
013: import org.apache.commons.lang.StringUtils;
014:
015: /**
016: * Contains utilities used within the WebService cartridge.
017: *
018: * @author Chad Brandon
019: */
020: public class WebServiceUtils {
021: /**
022: * Retrieves all roles from the given <code>services</code> collection.
023: *
024: * @param services the collection services.
025: * @return all roles from the collection.
026: */
027: public Collection getAllRoles(Collection services) {
028: final Collection allRoles = new LinkedHashSet();
029: CollectionUtils.forAllDo(services, new Closure() {
030: public void execute(Object object) {
031: if (object != null
032: && Service.class.isAssignableFrom(object
033: .getClass())) {
034: allRoles.addAll(((Service) object).getAllRoles());
035: }
036: }
037: });
038: return allRoles;
039: }
040:
041: /**
042: * Reverses the <code>packageName</code>.
043: *
044: * @param packageName the package name to reverse.
045: * @return the reversed package name.
046: */
047: public static String reversePackage(String packageName) {
048: return StringUtils.reverseDelimited(packageName,
049: WebServiceGlobals.NAMESPACE_DELIMITER);
050: }
051:
052: /**
053: * <p/> Creates and returns the schema type for the given <code>type</code>.
054: * It finds the mapped schema type from the passed in
055: * <code>schemaTypeMappings</code>.
056: * </p>
057: *
058: * @param type the ClassifierFacade instance
059: * @param schemaTypeMappings contains the mappings from model datatypes to
060: * schema datatypes.
061: * @param namespacePrefix the prefix given to the schema type if it's a
062: * custom type (non XSD type).
063: * @param qName the qualifed name
064: * @param wrappedArrayTypePrefix a prefix to give to wrapped array types.
065: * @param withPrefix a flag indicating whether or not the type should have
066: * the prefix defined
067: * @param preserveArray true/false, if true then if the schema type is an
068: * array we'll preserve the fact that its an array and return an
069: * array schema type name. If false we will return back the non array
070: * type even if its an array.
071: * @return the schema type name.
072: */
073: public static java.lang.String getSchemaType(ClassifierFacade type,
074: TypeMappings schemaTypeMappings, String namespacePrefix,
075: String qName, String wrappedArrayTypePrefix,
076: boolean withPrefix, boolean preserveArray) {
077: StringBuffer schemaType = new StringBuffer();
078: String modelName = type.getFullyQualifiedName(true);
079: if (schemaTypeMappings != null) {
080: namespacePrefix = namespacePrefix + ':';
081: String mappedValue = schemaTypeMappings.getTo(modelName);
082: if (!mappedValue.equals(modelName)) {
083: schemaType.append(mappedValue);
084: } else {
085: if (withPrefix) {
086: schemaType.append(namespacePrefix);
087: }
088: if (type.isArrayType()) {
089: ClassifierFacade nonArray = type.getNonArray();
090: if (nonArray != null) {
091: if (nonArray instanceof WSDLType) {
092: schemaType.append(((WSDLType) nonArray)
093: .getQName());
094: } else if (nonArray instanceof WSDLEnumerationType) {
095: schemaType
096: .append(((WSDLEnumerationType) nonArray)
097: .getQName());
098: }
099: }
100: } else {
101: schemaType.append(qName);
102: }
103: }
104: // remove any array '[]' suffix
105: schemaType = new StringBuffer(schemaType.toString()
106: .replaceAll("\\[\\]", ""));
107: if (preserveArray && type.isArrayType()) {
108: int insertIndex = namespacePrefix.length();
109: if (!schemaType.toString().startsWith(namespacePrefix)) {
110: if (withPrefix) {
111: // add the prefix for any normal XSD types
112: // that may not have been set above
113: schemaType.insert(0, namespacePrefix);
114: } else {
115: // since we aren't adding the prefix, set
116: // the correct insert index
117: insertIndex = 0;
118: }
119: }
120: schemaType.insert(insertIndex, wrappedArrayTypePrefix);
121: }
122: if (withPrefix
123: && !schemaType.toString().startsWith(
124: namespacePrefix)) {
125: schemaType.insert(0,
126: WebServiceGlobals.XSD_NAMESPACE_PREFIX);
127: }
128: }
129: return schemaType.toString();
130: }
131: }
|