I have drawn a class diagram by Visual Paradigm 8 and exported the project as XMI 2.1 file (Export for UML2). so there are two files (test.xmi.uml
and test.xmi.profile.uml
).
To parse test.xmi.uml
I wrote this java code using eclipse uml2 and emf:
ResourceSet set = new ResourceSetImpl();
set.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
set.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
Resource res = set.getResource(URI.createFileURI("C:\\test.xmi.uml"), true);
EObject eobj = res.getContents().get(0);
but the output object (eobj
) is an AnyTypeImpl
which can't be read very well.
what should I do to get well formatted objects like org.eclipse.uml2.uml.Model
objects by parsing this file?
I have finally found my answer:
Using org.eclipse.uml2.uml.resources.util.UMLResourcesUtil.init()
to initialize the parameters needed to parse the .uml
file.
So the code must be like this:
ResourceSet set = new ResourceSetImpl();
UMLResourcesUtil.init(set);
Resource res = set.getResource(typesUri, true);
EObject eobj = res.getContents().get(0);
the eobj
is an instance of org.eclipse.uml2.uml.Model
which is a well-formed object.