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