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