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