Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package org.etsi.mts.tdl.importers.ui.handlers;
import java.util.Arrays;
import java.util.LinkedHashMap;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Platform;
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.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
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.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.openapi2tdl.OpenAPI2TDLTranslator;
import org.etsi.mts.tdl.to2tdl.TO2TDLTranslator;
import org.osgi.framework.Bundle;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
/**
* Our sample handler extends AbstractHandler, an IHandler base class.
* @see org.eclipse.core.commands.IHandler
* @see org.eclipse.core.commands.AbstractHandler
*/
public class TranslationHandler extends AbstractHandler {
@Inject Injector injector;
@Inject Provider<ResourceSet> rsp;
@Inject Provider<XtextResourceSet> xrsp;
@Inject
IResourceSetProvider resourceSetProvider;
LinkedHashMap<String, String> targetFormats = new LinkedHashMap<>();
public static String translationTarget = "TDL XF (Part 3, XMI)";
private IWorkbenchWindow window;
/**
* The constructor.
*/
public TranslationHandler() {
init();
}
private void init() {
//TODO: update rt.ui as well
//TODO: do not reload after first init
if (!targetFormats.isEmpty()) {
// return;
}
targetFormats.clear();
targetFormats.put("TDL XF (Part 3, XMI)", "tdl");
loadTargetFormat("org.etsi.mts.tdl.TDLtx", "TDL TX (Part 8, Braces)", "tdltx");
loadTargetFormat("org.etsi.mts.tdl.TDLtxi", "TDL TX (Part 8, Indentation)", "tdltxi");
loadTargetFormat("org.etsi.mts.tdl.TDLan2", "TDLan2 (Part 1, Annex B)", "tdlan2");
loadTargetFormat("org.etsi.mts.tdl.TPLan2", "TPLan2 (Part 4, Annex B)", "tplan2");
}
private void loadTargetFormat(String className, String label, String extension) {
//TODO: Rename className to BundleName
if (Platform.getBundle(className) != null) {
targetFormats.put(label, extension);
}
}
/**
* the command has been executed, so extract extract the needed information
* from the application context.
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
init();
ISelection selection = HandlerUtil.getCurrentSelection(event);
IEditorInput input = HandlerUtil.getActiveEditorInput(event);
IFile file = null;
if (input != null && input instanceof FileEditorInput) {
file = ((FileEditorInput) input).getFile();
} else if (selection !=null && selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
Object firstElement = structuredSelection.getFirstElement();
if (firstElement instanceof IFile) {
file = (IFile) firstElement;
}
}
if (file !=null) {
URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
ResourceSet rs = new ResourceSetImpl();
String filepath = file.getLocation().toFile().getAbsolutePath();
String type = file.getFileExtension();
ElementListSelectionDialog dialog = new ElementListSelectionDialog(Display.getDefault().getActiveShell(), new LabelProvider());
dialog.setTitle("Import configuration");
dialog.setMessage("Importing "+type+" from "+file.getName()+"\n\nSelect the target format");
dialog.setElements(targetFormats.keySet().toArray());
dialog.setInitialElementSelections(Arrays.asList(new String[] {TranslationHandler.translationTarget}));
// user pressed cancel
if (dialog.open() != Window.OK) {
return false;
} else {
injector = Guice.createInjector();
Object[] result = dialog.getResult();
String selected = (String)result[0];
TranslationHandler.translationTarget = selected;
String extension = targetFormats.get(selected);
URI targetURI = URI.createURI(uri.toString()+"-generated."+extension);
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
Resource tr = resourceSet.createResource(targetURI);
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();
}
}
if (type.contains("yaml")) {
OpenAPI2TDLTranslator translator = new OpenAPI2TDLTranslator();
translator.setTargetResource(tr);
translator.initTargetResource(translator.cleanName(file.getName()));
try {
translator.translate(filepath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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.setTargetResource(tr);
translator.initTargetResource("generated_from_"+p.getName());
translator.addImports(p);
translator.transform(p);
}
//tr.getContents().addAll(EcoreUtil.copyAll(r.getContents()));
try {
tr.save(null);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
return null;
}
public void init(IWorkbenchWindow window) {
this.window = window;
}
@Override
public boolean isEnabled() {
return true;
}
}