UML save to *.uml file [message #998692] |
Thu, 10 January 2013 09:15  |
Eclipse User |
|
|
|
I am currently using the following method to save my UML Model.
public static void saveModel(org.eclipse.uml2.uml.Package package_, URI uri)
{
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
Resource resource = new ResourceSetImpl().createResource(uri);
resource.getContents().add(package_);
try {
resource.save(null);
} catch (IOException ioe) {
}
}
It is working fine but I recently discovered that it is slightly different from *.uml files created with Papyrus or Topcased:
1. The File Header changes from
<?xml version="1.0" encoding="UTF-8"?>
<uml:Model xmi:version="2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML" xmi:id="_qb8akM37EdqwVrslYOdUDA" name="eShopModel" viewpoint="">
to
<?xml version="1.0" encoding="ASCII"?>
<uml:Model xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML" name="eShopModel" viewpoint="">
2. <subvertex xmi:type="uml:State" xmi:id="_mq2sED0qEeKGTJtY_dTOTg" name="Product Selection">
changes to
<subvertex xsi:type="uml:State" name="Product Selection">
Especially the lost id's and the different file encoding are problems.
Is there a chance to configure this?
regards
F. Wartenberg
|
|
|
Re: UML save to *.uml file [message #998706 is a reply to message #998692] |
Thu, 10 January 2013 09:34   |
Eclipse User |
|
|
|
Hi, Florian,
Does your code run in the context of an Eclipse instance? If so, then
you don't need to manage any resource factory registrations. If not,
then the UMLResourcesUtil::init(ResourceSet) method will set up the
proper resource factories and other configuration of your ResourceSet.
In particular, you are missing the registration of the UMLResource
implementation, which provides the UTF-8 encoding, XMI IDs, etc.
BTW, your code probably has a memory leak: You're putting the Package
into a resource in a resource set. These will all be retained in
memory by the static CacheAdapter, along with the entire contents of
the package. When working with UML models, you *must* always maintain
your UML resources explicitly in a ResourceSet and ensure that you
unload all of your UML resources and clear the adapters of your
ResourceSet when you have finished with them.
HTH,
Christian
On 2013-01-10 14:15:12 +0000, Florian Wartenberg said:
> I am currently using the following method to save my UML Model.
>
>
> public static void saveModel(org.eclipse.uml2.uml.Package package_,
> URI uri)
> {
> Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
> Map<String, Object> m = reg.getExtensionToFactoryMap();
> m.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new
> XMIResourceFactoryImpl());
>
> Resource resource = new ResourceSetImpl().createResource(uri);
> resource.getContents().add(package_);
> try {
> resource.save(null);
> } catch (IOException ioe) {
> }
> }
>
> It is working fine but I recently discovered that it is slightly
> different from *.uml files created with Papyrus or Topcased:
>
> 1. The File Header changes from <?xml version="1.0" encoding="UTF-8"?>
> <uml:Model xmi:version="2.1"
> xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
> xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML"
> xmi:id="_qb8akM37EdqwVrslYOdUDA" name="eShopModel" viewpoint="">
>
> to
> <?xml version="1.0" encoding="ASCII"?>
> <uml:Model xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML" name="eShopModel"
> viewpoint="">
>
> 2. <subvertex xmi:type="uml:State" xmi:id="_mq2sED0qEeKGTJtY_dTOTg"
> name="Product Selection">
> changes to
> <subvertex xsi:type="uml:State" name="Product Selection">
> Especially the lost id's and the different file encoding are problems.
>
> Is there a chance to configure this?
>
> regards
> F. Wartenberg
|
|
|
|
|
|
|
|
|
Re: UML save to *.uml file [message #1000849 is a reply to message #1000788] |
Tue, 15 January 2013 09:39  |
Eclipse User |
|
|
|
I load my model with this method:
public Model getModel(String pathToModel)
{
URI typesUri = URI.createFileURI(pathToModel);
ResourceSet set = new ResourceSetImpl();
set.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
set.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
set.createResource(typesUri);
Resource r = set.getResource(typesUri, true);
Model model = (Model) EcoreUtil.getObjectByType(r.getContents(), UMLPackage.Literals.MODEL);
return model;
}
Regards
Florian
|
|
|
Powered by
FUDForum. Page generated in 0.06032 seconds