PageRenderTime 26ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/eclipse-wtp-webservices-R3.4.0/org.eclipse.wst.wsdl.ui/src-asd-wsdl11/org/eclipse/wst/wsdl/ui/internal/util/WSDLSetComponentHelper.java

#
Java | 252 lines | 188 code | 39 blank | 25 comment | 37 complexity | 90481206a9eddef693180e7998b26ede MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2001, 2009 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.wst.wsdl.ui.internal.util;
  12. import java.net.URL;
  13. import java.util.ArrayList;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import java.util.Map;
  17. import org.eclipse.core.resources.IFile;
  18. import org.eclipse.core.runtime.FileLocator;
  19. import org.eclipse.core.runtime.Path;
  20. import org.eclipse.wst.common.ui.internal.search.dialogs.ComponentSpecification;
  21. import org.eclipse.wst.common.uriresolver.internal.util.URIHelper;
  22. import org.eclipse.wst.wsdl.Definition;
  23. import org.eclipse.wst.wsdl.Part;
  24. import org.eclipse.wst.wsdl.WSDLElement;
  25. import org.eclipse.wst.wsdl.XSDSchemaExtensibilityElement;
  26. import org.eclipse.wst.wsdl.internal.impl.ImportImpl;
  27. import org.eclipse.wst.wsdl.ui.internal.actions.AddElementDeclarationAction;
  28. import org.eclipse.wst.wsdl.ui.internal.actions.AddWSISchemaImportAction;
  29. import org.eclipse.wst.wsdl.ui.internal.commands.AddImportCommand;
  30. import org.eclipse.wst.wsdl.util.WSDLConstants;
  31. import org.eclipse.xsd.XSDImport;
  32. import org.eclipse.xsd.XSDSchema;
  33. import org.eclipse.xsd.XSDSchemaContent;
  34. public class WSDLSetComponentHelper {
  35. private Definition definition;
  36. private IFile currentIFile;
  37. public WSDLSetComponentHelper(IFile iFile, Definition definition) {
  38. currentIFile = iFile;
  39. this.definition = definition;
  40. }
  41. public void setWSDLComponent(WSDLElement inputElement, String property, ComponentSpecification spec) {
  42. addImportIfNecessary(spec);
  43. String componentObject = getPrefixedComponentName(spec);
  44. org.w3c.dom.Element wsdlElement = inputElement.getElement();
  45. wsdlElement.setAttribute(property, componentObject); //$NON-NLS-1$
  46. // Remove any unused imports, counter to addImportIfNecessary
  47. WSDLImportManager.removeUnusedImports(definition);
  48. }
  49. public void setXSDTypeComponent(Part part, ComponentSpecification spec) {
  50. if (spec.getName() != null && spec.getQualifier() != null) {
  51. addImportIfNecessary(spec);
  52. }
  53. String componentObject = getPrefixedComponentName(spec);
  54. ComponentReferenceUtil.setComponentReference((Part) part, true, componentObject);
  55. // Remove any unused imports, counter to addImportIfNecessary
  56. WSDLImportManager.removeUnusedImports(definition);
  57. }
  58. public void setXSDElementComponent(Part part, ComponentSpecification spec) {
  59. addImportIfNecessary(spec);
  60. String componentObject = getPrefixedComponentName(spec);
  61. ComponentReferenceUtil.setComponentReference((Part) part, false, componentObject);
  62. // Remove any unused imports, counter to addImportIfNecessary
  63. WSDLImportManager.removeUnusedImports(definition);
  64. }
  65. /*
  66. * Return the prefixed component name described by the given
  67. * ComponentSpecification object.
  68. */
  69. public String getPrefixedComponentName(ComponentSpecification spec) {
  70. String name = (String) spec.getName();
  71. List prefixes = getPrefixes(definition, spec.getQualifier());
  72. if (prefixes.size() > 0) {
  73. name = prefixes.get(0) + ":" + name; //$NON-NLS-1$
  74. }
  75. return name;
  76. }
  77. private List getPrefixes(Definition definition, String namespace) {
  78. List list = new ArrayList();
  79. Map map = definition.getNamespaces();
  80. for (Iterator i = map.keySet().iterator(); i.hasNext();) {
  81. String prefix = (String) i.next();
  82. String theNamespace = (String) map.get(prefix);
  83. if (theNamespace != null && theNamespace.equals(namespace)) {
  84. list.add(prefix);
  85. }
  86. }
  87. return list;
  88. }
  89. private void addImportIfNecessary(ComponentSpecification spec) {
  90. boolean foundMatch = false;
  91. // Check itself
  92. Path currentFileLocation = new Path(currentIFile.getLocation().toString());
  93. if (spec.getFile() == null || currentFileLocation.equals(spec.getFile().getLocation())) {
  94. // if the ComponentSpecification's getFile() returns null, forget about adding necessary imports
  95. foundMatch = true;
  96. }
  97. // Check regular Imports
  98. if (!foundMatch) {
  99. Iterator importsIt = definition.getEImports().iterator();
  100. while (importsIt.hasNext()) {
  101. String importLocation = ""; //$NON-NLS-1$
  102. ImportImpl importItem = (ImportImpl) importsIt.next();
  103. importItem.importDefinitionOrSchema();
  104. if (importItem.getESchema() != null) {
  105. XSDSchema schema = importItem.getESchema();
  106. importLocation = getNormalizedLocation(schema.getSchemaLocation());
  107. }
  108. else {
  109. Definition importDefinition = importItem.getEDefinition();
  110. importLocation = getNormalizedLocation(importDefinition.getLocation());
  111. }
  112. if (spec.getFile().getLocation().equals(new Path(importLocation))) {
  113. foundMatch = true;
  114. break;
  115. }
  116. }
  117. }
  118. // Check inline Schemas
  119. if (!foundMatch) {
  120. List imports = new ArrayList();
  121. if (definition.getETypes() != null) {
  122. Iterator it = definition.getETypes().getEExtensibilityElements().iterator();
  123. while (it.hasNext()) {
  124. XSDSchemaExtensibilityElement eeElement = (XSDSchemaExtensibilityElement) it.next();
  125. XSDSchema schema = eeElement.getSchema();
  126. if (schema.getTargetNamespace() == null || schema.getTargetNamespace().equals("")) { //$NON-NLS-1$
  127. Iterator contents = schema.getContents().iterator();
  128. while (contents.hasNext()) {
  129. XSDSchemaContent content = (XSDSchemaContent) contents.next();
  130. if (content instanceof XSDImport) {
  131. imports.add(content);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. Iterator importIt = imports.iterator();
  138. while (importIt.hasNext()) {
  139. XSDImport importItem = (XSDImport) importIt.next();
  140. XSDSchema resolvedSchema = importItem.getResolvedSchema();
  141. String resolvedString = resolvedSchema.getSchemaLocation();
  142. String importLocation = getNormalizedLocation(resolvedString);
  143. if (spec.getFile().getLocation().equals(new Path(importLocation))) {
  144. foundMatch = true;
  145. break;
  146. }
  147. }
  148. }
  149. if (!foundMatch) {
  150. boolean wsiStyleImport = isXSDSchemaFile(spec);
  151. if (wsiStyleImport) {
  152. AddElementDeclarationAction action = new AddElementDeclarationAction(definition, spec.getQualifier(), "xsd"); //$NON-NLS-1$
  153. action.run();
  154. String location = URIHelper.getRelativeURI(spec.getFile().getLocation(), currentIFile.getLocation());
  155. AddWSISchemaImportAction addImport = new AddWSISchemaImportAction(definition, spec.getQualifier(), location);
  156. addImport.run();
  157. }
  158. else {
  159. String newSelectedFileLoc = spec.getFile().getLocation().toOSString();
  160. String currentFileLoc = getNormalizedLocation(definition.getLocation());
  161. String relativeLoc = ComponentReferenceUtil.computeRelativeURI(newSelectedFileLoc, currentFileLoc, true);
  162. String prefix = definition.getPrefix(WSDLConstants.WSDL_NAMESPACE_URI);
  163. String uniquePrefix = getUniquePrefix(definition, prefix);
  164. String namespace = spec.getQualifier();
  165. AddImportCommand command = new AddImportCommand(definition, namespace, relativeLoc);
  166. command.run();
  167. definition.addNamespace(uniquePrefix, namespace);
  168. }
  169. }
  170. }
  171. /*
  172. * Try to determine if the passed in ComponentSpecification refers to
  173. * an XSD or WSDL file. If it's an XSD, return true.
  174. */
  175. private boolean isXSDSchemaFile(ComponentSpecification spec) {
  176. String fileLocation = spec.getFile().getLocation().toOSString();
  177. int periodIndex = fileLocation.lastIndexOf("."); //$NON-NLS-1$
  178. if (periodIndex > 0) {
  179. String extension = fileLocation.substring(periodIndex + 1);
  180. if (extension.equalsIgnoreCase("xsd")) { //$NON-NLS-1$
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. private String getUniquePrefix(Definition definition, String initPrefix) {
  187. String uniquePrefix;
  188. Map map = definition.getNamespaces();
  189. if (definition.getNamespace(initPrefix) == null) {
  190. uniquePrefix = initPrefix;
  191. }
  192. else {// if used, then try to create a unique one
  193. String tempPrefix = initPrefix;
  194. int i = 1;
  195. while(map.containsKey(tempPrefix + i)) {
  196. i++;
  197. }
  198. uniquePrefix = tempPrefix + i;
  199. }
  200. return uniquePrefix;
  201. }
  202. private String getNormalizedLocation(String location) {
  203. try {
  204. URL url = new URL(location);
  205. URL resolvedURL = FileLocator.resolve(url);
  206. location = resolvedURL.getPath();
  207. }
  208. catch (Exception e) {
  209. e.printStackTrace();
  210. }
  211. return location;
  212. }
  213. }