|
|
|
|
Re: From .jar to any model [message #1059738 is a reply to message #1053285] |
Tue, 21 May 2013 13:12 |
Edson Filho Messages: 6 Registered: April 2013 |
Junior Member |
|
|
Thank you Fabien and Hugo,
This is the method to extract the model from a given jar file. The method saves the model into a .xmi file. It's working, but need a refactoring.
Problem solved.
public void getModel(String jar) throws RemoteException {
if (jar == null)
return;
try {
/* Open eclipse project */
IWorkspace ws = ResourcesPlugin.getWorkspace();
IProject project = ws.getRoot().getProject("FeatureExtractor");
IProgressMonitor monitor = new NullProgressMonitor();
/* Create a new project, in case there isn't one */
if (!project.exists()) {
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] {"org.eclipse.jdt.core.javanature"});
project.setDescription(description, monitor);
project.create(monitor);
if (!project.isOpen())
project.open(monitor);
}
/* Create a link to the jar file, which is outside the project */
IPath location = new Path(jar);
IFile file = project.getFile(location.lastSegment());
file.createLink(location, IResource.REPLACE, monitor);
/* Create Java project, and
* add location (jar file) to the build path */
IJavaProject javaProject = JavaCore.create(project);
IPath filePath = project.getWorkspace().getRoot().getLocation().append(file.getFullPath());
/* Set project fragment roots */
Set<IClasspathEntry> e = new HashSet<IClasspathEntry>();
e.addAll(Arrays.asList(javaProject.getRawClasspath()));
boolean entryExist = false;
for (IClasspathEntry c : javaProject.getRawClasspath()) {
if ( filePath.toOSString().matches(c.getPath().toOSString()) ) {
entryExist = true;
break;
}
}
IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
LibraryLocation[] locations= JavaRuntime.getLibraryLocations(vmInstall);
for (LibraryLocation element : locations) {
e.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
}
for (IPackageFragmentRoot r : javaProject.getPackageFragmentRoots()) {
if ( filePath.toOSString().matches(r.getPath().toOSString()) ) {
entryExist = true;
break;
}
}
if (!entryExist) {
e.add(JavaCore.newLibraryEntry(filePath, null, null));
}
javaProject.setRawClasspath(e.toArray(new IClasspathEntry[e.size()]), monitor);
/* Create discover for jar file */
DiscoverJavaModelFromLibrary discover = new DiscoverJavaModelFromLibrary();
IPackageFragmentRoot root = JavaCore.createJarPackageFragmentRootFrom(file);
if (root == null) {
System.out.println("Error: Fragment root is null. Aborting...");
return null;
}
/* Create model from jar file */
discover.discoverElement(root, monitor);
Resource resource = discover.getTargetModel();
// Model model = (Model) resource.getContents().get(0);
/* Save Model XMI */
exportModelToXMI(resource, filePath.toOSString().replace(".jar", ".xmi"));
} catch (CoreException e) {
e.getMessage();
e.printStackTrace();
} catch (DiscoveryException e) {
e.getMessage();
e.printStackTrace();
} catch (ParserException e) {
e.getMessage();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03550 seconds