1 package org.apache.maven.plugins.site;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.StringWriter;
25 import java.io.Writer;
26
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29
30 import java.util.Date;
31 import java.util.List;
32 import java.util.Locale;
33
34 import org.apache.maven.doxia.site.decoration.DecorationModel;
35 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
36 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.plugin.MojoFailureException;
39
40 import org.apache.maven.plugins.annotations.Mojo;
41 import org.apache.maven.plugins.annotations.Parameter;
42 import org.codehaus.plexus.util.IOUtil;
43 import org.codehaus.plexus.util.StringUtils;
44 import org.codehaus.plexus.util.WriterFactory;
45 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
46 import org.codehaus.plexus.util.xml.XMLWriter;
47 import org.codehaus.plexus.util.xml.XmlWriterUtil;
48
49
50
51
52
53
54
55
56
57 @Mojo( name = "effective-site", requiresReports = true )
58 public class EffectiveSiteMojo
59 extends AbstractSiteRenderingMojo
60 {
61
62
63
64
65
66 @Parameter( property = "output" )
67 protected File output;
68
69
70
71
72 public void execute()
73 throws MojoExecutionException, MojoFailureException
74 {
75 String effectiveSite;
76
77 try
78 {
79 List<Locale> localesList = siteTool.getAvailableLocales( locales );
80
81 SiteRenderingContext context = createSiteRenderingContext( localesList.get( 0 ) );
82
83 DecorationModel decorationModel = context.getDecoration();
84
85 StringWriter w = new StringWriter();
86 XMLWriter writer =
87 new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
88 decorationModel.getModelEncoding(), null );
89
90 writeHeader( writer );
91
92 writeEffectiveSite( decorationModel, writer );
93
94 effectiveSite = w.toString();
95 }
96 catch ( IOException e )
97 {
98 throw new MojoExecutionException( "Error during site descriptor calculation", e );
99 }
100
101 if ( output != null )
102 {
103 try
104 {
105 writeXmlFile( output, effectiveSite );
106 }
107 catch ( IOException e )
108 {
109 throw new MojoExecutionException( "Cannot write effective site descriptor to output: " + output, e );
110 }
111
112 if ( getLog().isInfoEnabled() )
113 {
114 getLog().info( "Effective site descriptor written to: " + output );
115 }
116 }
117 else
118 {
119 StringBuilder message = new StringBuilder();
120
121 message.append( "\nEffective site descriptor, after inheritance and interpolation:\n\n" );
122 message.append( effectiveSite );
123 message.append( "\n" );
124
125 if ( getLog().isInfoEnabled() )
126 {
127 getLog().info( message.toString() );
128 }
129 }
130 }
131
132
133
134
135
136
137 protected static void writeHeader( XMLWriter writer )
138 {
139 XmlWriterUtil.writeCommentLineBreak( writer );
140 XmlWriterUtil.writeComment( writer, " " );
141
142 DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ss" );
143 XmlWriterUtil.writeComment( writer,
144 "Generated by Maven Site Plugin on "
145 + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
146 XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-site-plugin/" );
147 XmlWriterUtil.writeComment( writer, " " );
148 XmlWriterUtil.writeCommentLineBreak( writer );
149
150 XmlWriterUtil.writeLineBreak( writer );
151 }
152
153
154
155
156
157
158
159 protected static void writeComment( XMLWriter writer, String comment )
160 {
161 XmlWriterUtil.writeCommentLineBreak( writer );
162 XmlWriterUtil.writeComment( writer, " " );
163 XmlWriterUtil.writeComment( writer, comment );
164 XmlWriterUtil.writeComment( writer, " " );
165 XmlWriterUtil.writeCommentLineBreak( writer );
166
167 XmlWriterUtil.writeLineBreak( writer );
168 }
169
170 private void writeEffectiveSite( DecorationModel decorationModel, XMLWriter writer )
171 throws MojoExecutionException
172 {
173 String effectiveSite;
174
175 StringWriter sWriter = new StringWriter();
176 DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
177 try
178 {
179 siteWriter.write( sWriter, decorationModel );
180 }
181 catch ( IOException e )
182 {
183 throw new MojoExecutionException( "Cannot serialize site descriptor to XML.", e );
184 }
185
186 effectiveSite = sWriter.toString();
187 effectiveSite = effectiveSite.substring( effectiveSite.indexOf( "<project " ) );
188
189 writeComment( writer, "Effective site descriptor for project \'" + project.getId() + "\'" );
190
191 writer.writeMarkup( effectiveSite );
192 }
193
194 protected static void writeXmlFile( File output, String content )
195 throws IOException
196 {
197 Writer out = null;
198 try
199 {
200 output.getParentFile().mkdirs();
201
202 out = WriterFactory.newXmlWriter( output );
203
204 out.write( content );
205
206 out.flush();
207 }
208 finally
209 {
210 IOUtil.close( out );
211 }
212 }
213 }