22
22
import java .util .Arrays ;
23
23
import java .util .Collection ;
24
24
import java .util .Collections ;
25
- import java .util .LinkedHashSet ;
25
+ import java .util .HashMap ;
26
26
import java .util .List ;
27
- import java .util .Set ;
27
+ import java .util .Map ;
28
28
29
29
import org .apache .log4j .Appender ;
30
30
import org .apache .log4j .AppenderSkeleton ;
@@ -63,58 +63,75 @@ public class Ecore2OmlApp {
63
63
private static final String ECORE = "ecore" ;
64
64
private static final String XCORE = "xcore" ;
65
65
66
- @ Parameter (
67
- names = {"--input-folder-path" ,"-i" },
68
- description ="Location of input Ecore folder (Required)" ,
69
- validateWith =InputFolderPath .class ,
70
- required =true ,
71
- order =1 )
72
- private String inputFolderPath = null ;
73
-
74
- @ Parameter (
75
- names = {"--output-folder-path" , "-o" },
76
- description ="Location of the output OML folder (Required)" ,
77
- validateWith =OutputFolderPath .class ,
78
- required =true ,
79
- order =2 )
80
- private String outputFolderPath ;
81
-
82
- @ Parameter (
83
- names = {"--referenced-ecore-path" , "-r" },
84
- description ="Location of a referenced ecore file (Optional)" ,
85
- validateWith = InputEcorePath .class ,
86
- required =false ,
87
- order =3
88
- )
89
- private List <String > referencedEcorePaths = new ArrayList <>();
66
+ static class Options {
67
+ @ Parameter (
68
+ names = {"--input-folder-path" ,"-i" },
69
+ description ="Location of input Ecore folder (Required)" ,
70
+ validateWith =InputFolderPath .class ,
71
+ required =true )
72
+ public String inputFolderPath = null ;
90
73
91
- @ Parameter (
92
- names = {"--input-file-extension" ,"-ie" },
93
- description ="Extension of input file (Optional, ecore/xcore by default)" ,
94
- required =false ,
95
- order =4 )
96
- private List <String > inputFileExtensions = Arrays .asList (ECORE , XCORE );
97
-
98
- @ Parameter (
99
- names = {"--output-file-extension" ,"-oe" },
100
- description ="Extension of output file (Optional, oml by default)" ,
101
- required =false ,
102
- order =5 )
103
- private String outputFileExtension = OmlConstants .OML_EXTENSION ;
104
-
105
- @ Parameter (
106
- names = {"--debug" , "-d" },
107
- description ="Shows debug logging statements" ,
108
- order =6 )
109
- private boolean debug ;
74
+ @ Parameter (
75
+ names = {"--output-folder-path" , "-o" },
76
+ description ="Location of the output OML folder (Required)" ,
77
+ validateWith =OutputFolderPath .class ,
78
+ required =true )
79
+ public String outputFolderPath ;
80
+
81
+ @ Parameter (
82
+ names = {"--referenced-ecore-path" , "-r" },
83
+ description ="Location of a referenced ecore file (Optional)" ,
84
+ validateWith = InputEcorePath .class ,
85
+ required =false )
86
+ public List <String > referencedEcorePaths = new ArrayList <>();
87
+
88
+ @ Parameter (
89
+ names = {"--input-file-extension" ,"-ie" },
90
+ description ="Extension of input file (Optional, ecore/xcore by default)" ,
91
+ required =false )
92
+ public List <String > inputFileExtensions = Arrays .asList (ECORE , XCORE );
93
+
94
+ @ Parameter (
95
+ names = {"--output-file-extension" ,"-oe" },
96
+ description ="Extension of output file (Optional, oml by default)" ,
97
+ required =false )
98
+ public String outputFileExtension = OmlConstants .OML_EXTENSION ;
99
+
100
+ @ Parameter (
101
+ names = {"--namespace-map" ,"-ns" },
102
+ description ="Mapping of existing namespace prefixes to new namespace prefixes (Optional, syntax is oldNsPrefix=newNsPrefix)" ,
103
+ validateWith = NamespaceMapping .class ,
104
+ required =false )
105
+ public List <String > namespaceMap = Arrays .asList (new String [] {
106
+ "http://www.eclipse.org/emf/2002/Ecore#EObject=http://www.w3.org/2002/07/owl#Thing" ,
107
+ "http://www.eclipse.org/emf/2002/Ecore#EString=http://www.w3.org/2001/XMLSchema#string" ,
108
+ "http://www.eclipse.org/emf/2002/Ecore#EFloat=http://www.w3.org/2001/XMLSchema#float" ,
109
+ "http://www.eclipse.org/emf/2002/Ecore#EInt=http://www.w3.org/2001/XMLSchema#int" ,
110
+ "http://www.eclipse.org/emf/2002/Ecore#EBoolean=http://www.w3.org/2001/XMLSchema#boolean" ,
111
+ "http://www.eclipse.org/emf/2003/XMLType#ID=http://www.w3.org/2001/XMLSchema#string" ,
112
+ "http://www.eclipse.org/emf/2003/XMLType#QName=http://www.w3.org/2001/XMLSchema#string" ,
113
+ "http://www.eclipse.org/emf/2003/XMLType#Int=http://www.w3.org/2001/XMLSchema#int"
114
+ });
110
115
111
- @ Parameter (
112
- names = {"--help" ,"-h" },
113
- description ="Displays summary of options" ,
114
- help =true ,
115
- order =7 )
116
- private boolean help ;
116
+ /*
117
+ * A map equivalent of namespaceMap
118
+ */
119
+ public Map <String , String > namespaceMap2 = new HashMap <>();
117
120
121
+ @ Parameter (
122
+ names = {"--debug" , "-d" },
123
+ description ="Shows debug logging statements" )
124
+ public boolean debug ;
125
+
126
+ @ Parameter (
127
+ names = {"--help" ,"-h" },
128
+ description ="Displays summary of options" ,
129
+ help =true )
130
+ public boolean help ;
131
+ }
132
+
133
+ private final Options options = new Options ();
134
+
118
135
private Logger LOGGER = LogManager .getLogger (Ecore2OmlApp .class );
119
136
120
137
/**
@@ -124,19 +141,25 @@ public class Ecore2OmlApp {
124
141
*/
125
142
public static void main (String ... args ) throws IOException {
126
143
final Ecore2OmlApp app = new Ecore2OmlApp ();
127
- final JCommander builder = JCommander .newBuilder ().addObject (app ).build ();
144
+ final JCommander builder = JCommander .newBuilder ().addObject (app . options ).build ();
128
145
builder .parse (args );
129
- if (app .help ) {
146
+ if (app .options . help ) {
130
147
builder .usage ();
131
148
return ;
132
149
}
133
- if (app .debug ) {
150
+ if (app .options . debug ) {
134
151
final Appender appender = LogManager .getRootLogger ().getAppender ("stdout" );
135
152
((AppenderSkeleton )appender ).setThreshold (Level .DEBUG );
136
153
}
137
- if (app .inputFolderPath .endsWith (File .separator )) {
138
- app .inputFolderPath = app .inputFolderPath .substring (0 , app .inputFolderPath .length ()-1 );
154
+ if (app .options . inputFolderPath .endsWith (File .separator )) {
155
+ app .options . inputFolderPath = app .options . inputFolderPath .substring (0 , app . options .inputFolderPath .length ()-1 );
139
156
}
157
+ app .options .namespaceMap .forEach (entry -> {
158
+ var parts = entry .split ("=" );
159
+ var oldNs = parts [0 ].trim ();
160
+ var newNs = parts [1 ].trim ();
161
+ app .options .namespaceMap2 .put (oldNs , newNs );
162
+ });
140
163
app .run ();
141
164
}
142
165
@@ -154,11 +177,11 @@ private void run() throws IOException {
154
177
LOGGER .info (" S T A R T" );
155
178
LOGGER .info (" Ecore to Oml " +getAppVersion ());
156
179
LOGGER .info ("=================================================================" );
157
- LOGGER .info ("Input Folder Path= " + inputFolderPath );
158
- LOGGER .info ("Output Folder Path= " + outputFolderPath );
180
+ LOGGER .info ("Input Folder Path= " + options . inputFolderPath );
181
+ LOGGER .info ("Output Folder Path= " + options . outputFolderPath );
159
182
160
- final File inputFolder = new File (inputFolderPath );
161
- final Collection <File > inputFiles = collectInputFiles (inputFolder , inputFileExtensions );
183
+ final File inputFolder = new File (options . inputFolderPath );
184
+ final Collection <File > inputFiles = collectInputFiles (inputFolder , options . inputFileExtensions );
162
185
163
186
final ResourceSet inputResourceSet = createInputResourceSet ();
164
187
@@ -170,7 +193,7 @@ private void run() throws IOException {
170
193
LOGGER .info ("Reading: " +inputURI );
171
194
inputResourceSet .getResource (inputURI , true );
172
195
}
173
-
196
+
174
197
// load the Oml registries here after the input have been read
175
198
OmlStandaloneSetup .doSetup ();
176
199
OmlXMIResourceFactory .register ();
@@ -180,7 +203,7 @@ private void run() throws IOException {
180
203
outputResourceSet .getLoadOptions ().put (OmlConstants .RESOLVE_IRI_USING_RESOURCE_SET , Boolean .TRUE );
181
204
outputResourceSet .eAdapters ().add (new ECrossReferenceAdapter ());
182
205
183
- final File outputFolder = new File (outputFolderPath );
206
+ // final File outputFolder = new File(options. outputFolderPath);
184
207
185
208
// create the Oml builder
186
209
final OmlBuilder builder = new OmlBuilder (outputResourceSet );
@@ -189,29 +212,22 @@ private void run() throws IOException {
189
212
builder .start ();
190
213
191
214
// convert the input resources
192
- List <URI > outputResourceURIs = new ArrayList <>();
193
- Set <URI > unconvertedResourceURIs = new LinkedHashSet <>(inputResourceURIs );
194
- while (!unconvertedResourceURIs .isEmpty ()) {
195
- List <URI > uris = new ArrayList <URI >(unconvertedResourceURIs );
196
- for (URI uri : uris ) {
197
- Resource inputResource = inputResourceSet .getResource (uri , true );
198
- Ecore2Oml e2o = new Ecore2Oml (inputFolder , inputResource , outputFolder , outputFileExtension , builder );
199
- Set <URI > newURIs = e2o .run ();
200
- assert (!outputResourceURIs .removeAll (newURIs ));
201
- outputResourceURIs .addAll (newURIs );
202
- unconvertedResourceURIs .addAll (e2o .getImportedURIs ());
203
- }
204
- unconvertedResourceURIs .removeAll (uris );
215
+ for (URI uri : inputResourceURIs ) {
216
+ Resource inputResource = inputResourceSet .getResource (uri , true );
217
+ Ecore2Oml e2o = new Ecore2Oml (inputResource , builder , options );
218
+ e2o .run ();
205
219
}
206
-
220
+
221
+ // get the output resources to save
222
+ List <Resource > outputResources = new ArrayList <>(builder .getResourceSet ().getResources ());
223
+
207
224
// finish the Oml builder
208
225
builder .finish ();
209
226
210
227
// save the output resources here instead of calling builder.save in order to log
211
- for (URI outputResourceURI : outputResourceURIs ) {
212
- if (outputResourceURI .fileExtension ().equals ("oml" )) {
213
- LOGGER .info ("Saving: " +outputResourceURI );
214
- final Resource outputResource = outputResourceSet .getResource (outputResourceURI , false );
228
+ for (Resource outputResource : outputResources ) {
229
+ if (outputResource .getURI ().fileExtension ().equals ("oml" )) {
230
+ LOGGER .info ("Saving: " +outputResource .getURI ());
215
231
outputResource .save (Collections .EMPTY_MAP );
216
232
}
217
233
}
@@ -230,7 +246,7 @@ private ResourceSet createInputResourceSet() {
230
246
231
247
// load any referenced Ecore files
232
248
EPackage .Registry packageRegistry = injector .getInstance (EPackage .Registry .class );
233
- for (String path : referencedEcorePaths ) {
249
+ for (String path : options . referencedEcorePaths ) {
234
250
final URI inputURI = URI .createFileURI (path );
235
251
Resource inputResource = resourceSet .getResource (inputURI , true );
236
252
if (inputResource != null ) {
@@ -340,4 +356,21 @@ public void validate(final String name, final String value) throws ParameterExce
340
356
}
341
357
}
342
358
359
+ /**
360
+ * A parameter validator for namespace mappings
361
+ */
362
+ public static class NamespaceMapping implements IParameterValidator {
363
+ /**
364
+ * Creates a new NamespaceMapping object
365
+ */
366
+ public NamespaceMapping () {
367
+ }
368
+ @ Override
369
+ public void validate (final String name , final String value ) throws ParameterException {
370
+ var parts = value .split ("=" );
371
+ if (parts .length != 2 ) {
372
+ throw new ParameterException ((("Parameter " + name ) + " does not follow the syntax oldNs=newNs" ));
373
+ }
374
+ }
375
+ }
343
376
}
0 commit comments