PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/argouml-0.34/argouml/src/argouml-app/src/org/argouml/notation/providers/uml/ClassifierRoleNotationUml.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 299 lines | 174 code | 30 blank | 95 comment | 57 complexity | 2d97d7ce369a96dfe7cbf5681e9aaefa MD5 | raw file
  1. /* $Id: ClassifierRoleNotationUml.java 18852 2010-11-20 19:27:11Z mvw $
  2. *****************************************************************************
  3. * Copyright (c) 2009-2010 Contributors - see below
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution, and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Michiel van der Wulp
  11. *****************************************************************************
  12. *
  13. * Some portions of this file was previously release using the BSD License:
  14. */
  15. // Copyright (c) 2006-2009 The Regents of the University of California. All
  16. // Rights Reserved. Permission to use, copy, modify, and distribute this
  17. // software and its documentation without fee, and without a written
  18. // agreement is hereby granted, provided that the above copyright notice
  19. // and this paragraph appear in all copies. This software program and
  20. // documentation are copyrighted by The Regents of the University of
  21. // California. The software program and documentation are supplied "AS
  22. // IS", without any accompanying services from The Regents. The Regents
  23. // does not warrant that the operation of the program will be
  24. // uninterrupted or error-free. The end-user understands that the program
  25. // was developed for research purposes and is advised not to rely
  26. // exclusively on the program for any reason. IN NO EVENT SHALL THE
  27. // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  28. // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
  29. // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  30. // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  31. // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
  32. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  33. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
  34. // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
  35. // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
  36. // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  37. package org.argouml.notation.providers.uml;
  38. import java.text.ParseException;
  39. import java.util.ArrayList;
  40. import java.util.Collection;
  41. import java.util.Iterator;
  42. import java.util.List;
  43. import java.util.NoSuchElementException;
  44. import org.argouml.application.events.ArgoEventPump;
  45. import org.argouml.application.events.ArgoEventTypes;
  46. import org.argouml.application.events.ArgoHelpEvent;
  47. import org.argouml.i18n.Translator;
  48. import org.argouml.model.Model;
  49. import org.argouml.notation.NotationSettings;
  50. import org.argouml.notation.providers.ClassifierRoleNotation;
  51. import org.argouml.util.MyTokenizer;
  52. /**
  53. * The UML notation for a ClassifierRole. <p>
  54. *
  55. * The following is supported: <p>
  56. *
  57. * <pre>
  58. * baselist := [base] [, base]*
  59. * classifierRole := [name] [/ role] [: baselist]
  60. * </pre>
  61. *
  62. * The <code>role </code> and <code>baselist</code> can be given in
  63. * any order.<p>
  64. *
  65. * The <code>name</code> is the Instance name, not used for a ClassifierRole.<p>
  66. *
  67. * This syntax is compatible with the UML 1.3 and 1.4 specification.
  68. *
  69. * @author Michiel van der Wulp
  70. */
  71. public class ClassifierRoleNotationUml extends ClassifierRoleNotation {
  72. /**
  73. * The Constructor.
  74. *
  75. * @param classifierRole the UML ClassifierRole
  76. */
  77. public ClassifierRoleNotationUml(Object classifierRole) {
  78. super(classifierRole);
  79. }
  80. /*
  81. * @see org.argouml.notation.providers.NotationProvider#getParsingHelp()
  82. */
  83. public String getParsingHelp() {
  84. return "parsing.help.fig-classifierrole";
  85. }
  86. /*
  87. * @see org.argouml.notation.providers.NotationProvider#parse(java.lang.Object, java.lang.String)
  88. */
  89. public void parse(Object modelElement, String text) {
  90. try {
  91. parseClassifierRole(modelElement, text);
  92. } catch (ParseException pe) {
  93. String msg = "statusmsg.bar.error.parsing.classifierrole";
  94. Object[] args = {pe.getLocalizedMessage(),
  95. Integer.valueOf(pe.getErrorOffset()), };
  96. ArgoEventPump.fireEvent(new ArgoHelpEvent(
  97. ArgoEventTypes.HELP_CHANGED, this,
  98. Translator.messageFormat(msg, args)));
  99. }
  100. }
  101. /**
  102. * Parses a ClassifierRole represented by the following line of the format:
  103. *
  104. * <pre>
  105. * baselist := [base] [, base]*
  106. * classifierRole := [name] [/ role] [: baselist]
  107. * </pre>
  108. *
  109. * <code>role </code> and <code>baselist</code> can be given in
  110. * any order.<p>
  111. *
  112. * This syntax is compatible with the UML 1.3 specification.
  113. *
  114. * (formerly: "name: base" )
  115. *
  116. * @param cls the classifier role to apply any changes to
  117. * @param s the String to parse
  118. * @return the classifier role with the applied changes
  119. * @throws ParseException when it detects an error in the attribute string.
  120. * See also ParseError.getErrorOffset().
  121. */
  122. protected Object parseClassifierRole(Object cls, String s)
  123. throws ParseException {
  124. String name = null;
  125. String token;
  126. String role = null;
  127. String base = null;
  128. List<String> bases = null;
  129. boolean hasColon = false;
  130. boolean hasSlash = false;
  131. try {
  132. MyTokenizer st = new MyTokenizer(s, " ,\t,/,:,\\,");
  133. while (st.hasMoreTokens()) {
  134. token = st.nextToken();
  135. if (" ".equals(token) || "\t".equals(token)) {
  136. /* Do nothing. */
  137. } else if ("/".equals(token)) {
  138. hasSlash = true;
  139. hasColon = false;
  140. if (base != null) {
  141. if (bases == null) {
  142. bases = new ArrayList<String>();
  143. }
  144. bases.add(base);
  145. }
  146. base = null;
  147. } else if (":".equals(token)) {
  148. hasColon = true;
  149. hasSlash = false;
  150. if (bases == null) {
  151. bases = new ArrayList<String>();
  152. }
  153. if (base != null) {
  154. bases.add(base);
  155. }
  156. base = null;
  157. } else if (",".equals(token)) {
  158. if (base != null) {
  159. if (bases == null) {
  160. bases = new ArrayList<String>();
  161. }
  162. bases.add(base);
  163. }
  164. base = null;
  165. } else if (hasColon) {
  166. if (base != null) {
  167. String msg = "parsing.error.classifier.extra-test";
  168. throw new ParseException(
  169. Translator.localize(msg),
  170. st.getTokenIndex());
  171. }
  172. base = token;
  173. } else if (hasSlash) {
  174. if (role != null) {
  175. String msg = "parsing.error.classifier.extra-test";
  176. throw new ParseException(
  177. Translator.localize(msg),
  178. st.getTokenIndex());
  179. }
  180. role = token;
  181. } else {
  182. if (name != null) {
  183. String msg = "parsing.error.classifier.extra-test";
  184. throw new ParseException(
  185. Translator.localize(msg),
  186. st.getTokenIndex());
  187. }
  188. name = token;
  189. }
  190. }
  191. } catch (NoSuchElementException nsee) {
  192. String msg = "parsing.error.classifier.unexpected-end-attribute";
  193. throw new ParseException(Translator.localize(msg), s.length());
  194. }
  195. if (base != null) {
  196. if (bases == null) {
  197. bases = new ArrayList<String>();
  198. }
  199. bases.add(base);
  200. }
  201. // TODO: What to do about object name???
  202. // if (name != null)
  203. // ;
  204. if (role != null) {
  205. Model.getCoreHelper().setName(cls, role.trim());
  206. }
  207. if (bases != null) {
  208. // Remove bases that aren't there anymore
  209. // copy - can't iterate modify live collection while iterating it
  210. Collection b = new ArrayList(Model.getFacade().getBases(cls));
  211. Iterator it = b.iterator();
  212. Object c;
  213. Object ns = Model.getFacade().getNamespace(cls);
  214. if (ns != null && Model.getFacade().getNamespace(ns) != null) {
  215. ns = Model.getFacade().getNamespace(ns);
  216. } else {
  217. ns = Model.getFacade().getRoot(cls);
  218. }
  219. while (it.hasNext()) {
  220. c = it.next();
  221. if (!bases.contains(Model.getFacade().getName(c))) {
  222. Model.getCollaborationsHelper().removeBase(cls, c);
  223. }
  224. }
  225. it = bases.iterator();
  226. addBases:
  227. while (it.hasNext()) {
  228. String d = ((String) it.next()).trim();
  229. Iterator it2 = b.iterator();
  230. while (it2.hasNext()) {
  231. c = it2.next();
  232. if (d.equals(Model.getFacade().getName(c))) {
  233. continue addBases;
  234. }
  235. }
  236. c = NotationUtilityUml.getType(d, ns);
  237. if (Model.getFacade().isACollaboration(
  238. Model.getFacade().getNamespace(c))) {
  239. Model.getCoreHelper().setNamespace(c, ns);
  240. }
  241. Model.getCollaborationsHelper().addBase(cls, c);
  242. }
  243. }
  244. return cls;
  245. }
  246. private String toString(Object modelElement) {
  247. String nameString = Model.getFacade().getName(modelElement);
  248. if (nameString == null) {
  249. nameString = "";
  250. }
  251. nameString = nameString.trim();
  252. // Loop through all base classes, building a comma separated list
  253. StringBuilder baseString = NotationUtilityUml.formatNameList(
  254. Model.getFacade().getBases(modelElement));
  255. baseString = new StringBuilder(baseString.toString().trim());
  256. // Build the final string
  257. if (nameString.length() != 0) {
  258. nameString = "/" + nameString;
  259. }
  260. if (baseString.length() != 0) {
  261. baseString = baseString.insert(0, ":");
  262. }
  263. return nameString + baseString.toString();
  264. }
  265. @Override
  266. public String toString(Object modelElement, NotationSettings settings) {
  267. return toString(modelElement);
  268. }
  269. }