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