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