1 package org.andromda.repositories.emf.uml22;
2
3 import java.net.URL;
4 import java.util.HashMap;
5 import java.util.Map;
6 import org.andromda.core.common.ComponentContainer;
7 import org.andromda.core.metafacade.ModelAccessFacade;
8 import org.andromda.core.repository.RepositoryFacadeException;
9 import org.andromda.metafacades.emf.uml22.UMLModelAccessFacade;
10 import org.andromda.metafacades.emf.uml22.UmlUtilities;
11 import org.andromda.repositories.emf.EMFRepositoryFacade;
12 import org.apache.commons.lang.StringUtils;
13 import org.apache.log4j.Logger;
14 import org.eclipse.emf.common.util.URI;
15 import org.eclipse.emf.ecore.EObject;
16 import org.eclipse.emf.ecore.EPackage;
17 import org.eclipse.emf.ecore.EcorePackage;
18 import org.eclipse.emf.ecore.EPackage.Registry;
19 import org.eclipse.emf.ecore.resource.Resource;
20 import org.eclipse.emf.ecore.resource.ResourceSet;
21 import org.eclipse.emf.ecore.resource.URIConverter;
22 import org.eclipse.emf.ecore.util.EcoreUtil;
23 import org.eclipse.emf.ecore.xmi.XMLResource;
24 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
25 import org.eclipse.emf.ecore.xmi.impl.XMLParserPoolImpl;
26 import org.eclipse.emf.mapping.ecore2xml.Ecore2XMLPackage;
27 import org.eclipse.uml2.types.TypesPackage;
28 import org.eclipse.uml2.uml.Model;
29 import org.eclipse.uml2.uml.UMLPackage;
30 import org.eclipse.uml2.uml.profile.standard.StandardPackage;
31 import org.eclipse.uml2.uml.resource.UML22UMLExtendedMetaData;
32 import org.eclipse.uml2.uml.resource.UML22UMLResource;
33 import org.eclipse.uml2.uml.resource.UMLResource;
34 import org.eclipse.uml2.uml.resource.XMI2UMLResource;
35
36
37
38
39
40
41
42
43
44
45 public class EMFUML2RepositoryFacade extends EMFRepositoryFacade
46 {
47
48
49
50 private static final Logger logger = Logger.getLogger(EMFUML2RepositoryFacade.class);
51
52
53
54
55
56
57 @Override
58 public ResourceSet createNewResourceSet()
59 {
60 if (logger.isDebugEnabled())
61 {
62 logger.debug("Registering resource factories");
63 }
64
65
66 final ResourceSet proxyResourceSet = new EMXProxyResolvingResourceSet();
67
68
69
70 Registry packageRegistry = proxyResourceSet.getPackageRegistry();
71
72 packageRegistry.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
73
74
75 packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
76
77
78 packageRegistry.put("http://www.eclipse.org/uml2/1.0.0/UML", UMLPackage.eINSTANCE);
79 packageRegistry.put("http://www.eclipse.org/uml2/2.0.0/UML", UMLPackage.eINSTANCE);
80 packageRegistry.put("http://www.eclipse.org/uml2/2.1.0/UML", UMLPackage.eINSTANCE);
81 packageRegistry.put("http://www.eclipse.org/uml2/2.2.0/UML", UMLPackage.eINSTANCE);
82 packageRegistry.put("http://www.eclipse.org/uml2/2.3.0/UML", UMLPackage.eINSTANCE);
83 packageRegistry.put("http://www.eclipse.org/uml2/3.0.0/UML", UMLPackage.eINSTANCE);
84 packageRegistry.put("http://www.eclipse.org/uml2/3.1.0/UML", UMLPackage.eINSTANCE);
85 packageRegistry.put("http://www.eclipse.org/uml2/4.0.0/UML", UMLPackage.eINSTANCE);
86 packageRegistry.put("http://www.eclipse.org/uml2/5.0.0/UML", UMLPackage.eINSTANCE);
87 packageRegistry.put("http://www.eclipse.org/uml2/4.0.0/Types", TypesPackage.eINSTANCE);
88 packageRegistry.put("http://www.eclipse.org/uml2/5.0.0/Types", TypesPackage.eINSTANCE);
89 packageRegistry.put("http://www.eclipse.org/uml2/4.0.0/UML/Profile/L2", StandardPackage.eINSTANCE);
90 packageRegistry.put("http://www.eclipse.org/uml2/4.0.0/UML/Profile/L3", StandardPackage.eINSTANCE);
91 packageRegistry.put("http://www.eclipse.org/uml2/5.0.0/UML/Profile/Standard", StandardPackage.eINSTANCE);
92 packageRegistry.put(Ecore2XMLPackage.eNS_URI, Ecore2XMLPackage.eINSTANCE);
93 packageRegistry.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
94
95
96 packageRegistry.put("http://schema.omg.org/spec/XMI/2.0", UMLPackage.eINSTANCE);
97 packageRegistry.put("http://schema.omg.org/spec/XMI/2.0.1", UMLPackage.eINSTANCE);
98 packageRegistry.put("http://schema.omg.org/spec/XMI/2.1", UMLPackage.eINSTANCE);
99 packageRegistry.put("http://schema.omg.org/spec/XMI/2.1.1", UMLPackage.eINSTANCE);
100 packageRegistry.put("http://schema.omg.org/spec/XMI/2.1.2", UMLPackage.eINSTANCE);
101 packageRegistry.put("http://schema.omg.org/spec/XMI/2.2", UMLPackage.eINSTANCE);
102 packageRegistry.put("http://schema.omg.org/spec/XMI/2.3", UMLPackage.eINSTANCE);
103 packageRegistry.put("http://schema.omg.org/spec/XMI/2.4", UMLPackage.eINSTANCE);
104 packageRegistry.put("http://schema.omg.org/spec/XMI/2.4.1", UMLPackage.eINSTANCE);
105 packageRegistry.put("http://schema.omg.org/spec/UML/2.0", UMLPackage.eINSTANCE);
106 packageRegistry.put("http://schema.omg.org/spec/UML/2.1", UMLPackage.eINSTANCE);
107 packageRegistry.put("http://schema.omg.org/spec/UML/2.1.1", UMLPackage.eINSTANCE);
108 packageRegistry.put("http://schema.omg.org/spec/UML/2.1.2", UMLPackage.eINSTANCE);
109 packageRegistry.put("http://schema.omg.org/spec/UML/2.2", UMLPackage.eINSTANCE);
110 packageRegistry.put("http://schema.omg.org/spec/UML/2.3", UMLPackage.eINSTANCE);
111 packageRegistry.put("http://schema.omg.org/spec/UML/2.4", UMLPackage.eINSTANCE);
112 packageRegistry.put("http://schema.omg.org/spec/UML/2.4.1", UMLPackage.eINSTANCE);
113 packageRegistry.put("http://schema.omg.org/spec/UML/2.5", UMLPackage.eINSTANCE);
114
115
116 final Map<String, Object> extensionToFactoryMap = proxyResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap();
117
118
119
120
121
122
123
124
125 extensionToFactoryMap.put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
126 extensionToFactoryMap.put("emx", UMLResource.Factory.INSTANCE);
127 extensionToFactoryMap.put("epx", UMLResource.Factory.INSTANCE);
128
129 extensionToFactoryMap.put("uml2", UML22UMLResource.Factory.INSTANCE);
130
131 extensionToFactoryMap.put(XMI2UMLResource.FILE_EXTENSION, new XMIResourceFactoryImpl());
132
133 extensionToFactoryMap.put("xml", new XMIResourceFactoryImpl());
134
135
136
137
138 registerOptionalRsmMetamodels(proxyResourceSet.getPackageRegistry());
139
140
141
142
143
144 URL url = this.getClass().getResource("/libraries/UMLPrimitiveTypes.library.uml");
145
146 if (url!=null)
147 {
148
149 String path = url.getPath().substring(0, url.getPath().indexOf("libraries"));
150 URI uri = URI.createURI("jar:" + path);
151
152
153
154
155
156 URIConverter.URI_MAP.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP),
157 uri.appendSegment("libraries").appendSegment(""));
158
159 URIConverter.URI_MAP.put(URI.createURI(UMLResource.METAMODELS_PATHMAP),
160 uri.appendSegment("metamodels").appendSegment(""));
161
162 URIConverter.URI_MAP.put(URI.createURI(UMLResource.PROFILES_PATHMAP),
163 uri.appendSegment("profiles").appendSegment(""));
164 }
165 else
166 {
167 logger.error("Could not load UML2 org.eclipse.uml2.resources jar from classpath");
168 }
169
170 Map<URI, URI> uriMap = proxyResourceSet.getURIConverter().getURIMap();
171 uriMap.putAll(UML22UMLExtendedMetaData.getURIMap());
172
173
174
175 uriMap.put(URI.createURI("http://schema.omg.org/spec/UML/2.0"),
176 URI.createURI(UMLPackage.eNS_URI));
177 uriMap.put(URI.createURI("http://schema.omg.org/spec/UML/2.1"),
178 URI.createURI(UMLPackage.eNS_URI));
179 uriMap.put(URI.createURI("http://schema.omg.org/spec/UML/2.1.1"),
180 URI.createURI(UMLPackage.eNS_URI));
181 uriMap.put(URI.createURI("http://schema.omg.org/spec/UML/2.1.2"),
182 URI.createURI(UMLPackage.eNS_URI));
183 uriMap.put(URI.createURI("http://schema.omg.org/spec/UML/2.2"),
184 URI.createURI(UMLPackage.eNS_URI));
185 uriMap.put(URI.createURI("http://schema.omg.org/spec/UML/2.3"),
186 URI.createURI(UMLPackage.eNS_URI));
187 uriMap.put(URI.createURI("http://schema.omg.org/spec/UML/2.4"),
188 URI.createURI(UMLPackage.eNS_URI));
189 uriMap.put(URI.createURI("http://schema.omg.org/spec/UML/2.4.1"),
190 URI.createURI(UMLPackage.eNS_URI));
191 uriMap.put(URI.createURI("http://schema.omg.org/spec/UML/2.5"),
192 URI.createURI(UMLPackage.eNS_URI));
193 uriMap.put(URI.createURI("http://www.eclipse.org/uml2/4.0.0/Types"),
194 URI.createURI(TypesPackage.eNS_URI));
195
196 url = this.getClass().getResource("/profiles/Default.epx");
197 if (url!=null)
198 {
199
200 String path = url.getPath().substring(0, url.getPath().indexOf("profiles"));
201 URI uri = URI.createURI("jar:" + path);
202
203
204
205 URIConverter.URI_MAP.put(URI.createURI("pathmap://UML2_MSL_PROFILES/"), uri.appendSegment("profiles").appendSegment(""));
206 }
207
208 url = this.getClass().getResource("/profiles/RUPAnalysis.epx");
209 if (url!=null)
210 {
211 String path = url.getPath().substring(0, url.getPath().indexOf("profiles"));
212 URI uri = URI.createURI("jar:" + path);
213
214
215 URIConverter.URI_MAP.put(URI.createURI("pathmap://RUP_PROFILES/"), uri.appendSegment("profiles").appendSegment(""));
216 }
217
218
219
220
221
222 String m2repository = System.getenv("M2_REPO");
223 if (m2repository!=null)
224 {
225 URI uri = URI.createURI("file:" + m2repository.replace("\\", "/") + '/');
226
227 URIConverter.URI_MAP.put(URI.createURI("pathmap://m2repository/"), uri.appendSegment(""));
228
229 URIConverter.URI_MAP.put(URI.createURI("pathmap://M2_REPO/"), uri.appendSegment(""));
230 }
231
232
233 final Map<String, Object> loadOptions = this.getLoadOptions();
234
235
236 loadOptions.put(XMLResource.OPTION_DISABLE_NOTIFY, Boolean.FALSE);
237 loadOptions.put(XMLResource.OPTION_DOM_USE_NAMESPACES_IN_SCOPE, Boolean.TRUE);
238 loadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);
239 loadOptions.put(XMLResource.OPTION_DEFER_ATTACHMENT, Boolean.TRUE);
240 loadOptions.put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE);
241 loadOptions.put(XMLResource.OPTION_PROCESS_DANGLING_HREF_RECORD, Boolean.TRUE);
242 loadOptions.put(XMLResource.OPTION_USE_DEPRECATED_METHODS, Boolean.TRUE);
243 loadOptions.put(XMLResource.OPTION_USE_PARSER_POOL, new XMLParserPoolImpl());
244 loadOptions.put(XMLResource.OPTION_USE_XML_NAME_TO_FEATURE_MAP, new HashMap());
245
246 return proxyResourceSet;
247 }
248
249
250
251
252
253
254
255
256
257 private boolean registerOptionalRsmMetamodels(EPackage.Registry registry)
258 {
259
260
261 boolean registered = registerOptionalMetamodel(registry, "com.ibm.xtools.umlnotation.UmlnotationPackage");
262
263 registered = registerOptionalMetamodel(registry, "org.eclipse.gmf.runtime.notation.NotationPackage");
264 return registered;
265 }
266
267
268
269
270
271
272
273
274 private boolean registerOptionalMetamodel(EPackage.Registry registry, String ePackageClassName)
275 {
276 boolean registered = false;
277 try
278 {
279
280
281 Class ePackageClass = Class.forName(ePackageClassName);
282 if (ePackageClass != null)
283 {
284
285 String nsURI = (String) ePackageClass.getField("eNS_URI").get(null);
286 Object eInstance = ePackageClass.getField("eINSTANCE").get(null);
287 registry.put(nsURI, eInstance);
288 if (logger.isDebugEnabled())
289 {
290 logger.debug("Optional metamodel registered: " + nsURI);
291 }
292 registered = true;
293 }
294 }
295 catch (Exception e)
296 {
297
298 }
299 return registered;
300 }
301
302
303
304
305
306 public final void readModel(
307 String[] modelUris,
308 String[] moduleSearchPaths)
309 {
310 super.readModel(modelUris, moduleSearchPaths);
311 }
312
313
314
315
316
317
318 @Override
319 public void readModel(final String uri)
320 {
321 UmlUtilities.getModels().clear();
322 super.readModel(uri);
323
324 for (Resource modelResource : this.model)
325 {
326
327
328
329
330
331
332
333
334 EObject modelPackage = (EObject) EcoreUtil.getObjectByType(
335 modelResource.getContents(), EcorePackage.eINSTANCE.getEObject());
336 if (modelPackage==null)
337 {
338 throw new RepositoryFacadeException("Model '" + uri + "' is not a valid EMF UML2 model: Model element not found");
339 }
340
341
342
343
344
345 else if (!(modelPackage instanceof Model) && !(modelPackage instanceof org.eclipse.uml2.uml.Package))
346 {
347 throw new RepositoryFacadeException("Model '" + uri + "' package " + modelPackage + " is not a valid EMF UML2 model");
348 }
349
350 if (!uri.contains("profile.") && !uri.contains("_Profile."))
351 {
352 org.eclipse.uml2.uml.Package pkg = (org.eclipse.uml2.uml.Package)modelPackage;
353 if (!UmlUtilities.getModels().contains(pkg))
354 {
355 UmlUtilities.getModels().add(pkg);
356 }
357 }
358 }
359 }
360
361
362
363
364 public ModelAccessFacade getModel()
365 {
366 return this.getModel(null);
367 }
368
369
370
371
372
373
374 public ModelAccessFacade getModel(String uri)
375 {
376 if (this.modelFacade == null)
377 {
378 try
379 {
380 this.modelFacade = (ModelAccessFacade) ComponentContainer.instance().newComponent(
381 UMLModelAccessFacade.class, ModelAccessFacade.class);
382 }
383 catch (final Throwable throwable)
384 {
385 throw new RepositoryFacadeException(throwable);
386 }
387 }
388 if (StringUtils.isNotBlank(uri))
389 {
390 URI resource = URI.createURI(uri);
391 Resource uriModel = this.resourceSet.getResource(resource, true);
392 this.modelFacade.setModel(uriModel);
393 }
394 if (this.model != null)
395 {
396 this.modelFacade.setModel(this.model);
397 }
398 else
399 {
400 this.modelFacade = null;
401 }
402 return this.modelFacade;
403 }
404
405
406
407
408 @Override
409 public void close()
410 {
411
412 }
413 }