Commit 54209d23 authored by Philip Makedonski's avatar Philip Makedonski
Browse files

+ added json2tdl translation to ui

* refactoring and other refinements
parent 8a4ba8bd
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -18,7 +18,8 @@ Require-Bundle: org.eclipse.ui,
 org.etsi.mts.tdl.common,
 org.etsi.mts.tdl.asn2tdl,
 org.etsi.mts.tdl.openapi2tdl.next,
 org.etsi.mts.tdl.to2tdl
 org.etsi.mts.tdl.to2tdl,
 org.etsi.mts.tdl.json2tdl
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.emf.common.util
Bundle-RequiredExecutionEnvironment: JavaSE-11
+36 −61
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
@@ -23,15 +22,15 @@ import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.internal.ide.dialogs.InternalErrorDialog;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.ui.resource.IResourceSetProvider;
import org.etsi.mts.tdl.Package;
import org.etsi.mts.tdl.asn2tdl.ASN2TDLTranslator;
import org.etsi.mts.tdl.json2tdl.JSON2TDLTranslator;
import org.etsi.mts.tdl.openapi2tdl.next.OpenAPI2TDLTranslatorNext;
import org.etsi.mts.tdl.to2tdl.TO2TDLTranslator;
import org.osgi.framework.Bundle;
import org.etsi.mts.tdl.transform.AbstractTranslator;

import com.google.inject.Guice;
import com.google.inject.Inject;
@@ -129,34 +128,17 @@ public class TranslationHandler extends AbstractHandler {
				URI targetURI = URI.createURI(uri.toString()+"-generated."+extension);
				XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
				Resource tr = resourceSet.createResource(targetURI);
				
				AbstractTranslator translator = null;
				if (type.contains("asn")) {
					ASN2TDLTranslator translator = new ASN2TDLTranslator();
					translator.setTargetResource(tr);
					translator.initTargetResource(translator.cleanName(file.getName()));
					try {
						translator.translate(filepath);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						MessageDialog errorDialog = new MessageDialog(
								Display.getDefault().getActiveShell(), 
								"Translation error...", 
								null, 
								"An error occurred during the translation. "
								+ "It may be due to problems occurring while processing the input, "
								+ "e.g if the input could not be processed. "
								+ "See details for a more technical description:\n\n"
								+ e.getMessage()
								,		
								MessageDialog.ERROR, 
								0, 
								new String[] {"OK"});
						errorDialog.open();
					translator = new ASN2TDLTranslator();
				}
				if (type.contains("json")) {
					translator = new JSON2TDLTranslator();
				}
				if (type.contains("yaml")) {
					OpenAPI2TDLTranslatorNext translator = new OpenAPI2TDLTranslatorNext();
					translator = new OpenAPI2TDLTranslatorNext();
				}
				if (translator != null) {
					translator.setTargetResource(tr);
					translator.initTargetResource(translator.cleanName(file.getName()));
					try {
@@ -164,57 +146,50 @@ public class TranslationHandler extends AbstractHandler {
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						MessageDialog errorDialog = new MessageDialog(
								Display.getDefault().getActiveShell(), 
								"Translation error...", 
								null, 
								"An error occurred during the translation. "
								+ "It may be due to problems occurring while processing the input, "
								+ "e.g if the input could not be processed. "
								+ "See details for a more technical description:\n\n"
								+ e.getMessage()
								,		
								MessageDialog.ERROR, 
								0, 
								new String[] {"OK"});
						errorDialog.open();
						showErrorDialog(e);
					}
					
				}
				
				if (type.contains("tplan2")) { //TODO: also new syntax
					Resource r = rs.getResource(uri, true);
					Package p = (Package) r.getContents().get(0);
					TO2TDLTranslator translator = new TO2TDLTranslator();
					translator = new TO2TDLTranslator();
					translator.setTargetResource(tr);
					translator.initTargetResource("generated_from_"+p.getName());
					translator.addImports(p);
					translator.transform(p);
					((TO2TDLTranslator)translator).transform(p);
				}
				//tr.getContents().addAll(EcoreUtil.copyAll(r.getContents()));

				try {
					tr.save(null);
				} catch (Exception e1) {
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					e.printStackTrace();
					showErrorDialog(e);
				}
			}
		}
		//TODO: throw proper execution exceptions?
		return null;
	}

	private void showErrorDialog(Exception e) {
		MessageDialog errorDialog = new MessageDialog(
				Display.getDefault().getActiveShell(), 
				"Translation error...", 
				null, 
				"An error occurred during the translation. "
							+ "It may be due to problems occurring while storing the output. "
							+ "See details for a more technical description and consider usind a different target format:\n\n"
							+ e1.getMessage()
				+ "It may be due to problems occurring while processing the input, "
				+ "e.g if the input could not be processed. "
				+ "See details for a more technical description:\n\n"
				+ e.getMessage()
				,		
				MessageDialog.ERROR, 
				0, 
				new String[] {"OK"});
		errorDialog.open();
	}
			}
		}
		//TODO: throw proper execution exceptions?
		return null;
	}
	
	public void init(IWorkbenchWindow window) {
		this.window = window;