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