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

# · Java · 594 lines · 425 code · 87 blank · 82 comment · 95 complexity · d0b0c384d9afbae17bbf0a0a8b58d77b 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.util.ArrayList;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. import org.eclipse.emf.ecore.EObject;
  16. import org.eclipse.wst.wsdl.Binding;
  17. import org.eclipse.wst.wsdl.Definition;
  18. import org.eclipse.wst.wsdl.Fault;
  19. import org.eclipse.wst.wsdl.Input;
  20. import org.eclipse.wst.wsdl.Message;
  21. import org.eclipse.wst.wsdl.MessageReference;
  22. import org.eclipse.wst.wsdl.Operation;
  23. import org.eclipse.wst.wsdl.Output;
  24. import org.eclipse.wst.wsdl.Part;
  25. import org.eclipse.wst.wsdl.Port;
  26. import org.eclipse.wst.wsdl.PortType;
  27. import org.eclipse.wst.wsdl.Service;
  28. import org.eclipse.xsd.XSDComplexTypeDefinition;
  29. import org.eclipse.xsd.XSDElementDeclaration;
  30. import org.eclipse.xsd.XSDModelGroup;
  31. import org.eclipse.xsd.XSDParticle;
  32. import org.eclipse.xsd.XSDSchema;
  33. public class NameUtil
  34. {
  35. /**
  36. * Return a name which is not used by any other fault in the operation.
  37. * @return String
  38. */
  39. public static String buildUniqueFaultName(Operation operation)
  40. {
  41. return buildUniqueFaultName(operation, "NewFault"); //$NON-NLS-1$
  42. }
  43. public static String buildUniqueFaultName(Operation operation, String baseName) {
  44. if (baseName == null)
  45. baseName = "NewFault"; //$NON-NLS-1$
  46. List names = getUsedFaultNames(operation);
  47. // Now search the list until we find an unused name
  48. return getUniqueNameHelper(baseName, names);
  49. }
  50. /**
  51. * Return a name which is not used by any other input in the portType. Returned name will be of the form:
  52. * <operationName> + <ending> [+ unique Integer]
  53. * @return String
  54. */
  55. public static String buildUniqueInputName(PortType portType, String operationName, String ending)
  56. {
  57. String name = null;
  58. String candidate = operationName + ending;
  59. int i = 0;
  60. // loop until we find a unique name (the name will consist of the operationName + ending + an integer)
  61. while (name == null)
  62. {
  63. boolean unique = true;
  64. // determine if this combination is unique within the current porttype
  65. for (Iterator it = portType.getEOperations().iterator(); it.hasNext() && unique;)
  66. {
  67. Operation current = (Operation) it.next();
  68. // TODO : port check
  69. // old if(current.isSetEInput() && current.getEInput().isSetName()) {
  70. if (current.getEInput() != null && current.getEInput().getName() != null)
  71. {
  72. if (current.getEInput().getName().equals(candidate))
  73. unique = false;
  74. }
  75. }
  76. if (unique)
  77. name = candidate;
  78. else
  79. candidate = operationName + ending + i;
  80. i++;
  81. }
  82. return name;
  83. }
  84. /**
  85. * Return a name which is not used by any other message in the definition.
  86. * @return String
  87. */
  88. public static String buildUniqueMessageName(Definition definition, String baseName)
  89. {
  90. if (baseName == null)
  91. {
  92. baseName = "NewMessage"; //$NON-NLS-1$
  93. }
  94. List names = getUsedMessageNames(definition);
  95. // Now search the list until we find an unused name
  96. return getUniqueNameHelper(baseName, names);
  97. }
  98. /**
  99. * Return a name which is not used by any other operation in the port type.
  100. * @return String
  101. */
  102. public static String buildUniqueOperationName(PortType portType)
  103. {
  104. return buildUniqueOperationName(portType, "NewOperation"); //$NON-NLS-1$
  105. }
  106. public static String buildUniqueOperationName(PortType portType, String baseName)
  107. {
  108. if (baseName == null) {
  109. baseName = "NewOperation"; //$NON-NLS-1$
  110. }
  111. List names = getUsedOperationNames(portType);
  112. // Now search the list until we find an unused name
  113. return getUniqueNameHelper(baseName, names);
  114. }
  115. /**
  116. * Return a name which is not used by any other output in the portType. Returned name will be of the form:
  117. * <operationName> + <ending> [+ unique Integer]
  118. * @return String
  119. */
  120. public static String buildUniqueOutputName(PortType portType, String operationName, String ending)
  121. {
  122. String name = null;
  123. String candidate = operationName + ending;
  124. int i = 0;
  125. // loop until we find a unique name (the name will consist of the operationName + ending + an integer)
  126. while (name == null)
  127. {
  128. boolean unique = true;
  129. // determine if this combination is unique within the current porttype
  130. for (Iterator it = portType.getEOperations().iterator(); it.hasNext() && unique;)
  131. {
  132. Operation current = (Operation) it.next();
  133. // TODO: port check
  134. // old if(current.isSetEOutput() && current.getEOutput().isSetName()) {
  135. if (current.getEOutput() != null && current.getEOutput().getName() != null)
  136. {
  137. if (current.getEOutput().getName().equals(candidate))
  138. unique = false;
  139. }
  140. }
  141. if (unique)
  142. name = candidate;
  143. else
  144. candidate = operationName + ending + i;
  145. i++;
  146. }
  147. return name;
  148. }
  149. /**
  150. * Return a name which is not used by any other part in the message.
  151. * @return String
  152. */
  153. public static String buildUniquePartName(Message message)
  154. {
  155. List names = getUsedPartNames(message);
  156. // Now search the list until we find an unused name
  157. return getUniqueNameHelper("NewPart", names); //$NON-NLS-1$
  158. }
  159. public static String buildUniquePartName(Message message, String baseName)
  160. {
  161. if (baseName == null)
  162. {
  163. baseName = "NewPart"; //$NON-NLS-1$
  164. }
  165. List names = getUsedPartNames(message);
  166. // Now search the list until we find an unused name
  167. return getUniqueNameHelper(baseName, names);
  168. }
  169. /**
  170. * Return a name which is not used by any other port type in the definition.
  171. * @return String
  172. */
  173. public static String buildUniquePortTypeName(Definition definition, String baseName)
  174. {
  175. if (baseName == null)
  176. {
  177. baseName = "NewPortType"; //$NON-NLS-1$
  178. }
  179. List names = getUsedPortTypeNames(definition);
  180. // Now search the list until we find an unused name
  181. return getUniqueNameHelper(baseName, names);
  182. }
  183. public static String getUniqueNameHelper(String baseName, List names)
  184. {
  185. int i = 0;
  186. String name = baseName;
  187. while (true)
  188. {
  189. if (!names.contains(name))
  190. {
  191. break;
  192. }
  193. i++;
  194. name = baseName + i;
  195. }
  196. return name;
  197. }
  198. /**
  199. * Return a name which is not used by any other service in the definition.
  200. * @return String
  201. */
  202. public static String buildUniqueServiceName(Definition definition)
  203. {
  204. List names = getUsedServiceNames(definition);
  205. // Now search the list until we find an unused name
  206. return getUniqueNameHelper("NewService", names); //$NON-NLS-1$
  207. }
  208. /**
  209. * Return a name which is not used by any other binding in the definition.
  210. * @return String
  211. */
  212. public static String buildUniqueBindingName(Definition definition, String baseName)
  213. {
  214. if (baseName == null)
  215. {
  216. baseName = "NewBinding"; //$NON-NLS-1$
  217. }
  218. List names = getUsedBindingNames(definition);
  219. return getUniqueNameHelper(baseName, names);
  220. }
  221. public static String buildUniquePrefix(Definition definition, String basePrefix)
  222. {
  223. String prefix = basePrefix;
  224. for (int i = 1; definition.getNamespace(prefix) != null; i++)
  225. {
  226. prefix = basePrefix + i;
  227. }
  228. return prefix;
  229. }
  230. public static String buildUniquePortName(Service service, String baseName)
  231. {
  232. if (baseName == null)
  233. {
  234. baseName = "NewPort"; //$NON-NLS-1$
  235. }
  236. List names = getUsedPortNames(service);
  237. return getUniqueNameHelper(baseName, names);
  238. }
  239. public static String buildUniqueMessageName(Definition definition, MessageReference messRef)
  240. {
  241. String name = null;
  242. if (messRef instanceof Input)
  243. {
  244. name = createOperationName(messRef, "Request"); //$NON-NLS-1$
  245. }
  246. else if (messRef instanceof Output)
  247. {
  248. name = createOperationName(messRef, "Response"); //$NON-NLS-1$
  249. }
  250. else if (messRef instanceof Fault)
  251. {
  252. String faultName = ((Fault) messRef).getName();
  253. if (faultName == null || faultName.length() == 0)
  254. {
  255. faultName = "Fault"; //$NON-NLS-1$
  256. }
  257. name = createOperationName(messRef, faultName);
  258. }
  259. return NameUtil.buildUniqueMessageName(definition, name);
  260. }
  261. public static List getUsedFaultNames(Operation operation) {
  262. ArrayList names = new ArrayList();
  263. for (Iterator i = operation.getEFaults().iterator(); i.hasNext();)
  264. {
  265. Fault fault = (Fault) i.next();
  266. names.add(fault.getName());
  267. }
  268. return names;
  269. }
  270. public static List getUsedOperationNames(PortType portType) {
  271. ArrayList names = new ArrayList();
  272. for (Iterator i = portType.getEOperations().iterator(); i.hasNext();)
  273. {
  274. Operation op = (Operation) i.next();
  275. names.add(op.getName());
  276. }
  277. return names;
  278. }
  279. public static List getUsedPartNames(Message message) {
  280. ArrayList names = new ArrayList();
  281. for (Iterator i = message.getEParts().iterator(); i.hasNext();)
  282. {
  283. Part part = (Part) i.next();
  284. names.add(part.getName());
  285. }
  286. return names;
  287. }
  288. public static List getUsedPortTypeNames(Definition definition) {
  289. ArrayList names = new ArrayList();
  290. for (Iterator i = definition.getEPortTypes().iterator(); i.hasNext();)
  291. {
  292. PortType portType = (PortType) i.next();
  293. // TODO: port check
  294. // if (portType.isSetQName())
  295. if (portType.getQName() != null)
  296. {
  297. names.add(portType.getQName().getLocalPart());
  298. }
  299. }
  300. return names;
  301. }
  302. public static List getUsedServiceNames(Definition definition) {
  303. // First build a list of names already used
  304. ArrayList names = new ArrayList();
  305. for (Iterator i = definition.getEServices().iterator(); i.hasNext();)
  306. {
  307. Service service = (Service) i.next();
  308. // TODO: port check
  309. // if(service.isSetQName())
  310. if (service.getQName() != null)
  311. names.add(service.getQName().getLocalPart());
  312. }
  313. return names;
  314. }
  315. public static List getUsedMessageNames(Definition definition) {
  316. ArrayList names = new ArrayList();
  317. for (Iterator i = definition.getEMessages().iterator(); i.hasNext();)
  318. {
  319. Message msg = (Message) i.next();
  320. // TODO: port check
  321. if (msg.getQName() != null)
  322. // if(msg.isSetQName())
  323. names.add(msg.getQName().getLocalPart());
  324. }
  325. return names;
  326. }
  327. public static List getUsedBindingNames(Definition definition) {
  328. ArrayList names = new ArrayList();
  329. for (Iterator i = definition.getEBindings().iterator(); i.hasNext();)
  330. {
  331. Binding binding = (Binding) i.next();
  332. // TODO: port check
  333. // if (binding.isSetQName())
  334. if (binding.getQName() != null)
  335. {
  336. names.add(binding.getQName().getLocalPart());
  337. }
  338. }
  339. return names;
  340. }
  341. public static List getUsedPortNames(Service service) {
  342. // First build a list of names already used
  343. ArrayList names = new ArrayList();
  344. for (Iterator i = service.getEPorts().iterator(); i.hasNext();)
  345. {
  346. Port port = (Port) i.next();
  347. if (port.getName() != null)
  348. {
  349. names.add(port.getName());
  350. }
  351. }
  352. return names;
  353. }
  354. private static String createOperationName(Object object, String suffix)
  355. {
  356. String result = null;
  357. if (object instanceof EObject)
  358. {
  359. EObject parent = ((EObject)object).eContainer();
  360. if (parent instanceof Operation)
  361. {
  362. result = ((Operation)parent).getName();
  363. }
  364. }
  365. if (result != null)
  366. {
  367. result += suffix;
  368. }
  369. return result;
  370. }
  371. public static String getMessageName(MessageReference messageRef) {
  372. String messageName = "NewMessage"; //$NON-NLS-1$
  373. List messageNames = new ArrayList();
  374. Operation operation = (Operation) messageRef.getContainer();
  375. Iterator messageIt = operation.getEnclosingDefinition().getEMessages().iterator();
  376. while (messageIt.hasNext()) {
  377. messageNames.add(((Message) messageIt.next()).getQName().getLocalPart());
  378. }
  379. // String requestResponseString = getRequestOrResponse(messageRef) + "Msg"; //$NON-NLS-1$
  380. String requestResponseString = getRequestOrResponse(messageRef); //$NON-NLS-1$
  381. messageName = getUniqueNameHelper(operation.getName() + requestResponseString, messageNames);
  382. return messageName;
  383. }
  384. public static String getPartName(MessageReference messageRef) {
  385. String partName = "NewPart"; //$NON-NLS-1$
  386. Message message = messageRef.getEMessage();
  387. Operation operation = (Operation) messageRef.getContainer();
  388. String operationName = operation.getName();
  389. String appendString = ""; //$NON-NLS-1$
  390. if (messageRef instanceof Input) {
  391. // appendString = "Parameters"; //$NON-NLS-1$
  392. appendString = "Request"; //$NON-NLS-1$
  393. }
  394. else if (messageRef instanceof Output) {
  395. // appendString = "Result"; //$NON-NLS-1$
  396. appendString = "Response"; //$NON-NLS-1$
  397. }
  398. else if (messageRef instanceof Fault) {
  399. // appendString = "Fault"; //$NON-NLS-1$
  400. appendString = "_Fault"; //$NON-NLS-1$
  401. }
  402. partName = operationName + appendString;
  403. List usedPartNames = new ArrayList();
  404. if (message != null) {
  405. partName = message.getQName().getLocalPart();
  406. Iterator partIt = message.getEParts().iterator();
  407. while (partIt.hasNext()) {
  408. usedPartNames.add(((Part) partIt.next()).getName());
  409. }
  410. }
  411. partName = getUniqueNameHelper(partName, usedPartNames);
  412. return partName;
  413. }
  414. public static String getOperationName(PortType portType) {
  415. String operationName = "NewOperation"; //$NON-NLS-1$
  416. Iterator operationIt = portType.getEOperations().iterator();
  417. List usedNames = new ArrayList();
  418. while (operationIt.hasNext()) {
  419. usedNames.add(((Operation) operationIt.next()).getName());
  420. }
  421. operationName = getUniqueNameHelper("NewOperation", usedNames); //$NON-NLS-1$
  422. return operationName;
  423. }
  424. public static String getRequestOrResponse(MessageReference messageRef) {
  425. if (messageRef instanceof Input)
  426. {
  427. return "Request"; //$NON-NLS-1$
  428. }
  429. else if (messageRef instanceof Output)
  430. {
  431. return "Response"; //$NON-NLS-1$
  432. }
  433. else if (messageRef instanceof Fault)
  434. {
  435. return "_Fault"; //$NON-NLS-1$
  436. }
  437. return ""; //$NON-NLS-1$
  438. }
  439. public static String getFaultName(Operation operation) {
  440. String faultName = "fault"; //$NON-NLS-1$
  441. List nameList = new ArrayList();
  442. Iterator faultIt = operation.getEFaults().iterator();
  443. while (faultIt.hasNext()) {
  444. nameList.add(((Fault) faultIt.next()).getName());
  445. }
  446. faultName = getUniqueNameHelper(faultName, nameList);
  447. return faultName;
  448. }
  449. public static String getXSDElementName(String baseName, Object parent) {
  450. String elementName = ""; //$NON-NLS-1$
  451. if (parent instanceof XSDSchema) {
  452. elementName = getUniqueNameHelper(baseName, getUsedElementNames((XSDSchema) parent));
  453. }
  454. else if (parent instanceof XSDModelGroup) {
  455. List existingNames = new ArrayList();
  456. XSDModelGroup modelGroup = (XSDModelGroup) parent;
  457. Iterator modelGroupIt = modelGroup.getContents().iterator();
  458. while (modelGroupIt.hasNext()) {
  459. Object item = modelGroupIt.next();
  460. if (item instanceof XSDParticle) {
  461. XSDParticle existingParticle = (XSDParticle) item;
  462. if (existingParticle.getContent() instanceof XSDElementDeclaration) {
  463. existingNames.add(((XSDElementDeclaration) existingParticle.getContent()).getName());
  464. }
  465. }
  466. }
  467. elementName = getUniqueNameHelper(baseName, existingNames);
  468. }
  469. return elementName;
  470. }
  471. public static String getXSDComplexTypeName(String baseName, XSDSchema schema) {
  472. String typeName = ""; //$NON-NLS-1$
  473. List existingNames = new ArrayList();
  474. Iterator it = schema.getTypeDefinitions().iterator();
  475. while (it.hasNext()) {
  476. Object item = it.next();
  477. if (item instanceof XSDComplexTypeDefinition) {
  478. existingNames.add(((XSDComplexTypeDefinition) item).getName());
  479. }
  480. }
  481. typeName = getUniqueNameHelper(baseName, existingNames);
  482. return typeName;
  483. }
  484. private static List getUsedElementNames(XSDSchema xsdSchema) {
  485. List usedNames = new ArrayList();
  486. Iterator schemaIt = xsdSchema.getContents().iterator();
  487. while (schemaIt.hasNext()) {
  488. Object item = schemaIt.next();
  489. if (item instanceof XSDElementDeclaration) {
  490. usedNames.add(((XSDElementDeclaration) item).getName());
  491. }
  492. }
  493. return usedNames;
  494. }
  495. /**
  496. * Returns a copy of the input string with the the first letter converted to uppercase.
  497. * @param name the input name. Must not be null.
  498. * @return a a copy of the input string with the the first letter converted to uppercase.
  499. */
  500. public static String buildFirstCharUppercaseName(String name) {
  501. int length = name.length();
  502. char[] buffer = new char[length];
  503. buffer[0] = Character.toUpperCase(name.charAt(0));
  504. name.getChars(1, length, buffer, 1);
  505. return String.copyValueOf(buffer);
  506. }
  507. }