-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathKerML2XMI.java
More file actions
236 lines (217 loc) · 7.79 KB
/
Copy pathKerML2XMI.java
File metadata and controls
236 lines (217 loc) · 7.79 KB
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
/*****************************************************************************
* SysML 2 Pilot Implementation
* Copyright (c) 2018-2022 Model Driven Solutions, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Eclipse Public License as published by
* the Eclipse Foundation, version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Eclipse Public License for more details.
*
* You should have received a copy of theEclipse Public License
* along with this program. If not, see <https://www.eclipse.org/legal/epl-2.0/>.
*
* @license EPL-2.0 <http://spdx.org/licenses/EPL-2.0>
*
* Contributors:
* Ed Seidewitz
*
*****************************************************************************/
package org.omg.kerml.xtext.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
import org.omg.kerml.xtext.KerMLStandaloneSetup;
import org.omg.sysml.io.SysMLUtil;
import org.omg.sysml.lang.sysml.Element;
/**
* This is a utility program for reading one or more KerML source files and writing the corresponding KerML
* Ecore XMI files. The path for a file or root directory for the KerMNL source is given as the first argument,
* which is required. The XMI files are written with a ".kermlx" extension in the same directory as the
* corresponding source file. Other arguments may be used to specify paths for library directories. Source
* files are read from these directories, in order to resolve cross-file proxy references, but no corresponding
* XMI files are written for them.
*
* @author Ed Seidewitz
*
*/
public class KerML2XMI extends SysMLUtil {
public static final String KERML_EXTENSION = "kerml";
public static final String KERML_XMI_EXTENSION = "kermlx";
protected boolean isAddImplicitElements = false;
protected String outputDirectoryBase = null;
protected Path inputDirectoryBasePath = null;
public KerML2XMI() {
super();
KerMLStandaloneSetup.doSetup();
this.addExtension("." + KERML_EXTENSION);
}
/**
* Create an XMIResource for output, using Element identifiers for XMI IDs, and add it to the
* resource set.
*
* @param path the path to be used for the new resource.
* @return the newly created resource
*/
protected XMIResource createOutputResource(String path) {
final XMIResource resource = new XMIResourceImpl(URI.createFileURI(path)) {
@Override
public void attachedHelper(EObject eObject) {
if (eObject instanceof Element) {
setID(eObject, ((Element)eObject).getElementId());
}
super.attachedHelper(eObject);
}
};
this.getResourceSet().getResources().add(resource);
return resource;
}
/**
* Write the given resource, logging this to the console.
*
* @param resource the resource to be written
* @throws IOException
*/
public void writeResource(final Resource resource) throws IOException {
println("Writing " + resource.getURI().toFileString() + "...");
resource.save(null);
}
/**
* Create KerML output resources for all resources that have been read, but only write out those
* output resources that correspond to identified input resources, not those that correspond to
* library resources. Note that, at the end of this method, all content has been removed from
* the originally read resources.
*
* @throws IOException
*/
public void write() throws IOException {
System.out.println("Transforming" +
(isAddImplicitElements? " (adding implicit elements)... ": "..."));
this.transformAll(isAddImplicitElements);
System.out.println("Resolving proxies...");
this.resolveAllInputResources();
Set<Resource> outputResources = new HashSet<Resource>();
for (Object object: this.getResourceSet().getResources().toArray()) {
Resource resource = (Resource)object;
Resource outputResource = this.createOutputResource(this.getOutputPath(resource.getURI()));
outputResource.getContents().addAll(resource.getContents());
if (this.isInputResource(resource)) {
outputResources.add(outputResource);
}
}
for (Resource resource: outputResources) {
this.writeResource(resource);
}
}
/**
* Get an output path to be used for the given input resource URI.
*
* @param inputUri the URI of a resource that is to be written to a corresponding output resource
* @return the path for the output resource
*/
protected String getOutputPath(URI inputUri) {
String fileName = inputUri.lastSegment();
int i = fileName.lastIndexOf('.');
if (i >= 0) {
fileName = fileName.substring(0, i);
}
fileName += "." + getExtension(inputUri);
String outputDirectory = inputUri.trimSegments(1).toFileString();
if (outputDirectoryBase != null && inputDirectoryBasePath != null) {
outputDirectory = inputDirectoryBasePath.relativize(Path.of(outputDirectory)).toString();
outputDirectory = outputDirectoryBase + "/" + outputDirectory;
}
if (!outputDirectory.endsWith("/")) {
outputDirectory += "/";
}
return outputDirectory + fileName;
}
/**
* Get the extension to be used for output files. By default this is the KerML XMI extension, but
* this can be overridden in subclasses.
*
* @param inputUri the URI of the input resource
* @return the extension to be used for the output file of the corresponding output resource
*/
protected String getExtension(URI inputUri) {
return KERML_XMI_EXTENSION;
}
/**
* Process the options in the command-line argument string.
*
* @param args the command-line arguments
* @return the argument array with the options removed
*/
protected String[] processOptions(String[] args) {
while (args.length > 0) {
String arg = args[0];
if ("-g".equals(arg)) {
isAddImplicitElements = true;
} else if ("-o".equals(arg)) {
if (args.length > 1) {
outputDirectoryBase = args[1];
if (outputDirectoryBase.endsWith("/")) {
outputDirectoryBase += outputDirectoryBase.substring(0, outputDirectoryBase.length() - 1);
}
args = Arrays.copyOfRange(args, 1, args.length);
}
} else {
break;
}
args = Arrays.copyOfRange(args, 1, args.length);
}
if (args.length > 0) {
File input = new File(args[0]);
if (input != null && !input.isDirectory()) {
input = input.getParentFile();
}
if (input != null) {
inputDirectoryBasePath = input.toPath();
}
}
return args;
}
/**
* Run the creation of XMI for the resources given in the main program arguments.
*
* @param args the array of main program arguments
*/
public void run(String[] args) {
try {
args = processOptions(args);
this.read(args);
this.write();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The main program reads all the KerML resources rooted in the paths given as arguments and then
* writes out KerML XMI files for the resources from the input-path argument.
*
* <p>Usage:
*
* <p>Kerml2XMI [-g] [-o output-path] input-path [library-path library-path...]
*
* <p>where:
*
* <li>-g specifies that implicit elements should be generated (the default is not to)</li>
* <li>-o output-path is a path for the output directory to be used to write resources</li>
* <li>input-path is a path for reading input resources</li>
* <li>library-paths are paths for reading library resources</li>
*/
public static void main(String[] args) {
new KerML2XMI().run(args);
}
}