| Summary: | I just got null model | ||
|---|---|---|---|
| Product: | [Modeling] MDT.UML2 | Reporter: | Amy <xiyan_cao> |
| Component: | Core | Assignee: | UML2 Inbox <mdt-uml2-inbox> |
| Status: | RESOLVED WORKSFORME | QA Contact: | |
| Severity: | normal | ||
| Priority: | P2 | CC: | Kenn.Hussey |
| Version: | unspecified | ||
| Target Milestone: | --- | ||
| Hardware: | PC | ||
| OS: | Windows 7 | ||
| Whiteboard: | |||
|
Description
Amy
This is the code for parsing xmi, which use uml 2.1.0.
public void processXMI2USE(String inputFile, String outputFile,String batFile){
try {
UMLPackage.eINSTANCE.getName();
System.out.println(inputFile);
//register XML resource factory
ResourceSet resourceSet = new ResourceSetImpl();
System.out.println("resourceSet "+resourceSet);
resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI,
UMLPackage.eINSTANCE);
System.out.println("UMLPackage.eNS_URI "+UMLPackage.eNS_URI+" UMLPackage.eINSTANCE "+UMLPackage.eINSTANCE);
Map extensionToFactoryMap = resourceSet.getResourceFactoryRegistry()
.getExtensionToFactoryMap();
extensionToFactoryMap.put(UMLResource.FILE_EXTENSION,
UMLResource.Factory.INSTANCE);
extensionToFactoryMap.put("ecore", new EcoreResourceFactoryImpl());
extensionToFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION,
new XMIResourceFactoryImpl());
Iterator it = extensionToFactoryMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
// Load a profile from XMI
Resource inputResource = resourceSet.createResource(URI
.createFileURI(inputFile));
try {
inputResource.load(null);
} catch (IOException exception) {
//exception.printStackTrace();
}
System.out.println("inputResource "+inputResource);
// Load a model from XMI
URI uri = URI.createFileURI(inputFile);
Resource resource = resourceSet.getResource(uri, true);
//getObjectsByType Returns a collection of objects of the specified type.
// Returns the registry used for looking up a package based namespace.
//the registry is like a map, so it can use put(key, value)
System.out.println(resource);
//resource.getContents() is a collection to check
//the type of object to find
//the returned value of method getObjectByType is the first object or a collection
//of object of the specified type
/* Iterator <EObject> iter = resource.getContents().iterator();
while(iter.hasNext()){
Object obj = iter.next();
System.out.println("obj "+obj.getClass().getName());
}*/
//output:resource contents [org.eclipse.uml2.uml.internal.impl.PackageImpl@181ed9e
//(name: Blank Package, visibility: <unset>)]
System.out.println("resource contents "+(ModelImpl)resource.getContents());
System.out.println(UMLPackage.eINSTANCE.getModel());
Model model = (Model) EcoreUtil.getObjectByType(resource.getContents(),
UMLPackage.eINSTANCE.getModel());
System.out.println(model);
performTransform(model, outputFile, batFile, "xmi21");
} catch (Exception e) {
e.printStackTrace();
}
}
I'll assign this to the UML2 project Are you trying to deserialize a model that was created with the older version of UML2? Are you able to open that model using the (open source) UML editor? Note that there are new utilities in UML2 4.0 which should make it much easier to programmatically load resources based on older versions of UML. Try using UMLUtil.init(ResourceSet) or UMLResourcesUtil.init(ResourceSet) to initialize a new resource set and then just load your resource, something like this: ResourceSet resourceSet = UMLResourcesUtil.init(new ResourceSetImpl()); Resource resource = resourceSet.getResource(URI.createFileURI(inputFile), true); ... Given that I and others have successfully used these utilities (and the open source UML editor) to open models from a myriad of older UML and XMI versions, I'm going to mark this bug as "works for me" unless/until you can verify that these utilities don't work for you and/or provide a sample resource that demonstrates the problem. |