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