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 package org.jomc.ant;
32
33 import java.io.BufferedReader;
34 import java.io.ByteArrayOutputStream;
35 import java.io.File;
36 import java.io.IOException;
37 import java.io.OutputStreamWriter;
38 import java.io.StringReader;
39 import java.io.StringWriter;
40 import java.util.logging.Level;
41 import javax.xml.bind.JAXBException;
42 import javax.xml.bind.Marshaller;
43 import javax.xml.bind.util.JAXBSource;
44 import org.apache.tools.ant.BuildException;
45 import org.apache.tools.ant.Project;
46 import org.jomc.model.Instance;
47 import org.jomc.model.Module;
48 import org.jomc.model.Modules;
49 import org.jomc.model.Specification;
50 import org.jomc.model.modlet.ModelHelper;
51 import org.jomc.modlet.Model;
52 import org.jomc.modlet.ModelContext;
53 import org.jomc.modlet.ModelException;
54 import org.jomc.modlet.ModelValidationReport;
55 import org.jomc.modlet.ObjectFactory;
56
57
58
59
60
61
62
63 public final class WriteModelTask extends JomcModelTask
64 {
65
66
67 private String specification;
68
69
70 private String implementation;
71
72
73 private String module;
74
75
76 private String modelEncoding;
77
78
79 private File modelFile;
80
81
82
83
84
85 private File specificationModelFile;
86
87
88
89
90
91 private File instanceModelFile;
92
93
94
95
96
97 private File moduleModelFile;
98
99
100 public WriteModelTask()
101 {
102 super();
103 }
104
105
106
107
108
109
110
111
112 public String getModelEncoding()
113 {
114 if ( this.modelEncoding == null )
115 {
116 this.modelEncoding = new OutputStreamWriter( new ByteArrayOutputStream() ).getEncoding();
117 }
118
119 return this.modelEncoding;
120 }
121
122
123
124
125
126
127
128
129 public void setModelEncoding( final String value )
130 {
131 this.modelEncoding = value;
132 }
133
134
135
136
137
138
139
140
141 public File getModelFile()
142 {
143 return this.modelFile;
144 }
145
146
147
148
149
150
151
152
153 public void setModelFile( final File value )
154 {
155 this.modelFile = value;
156 }
157
158
159
160
161
162
163
164
165
166
167 public File getSpecificationModelFile()
168 {
169 return this.specificationModelFile;
170 }
171
172
173
174
175
176
177
178
179
180
181 public void setSpecificationModelFile( final File value )
182 {
183 this.specificationModelFile = value;
184 }
185
186
187
188
189
190
191
192
193
194
195 public File getInstanceModelFile()
196 {
197 return this.instanceModelFile;
198 }
199
200
201
202
203
204
205
206
207
208
209 public void setInstanceModelFile( final File value )
210 {
211 this.instanceModelFile = value;
212 }
213
214
215
216
217
218
219
220
221
222
223 public File getModuleModelFile()
224 {
225 return this.moduleModelFile;
226 }
227
228
229
230
231
232
233
234
235
236
237 public void setModuleModelFile( final File value )
238 {
239 this.moduleModelFile = value;
240 }
241
242
243
244
245
246
247
248
249 public String getSpecification()
250 {
251 return this.specification;
252 }
253
254
255
256
257
258
259
260
261 public void setSpecification( final String value )
262 {
263 this.specification = value;
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277 public Specification getSpecification( final Model model )
278 {
279 if ( model == null )
280 {
281 throw new NullPointerException( "model" );
282 }
283
284 Specification s = null;
285
286 if ( this.getSpecification() != null )
287 {
288 final Modules modules = ModelHelper.getModules( model );
289
290 if ( modules != null )
291 {
292 s = modules.getSpecification( this.getSpecification() );
293 }
294
295 if ( s == null )
296 {
297 this.log( Messages.getMessage( "specificationNotFound", this.getSpecification() ), Project.MSG_WARN );
298 }
299 }
300
301 return s;
302 }
303
304
305
306
307
308
309
310
311 public String getImplementation()
312 {
313 return this.implementation;
314 }
315
316
317
318
319
320
321
322
323 public void setImplementation( final String value )
324 {
325 this.implementation = value;
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339 public Instance getInstance( final Model model )
340 {
341 if ( model == null )
342 {
343 throw new NullPointerException( "model" );
344 }
345
346 Instance i = null;
347
348 if ( this.getImplementation() != null )
349 {
350 final Modules modules = ModelHelper.getModules( model );
351
352 if ( modules != null )
353 {
354 i = modules.getInstance( this.getImplementation() );
355 }
356
357 if ( i == null )
358 {
359 this.log( Messages.getMessage( "implementationNotFound", this.getImplementation() ), Project.MSG_WARN );
360 }
361 }
362
363 return i;
364 }
365
366
367
368
369
370
371
372
373 public String getModule()
374 {
375 return this.module;
376 }
377
378
379
380
381
382
383
384
385 public void setModule( final String value )
386 {
387 this.module = value;
388 }
389
390
391
392
393
394
395
396
397
398
399
400
401 public Module getModule( final Model model )
402 {
403 if ( model == null )
404 {
405 throw new NullPointerException( "model" );
406 }
407
408 Module m = null;
409
410 if ( this.getModule() != null )
411 {
412 final Modules modules = ModelHelper.getModules( model );
413
414 if ( modules != null )
415 {
416 m = modules.getModule( this.getModule() );
417 }
418
419 if ( m == null )
420 {
421 this.log( Messages.getMessage( "moduleNotFound", this.getModule() ), Project.MSG_WARN );
422 }
423 }
424
425 return m;
426 }
427
428
429 @Override
430 public void executeTask() throws BuildException
431 {
432 BufferedReader reader = null;
433 ProjectClassLoader classLoader = null;
434 boolean suppressExceptionOnClose = true;
435
436 try
437 {
438 classLoader = this.newProjectClassLoader();
439 final ModelContext modelContext = this.newModelContext( classLoader );
440 final Model model = this.getModel( modelContext );
441 final Marshaller marshaller = modelContext.createMarshaller( this.getModel() );
442 final ModelValidationReport validationReport = modelContext.validateModel(
443 this.getModel(), new JAXBSource( marshaller, new ObjectFactory().createModel( model ) ) );
444
445 this.logValidationReport( modelContext, validationReport );
446 marshaller.setProperty( Marshaller.JAXB_ENCODING, this.getModelEncoding() );
447 marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
448
449 Model displayModel = new Model();
450 displayModel.setIdentifier( this.getModel() );
451
452 final Specification s = this.getSpecification( model );
453 if ( s != null )
454 {
455 displayModel.getAny().add( new org.jomc.model.ObjectFactory().createSpecification( s ) );
456
457 if ( this.getSpecificationModelFile() != null )
458 {
459 this.log( Messages.getMessage( "writingSpecification", s.getIdentifier(),
460 this.getSpecificationModelFile().getAbsolutePath() ),
461 Project.MSG_INFO );
462
463 marshaller.marshal( new org.jomc.model.ObjectFactory().createSpecification( s ),
464 this.getSpecificationModelFile() );
465
466 }
467 }
468
469 final Instance i = this.getInstance( model );
470 if ( i != null )
471 {
472 displayModel.getAny().add( new org.jomc.model.ObjectFactory().createInstance( i ) );
473
474 if ( this.getInstanceModelFile() != null )
475 {
476 this.log( Messages.getMessage( "writingInstance", i.getIdentifier(),
477 this.getInstanceModelFile().getAbsolutePath() ),
478 Project.MSG_INFO );
479
480 marshaller.marshal( new org.jomc.model.ObjectFactory().createInstance( i ),
481 this.getInstanceModelFile() );
482
483 }
484 }
485
486 final Module m = this.getModule( model );
487 if ( m != null )
488 {
489 displayModel.getAny().add( new org.jomc.model.ObjectFactory().createModule( m ) );
490
491 if ( this.getModuleModelFile() != null )
492 {
493 this.log( Messages.getMessage( "writingModule", m.getName(),
494 this.getModuleModelFile().getAbsolutePath() ),
495 Project.MSG_INFO );
496
497 marshaller.marshal( new org.jomc.model.ObjectFactory().createModule( m ),
498 this.getModuleModelFile() );
499
500 }
501 }
502
503 if ( displayModel.getAny().isEmpty() )
504 {
505 displayModel = model;
506 }
507
508 if ( this.getModelFile() != null )
509 {
510 this.log( Messages.getMessage( "writingModelObjects", this.getModel(),
511 this.getModelFile().getAbsolutePath() ), Project.MSG_INFO );
512
513 marshaller.marshal( new ObjectFactory().createModel( displayModel ), this.getModelFile() );
514 }
515 else
516 {
517 this.log( Messages.getMessage( "showingModelObjects", this.getModel() ), Project.MSG_INFO );
518
519 final StringWriter writer = new StringWriter();
520 marshaller.marshal( new ObjectFactory().createModel( displayModel ), writer );
521
522 reader = new BufferedReader( new StringReader( writer.toString() ) );
523 String line;
524
525 while ( ( line = reader.readLine() ) != null )
526 {
527 this.log( line, Project.MSG_INFO );
528 }
529 }
530
531 suppressExceptionOnClose = false;
532 }
533 catch ( final IOException e )
534 {
535 throw new BuildException( Messages.getMessage( e ), e, this.getLocation() );
536 }
537 catch ( final JAXBException e )
538 {
539 String message = Messages.getMessage( e );
540 if ( message == null )
541 {
542 message = Messages.getMessage( e.getLinkedException() );
543 }
544
545 throw new BuildException( message, e, this.getLocation() );
546 }
547 catch ( final ModelException e )
548 {
549 throw new BuildException( Messages.getMessage( e ), e, this.getLocation() );
550 }
551 finally
552 {
553 try
554 {
555 if ( reader != null )
556 {
557 reader.close();
558 }
559 }
560 catch ( final IOException e )
561 {
562 if ( suppressExceptionOnClose )
563 {
564 this.logMessage( Level.SEVERE, Messages.getMessage( e ), e );
565 }
566 else
567 {
568 throw new BuildException( Messages.getMessage( e ), e, this.getLocation() );
569 }
570 }
571 finally
572 {
573 try
574 {
575 if ( classLoader != null )
576 {
577 classLoader.close();
578 }
579 }
580 catch ( final IOException e )
581 {
582 if ( suppressExceptionOnClose )
583 {
584 this.logMessage( Level.SEVERE, Messages.getMessage( e ), e );
585 }
586 else
587 {
588 throw new BuildException( Messages.getMessage( e ), e, this.getLocation() );
589 }
590 }
591 }
592 }
593 }
594
595
596 @Override
597 public WriteModelTask clone()
598 {
599 return (WriteModelTask) super.clone();
600 }
601
602 }