Skip to content
Snippets Groups Projects
Commit 45ea3749 authored by Luca Cristoforetti's avatar Luca Cristoforetti
Browse files

Added a new plugin, OSSImporter

parent cde4e233
No related branches found
No related tags found
No related merge requests found
Showing
with 2105 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
/bin/
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.polarsys.chess.OSSImporter</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: org.polarsys.chess.OSSImporter
Bundle-SymbolicName: org.polarsys.chess.OSSImporter;singleton:=true
Bundle-Version: 1.0.0.qualifier
Require-Bundle: eu.fbk.eclipse.standardtools.ModelTranslatorToOcra;bundle-version="1.0.0",
org.eclipse.xtext,
eu.fbk.tools.editor.oss,
org.eclipse.papyrus.uml.tools,
eu.fbk.eclipse.standardtools.utils;bundle-version="1.0.0",
eu.fbk.tools.editor.contract.expression,
eu.fbk.tools.editor.basetype,
org.polarsys.chess.contracts.profile,
eu.fbk.tools.editor.contract,
org.polarsys.chess.core,
org.eclipse.papyrus.uml.tools.utils,
org.polarsys.chess.service;bundle-version="0.11.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: com.google.inject
Bundle-Vendor: Fondazione Bruno Kessler
source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
META-INF/,\
.,\
icons/,\
contexts.xml
<?xml version="1.0" encoding="UTF-8"?>
<contexts>
<context id="viewer" title="Sample View">
<description>This is the context help for the sample view with a table viewer. It was generated by a PDE template.</description>
<topic href="/PLUGINS_ROOT/org.eclipse.platform.doc.isv/guide/ua_help_context.htm" label="Context-sensitive help">
<enablement>
<with variable="platform">
<test property="org.eclipse.core.runtime.isBundleInstalled" args="org.eclipse.platform.doc.isv"/>
</with>
</enablement>
</topic>
</context>
</contexts>
/sample.gif
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="popup:org.polarsys.chess.modelexplorerpopupmenu.id">
<menu
id="org.eclipse.ui.main.diagramEditorMenu.CHESS.basicOperations"
label="Basic Operations">
<command
commandId="org.polarsys.chess.OSSImporter.commands.AddOSSFile"
label="Import &lt;&lt;SystemView&gt;&gt; components from .oss file"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="and">
<adapt
type="org.eclipse.emf.ecore.EObject">
<instanceof
value="org.eclipse.uml2.uml.Package">
</instanceof>
</adapt>
</iterate>
</visibleWhen>
</command>
</menu>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
defaultHandler="org.polarsys.chess.OSSImporter.commands.AddOSSFileCommand"
id="org.polarsys.chess.OSSImporter.commands.AddOSSFile"
name="Add System View components from .oss file">
</command>
</extension>
</plugin>
/CreateIBDSingleCommand_app.java
package org.polarsys.chess.OSSImporter.commands;
import java.io.File;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.edit.ui.action.ValidateAction.EclipseResourcesUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.polarsys.chess.OSSImporter.actions.ImportOSSFileAction;
import org.polarsys.chess.service.utils.SelectionUtil;
import org.eclipse.uml2.uml.Package;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import eu.fbk.eclipse.standardtools.ModelTranslatorToOcra.exceptions.NoResourceException;
import eu.fbk.eclipse.standardtools.ModelTranslatorToOcra.utils.OCRARuntimeErrorsDialogUtil;
import eu.fbk.eclipse.standardtools.commands.AbstractJobCommand;
public class AddOSSFileCommand extends AbstractJobCommand implements IHandler {
private static final String SYSVIEW = "CHESS::Core::CHESSViews::SystemView";
private static final String PROBLEMS_VIEW = "org.eclipse.ui.views.ProblemView";
private ImportOSSFileAction sampleView;
private Object umlObject;
private File ossFile;
private Resource modelResource;
private boolean showNoErrorPopup = true;
private OCRARuntimeErrorsDialogUtil ocraRuntimeErrorsDialogUtil = OCRARuntimeErrorsDialogUtil.getInstance();
private SelectionUtil selectionUtil = SelectionUtil.getInstance();
public AddOSSFileCommand() {
super("Add content from OSS file");
}
public String[] showOSSRuntimeErrors(Resource resource, File file, boolean showNoErrorPopup, IProgressMonitor monitor) throws NoResourceException {
if (resource != null) {
if (file != null) {
if (file.exists()) {
monitor.subTask("show Runtime Errors");
String[] errors = showRuntimeErrors(resource, file);
ocraRuntimeErrorsDialogUtil.showMessage_RuntimeErrors(file.getName(), errors, showNoErrorPopup);
monitor.worked(1);
return errors;
}
}
} else {
throw new NoResourceException();
}
return null;
}
/**
* It visualizes all the errors in the error view of Eclipse.
*/
private String[] showRuntimeErrors(Resource resource, File ossFile) {
XtextResource xTextRes = getXtextResourceFromFile(ossFile.getPath());
EclipseResourcesUtil eclipseResourcesUtil = new EclipseResourcesUtil();
eclipseResourcesUtil.deleteMarkers(resource);
Diagnostic diagnostic = Diagnostician.INSTANCE.validate(xTextRes.getContents().get(0));
if (diagnostic.getChildren().size() > 0) {
for (Diagnostic d : diagnostic.getChildren()) {
eclipseResourcesUtil.createMarkers(resource, d);
}
openProblemsView();
return getErrorsString(diagnostic);
}
return null;
}
private void openProblemsView() {
Display defaultDisplay = Display.getDefault();
defaultDisplay.syncExec(new Runnable() {
@Override
public void run() {
try {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(PROBLEMS_VIEW);
} catch (PartInitException e) {
e.printStackTrace();
}
}
});
}
private String[] getErrorsString(Diagnostic diagnostic) {
String[] errors = new String[diagnostic.getChildren().size()];
int i = 0;
for (Diagnostic d : diagnostic.getChildren()) {
errors[i] = d.getMessage();
i++;
}
return errors;
}
/**
* @param path
* the path of the oss file
* @return the XTextResource of the oss model
*/
private XtextResource getXtextResourceFromFile(String path) {
final XtextResourceSet resourceSet = new XtextResourceSet();
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
return (XtextResource) resourceSet.getResource(URI.createFileURI(path), true);
}
/**
* Checks if the given OSS file is valid or contains errors
*/
private String[] showRuntimeErrors(Resource resource, File ossFile, boolean showNoErrorPopup, IProgressMonitor monitor) throws Exception {
monitor.beginTask("Show Runtime Errors", 1);
String[] errors = showOSSRuntimeErrors(resource, ossFile,showNoErrorPopup, monitor);
monitor.done();
return errors;
}
/**
* Utility dialog to display a message on screen
* @param message the text to display
*/
private void showMessage(String message) {
MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "OSS parser", message);
}
/**
* Displays a file dialog to select the OSS file
* @return the selected File
*/
private File getOSSFile() {
File ossFile = null;
FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);
dialog.setFilterExtensions(new String [] {"*.oss", "*.OSS"});
String result = dialog.open();
try {
ossFile = new File(result);
} catch (NullPointerException e) {
e.printStackTrace();
showMessage("File not valid!");
}
return ossFile;
}
/**
* Checks if the selected object is a package in the <<SystemView>> branch
* @param pkg the selected object
* @return true if the package is valid
*/
private boolean objectIsSystemViewPackage(Object obj) {
if (obj instanceof Package) {
final Package pkg = (Package) obj;
if(pkg.getAppliedStereotype(SYSVIEW) != null) {
return true;
} else {
EList<Package> owningPackages = pkg.allOwningPackages();
for (Package owningPackage : owningPackages) {
if (owningPackage.getAppliedStereotype(SYSVIEW) != null) {
return true;
}
}
}
}
return false;
}
@Override
public void execPreJobOperations(ExecutionEvent event, IProgressMonitor monitor) throws Exception {
final ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
umlObject = selectionUtil.getUmlSelectedObject(selection);
modelResource = selectionUtil.getSelectedModelResource();
System.out.println("selectedUmlElement: " + umlObject);
if (objectIsSystemViewPackage(umlObject)) {
ossFile = getOSSFile();
// ossFile = new File("/hardmnt/nemesis0/home/cristofo/Downloads/System_simple.oss");
String[] errors = showRuntimeErrors(modelResource, ossFile, showNoErrorPopup, monitor);
monitor.beginTask("Importing elements from OSS file", 1);
sampleView = ImportOSSFileAction.getInstance();
if (errors == null && sampleView != null) {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
activePage.setEditorAreaVisible(false);
sampleView.startParsing((Package) umlObject, ossFile);
activePage.setEditorAreaVisible(true);
}
monitor.done();
showMessage("Import done!");
return;
}
showMessage("Please select a package from <<SystemView>>");
}
@Override
public void execJobCommand(ExecutionEvent event, IProgressMonitor monitor) throws Exception {
// String[] errors = showRuntimeErrors(modelResource, ossFile, showNoErrorPopup, monitor);
//
// monitor.beginTask("Importing elements from OSS file", 1);
//
// if (errors == null && sampleView != null) {
// sampleView.startParsing((Package) umlObject, ossFile);
// }
//
// monitor.done();
}
}
package org.polarsys.chess.OSSImporter.parser;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
/**
* An input stream based on a string that keeps a reference to the string
* itself and converts it to bytes only on demand.
*
* @author Sebastian Zarnekow - Initial contribution and API
*
* @since 2.5
*/
public class LazyStringInputStream extends InputStream {
private InputStream delegate;
private String string;
private String encoding;
public LazyStringInputStream(String string) {
this(string, Charset.defaultCharset().name());
}
public LazyStringInputStream(String string, Charset encoding) {
this(string, encoding.name());
}
public LazyStringInputStream(String string, String encoding) {
this.string = string;
this.encoding = encoding;
}
@Override
public int read() throws IOException {
return delegate().read();
}
private InputStream delegate() throws IOException {
if (delegate == null) {
delegate = new StringInputStream(string, encoding);
}
return delegate;
}
@Override
public int read(byte[] b) throws IOException {
return delegate().read(b);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return delegate().read(b, off, len);
}
@Override
public long skip(long n) throws IOException {
return delegate().skip(n);
}
@Override
public int available() throws IOException {
return delegate().available();
}
@Override
public void close() throws IOException {
delegate().close();
}
@Override
public synchronized void mark(int readlimit) {
try {
delegate().mark(readlimit);
} catch (IOException e) {
throw new RuntimeException();
}
}
@Override
public synchronized void reset() throws IOException {
delegate().reset();
}
@Override
public boolean markSupported() {
try {
return delegate().markSupported();
} catch (IOException e) {
return false;
}
}
@Override
public String toString() {
return string;
}
public String getString() {
return string;
}
public String getEncoding() {
return encoding;
}
}
package org.polarsys.chess.OSSImporter.parser;
/*******************************************************************************
* Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
import java.io.InputStream;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.resource.FileExtensionProvider;
import com.google.inject.Inject;
/**
* Some convenience methods for parsing (i.e. testing, etc.)
*
* @author Sven Efftinge - Initial contribution and API
*/
public class ParseHelper<T extends EObject> {
@Inject
private ResourceHelper resourceHelper;
public String fileExtension;
@Inject
public void setFileExtensionProvider(FileExtensionProvider extensionProvider) {
fileExtension = extensionProvider.getPrimaryFileExtension();
}
@SuppressWarnings("unchecked")
public T parse(InputStream in, URI uriToUse, Map<?, ?> options, ResourceSet resourceSet) {
resourceHelper.setFileExtension(fileExtension);
Resource resource = resourceHelper.resource(in, uriToUse, options, resourceSet);
final T root = (T) (resource.getContents().isEmpty() ? null : resource.getContents().get(0));
return root;
}
public T parse(CharSequence text) throws Exception {
return parse(text, resourceHelper.createResourceSet());
}
public T parse(CharSequence text, ResourceSet resourceSetToUse) throws Exception {
return parse(getAsStream(text), computeUnusedUri(resourceSetToUse), null, resourceSetToUse);
}
public T parse(CharSequence text, URI uriToUse, ResourceSet resourceSetToUse) throws Exception {
return parse(getAsStream(text), uriToUse, null, resourceSetToUse);
}
protected URI computeUnusedUri(ResourceSet resourceSet) {
return resourceHelper.computeUnusedUri(resourceSet);
}
protected InputStream getAsStream(CharSequence text) {
return resourceHelper.getAsStream(text);
}
}
package org.polarsys.chess.OSSImporter.parser;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.resource.FileExtensionProvider;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.resource.XtextResourceSet;
//import org.eclipse.xtext.util.LazyStringInputStream;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* Convenience to create resource from text.
* Useful if you want to avoid calls to {@link Resource#getContents()} before
* all resources are available in the resource set.
*
* @author Sebastian Zarnekow - Initial contribution and API
*/
public class ResourceHelper {
@Inject
private Provider<XtextResourceSet> resourceSetProvider;
@Inject
private IResourceFactory resourceFactory;
private String fileExtension;
@Inject
public void setFileExtensionProvider(FileExtensionProvider extensionProvider) {
fileExtension = extensionProvider.getPrimaryFileExtension();
}
void setFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
}
public Resource resource(InputStream in, URI uriToUse, Map<?, ?> options, ResourceSet resourceSet) {
Resource resource = resourceFactory.createResource(uriToUse);
resourceSet.getResources().add(resource);
try {
resource.load(in, options);
return resource;
} catch (IOException e) {
throw new WrappedException(e);
}
}
public Resource resource(CharSequence text) throws Exception {
return resource(text, createResourceSet());
}
public Resource resource(CharSequence text, ResourceSet resourceSetToUse) throws Exception {
return resource(getAsStream(text), computeUnusedUri(resourceSetToUse), null, resourceSetToUse);
}
public Resource resource(CharSequence text, URI uriToUse, ResourceSet resourceSetToUse) throws Exception {
return resource(getAsStream(text), uriToUse, null, resourceSetToUse);
}
public Resource resource(CharSequence text, URI uriToUse) throws Exception {
return resource(getAsStream(text), uriToUse, null, createResourceSet());
}
protected URI computeUnusedUri(ResourceSet resourceSet) {
String name = "__synthetic";
for (int i = 0; i < Integer.MAX_VALUE; i++) {
URI syntheticUri = URI.createURI(name + i + "." + fileExtension);
if (resourceSet.getResource(syntheticUri, false) == null)
return syntheticUri;
}
throw new IllegalStateException();
}
protected InputStream getAsStream(CharSequence text) {
return new LazyStringInputStream(text == null ? "" : text.toString());
}
protected String getFileExtension() {
return fileExtension;
}
protected XtextResourceSet createResourceSet() {
return resourceSetProvider.get();
}
}
package org.polarsys.chess.OSSImporter.parser;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
/**
* @author Dennis H�bner - Initial contribution and API
* @author Jan Koehnlein
*/
public class StringInputStream extends ByteArrayInputStream {
public StringInputStream(String string) {
super(string.getBytes());
}
public StringInputStream(String content, String encoding) throws UnsupportedEncodingException {
super(content.getBytes(encoding));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment