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