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.mojo;
32
33 import java.io.File;
34 import java.io.StringWriter;
35 import java.util.logging.Level;
36 import javax.xml.bind.Marshaller;
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.jomc.modlet.Model;
39 import org.jomc.modlet.ModelContext;
40 import org.jomc.modlet.ObjectFactory;
41
42
43
44
45
46
47
48
49 public abstract class AbstractModelShowMojo extends AbstractJomcMojo
50 {
51
52
53 private static final String TOOLNAME = "ModelProcessor";
54
55
56
57
58
59
60 private File document;
61
62
63
64
65
66
67 private String documentEncoding;
68
69
70 public AbstractModelShowMojo()
71 {
72 super();
73 }
74
75 @Override
76 protected final void executeTool() throws Exception
77 {
78 this.logSeparator();
79 this.logProcessingModel( TOOLNAME, this.getModel() );
80
81 final ModelContext modelContext = this.createModelContext( this.getDisplayClassLoader() );
82 final Marshaller m = modelContext.createMarshaller( this.getModel() );
83 final Model displayModel = this.getDisplayModel( modelContext );
84 m.setSchema( modelContext.createSchema( this.getModel() ) );
85 m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
86
87 if ( displayModel != null )
88 {
89 if ( this.document == null )
90 {
91 final StringWriter stringWriter = new StringWriter();
92 m.marshal( new ObjectFactory().createModel( displayModel ), stringWriter );
93
94 final boolean verbose = this.isVerbose();
95 try
96 {
97 this.setVerbose( true );
98
99 if ( this.isLoggable( Level.INFO ) )
100 {
101 this.log( Level.INFO, stringWriter.toString(), null );
102 }
103 }
104 finally
105 {
106 this.setVerbose( verbose );
107 }
108 }
109 else
110 {
111 if ( this.document.exists() && !this.document.delete() && this.isLoggable( Level.WARNING ) )
112 {
113 this.log( Level.WARNING, Messages.getMessage(
114 "failedDeletingFile", this.document.getAbsolutePath() ), null );
115
116 }
117
118 if ( this.isLoggable( Level.INFO ) )
119 {
120 this.log( Level.INFO, Messages.getMessage(
121 "writingEncoded", this.document.getAbsolutePath(), this.documentEncoding ), null );
122
123 }
124
125 m.setProperty( Marshaller.JAXB_ENCODING, this.documentEncoding );
126 m.marshal( new ObjectFactory().createModel( displayModel ), this.document );
127 }
128
129 this.logToolSuccess( TOOLNAME );
130 }
131 else if ( this.isLoggable( Level.WARNING ) )
132 {
133 this.log( Level.WARNING, Messages.getMessage( "modelObjectNotFound" ), null );
134 }
135 }
136
137
138
139
140
141
142
143
144
145
146 protected abstract Model getDisplayModel( ModelContext modelContext ) throws MojoExecutionException;
147
148
149
150
151
152
153
154
155 protected abstract ClassLoader getDisplayClassLoader() throws MojoExecutionException;
156
157 }