/
GenerateCommon.java
158 lines (146 loc) · 5.39 KB
/
GenerateCommon.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
package com.opcoach.genmodeladdon.core;
import com.opcoach.genmodeladdon.core.GMAConstants;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
/**
* A class to provide some generation common methods
*/
@SuppressWarnings("all")
public class GenerateCommon implements GMAConstants {
/**
* Extract the project name from the genmodel resource
*/
public static String getProjectName(final GenModel gm) {
return GenerateCommon.getProjectNameFromURI(gm.eResource().getURI());
}
public static String getProjectNameFromURI(final URI genModelUri) {
final URI rootUri = URI.createURI(ResourcesPlugin.getWorkspace().getRoot().getLocationURI().toString());
final String lastSegOfRootUri = rootUri.lastSegment();
final String genModelUriStr = genModelUri.toString();
boolean _startsWith = genModelUriStr.startsWith("platform:/resource/");
if (_startsWith) {
final String s = genModelUriStr.replace("platform:/resource/", "");
final int lastSlash = s.indexOf("/");
return s.substring(0, lastSlash);
}
final List<String> segments = genModelUri.segmentsList();
final int lastIndex = segments.lastIndexOf(lastSegOfRootUri);
return segments.get((lastIndex + 1));
}
/**
* Find the project from a genmodel
*/
public static IProject getProject(final GenModel gm) {
IProject _xblockexpression = null;
{
final String projectName = GenerateCommon.getProjectName(gm);
_xblockexpression = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
}
return _xblockexpression;
}
/**
* Find the IFile from a genmodel
*/
public static IFile getModelFile(final GenModel gm) {
Resource _eResource = gm.eResource();
boolean _tripleNotEquals = (_eResource != null);
if (_tripleNotEquals) {
final URI genModelUri = gm.eResource().getURI();
String _replaceFirst = genModelUri.toString().replaceFirst("platform:/resource", "");
final Path p = new Path(_replaceFirst);
final IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot();
return ws.getFile(p);
}
return null;
}
public static String getProperty(final IFile f, final QualifiedName qn) {
String result = null;
try {
result = f.getPersistentProperty(qn);
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception)_t;
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return result;
}
public static void setProperty(final IFile f, final QualifiedName qn, final String value) {
try {
f.setPersistentProperty(qn, value);
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception)_t;
final Bundle bndl = FrameworkUtil.getBundle(GenerateCommon.class);
final ILog logger = Platform.getLog(bndl);
Status _status = new Status(IStatus.WARNING, GMAConstants.PLUGIN_ID, ("Unable to store the property : " + qn), e);
logger.log(_status);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
}
/**
* Find the model name from the genmodel
*/
public static String getModelName(final GenModel gm) {
String _xblockexpression = null;
{
final URI uri = gm.eResource().getURI();
final String s = uri.toString();
int pos = s.lastIndexOf("/");
String modelName = s.substring((pos + 1));
pos = modelName.indexOf(".genmodel");
_xblockexpression = modelName.substring(0, pos);
}
return _xblockexpression;
}
/**
* Find the model directory in its project
*/
public static String getModelPath(final GenModel gm) {
final URI uri = gm.eResource().getURI();
final String projectName = GenerateCommon.getProject(gm).getName();
return GenerateCommon.getModelPathFromStringURI(projectName, uri.toString());
}
/**
* Find the model directory in its project
*/
public static String getModelPathFromStringURI(final String projectName, final String uri) {
int _lastIndexOf = uri.lastIndexOf(projectName);
int _length = projectName.length();
int _plus = (_lastIndexOf + _length);
final int pathPos = (_plus + 1);
final int lastSlashPos = uri.lastIndexOf("/");
String modelDir = ".";
if ((pathPos < lastSlashPos)) {
modelDir = uri.substring(pathPos, lastSlashPos);
}
return modelDir;
}
public static boolean isMapType(final EClassifier c) {
final String name = c.getInstanceClassName();
return ("java.util.Map.Entry".equals(name) || "java.util.Map$Entry".equals(name));
}
public static boolean isMapType(final GenClass c) {
return GenerateCommon.isMapType(c.getEcoreClass());
}
}