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