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