Index: src/org/eclipse/bpel/ui/util/WSDLImportHelper.java =================================================================== --- src/org/eclipse/bpel/ui/util/WSDLImportHelper.java (revision 23819) +++ src/org/eclipse/bpel/ui/util/WSDLImportHelper.java (working copy) @@ -19,6 +19,7 @@ import org.eclipse.bpel.model.messageproperties.MessagepropertiesPackage; import org.eclipse.bpel.model.messageproperties.Property; import org.eclipse.bpel.model.messageproperties.PropertyAlias; +import org.eclipse.bpel.model.messageproperties.Query; import org.eclipse.bpel.model.messageproperties.util.MessagepropertiesConstants; import org.eclipse.bpel.model.partnerlinktype.PartnerLinkType; import org.eclipse.bpel.model.partnerlinktype.PartnerlinktypePackage; @@ -42,23 +43,22 @@ import org.eclipse.xsd.XSDSchema; import org.eclipse.xsd.XSDTypeDefinition; - /** * This class contains helpers to place the necessary and - * declarations in a WSDL Definition so that it will serialize properly. + * declarations in a WSDL Definition so that it will serialize properly. */ public class WSDLImportHelper { static final String WSDL_PREFIX_KIND = "wsdl"; //$NON-NLS-1$ - static final String XSD_PREFIX_KIND = "xsd"; //$NON-NLS-1$ - - - - public static void addAllImportsAndNamespaces(Definition definition, IResource contextObject) { + static final String XSD_PREFIX_KIND = "xsd"; //$NON-NLS-1$ + + public static void addAllImportsAndNamespaces(Definition definition, + IResource contextObject) { String TNS = definition.getTargetNamespace(); if (TNS == null) { TNS = definition.getNamespace("tns"); //$NON-NLS-1$ - if (TNS == null) throw new IllegalStateException(); + if (TNS == null) + throw new IllegalStateException(); definition.setTargetNamespace(TNS); } else { definition.addNamespace("tns", TNS); //$NON-NLS-1$ @@ -66,137 +66,170 @@ addToolingNamespaces(definition); - for (Iterator it = definition.getEExtensibilityElements().iterator(); it.hasNext(); ) { - ExtensibilityElement ee = (ExtensibilityElement)it.next(); + for (Iterator it = definition.getEExtensibilityElements().iterator(); it + .hasNext();) { + ExtensibilityElement ee = (ExtensibilityElement) it.next(); if (ee instanceof PartnerLinkType) { - // for each with a , import the file with the portType in it - for (Role role : ((PartnerLinkType)ee).getRole()) { + // for each with a , import the file with the + // portType in it + for (Role role : ((PartnerLinkType) ee).getRole()) { if (role.getPortType() != null) { - PortType pt = (PortType)role.getPortType(); + PortType pt = (PortType) role.getPortType(); if (pt != null && pt.getQName() != null) { - addImportAndNamespace(definition, pt.getEnclosingDefinition()); + addImportAndNamespace(definition, + pt.getEnclosingDefinition()); } } } } if (ee instanceof PropertyAlias) { - Message msg = (Message)((PropertyAlias)ee).getMessageType(); + Message msg = (Message) ((PropertyAlias) ee).getMessageType(); if (msg != null && msg.getQName() != null) { - addImportAndNamespace(definition, msg.getEnclosingDefinition()); - // add the namespaces of the propertyalias, message, part, type definition - // for maybe the query of the propertyalias will use the elements in the namespaces - if (((PropertyAlias) ee).getQuery() != null - && !"".equals(((PropertyAlias) ee).getQuery().getValue())) { - String query = ((PropertyAlias) ee).getQuery().getValue(); - String[] queryArr = query.split("/"); - List prefixList = new LinkedList(); - for (String qname : queryArr) { - String[] strs = qname.split(":"); - if (strs.length > 1) { - prefixList.add(strs[0]); - } - } - if (prefixList.size() > 0) { - Types types = (Types) msg.getEnclosingDefinition().getTypes(); - if (types != null && types.getSchemas() != null) { - XSDSchema xsd = null; - for (int i = 0; i < types.getSchemas().size(); i++) { - xsd = (XSDSchema) types.getSchemas().get(i); - Map map = xsd - .getQNamePrefixToNamespaceMap(); - if (map != null) { - for (Object obj : map.keySet().toArray()) { - if (prefixList.contains((String) obj)) { - definition.addNamespace((String) obj, - (String) map.get((String) obj)); - } + addImportAndNamespace(definition, + msg.getEnclosingDefinition()); + // add the namespaces of the propertyalias, message, part, + // type definition + // for maybe the query of the propertyalias will use the + // elements in the namespaces + Query q = ((PropertyAlias) ee).getQuery(); + if (q != null && q.getValue() != null && !"".equals(q.getValue())) { + String query = ((PropertyAlias) ee).getQuery().getValue(); + String[] queryArr = query.split("/"); + List prefixList = new LinkedList(); + for (String qname : queryArr) { + String[] strs = qname.split(":"); + if (strs.length > 1) { + prefixList.add(strs[0]); + } + } + if (prefixList.size() > 0) { + Types types = (Types) msg.getEnclosingDefinition() + .getTypes(); + if (types != null && types.getSchemas() != null) { + XSDSchema xsd = null; + for (int i = 0; i < types.getSchemas().size(); i++) { + xsd = (XSDSchema) types.getSchemas().get(i); + Map map = xsd + .getQNamePrefixToNamespaceMap(); + if (map != null) { + for (Object obj : map.keySet() + .toArray()) { + if (prefixList + .contains((String) obj)) { + definition + .addNamespace( + (String) obj, + (String) map + .get((String) obj)); + } - } - } - } - } - } - } + } + } + } + } + } + } } } if (ee instanceof Property) { - Object xsdType = ((Property)ee).getType(); + Object xsdType = ((Property) ee).getType(); if (xsdType instanceof XSDTypeDefinition) { - XSDTypeDefinition td = (XSDTypeDefinition)xsdType; - if (td.eResource() != null && !XSDTypeOrElementContentProvider.isBuiltInType(td)) { - addImportAndNamespace(definition, td.getSchema(), contextObject); + XSDTypeDefinition td = (XSDTypeDefinition) xsdType; + if (td.eResource() != null + && !XSDTypeOrElementContentProvider + .isBuiltInType(td)) { + addImportAndNamespace(definition, td.getSchema(), + contextObject); } else { // namespace only! - addNamespace(definition,td.getTargetNamespace(), XSD_PREFIX_KIND ); + addNamespace(definition, td.getTargetNamespace(), + XSD_PREFIX_KIND); } } else if (xsdType instanceof XSDElementDeclaration) { - XSDElementDeclaration ed = (XSDElementDeclaration)xsdType; + XSDElementDeclaration ed = (XSDElementDeclaration) xsdType; if (ed.eResource() != null) { - addImportAndNamespace(definition, ed.getSchema(), contextObject); + addImportAndNamespace(definition, ed.getSchema(), + contextObject); } else { // namespace only! - addNamespace(definition, ed.getTargetNamespace(), XSD_PREFIX_KIND); + addNamespace(definition, ed.getTargetNamespace(), + XSD_PREFIX_KIND); } } } } - + } - // TODO: is this truly necessary, or is the model doing it for us somewhere else? - // TODO: michal.chmielewski@oracle.com: The partner link namespace was somehow getting placed twice in the WSDL - // and so I have added the tooling namespace back to existence. I have no idea why at this point. + // TODO: is this truly necessary, or is the model doing it for us somewhere + // else? + // TODO: michal.chmielewski@oracle.com: The partner link namespace was + // somehow getting placed twice in the WSDL + // and so I have added the tooling namespace back to existence. I have no + // idea why at this point. protected static void addToolingNamespaces(Definition definition) { - - addNamespace(definition, PartnerlinktypeConstants.NAMESPACE, PartnerlinktypePackage.eNS_PREFIX ); - addNamespace(definition, MessagepropertiesConstants.NAMESPACE, MessagepropertiesPackage.eNS_PREFIX ); - -// if (getEnclosingDefinition().getPrefix(MessagepropertiesConstants.NAMESPACE) == null) { - // getEnclosingDefinition().addNamespace(MessagepropertiesPackage.eNS_PREFIX, MessagepropertiesConstants.NAMESPACE); + + addNamespace(definition, PartnerlinktypeConstants.NAMESPACE, + PartnerlinktypePackage.eNS_PREFIX); + addNamespace(definition, MessagepropertiesConstants.NAMESPACE, + MessagepropertiesPackage.eNS_PREFIX); + + // if + // (getEnclosingDefinition().getPrefix(MessagepropertiesConstants.NAMESPACE) + // == null) { + // getEnclosingDefinition().addNamespace(MessagepropertiesPackage.eNS_PREFIX, + // MessagepropertiesConstants.NAMESPACE); + // } + // if (definition.getNamespace(PartnerlinktypePackage.eNS_PREFIX) == + // null) { + // definition.addNamespace(PartnerlinktypePackage.eNS_PREFIX, + // PartnerlinktypePackage.eNS_URI); // } -// if (definition.getNamespace(PartnerlinktypePackage.eNS_PREFIX) == null) { -// definition.addNamespace(PartnerlinktypePackage.eNS_PREFIX, -// PartnerlinktypePackage.eNS_URI); -// } -// if (definition.getNamespace(MessagepropertiesPackage.eNS_PREFIX) == null) { -// definition.addNamespace(MessagepropertiesPackage.eNS_PREFIX, -// MessagepropertiesPackage.eNS_URI); -// } + // if (definition.getNamespace(MessagepropertiesPackage.eNS_PREFIX) == + // null) { + // definition.addNamespace(MessagepropertiesPackage.eNS_PREFIX, + // MessagepropertiesPackage.eNS_URI); + // } } - public static void addImportAndNamespace(Definition definition, XSDSchema importedSchema, - IResource contextObject) - { + public static void addImportAndNamespace(Definition definition, + XSDSchema importedSchema, IResource contextObject) { String namespace = importedSchema.getTargetNamespace(); // TODO LOGTHIS: need better error handling here! - if (namespace == null) return; - - addNamespace( definition, namespace, XSD_PREFIX_KIND ); - addImport(namespace, definition, definition.eResource().getURI(), importedSchema, - importedSchema.eResource().getURI(), contextObject); + if (namespace == null) + return; + + addNamespace(definition, namespace, XSD_PREFIX_KIND); + addImport(namespace, definition, definition.eResource().getURI(), + importedSchema, importedSchema.eResource().getURI(), + contextObject); } - public static void addImportAndNamespace(Definition definition, Definition importedDefinition) - { - if (importedDefinition == null || definition == null) return; - if (definition == importedDefinition) return; + public static void addImportAndNamespace(Definition definition, + Definition importedDefinition) { + if (importedDefinition == null || definition == null) + return; + if (definition == importedDefinition) + return; String namespace = importedDefinition.getTargetNamespace(); // TODO LOGTHIS: need better error handling here! - if (namespace == null) return; - - addNamespace(definition, namespace, WSDL_PREFIX_KIND ); - addImport(namespace, definition, definition.eResource().getURI(), importedDefinition, - importedDefinition.eResource().getURI()); + if (namespace == null) + return; + + addNamespace(definition, namespace, WSDL_PREFIX_KIND); + addImport(namespace, definition, definition.eResource().getURI(), + importedDefinition, importedDefinition.eResource().getURI()); } - - protected static void addNamespace ( Definition definition, String namespace, String pfxRoot ) { - + + protected static void addNamespace(Definition definition, String namespace, + String pfxRoot) { + String prefix = definition.getPrefix(namespace); if (prefix != null) { - return ; + return; } // Find a suitable prefix prefix = pfxRoot; @@ -205,14 +238,15 @@ if (definition.getNamespace(prefix) == null) { definition.addNamespace(prefix, namespace); break; - } + } prefix = pfxRoot + idx; idx += 1; } while (true); } - - protected static void addImport(String namespace, Definition importingDefinition, - URI importingUri, Definition importedDefinition, URI importedUri) { + + protected static void addImport(String namespace, + Definition importingDefinition, URI importingUri, + Definition importedDefinition, URI importedUri) { WSDLFactory wsdlFactory = WSDLPackage.eINSTANCE.getWSDLFactory(); List imports = importingDefinition.getImports(namespace); if (imports == null) { @@ -221,15 +255,16 @@ boolean found = false; for (int i = 0; i < imports.size() && !found; i++) { Import _import = imports.get(i); - if (_import.getEDefinition()== importedDefinition) { + if (_import.getEDefinition() == importedDefinition) { found = true; } } if (!found) { - String locationURI = createBuildPathRelativeReference(importingUri, importedUri); - + String locationURI = createBuildPathRelativeReference(importingUri, + importedUri); + if (locationURI != null && locationURI.length() != 0) { - // Create and add the import to the definition + // Create and add the import to the definition Import _import = wsdlFactory.createImport(); _import.setEDefinition(importedDefinition); _import.setLocationURI(locationURI); @@ -242,8 +277,9 @@ } } - protected static void addImport(String namespace, Definition importingDefinition, - URI importingUri, XSDSchema importedSchema, URI importedUri, IResource contextObject) { + protected static void addImport(String namespace, + Definition importingDefinition, URI importingUri, + XSDSchema importedSchema, URI importedUri, IResource contextObject) { WSDLFactory wsdlFactory = WSDLPackage.eINSTANCE.getWSDLFactory(); List imports = importingDefinition.getImports(namespace); if (imports == null) { @@ -253,26 +289,31 @@ boolean found = false; for (int i = 0; i < imports.size() && !found; i++) { Import _import = imports.get(i); - if (_import.getESchema()== importedSchema) { - found = true; continue; + if (_import.getESchema() == importedSchema) { + found = true; + continue; } } - if (found) return; - URI locationURI = importedUri.deresolve(importingUri, true, true, false); + if (found) + return; + URI locationURI = importedUri + .deresolve(importingUri, true, true, false); if ("bundleentry".equals(locationURI.scheme())) { //$NON-NLS-1$ // Don't add this import! // It's not for something in the workspace. } else { - String locationString = createBuildPathRelativeReference(importingUri, importedUri); - + String locationString = createBuildPathRelativeReference( + importingUri, importedUri); + if (locationString != null && locationString.length() != 0) { - // Create and add the import to the definition + // Create and add the import to the definition Import _import = wsdlFactory.createImport(); _import.setESchema(importedSchema); _import.setLocationURI(locationString); _import.setNamespaceURI(namespace); - //imports.add(_import); - //importingDefinition.getImports().put(importedSchema.getTargetNamespace(), imports); + // imports.add(_import); + // importingDefinition.getImports().put(importedSchema.getTargetNamespace(), + // imports); importingDefinition.addImport(_import); } else { // TODO handle errors here? @@ -281,28 +322,33 @@ } } - public static String createBuildPathRelativeReference(URI sourceURI, URI targetURI) { + public static String createBuildPathRelativeReference(URI sourceURI, + URI targetURI) { if (sourceURI == null || targetURI == null) throw new IllegalArgumentException(); - - //BaseURI source = new BaseURI(sourceURI); - //return source.getRelativeURI(targetURI); + + // BaseURI source = new BaseURI(sourceURI); + // return source.getRelativeURI(targetURI); // TODO: this is probably bogus. - String result = targetURI.deresolve(sourceURI, true, true, true).toFileString(); - // When absolute URLs + String result = targetURI.deresolve(sourceURI, true, true, true) + .toFileString(); + // When absolute URLs return (result == null ? targetURI.toString() : result); } - public static Definition getDefinition(org.eclipse.bpel.model.Import bpelImport) { - Resource baseResource = bpelImport.eResource(); - String location = bpelImport.getLocation(); - if (!baseResource.getURI().isRelative()) { - location = URI.createURI(location).resolve(baseResource.getURI()).toString(); - } - URI locationURI = URI.createURI(location); - ResourceSet resourceSet = baseResource.getResourceSet(); - Resource resource = resourceSet.getResource(locationURI, true); - return (resource instanceof WSDLResourceImpl) ? ((WSDLResourceImpl)resource).getDefinition() : null; + public static Definition getDefinition( + org.eclipse.bpel.model.Import bpelImport) { + Resource baseResource = bpelImport.eResource(); + String location = bpelImport.getLocation(); + if (!baseResource.getURI().isRelative()) { + location = URI.createURI(location).resolve(baseResource.getURI()) + .toString(); + } + URI locationURI = URI.createURI(location); + ResourceSet resourceSet = baseResource.getResourceSet(); + Resource resource = resourceSet.getResource(locationURI, true); + return (resource instanceof WSDLResourceImpl) ? ((WSDLResourceImpl) resource) + .getDefinition() : null; } - + } \ No newline at end of file