1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
package org.argouml.notation.providers.java; |
40 | |
|
41 | |
import java.text.ParseException; |
42 | |
import java.util.ArrayList; |
43 | |
import java.util.List; |
44 | |
import java.util.NoSuchElementException; |
45 | |
|
46 | |
import org.argouml.application.events.ArgoEventPump; |
47 | |
import org.argouml.application.events.ArgoEventTypes; |
48 | |
import org.argouml.application.events.ArgoHelpEvent; |
49 | |
import org.argouml.i18n.Translator; |
50 | |
import org.argouml.model.Model; |
51 | |
import org.argouml.notation.NotationSettings; |
52 | |
import org.argouml.notation.providers.ModelElementNameNotation; |
53 | |
import org.argouml.util.MyTokenizer; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public class ModelElementNameNotationJava extends ModelElementNameNotation { |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public ModelElementNameNotationJava(Object name) { |
68 | 0 | super(name); |
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public void parse(Object modelElement, String text) { |
76 | |
try { |
77 | 0 | parseModelElement(modelElement, text); |
78 | 0 | } catch (ParseException pe) { |
79 | 0 | String msg = "statusmsg.bar.error.parsing.node-modelelement"; |
80 | 0 | Object[] args = { |
81 | |
pe.getLocalizedMessage(), |
82 | |
Integer.valueOf(pe.getErrorOffset()), |
83 | |
}; |
84 | 0 | ArgoEventPump.fireEvent(new ArgoHelpEvent( |
85 | |
ArgoEventTypes.HELP_CHANGED, this, |
86 | |
Translator.messageFormat(msg, args))); |
87 | 0 | } |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
static void parseModelElement(Object modelElement, String text) |
95 | |
throws ParseException { |
96 | |
MyTokenizer st; |
97 | |
|
98 | 0 | boolean abstrac = false; |
99 | 0 | boolean fina = false; |
100 | 0 | boolean publi = false; |
101 | 0 | boolean privat = false; |
102 | 0 | boolean protect = false; |
103 | |
String token; |
104 | 0 | List<String> path = null; |
105 | 0 | String name = null; |
106 | |
|
107 | |
try { |
108 | 0 | st = new MyTokenizer(text, |
109 | |
" ,.,abstract,final,public,private,protected"); |
110 | 0 | while (st.hasMoreTokens()) { |
111 | 0 | token = st.nextToken(); |
112 | |
|
113 | 0 | if (" ".equals(token)) { |
114 | |
|
115 | 0 | } else if ("abstract".equals(token)) { |
116 | 0 | abstrac = true; |
117 | 0 | } else if ("final".equals(token)) { |
118 | 0 | fina = true; |
119 | 0 | } else if ("public".equals(token)) { |
120 | 0 | publi = true; |
121 | 0 | } else if ("private".equals(token)) { |
122 | 0 | privat = true; |
123 | 0 | } else if ("protected".equals(token)) { |
124 | 0 | protect = true; |
125 | 0 | } else if (".".equals(token)) { |
126 | 0 | if (name != null) { |
127 | 0 | name = name.trim(); |
128 | |
} |
129 | |
|
130 | 0 | if (path != null && (name == null || "".equals(name))) { |
131 | 0 | String msg = |
132 | |
"parsing.error.model-element-name.anon-qualifiers"; |
133 | 0 | throw new ParseException(Translator.localize(msg), |
134 | |
st.getTokenIndex()); |
135 | |
} |
136 | |
|
137 | 0 | if (path == null) { |
138 | 0 | path = new ArrayList<String>(); |
139 | |
} |
140 | 0 | if (name != null) { |
141 | 0 | path.add(name); |
142 | |
} |
143 | 0 | name = null; |
144 | |
|
145 | |
} else { |
146 | 0 | if (name != null) { |
147 | 0 | String msg = |
148 | |
"parsing.error.model-element-name.twin-names"; |
149 | 0 | throw new ParseException(Translator.localize(msg), |
150 | |
st.getTokenIndex()); |
151 | |
} |
152 | 0 | name = token; |
153 | |
} |
154 | |
} |
155 | 0 | } catch (NoSuchElementException nsee) { |
156 | 0 | String msg = |
157 | |
"parsing.error.model-element-name.unexpected-name-element"; |
158 | 0 | throw new ParseException(Translator.localize(msg), |
159 | |
text.length()); |
160 | 0 | } catch (ParseException pre) { |
161 | 0 | throw pre; |
162 | 0 | } |
163 | |
|
164 | 0 | if (name != null) { |
165 | 0 | name = name.trim(); |
166 | |
} |
167 | |
|
168 | 0 | if (path != null && (name == null || "".equals(name))) { |
169 | 0 | String msg = "parsing.error.model-element-name.must-end-with-name"; |
170 | 0 | throw new ParseException(Translator.localize(msg), 0); |
171 | |
} |
172 | |
|
173 | |
|
174 | 0 | if (!isValidJavaClassName(name)) { |
175 | 0 | throw new ParseException( |
176 | |
"Invalid class name for Java: " |
177 | |
+ name, 0); |
178 | |
} |
179 | |
|
180 | 0 | if (path != null) { |
181 | 0 | Object nspe = |
182 | |
Model.getModelManagementHelper().getElement( |
183 | |
path, |
184 | |
Model.getFacade().getRoot(modelElement)); |
185 | |
|
186 | 0 | if (nspe == null || !(Model.getFacade().isANamespace(nspe))) { |
187 | 0 | String msg = |
188 | |
"parsing.error.model-element-name.namespace-unresolved"; |
189 | 0 | throw new ParseException(Translator.localize(msg), |
190 | |
0); |
191 | |
} |
192 | 0 | if (!Model.getCoreHelper().isValidNamespace( |
193 | |
modelElement, nspe)) { |
194 | 0 | String msg = |
195 | |
"parsing.error.model-element-name.namespace-invalid"; |
196 | 0 | throw new ParseException(Translator.localize(msg), |
197 | |
0); |
198 | |
} |
199 | 0 | Model.getCoreHelper().addOwnedElement(nspe, modelElement); |
200 | |
} |
201 | |
|
202 | 0 | Model.getCoreHelper().setName(modelElement, name); |
203 | |
|
204 | 0 | if (abstrac) { |
205 | 0 | Model.getCoreHelper().setAbstract(modelElement, abstrac); |
206 | |
} |
207 | 0 | if (fina) { |
208 | 0 | Model.getCoreHelper().setLeaf(modelElement, fina); |
209 | |
} |
210 | 0 | if (publi) { |
211 | 0 | Model.getCoreHelper().setVisibility(modelElement, |
212 | |
Model.getVisibilityKind().getPublic()); |
213 | |
} |
214 | 0 | if (privat) { |
215 | 0 | Model.getCoreHelper().setVisibility(modelElement, |
216 | |
Model.getVisibilityKind().getPrivate()); |
217 | |
} |
218 | 0 | if (protect) { |
219 | 0 | Model.getCoreHelper().setVisibility(modelElement, |
220 | |
Model.getVisibilityKind().getProtected()); |
221 | |
} |
222 | 0 | } |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
private static boolean isValidJavaClassName(String name) { |
229 | |
|
230 | 0 | return true; |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
public String getParsingHelp() { |
237 | 0 | return "parsing.help.java.fig-nodemodelelement"; |
238 | |
} |
239 | |
|
240 | |
public String toString(Object modelElement, NotationSettings settings) { |
241 | |
String name; |
242 | 0 | name = Model.getFacade().getName(modelElement); |
243 | 0 | if (name == null) { |
244 | 0 | return ""; |
245 | |
} |
246 | 0 | String visibility = ""; |
247 | 0 | if (settings.isShowVisibilities()) { |
248 | 0 | visibility = NotationUtilityJava.generateVisibility(modelElement); |
249 | |
} |
250 | 0 | String path = ""; |
251 | 0 | if (settings.isShowPaths()) { |
252 | 0 | path = NotationUtilityJava.generatePath(modelElement); |
253 | |
} |
254 | 0 | return NotationUtilityJava.generateLeaf(modelElement) |
255 | |
+ NotationUtilityJava.generateAbstract(modelElement) |
256 | |
+ visibility |
257 | |
+ path |
258 | |
+ name; |
259 | |
} |
260 | |
|
261 | |
} |