Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FormatingStrategyUML |
|
| 3.5454545454545454;3.545 |
1 | /* $Id: FormatingStrategyUML.java 17835 2010-01-12 19:05:31Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 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 | * euluis | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 2007-2009 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.profile.internal; | |
40 | ||
41 | import java.util.Iterator; | |
42 | ||
43 | import org.argouml.i18n.Translator; | |
44 | import org.argouml.model.Model; | |
45 | import org.argouml.profile.FormatingStrategy; | |
46 | ||
47 | /** | |
48 | * The Formating Strategy based on UML naming conventions. | |
49 | * | |
50 | * @author Marcos Aur�lio | |
51 | */ | |
52 | 900 | public class FormatingStrategyUML implements FormatingStrategy { |
53 | ||
54 | public String formatElement(Object element, Object namespace) { | |
55 | 0 | String value = null; |
56 | 0 | if (element == null) { |
57 | 0 | value = ""; |
58 | } else { | |
59 | 0 | Object elementNs = Model.getFacade().getNamespace(element); |
60 | // | |
61 | // if element is an AssociationEnd use | |
62 | // the namespace of containing association | |
63 | // | |
64 | 0 | if (Model.getFacade().isAAssociationEnd(element)) { |
65 | 0 | Object assoc = Model.getFacade().getAssociation(element); |
66 | 0 | if (assoc != null) { |
67 | 0 | elementNs = Model.getFacade().getNamespace(assoc); |
68 | } | |
69 | } | |
70 | 0 | if (elementNs == namespace) { |
71 | 0 | value = Model.getFacade().getName(element); |
72 | 0 | if (value == null || value.length() == 0) { |
73 | 0 | value = defaultName(element, namespace); |
74 | } | |
75 | } else { | |
76 | 0 | StringBuffer buffer = new StringBuffer(); |
77 | 0 | String pathSep = getPathSeparator(); |
78 | 0 | buildPath(buffer, element, pathSep); |
79 | 0 | value = buffer.toString(); |
80 | } | |
81 | } | |
82 | 0 | return value; |
83 | } | |
84 | ||
85 | /** | |
86 | * Create a default association end name from the type of assocEnd. | |
87 | * Follows the conventions in UML 2.2 Infrastructure, | |
88 | * 6.2.1. "Diagram format". | |
89 | * | |
90 | * @param assocEnd the given association end name | |
91 | * @param namespace the namespace | |
92 | * @return the default name for the given associationend | |
93 | */ | |
94 | protected String defaultAssocEndName(Object assocEnd, | |
95 | Object namespace) { | |
96 | 0 | String name = null; |
97 | 0 | Object type = Model.getFacade().getType(assocEnd); |
98 | 0 | if (type != null) { |
99 | 0 | name = formatElement(type, namespace); |
100 | 0 | name = ensureFirstCharLowerCase(name); |
101 | } else { | |
102 | 0 | name = Translator.localize("profile.unknown-type"); |
103 | } | |
104 | 0 | Object mult = Model.getFacade().getMultiplicity(assocEnd); |
105 | 0 | if (mult != null) { |
106 | 0 | int lower = Model.getFacade().getLower(mult); |
107 | 0 | int upper = Model.getFacade().getUpper(mult); |
108 | 0 | if (lower == upper && lower == 1) { |
109 | // simply use name as it is | |
110 | } | |
111 | else { | |
112 | 0 | StringBuffer buf = new StringBuffer(name); |
113 | 0 | buf.append("["); |
114 | 0 | buf.append(Integer.toString(lower)); |
115 | 0 | buf.append(".."); |
116 | 0 | if (upper >= 0) { |
117 | 0 | buf.append(Integer.toString(upper)); |
118 | } else { | |
119 | 0 | buf.append("*"); |
120 | } | |
121 | 0 | buf.append("]"); |
122 | 0 | name = buf.toString(); |
123 | } | |
124 | } | |
125 | 0 | return name; |
126 | } | |
127 | ||
128 | String ensureFirstCharLowerCase(String s) { | |
129 | 0 | if (s.length() > 0) { |
130 | 0 | return s.substring(0, 1).toLowerCase() + s.substring(1); |
131 | } | |
132 | 0 | return s; |
133 | } | |
134 | ||
135 | /** | |
136 | * Create a default association name from its ends. Follows the conventions | |
137 | * in UML 2.2 Infrastructure, 6.2.1. "Diagram format". | |
138 | * | |
139 | * @param assoc the given association | |
140 | * @param ns the namespace | |
141 | * @return the default association name | |
142 | */ | |
143 | protected String defaultAssocName(Object assoc, Object ns) { | |
144 | 0 | StringBuffer buf = new StringBuffer("A_"); |
145 | 0 | Iterator iter = Model.getFacade().getConnections(assoc).iterator(); |
146 | 0 | for (int i = 0; iter.hasNext(); i++) { |
147 | 0 | if (i != 0) { |
148 | 0 | buf.append("_"); |
149 | } | |
150 | 0 | buf.append(defaultAssocEndName(iter.next(), ns)); |
151 | } | |
152 | 0 | return buf.toString(); |
153 | } | |
154 | ||
155 | /** | |
156 | * Use the term specializes, which is referred some times in the | |
157 | * UML 2.2 Infrastructure specification. | |
158 | * | |
159 | * @param gen the given Generalization | |
160 | * @param namespace the namespace | |
161 | * @return the default generalization name | |
162 | */ | |
163 | protected String defaultGeneralizationName(Object gen, Object namespace) { | |
164 | 0 | Object child = Model.getFacade().getSpecific(gen); |
165 | 0 | Object parent = Model.getFacade().getGeneral(gen); |
166 | 0 | return Translator.messageFormat( |
167 | "profile.default.specializes.expression", | |
168 | new Object[] {formatElement(child, namespace), | |
169 | formatElement(parent, namespace), }); | |
170 | } | |
171 | ||
172 | /** | |
173 | * @param element the given modelelement | |
174 | * @param namespace the namespace | |
175 | * @return a default name for this modelelement | |
176 | */ | |
177 | protected String defaultName(Object element, Object namespace) { | |
178 | 0 | String name = null; |
179 | 0 | if (Model.getFacade().isAAssociationEnd(element)) { |
180 | 0 | name = defaultAssocEndName(element, namespace); |
181 | } else { | |
182 | 0 | if (Model.getFacade().isAAssociation(element)) { |
183 | 0 | name = defaultAssocName(element, namespace); |
184 | } | |
185 | 0 | if (Model.getFacade().isAGeneralization(element)) { |
186 | 0 | name = defaultGeneralizationName(element, namespace); |
187 | } | |
188 | } | |
189 | 0 | if (name == null) { |
190 | 0 | name = Translator.localize("profile.anonymous"); |
191 | } | |
192 | 0 | return name; |
193 | } | |
194 | ||
195 | /** | |
196 | * @return the path separator (currently "::") | |
197 | */ | |
198 | protected String getPathSeparator() { | |
199 | 0 | return "::"; |
200 | } | |
201 | ||
202 | /** | |
203 | * @param buffer (out) the buffer that will contain the path build | |
204 | * @param element the given model element | |
205 | * @param pathSep the path separator character(s) | |
206 | */ | |
207 | private void buildPath(StringBuffer buffer, Object element, | |
208 | String pathSep) { | |
209 | 0 | if (element != null) { |
210 | 0 | Object parent = Model.getFacade().getNamespace(element); |
211 | 0 | if (parent != null && parent != element) { |
212 | 0 | buildPath(buffer, parent, pathSep); |
213 | 0 | buffer.append(pathSep); |
214 | } | |
215 | 0 | String name = Model.getFacade().getName(element); |
216 | 0 | if (name == null || name.length() == 0) { |
217 | 0 | name = defaultName(element, null); |
218 | } | |
219 | 0 | buffer.append(name); |
220 | } | |
221 | 0 | } |
222 | ||
223 | /** | |
224 | * @return the string that separates elements | |
225 | */ | |
226 | protected String getElementSeparator() { | |
227 | 0 | return ", "; |
228 | } | |
229 | ||
230 | /** | |
231 | * @return the string that represents an empty collection | |
232 | */ | |
233 | protected String getEmptyCollection() { | |
234 | 0 | return Translator.localize("profile.empty.collection"); |
235 | } | |
236 | ||
237 | ||
238 | public String formatCollection(Iterator iter, Object namespace) { | |
239 | 0 | String value = null; |
240 | 0 | if (iter.hasNext()) { |
241 | 0 | StringBuffer buffer = new StringBuffer(); |
242 | 0 | String elementSep = getElementSeparator(); |
243 | 0 | Object obj = null; |
244 | 0 | for (int i = 0; iter.hasNext(); i++) { |
245 | 0 | if (i > 0) { |
246 | 0 | buffer.append(elementSep); |
247 | } | |
248 | 0 | obj = iter.next(); |
249 | 0 | if (Model.getFacade().isAModelElement(obj)) { |
250 | 0 | buffer.append(formatElement(obj, namespace)); |
251 | } else { | |
252 | 0 | buffer.append(obj.toString()); |
253 | } | |
254 | } | |
255 | 0 | value = buffer.toString(); |
256 | 0 | } else { |
257 | 0 | value = getEmptyCollection(); |
258 | } | |
259 | 0 | return value; |
260 | } | |
261 | ||
262 | } |