-
Notifications
You must be signed in to change notification settings - Fork 18
/
JaMoPPC.java
314 lines (274 loc) · 10.6 KB
/
JaMoPPC.java
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*******************************************************************************
* Copyright (c) 2006-2012
* Software Technology Group, Dresden University of Technology
* DevBoost GmbH, Berlin, Amtsgericht Charlottenburg, HRB 140026
*
* 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
*
* Contributors:
* Software Technology Group - TU Dresden, Germany;
* DevBoost GmbH - Berlin, Germany
* - initial API and implementation
******************************************************************************/
package org.emftext.language.java.jamoppc;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.InternalEObject;
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.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.emftext.commons.layout.LayoutPackage;
import org.emftext.language.java.JavaClasspath;
import org.emftext.language.java.JavaPackage;
import org.emftext.language.java.resource.JavaSourceOrClassFileResourceFactoryImpl;
import org.emftext.language.java.resource.java.IJavaOptions;
public class JaMoPPC {
protected static final ResourceSet rs = new ResourceSetImpl();
public static void main(String[] args) throws IOException {
if ((args.length >= 1) && args[0].equals("--disable-layout")) {
rs.getLoadOptions().put(IJavaOptions.DISABLE_LAYOUT_INFORMATION_RECORDING, Boolean.TRUE);
args = Arrays.copyOfRange(args, 1, args.length);
}
if (args.length < 2) {
printUsageAndExit();
}
setUp();
File srcFolder = new File(args[0]);
if (!srcFolder.exists()) {
System.out.println("not found: " + args[0]);
return;
}
JavaClasspath cp = JavaClasspath.get(rs);
// register jar files
for (int i = 2; i < args.length; i++) {
File jarPath = new File(args[i]);
if (!jarPath.exists()) {
System.out.println("not found: " + args[i]);
return;
}
System.out.println("Registering JAR " + jarPath.getCanonicalPath());
cp.registerClassifierJar(URI.createFileURI(jarPath
.getCanonicalPath()));
}
// load all java files into resource set
loadAllFilesInResourceSet(srcFolder, ".java");
if (!resolveAllProxies(0)) {
System.err.println("Resolution of some Proxies failed...");
Iterator<Notifier> it = rs.getAllContents();
while (it.hasNext()) {
Notifier next = it.next();
if (next instanceof EObject) {
EObject o = (EObject) next;
if (o.eIsProxy()) {
try {
it.remove();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
}
}
}
}
// return;
}
URI srcUri = URI.createFileURI(srcFolder.getCanonicalPath());
String outFileOrDir = args[1];
if (outFileOrDir.endsWith(".xmi")) {
saveToSingleXMIFile(srcUri, new File(outFileOrDir));
} else {
saveToFolder(srcUri, new File(outFileOrDir));
}
}
private static void saveToSingleXMIFile(URI srcUri, File file)
throws IOException {
File parentDir = file.getParentFile();
if (parentDir == null) {
parentDir = new File(System.getProperty("user.dir"));
} else if (!parentDir.exists()) {
parentDir.mkdirs();
}
URI outUri = URI.createFileURI(file.getCanonicalPath());
Resource xmiResource = rs.createResource(outUri);
for (Resource javaResource : new ArrayList<Resource>(rs.getResources())) {
xmiResource.getContents().addAll(javaResource.getContents());
}
// save the metamodels (schemas) for dynamic loading
serializeMetamodel(parentDir);
saveXmiResource(xmiResource);
}
private static void saveToFolder(URI srcUri, File outFolder)
throws IOException {
List<Resource> result = new ArrayList<Resource>();
URI outUri = URI.createFileURI(outFolder.getCanonicalPath());
for (Resource javaResource : new ArrayList<Resource>(rs.getResources())) {
URI srcURI = javaResource.getURI();
srcURI = rs.getURIConverter().normalize(srcURI);
URI outFileURI = outUri.appendSegments(
srcURI.deresolve(srcUri.appendSegment("")).segments())
.appendFileExtension("xmi");
Resource xmiResource = rs.createResource(outFileURI);
xmiResource.getContents().addAll(javaResource.getContents());
result.add(xmiResource);
}
// save the metamodels (schemas) for dynamic loading
serializeMetamodel(outFolder);
for (Resource xmiResource : result) {
saveXmiResource(xmiResource);
}
}
private static void saveXmiResource(Resource xmiResource)
throws IOException {
Map<Object, Object> options = new HashMap<Object, Object>();
options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
xmiResource.save(options);
}
private static void printUsageAndExit() {
System.out.println("JaMoPPC Usage:");
System.out.println("==============");
System.out.println();
System.out
.println("To parse all files in a source folder and produce one model file per\n"
+ "parsed compilation unit in the target folder, use:");
System.out.println();
System.out
.println(" jamoppc [--disable-layout] <source folder path> <target folder path> <jar file paths>*");
System.out.println();
System.out
.println("To parse all files in a source folder and produce one XMI file\n"
+ "with the complete syntax graph, use:");
System.out.println();
System.out
.println(" jamoppc [--disable-layout] <source folder path> <target XMI file> <jar file paths>*");
System.out.println();
System.out
.println("In the latter case, the second parameter has to end in \".xmi\".");
System.out.println();
System.out.println("The --disable-layout option disables the generation of layout information.");
System.out.println("If given, it must be the first argument given to JaMoPPC.");
System.exit(1);
}
protected static void setUp() {
rs.getLoadOptions().put(IJavaOptions.DISABLE_LOCATION_MAP, Boolean.TRUE);
EPackage.Registry.INSTANCE.put("http://www.emftext.org/java",
JavaPackage.eINSTANCE);
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"java", new JavaSourceOrClassFileResourceFactoryImpl());
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
Resource.Factory.Registry.DEFAULT_EXTENSION,
new XMIResourceFactoryImpl());
}
protected static void serializeMetamodel(File outFolder) throws IOException {
URI outUri = URI.createFileURI(outFolder.getCanonicalPath());
URI javaEcoreURI = outUri.appendSegment("java.ecore");
Resource javaEcoreResource = rs.createResource(javaEcoreURI);
javaEcoreResource.getContents().addAll(
JavaPackage.eINSTANCE.getESubpackages());
javaEcoreResource.save(null);
URI layoutEcoreURI = outUri.appendSegment("layout.ecore");
Resource layoutEcoreResource = rs.createResource(layoutEcoreURI);
layoutEcoreResource.getContents().add(
LayoutPackage.eINSTANCE);
layoutEcoreResource.save(null);
}
protected static void loadAllFilesInResourceSet(File startFolder,
String extension) throws IOException {
for (File member : startFolder.listFiles()) {
if (member.isFile()) {
if (member.getName().endsWith(extension)) {
System.out.println("Parsing " + member);
parseResource(member);
} else {
System.out.println("Skipping " + member);
}
}
if (member.isDirectory()) {
if (!member.getName().startsWith(".")) {
System.out.println("Recursing into " + member);
loadAllFilesInResourceSet(member, extension);
} else {
System.out.println("Skipping " + member);
}
}
}
}
protected static boolean resolveAllProxies(int resourcesProcessedBefore) {
boolean failure = false;
List<EObject> eobjects = new LinkedList<EObject>();
for (Iterator<Notifier> i = rs.getAllContents(); i.hasNext();) {
Notifier next = i.next();
if (next instanceof EObject) {
eobjects.add((EObject) next);
}
}
int resourcesProcessed = rs.getResources().size();
if (resourcesProcessed == resourcesProcessedBefore) {
return true;
}
System.out.println("Resolving cross-references of " + eobjects.size()
+ " EObjects.");
int resolved = 0;
int notResolved = 0;
int eobjectCnt = 0;
for (EObject next : eobjects) {
eobjectCnt++;
if (eobjectCnt % 1000 == 0) {
System.out.println(eobjectCnt + "/" + eobjects.size()
+ " done: Resolved " + resolved + " crossrefs, "
+ notResolved + " crossrefs could not be resolved.");
}
InternalEObject nextElement = (InternalEObject) next;
for (EObject crElement : nextElement.eCrossReferences()) {
crElement = EcoreUtil.resolve(crElement, rs);
if (crElement.eIsProxy()) {
failure = true;
notResolved++;
System.out
.println("Can not find referenced element in classpath: "
+ ((InternalEObject) crElement).eProxyURI());
} else {
resolved++;
}
}
}
System.out.println(eobjectCnt + "/" + eobjects.size()
+ " done: Resolved " + resolved + " crossrefs, " + notResolved
+ " crossrefs could not be resolved.");
//call this method again, because the resolving might have triggered loading
//of additional resources that may also contain references that need to be resolved.
return !failure && resolveAllProxies(resourcesProcessed);
}
protected static void parseResource(File file) throws IOException {
loadResource(file.getCanonicalPath());
}
protected static void parseResource(ZipFile file, ZipEntry entry)
throws IOException {
loadResource(URI.createURI("archive:file:///"
+ new File(".").getAbsoluteFile().toURI().getRawPath()
+ file.getName().replaceAll("\\\\", "/") + "!/"
+ entry.getName()));
}
protected static void loadResource(String filePath) throws IOException {
loadResource(URI.createFileURI(filePath));
}
protected static void loadResource(URI uri) throws IOException {
rs.getResource(uri, true);
}
}