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
package org.etsi.mts.tdl.tools.rt.ui.handlers;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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.resources.IProject;
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.emf.ecore.xmi.impl.XMIResourceImpl;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.serializer.impl.Serializer;
import org.eclipse.xtext.util.Files;
import org.etsi.mts.tdl.Package;
import org.etsi.mts.tdl.util.tdlResourceFactoryImpl;
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 Provider<ResourceSet> rsp;
@Inject Injector injector;
/**
* The constructor.
*/
public TranslationHandler() {
}
/**
* the command has been executed, so extract extract the needed information
* from the application context.
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
ISelection selection = HandlerUtil.getCurrentSelection(event);
IEditorInput input = HandlerUtil.getActiveEditorInput(event);
IFile file = null;
IProject project = null;
if (input != null && input instanceof FileEditorInput) {
file = ((FileEditorInput) input).getFile();
project = file.getProject();
} else if (selection !=null && selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
Object firstElement = structuredSelection.getFirstElement();
if (firstElement instanceof IFile) {
file = (IFile) firstElement;
project = file.getProject();
}
}
if (file !=null) {
URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
ResourceSet rs = new ResourceSetImpl();
Resource r = rs.getResource(uri, true);
Package p = (Package) r.getContents().get(0);
if (file.getFullPath().toString().endsWith(".tdl")) {
String s = "";
URI targetURI = URI.createURI(uri.toString()+".tdlan2");
Injector injector = Guice.createInjector(new org.etsi.mts.tdl.TDLan2RuntimeModule());
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
Resource tr = resourceSet.createResource(targetURI);
boolean method1 = true;
//method 1
if (method1) {
tr.getContents().addAll(EcoreUtil.copyAll(r.getContents()));
try {
tr.save(null);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
Serializer serializer = injector.getInstance(Serializer.class);
tr.getContents().add(p);
try {
s += serializer.serialize(p);
} catch (Exception ex) { // fall back:
System.out.println("Model could not be serialized");
ex.printStackTrace();
}
System.out.println(s);
Files.writeStringIntoFile(file.getRawLocation().toOSString()+".tdlan2", s);
}
} else if (file.getFullPath().toString().endsWith(".tdlan2")) {
String extension = "tdl";
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
tdlResourceFactoryImpl factory = new tdlResourceFactoryImpl();
m.put(extension, factory);
Resource outputResource = factory.createResource(URI.createURI(r.getURI().toString()+".tdl"));
outputResource.getContents().addAll(EcoreUtil.copyAll(r.getContents()));
Map options = new HashMap<>();
options.put(XMIResourceImpl.OPTION_ENCODING, "UTF-8");
try {
outputResource.save(options);
} catch (IOException e) {
e.printStackTrace();
}
} else if (file.getFullPath().toString().endsWith(".tplan2")) {
String extension = "tdl";
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
tdlResourceFactoryImpl factory = new tdlResourceFactoryImpl();
m.put(extension, factory);
Resource outputResource = factory.createResource(URI.createURI(r.getURI().toString()+".tdl"));
outputResource.getContents().addAll(EcoreUtil.copyAll(r.getContents()));
Map options = new HashMap<>();
options.put(XMIResourceImpl.OPTION_ENCODING, "UTF-8");
try {
outputResource.save(options);
} catch (IOException e) {
e.printStackTrace();
}
} else {
//TODO: handle unknown resource type
}
}
return null;
}
}